2025-07-24 14:27:56 +01:00
|
|
|
# api/routes/chat_ai.py
|
|
|
|
|
|
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
2025-08-01 19:33:30 +01:00
|
|
|
from api.models.requests import ChatRequest, ChatMessage,SurveyRequest
|
|
|
|
|
from api.models.responses import ChatResponse,SurveyAgentResponse
|
2025-07-24 14:27:56 +01:00
|
|
|
from api.dependencies.auth import get_api_key
|
|
|
|
|
from src.llm.orchestrator import DroneBot, Message # Adjust import as needed
|
2025-08-01 19:33:30 +01:00
|
|
|
from src.llm.agent.flight_assesment import DroneAssessmentAgent
|
|
|
|
|
import json
|
2025-08-12 19:32:39 +01:00
|
|
|
from logger import logger
|
2025-07-24 14:27:56 +01:00
|
|
|
router = APIRouter(
|
2025-08-01 19:33:30 +01:00
|
|
|
prefix="/chat",
|
2025-07-24 14:27:56 +01:00
|
|
|
tags=["chat"]
|
|
|
|
|
)
|
|
|
|
|
|
2025-08-01 19:33:30 +01:00
|
|
|
@router.post("/booking-assistant", response_model=ChatResponse)
|
2025-07-24 14:27:56 +01:00
|
|
|
async def chat_ai(
|
|
|
|
|
request: ChatRequest,
|
|
|
|
|
_: str = Depends(get_api_key)
|
|
|
|
|
):
|
|
|
|
|
"""Chat with DroneBot using query and history."""
|
|
|
|
|
try:
|
2025-08-12 19:32:39 +01:00
|
|
|
logger.info(f"Starting chat request with query: {request.query}")
|
|
|
|
|
logger.info(f"History length: {len(request.history)}")
|
|
|
|
|
|
2025-08-15 17:33:43 +01:00
|
|
|
# Extract customer metadata if provided
|
|
|
|
|
customer_metadata = request.customer_metadata
|
|
|
|
|
if customer_metadata:
|
|
|
|
|
logger.info(f"Customer metadata provided: {list(customer_metadata.keys())}")
|
|
|
|
|
else:
|
|
|
|
|
logger.info("No customer metadata provided")
|
|
|
|
|
|
2025-07-24 14:27:56 +01:00
|
|
|
# Convert to internal Message format
|
|
|
|
|
history = [Message(role=msg.role, content=msg.content) for msg in request.history]
|
2025-08-12 19:32:39 +01:00
|
|
|
logger.info(f"Converted history to internal format: {len(history)} messages")
|
2025-07-24 14:27:56 +01:00
|
|
|
|
2025-08-15 17:33:43 +01:00
|
|
|
# Initialize DroneBot with history and customer metadata
|
2025-08-12 19:32:39 +01:00
|
|
|
logger.info("Initializing DroneBot...")
|
2025-08-18 14:43:03 +01:00
|
|
|
logger.info(f"History: {history}")
|
2025-08-15 17:33:43 +01:00
|
|
|
bot = DroneBot(history=history, use_openai_as_fallback=True, customer_metadata=customer_metadata)
|
2025-08-12 19:32:39 +01:00
|
|
|
logger.info("DroneBot initialized successfully")
|
|
|
|
|
|
|
|
|
|
# Get response from DroneBot
|
|
|
|
|
logger.info("Calling DroneBot.chat()...")
|
2025-08-18 21:21:08 +01:00
|
|
|
result = await bot.chat(request.query)
|
2025-08-12 19:32:39 +01:00
|
|
|
logger.info(f"DroneBot response received: {result}")
|
|
|
|
|
|
|
|
|
|
|
2025-07-24 14:27:56 +01:00
|
|
|
return ChatResponse(
|
|
|
|
|
status="success",
|
2025-08-18 21:21:08 +01:00
|
|
|
message=result
|
2025-07-24 14:27:56 +01:00
|
|
|
)
|
2025-08-12 19:32:39 +01:00
|
|
|
except HTTPException:
|
|
|
|
|
logger.error("Re-raising HTTPException")
|
|
|
|
|
raise
|
2025-07-24 14:27:56 +01:00
|
|
|
except Exception as e:
|
2025-08-12 19:32:39 +01:00
|
|
|
logger.error(f"Unexpected error in chat_ai: {str(e)}", exc_info=True)
|
2025-07-24 14:27:56 +01:00
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=500,
|
|
|
|
|
detail=str(e)
|
|
|
|
|
)
|
2025-08-01 19:33:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/analyse-survey", response_model=SurveyAgentResponse)
|
|
|
|
|
async def run_survey_agent(
|
|
|
|
|
request: SurveyRequest,
|
|
|
|
|
_: str = Depends(get_api_key)
|
|
|
|
|
):
|
|
|
|
|
"""Chat with DroneBot using query and history."""
|
|
|
|
|
try:
|
2025-08-12 15:57:07 +01:00
|
|
|
booking_form_input = request.booking_form_input
|
|
|
|
|
#from test2 import booking_form_input
|
2025-08-01 19:33:30 +01:00
|
|
|
agent = DroneAssessmentAgent()
|
|
|
|
|
result = await agent.run(booking_form_input)
|
|
|
|
|
return SurveyAgentResponse(
|
|
|
|
|
status="success",
|
|
|
|
|
result=result
|
|
|
|
|
)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=500,
|
|
|
|
|
detail=str(e)
|
|
|
|
|
)
|