Spaces:
Sleeping
Sleeping
| from fastapi import APIRouter, HTTPException | |
| from pydantic import BaseModel | |
| from app.chatbot.demo_rag import get_response | |
| from app.config import demo_chatbot_configs, demo_form_submissions | |
| from app.chatbot.demo_rag import session_histories, HumanMessage, AIMessage, log_chat | |
| router = APIRouter() | |
| class ChatInput(BaseModel): | |
| question: str | |
| session_id: str | |
| name: str | |
| email: str | |
| #FAQ logging endpoint | |
| class FAQInput(BaseModel): | |
| session_id: str | |
| question: str | |
| answer: str | |
| name: str | |
| email: str | |
| async def demo_chat(submission_id: str, input: ChatInput): | |
| # Lazy-load chatbot config by submission_id | |
| print(f"Got question: {input.question} for submission_id: {submission_id} and session_id: {input.session_id}") | |
| # Fetch RAG config using submission_id | |
| rag_config = demo_chatbot_configs.find_one({"submission_id": submission_id}) | |
| if not rag_config: | |
| raise HTTPException(status_code=404, detail="Chatbot not found") | |
| config = { | |
| "configurable": { | |
| "thread_id": input.session_id | |
| } | |
| } | |
| response = await get_response( | |
| query=input.question, | |
| session_id=input.session_id, | |
| name=input.name, | |
| email=input.email, | |
| rag_config=rag_config, | |
| config=config | |
| ) | |
| return {"answer": response.get("response", "")} | |
| async def demo_chatbot_status(submission_id: str): | |
| """ | |
| Returns the per-stage progress of a demo chatbot based on the submission_id. | |
| """ | |
| config = demo_form_submissions.find_one({"submission_id": submission_id}) | |
| if not config: | |
| raise HTTPException(status_code=404, detail="Demo submission not found") | |
| return { | |
| "stages": config.get("stages", {}), | |
| "error": config.get("error"), | |
| } | |
| async def demo_chatbot_info(submission_id: str): | |
| """ | |
| Returns basic info about the demo chatbot. | |
| """ | |
| config = demo_chatbot_configs.find_one({"submission_id": submission_id}) | |
| if not config: | |
| raise HTTPException(status_code=404, detail="Demo submission not found") | |
| print(config.keys) | |
| return { | |
| "chatbot_name": config.get("chatbot_name"), | |
| "company_name": config.get("company_name"), | |
| "welcome_message": config.get("welcome_message") | |
| } | |
| async def log_faq(chatbot_id: str, faq: FAQInput): | |
| print(f'{chatbot_id}') | |
| session_id = faq.session_id | |
| question = faq.question | |
| answer = faq.answer | |
| name = faq.name | |
| email = faq.email | |
| # Load RAG config | |
| rag_config = demo_chatbot_configs.find_one({"chatbot_id": chatbot_id}) | |
| if not rag_config: | |
| raise HTTPException(status_code=404, detail="Chatbot not found") | |
| # Ensure session exists | |
| if session_id not in session_histories: | |
| session_histories[session_id] = [] | |
| # Append FAQ to session history | |
| session_histories[session_id].append(HumanMessage(content=question)) | |
| session_histories[session_id].append(AIMessage(content=answer)) | |
| # Log to DB | |
| log_chat( | |
| session_id=session_id, | |
| company_id=rag_config.get('company_id'), | |
| chatbot_id=rag_config.get('chatbot_id'), | |
| name=name, | |
| email=email, | |
| query=question, | |
| answer=answer, | |
| metadata={"source": "FAQ"} | |
| ) | |
| return {"status": "success"} |