Spaces:
Sleeping
Sleeping
Fix user content structure in Groq API request
Browse files- ai_service.py +7 -7
ai_service.py
CHANGED
|
@@ -12,12 +12,10 @@ async def get_ai_response(user_query: str, telegram_id: int = None):
|
|
| 12 |
return "Ai service is not available at the moment. Please try again later."
|
| 13 |
|
| 14 |
# Save user message if database is available and telegram_id is provided
|
| 15 |
-
if telegram_id and db_manager:
|
| 16 |
-
db_manager.save_message(telegram_id, user_query, "user")
|
| 17 |
-
|
| 18 |
conversation_history = ""
|
| 19 |
if telegram_id and db_manager:
|
| 20 |
-
|
|
|
|
| 21 |
|
| 22 |
|
| 23 |
query_embedding = pc.inference.embed(
|
|
@@ -36,6 +34,7 @@ async def get_ai_response(user_query: str, telegram_id: int = None):
|
|
| 36 |
retrieved_context = "\n".join([res.metadata['original_text'] for res in search_results.matches])
|
| 37 |
|
| 38 |
|
|
|
|
| 39 |
user_content = f"""
|
| 40 |
### Historical Conversation:
|
| 41 |
{conversation_history}
|
|
@@ -46,12 +45,13 @@ async def get_ai_response(user_query: str, telegram_id: int = None):
|
|
| 46 |
### Current User Message:
|
| 47 |
{user_query}
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
|
|
|
| 51 |
completion = groq_client.chat.completions.create(
|
| 52 |
messages=[
|
| 53 |
{"role": "system", "content": PROMPT},
|
| 54 |
-
{"role": "user", "content":
|
| 55 |
],
|
| 56 |
model=GROQ_MODEL,
|
| 57 |
temperature=0.1,
|
|
|
|
| 12 |
return "Ai service is not available at the moment. Please try again later."
|
| 13 |
|
| 14 |
# Save user message if database is available and telegram_id is provided
|
|
|
|
|
|
|
|
|
|
| 15 |
conversation_history = ""
|
| 16 |
if telegram_id and db_manager:
|
| 17 |
+
db_manager.save_message(telegram_id, user_query, "user")
|
| 18 |
+
conversation_history = db_manager.get_formatted_history(telegram_id, limit=6)
|
| 19 |
|
| 20 |
|
| 21 |
query_embedding = pc.inference.embed(
|
|
|
|
| 34 |
retrieved_context = "\n".join([res.metadata['original_text'] for res in search_results.matches])
|
| 35 |
|
| 36 |
|
| 37 |
+
# Prepare the user content with conversation history and context
|
| 38 |
user_content = f"""
|
| 39 |
### Historical Conversation:
|
| 40 |
{conversation_history}
|
|
|
|
| 45 |
### Current User Message:
|
| 46 |
{user_query}
|
| 47 |
|
| 48 |
+
Based on the above information, provide an accurate and helpful response to the customer:
|
| 49 |
+
"""
|
| 50 |
+
|
| 51 |
completion = groq_client.chat.completions.create(
|
| 52 |
messages=[
|
| 53 |
{"role": "system", "content": PROMPT},
|
| 54 |
+
{"role": "user", "content": user_content}
|
| 55 |
],
|
| 56 |
model=GROQ_MODEL,
|
| 57 |
temperature=0.1,
|