Spaces:
Runtime error
Runtime error
Commit
·
41ad623
1
Parent(s):
d68a7c6
fix: improve query and bot service
Browse files- core/chat/bot_service.py +0 -7
- db/query/query_user_meta.py +20 -4
core/chat/bot_service.py
CHANGED
|
@@ -173,10 +173,3 @@ 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)
|
| 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)
|
|
|
|
| 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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db/query/query_user_meta.py
CHANGED
|
@@ -28,18 +28,34 @@ class UserMetaQuery(BaseQuery):
|
|
| 28 |
return result
|
| 29 |
|
| 30 |
def insert_user_meta_entries(self, db, metadata_ids):
|
| 31 |
-
"""Insert new user meta entries."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
user_meta_entries = [
|
| 33 |
User_Meta(user_id=self.user.get("id"), metadata_id=mid)
|
| 34 |
-
for mid in
|
| 35 |
]
|
| 36 |
|
| 37 |
# Use the method from BaseQuery to insert entries
|
| 38 |
self.insert_entries(db, user_meta_entries)
|
| 39 |
return {
|
| 40 |
-
"status":"success",
|
| 41 |
"message": "User meta entries added successfully.",
|
| 42 |
-
"metadata_ids":
|
| 43 |
}
|
| 44 |
|
| 45 |
def update_user_meta_entries(self, db, metadata_ids):
|
|
|
|
| 28 |
return result
|
| 29 |
|
| 30 |
def insert_user_meta_entries(self, db, metadata_ids):
|
| 31 |
+
"""Insert new user meta entries if they don't already exist."""
|
| 32 |
+
# Fetch existing metadata IDs for the user
|
| 33 |
+
existing_metadata_ids = {
|
| 34 |
+
entry.metadata_id
|
| 35 |
+
for entry in db.query(User_Meta).filter_by(user_id=self.user.get("id")).all()
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
# Filter out metadata IDs that already exist
|
| 39 |
+
new_metadata_ids = [
|
| 40 |
+
mid for mid in metadata_ids if mid not in existing_metadata_ids
|
| 41 |
+
]
|
| 42 |
+
|
| 43 |
+
if not new_metadata_ids:
|
| 44 |
+
return {
|
| 45 |
+
"message": "No new entries to add; all specified metadata IDs already exist.",
|
| 46 |
+
"existing_metadata_ids": existing_metadata_ids,
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
user_meta_entries = [
|
| 50 |
User_Meta(user_id=self.user.get("id"), metadata_id=mid)
|
| 51 |
+
for mid in new_metadata_ids
|
| 52 |
]
|
| 53 |
|
| 54 |
# Use the method from BaseQuery to insert entries
|
| 55 |
self.insert_entries(db, user_meta_entries)
|
| 56 |
return {
|
|
|
|
| 57 |
"message": "User meta entries added successfully.",
|
| 58 |
+
"metadata_ids": new_metadata_ids, # Include only new metadata IDs in the result
|
| 59 |
}
|
| 60 |
|
| 61 |
def update_user_meta_entries(self, db, metadata_ids):
|