dsmultimedika commited on
Commit
cbcabed
·
1 Parent(s): 9f7b904

fix : improve mongo db

Browse files
Files changed (1) hide show
  1. core/chat/bot_service.py +9 -2
core/chat/bot_service.py CHANGED
@@ -153,13 +153,13 @@ class ChatCompletionService:
153
  def _save_chat_history_to_db(self, response, metadata_collection):
154
  chat_history_db = [
155
  ChatMessage(
156
- role=MessageRole.SYSTEM,
157
  content=self.user_request,
158
  timestamp=datetime.now(),
159
  payment="free" if self.type_bot == "general" else None,
160
  ),
161
  ChatMessage(
162
- role=MessageRole.ASSISTANT,
163
  content=response,
164
  metadata=metadata_collection,
165
  timestamp=datetime.now(),
@@ -173,3 +173,10 @@ class ChatCompletionService:
173
  collection = db[self.session_id] # Replace with your collection name
174
  result = collection.insert_many(chat_history_json)
175
  print("Data inserted with record ids", result.inserted_ids)
 
 
 
 
 
 
 
 
153
  def _save_chat_history_to_db(self, response, metadata_collection):
154
  chat_history_db = [
155
  ChatMessage(
156
+ role=MessageRole.ASSISTANT,
157
  content=self.user_request,
158
  timestamp=datetime.now(),
159
  payment="free" if self.type_bot == "general" else None,
160
  ),
161
  ChatMessage(
162
+ role=MessageRole.SYSTEM,
163
  content=response,
164
  metadata=metadata_collection,
165
  timestamp=datetime.now(),
 
173
  collection = db[self.session_id] # Replace with your collection name
174
  result = collection.insert_many(chat_history_json)
175
  print("Data inserted with record ids", result.inserted_ids)
176
+
177
+ chat_history_json = [message.model_dump() for message in chat_history_db]
178
+
179
+ db = self.client["bot_database"] # Replace with your database name
180
+ collection = db[self.session_id] # Replace with your collection name
181
+ result = collection.insert_many(chat_history_json)
182
+ print("Data inserted with record ids", result.inserted_ids)