add initialize endpoint

This commit is contained in:
ipu 2025-09-29 18:09:44 +03:00
parent 52c7292d94
commit e0de48a66a
3 changed files with 92 additions and 1 deletions

View file

@ -8,6 +8,14 @@ from .session_service import session_service
from ..api.v1.models import Source, HistoryItem
from ..config import settings
from ..database import Session, UserSession
from datetime import datetime
# vars: name
INIT_MESSAGES = [
"Hi. Im Alia, your personal benefits assistant! If you have a question at any point in the enrollment process, just ask, and Ill try to get you an answer.",
"Hi, {name}! Welcome back! As usual, Im here if you have any questions!",
"Hi, {name}! Welcome back! Do you want to pick up where you left off and complete your PHQ?",
]
class ChatService:
@ -125,6 +133,18 @@ class ChatService:
# raise e
print(f"Error getting chat history: {e}")
return []
async def get_last_chat_message_date(self, session_id: str):
async with await self.get_client() as client:
try:
response = await client.get("/chat/", params={"chat_session_id": session_id, "limit": 1})
resp_json = response.json()
msg_date = resp_json[0]["created_at"]
return datetime.fromisoformat(msg_date)
except:
return None
def _extract_sources_from_response(self, response_text: str) -> List[Source]:
"""Extract sources from RAG search results if available"""
@ -153,6 +173,51 @@ class ChatService:
session.add(user_session)
session.commit()
async def initialize_chat(self, uid: str, application):
session_id = await self.get_user_session(uid)
if not session_id or not await session_service.validate_session(session_id):
session_id = await session_service.create_session(agent_id=settings.TALESTORM_AGENT_ID)
try:
await self.create_user_session(uid, session_id)
except:
pass
try:
name = application["applicants"][0]["firstName"]
except:
return {
"session_id": session_id,
"answer": INIT_MESSAGES[0],
}
last_message = self.get_last_chat_message_date(session_id)
if not last_message:
return {
"session_id": session_id,
"answer": INIT_MESSAGES[0],
}
applicant = application["applicants"][0]
if not applicant.get("gender") or not applicant.get("dob") or applicant.get("dob") == "-01-" or not applicant.get("weight") or applicant.get("heightFt") is None or applicant.get("heightIn") is None:
return {
"session_id": session_id,
"answer": INIT_MESSAGES[2].format(name=name)
}
return {
"session_id": session_id,
"answer": INIT_MESSAGES[1].format(name=name)
}
async def process_insurance_chat(self, message: str, session_id: Optional[str] = None, uid: Optional[str] = None, current_page: Optional[str] = None, application: Optional[dict] = None) -> Dict[str, Any]:
"""Process an insurance chat request"""
@ -162,7 +227,10 @@ class ChatService:
session_id = await self.get_user_session(uid)
if not session_id or not await session_service.validate_session(session_id):
session_id = await session_service.create_session(agent_id=settings.TALESTORM_AGENT_ID)
await self.create_user_session(uid, session_id)
try:
await self.create_user_session(uid, session_id)
except:
pass
else:
session_id = await session_service.create_session(agent_id=settings.TALESTORM_AGENT_ID)