Spaces:
Paused
Paused
Commit ·
4bdfa71
1
Parent(s): ad8945a
Bugfixes
Browse files
app.py
CHANGED
|
@@ -249,6 +249,9 @@ async def main(message: cl.Message):
|
|
| 249 |
}
|
| 250 |
)
|
| 251 |
|
|
|
|
|
|
|
|
|
|
| 252 |
for tool_call in response_message.tool_calls:
|
| 253 |
tool_name = tool_call.function.name
|
| 254 |
tool_args = json.loads(tool_call.function.arguments)
|
|
@@ -441,34 +444,52 @@ async def main(message: cl.Message):
|
|
| 441 |
f"Added tool result to history for {tool_name}: {str(tool_result)[:100]}..."
|
| 442 |
)
|
| 443 |
|
|
|
|
|
|
|
|
|
|
| 444 |
# Now let the AI provide a final response with the tool results
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
|
|
|
| 454 |
|
| 455 |
-
|
| 456 |
-
|
| 457 |
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 463 |
else:
|
| 464 |
# If no tool was called, the AI's first response is the final answer.
|
| 465 |
final_answer = response_message.content
|
| 466 |
if final_answer:
|
| 467 |
await cl.Message(content=final_answer).send()
|
|
|
|
|
|
|
|
|
|
| 468 |
|
| 469 |
except Exception as e:
|
| 470 |
log.error(f"Error in main message loop: {e}")
|
| 471 |
traceback.print_exc()
|
| 472 |
await cl.Message(content=f"An error occurred: {e}").send()
|
| 473 |
-
|
| 474 |
-
|
|
|
|
| 249 |
}
|
| 250 |
)
|
| 251 |
|
| 252 |
+
# Save history after adding tool calls to prevent corruption
|
| 253 |
+
cl.user_session.set("message_history", history)
|
| 254 |
+
|
| 255 |
for tool_call in response_message.tool_calls:
|
| 256 |
tool_name = tool_call.function.name
|
| 257 |
tool_args = json.loads(tool_call.function.arguments)
|
|
|
|
| 444 |
f"Added tool result to history for {tool_name}: {str(tool_result)[:100]}..."
|
| 445 |
)
|
| 446 |
|
| 447 |
+
# Save history immediately after adding tool result to prevent corruption
|
| 448 |
+
cl.user_session.set("message_history", history)
|
| 449 |
+
|
| 450 |
# Now let the AI provide a final response with the tool results
|
| 451 |
+
try:
|
| 452 |
+
async with cl.Step(name="Responding", type="llm") as response_step:
|
| 453 |
+
response_step.input = "Generating response based on tool results"
|
| 454 |
+
|
| 455 |
+
final_response = await openai_client.chat.completions.create(
|
| 456 |
+
model="gpt-4o",
|
| 457 |
+
messages=history,
|
| 458 |
+
tools=aggregated_tools if aggregated_tools else None,
|
| 459 |
+
tool_choice="none", # No more tool calls - just respond
|
| 460 |
+
)
|
| 461 |
|
| 462 |
+
final_message = final_response.choices[0].message
|
| 463 |
+
response_step.output = final_message
|
| 464 |
|
| 465 |
+
if final_message.content:
|
| 466 |
+
await cl.Message(content=final_message.content).send()
|
| 467 |
+
history.append(
|
| 468 |
+
{"role": "assistant", "content": final_message.content}
|
| 469 |
+
)
|
| 470 |
+
# Save history after successful final response
|
| 471 |
+
cl.user_session.set("message_history", history)
|
| 472 |
+
except Exception as final_response_error:
|
| 473 |
+
log.error(f"Error in final response generation: {final_response_error}")
|
| 474 |
+
# Even if final response fails, tool results are already in history
|
| 475 |
+
# Send a fallback message to user
|
| 476 |
+
await cl.Message(
|
| 477 |
+
content="I've executed the tool successfully but encountered an issue generating a response. The results should be displayed above."
|
| 478 |
+
).send()
|
| 479 |
+
# Make sure history is saved even on error
|
| 480 |
+
cl.user_session.set("message_history", history)
|
| 481 |
else:
|
| 482 |
# If no tool was called, the AI's first response is the final answer.
|
| 483 |
final_answer = response_message.content
|
| 484 |
if final_answer:
|
| 485 |
await cl.Message(content=final_answer).send()
|
| 486 |
+
history.append({"role": "assistant", "content": final_answer})
|
| 487 |
+
# Save history after assistant response
|
| 488 |
+
cl.user_session.set("message_history", history)
|
| 489 |
|
| 490 |
except Exception as e:
|
| 491 |
log.error(f"Error in main message loop: {e}")
|
| 492 |
traceback.print_exc()
|
| 493 |
await cl.Message(content=f"An error occurred: {e}").send()
|
| 494 |
+
# Save history even on top-level error to prevent corruption
|
| 495 |
+
cl.user_session.set("message_history", history)
|