add user application status to chat
This commit is contained in:
parent
f55e635d77
commit
8dfe081311
3 changed files with 42 additions and 8 deletions
|
|
@ -13,7 +13,8 @@ async def insurance_chat(request: models.InsuranceChatRequest):
|
|||
try:
|
||||
result = await chat_service.process_insurance_chat(
|
||||
message=request.message,
|
||||
session_id=request.session_id
|
||||
session_id=request.session_id,
|
||||
uid=request.uid,
|
||||
)
|
||||
|
||||
return models.InsuranceChatResponse(
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ class Settings(BaseSettings):
|
|||
model_config = SettingsConfigDict(extra='ignore', env_file=env_path, env_file_encoding='utf-8')
|
||||
|
||||
INSURANCE_API_BASE_URL: str
|
||||
INSURANCE_API_KEY: str = ""
|
||||
TALESTORM_API_BASE_URL: str = ""
|
||||
TALESTORM_API_KEY: str
|
||||
TALESTORM_AGENT_ID: str
|
||||
|
|
|
|||
|
|
@ -13,7 +13,8 @@ class ChatService:
|
|||
def __init__(self):
|
||||
self.base_url = settings.TALESTORM_API_BASE_URL
|
||||
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:
|
||||
"""Get HTTP client for talestorm-ai API"""
|
||||
headers = {}
|
||||
|
|
@ -25,14 +26,40 @@ class ChatService:
|
|||
headers=headers,
|
||||
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"""
|
||||
async with await self.get_client() as client:
|
||||
try:
|
||||
response = await client.post("/chat/", json={
|
||||
"chat_session_id": session_id,
|
||||
"user_message": message
|
||||
"user_message": message,
|
||||
"instructions": instructions,
|
||||
})
|
||||
|
||||
if response.status_code == 200:
|
||||
|
|
@ -103,7 +130,7 @@ class ChatService:
|
|||
# For now, return empty list - this would be populated when RAG is implemented
|
||||
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"""
|
||||
try:
|
||||
if not session_id:
|
||||
|
|
@ -111,8 +138,13 @@ class ChatService:
|
|||
|
||||
elif not await session_service.validate_session(session_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)
|
||||
|
||||
ai_response = json.loads(chat_response.get("message", {}))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue