Spaces:
Sleeping
Sleeping
Fix: conversation history field names and save messages after each response
Browse files- ai_service.py +12 -4
ai_service.py
CHANGED
|
@@ -51,9 +51,11 @@ async def get_ai_response(user_query: str, telegram_id: int):
|
|
| 51 |
conversation_history = []
|
| 52 |
if db_manager:
|
| 53 |
raw_history = db_manager.get_conversation_history(telegram_id, limit=6)
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
| 57 |
|
| 58 |
messages = [{"role": "system", "content": PROMPT}] + conversation_history + [{"role": "user", "content": user_query}]
|
| 59 |
|
|
@@ -91,4 +93,10 @@ async def get_ai_response(user_query: str, telegram_id: int):
|
|
| 91 |
completion = await loop.run_in_executor(None, lambda: call_hf(messages))
|
| 92 |
response_message = completion.choices[0].message
|
| 93 |
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
conversation_history = []
|
| 52 |
if db_manager:
|
| 53 |
raw_history = db_manager.get_conversation_history(telegram_id, limit=6)
|
| 54 |
+
raw_history.reverse()
|
| 55 |
+
for msg in raw_history:
|
| 56 |
+
if msg.get('message_text'):
|
| 57 |
+
role = "user" if msg['message_type'] == 'user' else "assistant"
|
| 58 |
+
conversation_history.append({"role": role, "content": msg['message_text']})
|
| 59 |
|
| 60 |
messages = [{"role": "system", "content": PROMPT}] + conversation_history + [{"role": "user", "content": user_query}]
|
| 61 |
|
|
|
|
| 93 |
completion = await loop.run_in_executor(None, lambda: call_hf(messages))
|
| 94 |
response_message = completion.choices[0].message
|
| 95 |
|
| 96 |
+
final_response = clean_ai_response(response_message.content)
|
| 97 |
+
|
| 98 |
+
if db_manager:
|
| 99 |
+
db_manager.save_message(telegram_id, user_query, "user")
|
| 100 |
+
db_manager.save_message(telegram_id, final_response, "assistant")
|
| 101 |
+
|
| 102 |
+
return final_response
|