Spaces:
Running
Running
| from fastapi import FastAPI | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from pydantic import BaseModel | |
| from app.rag import get_response | |
| from datetime import datetime | |
| app = FastAPI(root_path="/aurochat") | |
| def root(): | |
| return {"message": "Wellness Chatbot API is live."} | |
| # CORS setup (so React can call API) | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| class ChatInput(BaseModel): | |
| question: str | |
| session_id: str | |
| name: str | |
| email: str | |
| async def chat(input: ChatInput): | |
| print("Got question:", input.question) | |
| print("Session ID:", input.session_id) | |
| config = { | |
| 'configurable': { | |
| 'thread_id': input.session_id | |
| } | |
| } | |
| response = await get_response(input.question, name=input.name, email=input.email, config=config) | |
| return {"answer": response['response']} | |
| #FAQ logging endpoint | |
| class FAQInput(BaseModel): | |
| session_id: str | |
| question: str | |
| answer: str | |
| name: str | |
| email: str | |
| #Log FAQ in conversation History even though RAG not being called so future RAG calls have access to info | |
| async def log_faq(faq: FAQInput): | |
| session_id = faq.session_id | |
| question = faq.question | |
| answer = faq.answer | |
| name=faq.name | |
| email=faq.email | |
| from app.rag import session_histories, HumanMessage, AIMessage, log_chat | |
| if session_id not in session_histories: | |
| session_histories[session_id] = [] | |
| session_histories[session_id].append(HumanMessage(content=question)) | |
| session_histories[session_id].append(AIMessage(content=answer)) | |
| log_chat(session_id=session_id, name=name, email=email, query=question, answer=answer, metadata={"source": "FAQ"}) | |
| return {"status": "success"} | |