add user application status to chat

This commit is contained in:
ipu 2025-08-31 16:23:57 +03:00
parent f55e635d77
commit 8dfe081311
3 changed files with 42 additions and 8 deletions

View file

@ -13,7 +13,8 @@ async def insurance_chat(request: models.InsuranceChatRequest):
try: try:
result = await chat_service.process_insurance_chat( result = await chat_service.process_insurance_chat(
message=request.message, message=request.message,
session_id=request.session_id session_id=request.session_id,
uid=request.uid,
) )
return models.InsuranceChatResponse( return models.InsuranceChatResponse(

View file

@ -9,6 +9,7 @@ class Settings(BaseSettings):
model_config = SettingsConfigDict(extra='ignore', env_file=env_path, env_file_encoding='utf-8') model_config = SettingsConfigDict(extra='ignore', env_file=env_path, env_file_encoding='utf-8')
INSURANCE_API_BASE_URL: str INSURANCE_API_BASE_URL: str
INSURANCE_API_KEY: str = ""
TALESTORM_API_BASE_URL: str = "" TALESTORM_API_BASE_URL: str = ""
TALESTORM_API_KEY: str TALESTORM_API_KEY: str
TALESTORM_AGENT_ID: str TALESTORM_AGENT_ID: str

View file

@ -13,7 +13,8 @@ class ChatService:
def __init__(self): def __init__(self):
self.base_url = settings.TALESTORM_API_BASE_URL self.base_url = settings.TALESTORM_API_BASE_URL
self.api_key = settings.TALESTORM_API_KEY self.api_key = settings.TALESTORM_API_KEY
self.insurance_base_url = settings.INSURANCE_API_BASE_URL
self.insurance_apikey = settings.INSURANCE_API_KEY
async def get_client(self) -> httpx.AsyncClient: async def get_client(self) -> httpx.AsyncClient:
"""Get HTTP client for talestorm-ai API""" """Get HTTP client for talestorm-ai API"""
headers = {} headers = {}
@ -26,13 +27,39 @@ class ChatService:
timeout=httpx.Timeout(60.0, connect=10.0) # 30s total timeout, 10s connect timeout timeout=httpx.Timeout(60.0, connect=10.0) # 30s total timeout, 10s connect timeout
) )
async def send_message(self, session_id: str, message: str) -> Dict[str, Any]: async def get_insurance_client(self) -> httpx.AsyncClient:
headers = {}
if self.insurance_apikey:
headers["Authorization"] = f"Bearer {self.insurance_apikey}"
return httpx.AsyncClient(
base_url=self.insurance_base_url,
headers =headers,
timeout=httpx.Timeout(60.0, connect=10.0)
)
async def get_user_state(self, user_id) -> str | None:
async with await self.get_insurance_client() as client:
response = await client.get(f"/user/{user_id}/application_state")
if response.status_code != 200:
return None
response_json = response.json()
# {"userId":32,"payload":null,"phqAcceptedAt":null,"phqRejectedAt":null,"phqPendingAt":null}
if response_json.get("phqAcceptedAt"):
return "accepted"
if response_json.get("phqRejectedAt"):
return "rejected"
return None
async def send_message(self, session_id: str, message: str, instructions: str = "") -> Dict[str, Any]:
"""Send a message to talestorm-ai and get response""" """Send a message to talestorm-ai and get response"""
async with await self.get_client() as client: async with await self.get_client() as client:
try: try:
response = await client.post("/chat/", json={ response = await client.post("/chat/", json={
"chat_session_id": session_id, "chat_session_id": session_id,
"user_message": message "user_message": message,
"instructions": instructions,
}) })
if response.status_code == 200: if response.status_code == 200:
@ -103,7 +130,7 @@ class ChatService:
# For now, return empty list - this would be populated when RAG is implemented # For now, return empty list - this would be populated when RAG is implemented
return [] return []
async def process_insurance_chat(self, message: str, session_id: Optional[str] = None) -> Dict[str, Any]: async def process_insurance_chat(self, message: str, session_id: Optional[str] = None, uid: Optional[int] = None) -> Dict[str, Any]:
"""Process an insurance chat request""" """Process an insurance chat request"""
try: try:
if not session_id: if not session_id:
@ -112,7 +139,12 @@ class ChatService:
elif not await session_service.validate_session(session_id): elif not await session_service.validate_session(session_id):
session_id = await session_service.create_session(agent_id=settings.TALESTORM_AGENT_ID) session_id = await session_service.create_session(agent_id=settings.TALESTORM_AGENT_ID)
chat_response = await self.send_message(session_id, message) instructions = ""
if uid:
user_state = self.get_user_state(uid)
instructions += f"\n\n# User Information\nApplication state (None means that application was not sent or pending):\n{user_state}"
chat_response = await self.send_message(session_id, message, instructions)
history = await self.get_chat_history(session_id) history = await self.get_chat_history(session_id)
ai_response = json.loads(chat_response.get("message", {})) ai_response = json.loads(chat_response.get("message", {}))