fixed issues

This commit is contained in:
OwusuBlessing
2025-08-18 14:43:03 +01:00
parent bbc94dfcc0
commit 3e7a300eef
5 changed files with 25 additions and 43 deletions
+12 -35
View File
@@ -36,6 +36,7 @@ async def chat_ai(
# Initialize DroneBot with history and customer metadata
logger.info("Initializing DroneBot...")
logger.info(f"History: {history}")
bot = DroneBot(history=history, use_openai_as_fallback=True, customer_metadata=customer_metadata)
logger.info("DroneBot initialized successfully")
@@ -44,45 +45,21 @@ async def chat_ai(
result = bot.chat(request.query)
logger.info(f"DroneBot response received: {result}")
# Validate result and final_message
if not result or "final_message" not in result:
logger.error(f"Invalid result from DroneBot: {result}")
raise HTTPException(
status_code=500,
detail="Invalid response from DroneBot: missing final_message"
)
final_message = result["final_message"]
logger.info(f"Final message extracted: {final_message}")
if not final_message or not isinstance(final_message, str):
logger.error(f"Final message is not a valid string: {type(final_message)} - {final_message}")
raise HTTPException(
status_code=500,
detail="Invalid response from DroneBot: final_message is not a valid string"
)
try:
logger.info("Attempting to parse final_message as JSON...")
message = json.loads(final_message)
logger.info("JSON parsing successful")
except json.JSONDecodeError as json_error:
logger.warning(f"JSON decode error: {json_error}")
logger.warning(f"Raw final_message: {final_message}")
final_message_json = json.loads(result["final_message"])
# If JSON parsing fails, create a fallback structure
message = {
"message": final_message,
"options": None,
"requires_selection": False,
"end": "in_progress",
"form": {}
}
logger.info("Created fallback message structure")
# message = {
# "message": final_message_json,
# "options": None,
# "requires_selection": False,
# "end": "in_progress",
# "form": {}
# }
logger.info("Created fallback message structure")
logger.info(f"Final message to return: {message}")
logger.info(f"Final message to return: {final_message_json}")
return ChatResponse(
status="success",
message=message
message=final_message_json
)
except HTTPException:
logger.error("Re-raising HTTPException")