add hooks

This commit is contained in:
ipu 2025-08-12 21:36:46 +03:00
parent a6933ecc1c
commit ee1bb20d54
3 changed files with 27 additions and 3 deletions

View file

@ -25,6 +25,7 @@ async def insurance_chat(request: models.InsuranceChatRequest):
) )
except Exception as e: except Exception as e:
# raise e
raise HTTPException(status_code=500, detail=f"Error processing chat request: {str(e)}") raise HTTPException(status_code=500, detail=f"Error processing chat request: {str(e)}")
@router.post("/estimation", response_model=models.EstimationResponse) @router.post("/estimation", response_model=models.EstimationResponse)

View file

@ -111,7 +111,9 @@ class ChatHook(BaseModel):
class AIChatResponse(BaseModel): class AIChatResponse(BaseModel):
answer: str answer: str
hooks: List[ChatHook] = [] show_plans: list[int] | None = None
compare_plans: list[int] | None = None
update_applicants: list[Applicant] | None = None
class InsuranceChatResponse(BaseModel): class InsuranceChatResponse(BaseModel):
session_id: str session_id: str

View file

@ -1,6 +1,8 @@
import json import json
import httpx import httpx
from typing import Dict, Any, List, Optional from typing import Dict, Any, List, Optional
from src.models import ApplicantParam, ChatHook, PlansParam
from ..config import settings from ..config import settings
from .session_service import session_service from .session_service import session_service
from ..api.v1.models import Source, HistoryItem from ..api.v1.models import Source, HistoryItem
@ -115,7 +117,25 @@ class ChatService:
ai_response = json.loads(chat_response.get("message", {})) ai_response = json.loads(chat_response.get("message", {}))
ai_message = ai_response.get("message", "") ai_message = ai_response.get("message", "")
hooks = ai_response.get("hooks", []) compare_plans = ai_response.get("compare_plans")
show_plans = ai_response.get("show_plans")
update_applicants = ai_response.get("update_applicants")
hooks = []
if update_applicants:
hooks.append(ChatHook(
tool="update_applicants",
params=ApplicantParam(applicants=update_applicants)
))
elif compare_plans:
hooks.append(ChatHook(
tool="compare_plans",
params=PlansParam(plans=compare_plans)
))
elif show_plans:
hooks.append(ChatHook(
tool="show_plans",
params=PlansParam(plans=show_plans)
))
return { return {
"session_id": session_id, "session_id": session_id,
@ -131,7 +151,8 @@ class ChatService:
"session_id": session_id or "fallback-session", "session_id": session_id or "fallback-session",
"answer": f"I'm sorry, I'm experiencing technical difficulties. Please try again later. Error: {str(e)}", "answer": f"I'm sorry, I'm experiencing technical difficulties. Please try again later. Error: {str(e)}",
"sources": [], "sources": [],
"history": [] "history": [],
"hooks": [],
} }
# Global chat service instance # Global chat service instance