Jeremy Live
commited on
Commit
路
0897890
1
Parent(s):
698b8b0
memory1
Browse files
app.py
CHANGED
@@ -291,8 +291,10 @@ def create_agent(llm, db_connection, *, run_test: bool = True):
|
|
291 |
# test_result = agent.run(test_query)
|
292 |
# logger.info(f"Agent test query successful: {str(test_result)[:200]}...")
|
293 |
# except Exception as e:
|
294 |
-
# logger.warning(
|
295 |
-
#
|
|
|
|
|
296 |
|
297 |
logger.info("SQL agent created successfully")
|
298 |
return agent, ""
|
@@ -488,6 +490,7 @@ def convert_to_messages_format(chat_history):
|
|
488 |
return []
|
489 |
|
490 |
messages = []
|
|
|
491 |
|
492 |
# If the first element is a list, assume it's in the old format
|
493 |
if isinstance(chat_history[0], list):
|
@@ -522,9 +525,13 @@ async def stream_agent_response(question: str, chat_history: List[List[str]], se
|
|
522 |
# Add previous chat history in the correct format for the agent
|
523 |
for msg_pair in chat_history:
|
524 |
if len(msg_pair) >= 1 and msg_pair[0]: # User message
|
525 |
-
|
|
|
|
|
526 |
if len(msg_pair) >= 2 and msg_pair[1]: # Assistant message
|
527 |
-
|
|
|
|
|
528 |
|
529 |
# Add current user's question
|
530 |
user_message = HumanMessage(content=question)
|
@@ -564,7 +571,11 @@ async def stream_agent_response(question: str, chat_history: List[List[str]], se
|
|
564 |
# Execute the agent with proper error handling
|
565 |
try:
|
566 |
# Let the agent use its memory; don't pass raw chat_history
|
567 |
-
|
|
|
|
|
|
|
|
|
568 |
logger.info(f"Agent response type: {type(response)}")
|
569 |
logger.info(f"Agent response content: {str(response)[:500]}...")
|
570 |
|
@@ -654,7 +665,10 @@ async def stream_agent_response(question: str, chat_history: List[List[str]], se
|
|
654 |
"Devuelve SOLO la consulta SQL en un bloque ```sql``` para responder a: "
|
655 |
f"{question}. No incluyas explicaci贸n ni texto adicional."
|
656 |
)
|
657 |
-
sql_only_resp = await active_agent.ainvoke({
|
|
|
|
|
|
|
658 |
sql_only_text = str(sql_only_resp)
|
659 |
sql_query2 = extract_sql_query(sql_only_text)
|
660 |
if sql_query2 and looks_like_sql(sql_query2):
|
|
|
291 |
# test_result = agent.run(test_query)
|
292 |
# logger.info(f"Agent test query successful: {str(test_result)[:200]}...")
|
293 |
# except Exception as e:
|
294 |
+
# logger.warning(
|
295 |
+
# f"Agent test query failed (this might be expected): {str(e)}"
|
296 |
+
# )
|
297 |
+
# Continue even if test fails, as it might be due to model limitations
|
298 |
|
299 |
logger.info("SQL agent created successfully")
|
300 |
return agent, ""
|
|
|
490 |
return []
|
491 |
|
492 |
messages = []
|
493 |
+
prev_messages = []
|
494 |
|
495 |
# If the first element is a list, assume it's in the old format
|
496 |
if isinstance(chat_history[0], list):
|
|
|
525 |
# Add previous chat history in the correct format for the agent
|
526 |
for msg_pair in chat_history:
|
527 |
if len(msg_pair) >= 1 and msg_pair[0]: # User message
|
528 |
+
hm = HumanMessage(content=msg_pair[0])
|
529 |
+
messages.append(hm)
|
530 |
+
prev_messages.append(hm)
|
531 |
if len(msg_pair) >= 2 and msg_pair[1]: # Assistant message
|
532 |
+
am = AIMessage(content=msg_pair[1])
|
533 |
+
messages.append(am)
|
534 |
+
prev_messages.append(am)
|
535 |
|
536 |
# Add current user's question
|
537 |
user_message = HumanMessage(content=question)
|
|
|
571 |
# Execute the agent with proper error handling
|
572 |
try:
|
573 |
# Let the agent use its memory; don't pass raw chat_history
|
574 |
+
# Pass explicit chat_history as well to maximize memory usage
|
575 |
+
response = await active_agent.ainvoke({
|
576 |
+
"input": question,
|
577 |
+
"chat_history": prev_messages,
|
578 |
+
})
|
579 |
logger.info(f"Agent response type: {type(response)}")
|
580 |
logger.info(f"Agent response content: {str(response)[:500]}...")
|
581 |
|
|
|
665 |
"Devuelve SOLO la consulta SQL en un bloque ```sql``` para responder a: "
|
666 |
f"{question}. No incluyas explicaci贸n ni texto adicional."
|
667 |
)
|
668 |
+
sql_only_resp = await active_agent.ainvoke({
|
669 |
+
"input": sql_only_prompt,
|
670 |
+
"chat_history": prev_messages,
|
671 |
+
})
|
672 |
sql_only_text = str(sql_only_resp)
|
673 |
sql_query2 = extract_sql_query(sql_only_text)
|
674 |
if sql_query2 and looks_like_sql(sql_query2):
|