add webpage info

This commit is contained in:
ipu 2025-09-03 22:42:24 +03:00
parent e4d2e64ce7
commit 959fd1ac86
5 changed files with 83 additions and 8 deletions

View file

@ -3,6 +3,7 @@ from fastapi import APIRouter, HTTPException
from src.services.estimation_service_v2 import EstimationService
from . import models
from ...cache.page_cache import get_page_description
from ...services.chat_service import chat_service
router = APIRouter()
@ -11,10 +12,15 @@ router = APIRouter()
async def insurance_chat(request: models.InsuranceChatRequest):
"""Handle insurance chat requests"""
try:
current_page = None
if request.context and request.context.page:
page_id = request.context.page
current_page = await get_page_description(page_id)
result = await chat_service.process_insurance_chat(
message=request.message,
session_id=request.session_id,
uid=str(request.userId),
current_page=current_page,
)
return models.InsuranceChatResponse(

9
src/cache/page_cache.py vendored Normal file
View file

@ -0,0 +1,9 @@
from src.database import Session, Webpage
async def get_page_description(page_id: str) -> str | None:
with Session() as session:
page = session.query(Webpage).filter(Webpage.id == page_id).first()
if not page:
return None
return page.description

View file

@ -1,4 +1,4 @@
from sqlalchemy import BigInteger, Column, MetaData, String, Float, Text, Index
from sqlalchemy import BigInteger, Column, MetaData, String, Float, Text, Index, Integer
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
@ -26,5 +26,13 @@ Index(
)
class Webpage(Base):
__tablename__ = "webpage"
id = Column(String, primary_key=True, index=True)
n = Column(Integer, index=True)
description = Column(String)
engine = create_engine(settings.DATABASE_URL)
Session = sessionmaker(autocommit=False, autoflush=False, bind=engine)

View file

@ -1,11 +1,13 @@
import json
import httpx
from typing import Dict, Any, List, Optional
from src.models import ApplicantParam, ChatHook, PlansParam
from ..config import settings
import httpx
from src.models import ApplicantParam, ChatHook, PlansParam, InsuranceChatContext
from .session_service import session_service
from ..api.v1.models import Source, HistoryItem
from ..config import settings
class ChatService:
"""Service for handling chat functionality with talestorm-ai"""
@ -93,7 +95,7 @@ class ChatService:
continue
kind = item.get("kind", "")
# Handle request messages (user input)
if kind == "request":
parts = item.get("parts", [])
@ -108,15 +110,18 @@ class ChatService:
elif kind == "response":
parts = item.get("parts", [])
for part in parts:
if part.get("part_kind") == "text":
if part.get("tool_name") == "final_result":
tool_args = json.loads(part.get("args"))
history.append(HistoryItem(
role="assistant",
message=part.get("content", "")
message=tool_args.get("answer", "")
))
return history
return []
except Exception as e:
# raise e
print(f"Error getting chat history: {e}")
return []
@ -130,7 +135,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, uid: Optional[int] = None) -> Dict[str, Any]:
async def process_insurance_chat(self, message: str, session_id: Optional[str] = None, uid: Optional[int] = None, current_page: Optional[str] = None) -> Dict[str, Any]:
"""Process an insurance chat request"""
try:
if not session_id:
@ -143,6 +148,8 @@ class ChatService:
if uid:
user_state = await 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}"
if current_page:
instructions += f"\n\n# User now is currently on page:"
chat_response = await self.send_message(session_id, message, instructions)
history = await self.get_chat_history(session_id)