Spaces:
Running
Running
File size: 1,636 Bytes
f823831 19a3700 bb806fb 0255469 f823831 9a038f5 f823831 83157ea f823831 83157ea 725b040 f823831 fe6ea46 f823831 51d2fe2 f823831 45f17bf f823831 2dded4c f823831 83157ea |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# %%
from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
import os
from dotenv import load_dotenv
from datetime import datetime, timezone
# %%
load_dotenv()
MONGODB_CONNECTION_STRING = os.getenv('MONGODB_CONNECTION_STRING')
# %%
# Create a new client and connect to the server
client = MongoClient(MONGODB_CONNECTION_STRING, server_api=ServerApi('1'))
# Send a ping to confirm a successful connection
try:
client.admin.command('ping')
print("Pinged your deployment. You successfully connected to MongoDB!")
database = client["AdminLogs"]
chat_logs = database["ChatLogs"]
print("Connected to MongoDB collection successfully!")
except Exception as e:
print(e)
# %%
def log_chat(session_id: str, name: str, email: str, query: str, answer: str, latency_ms: float | None=None, metadata: dict=None):
"""
Logs a chat interaction to the MongoDB 'ChatLogs' collection.
"""
company_id = "80bd8dd8-1e20-5271-927a-02beab69abdd"
chatbot_id = "a3a86959-0f92-5124-8eb8-d3012f8b1ea3"
data = {
"company_id": company_id,
"chatbot_id": chatbot_id,
"session_id": session_id,
"name": name,
"email": email,
"timestamp": datetime.now(timezone.utc),
"query": query,
"answer": answer,
"metadata": metadata or {},
"starred": False
}
if latency_ms is not None:
data["latency_ms"] = latency_ms
try:
print("Logging chat:", data)
result = chat_logs.insert_one(data)
except Exception as e:
print("Failed to log chat interaction:", e)
# %%
|