File size: 1,809 Bytes
abd032e
 
 
 
5359834
45f17bf
abd032e
542f881
abd032e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc46d8e
 
abd032e
 
52fecdc
abd032e
 
 
 
 
 
 
bc46d8e
5704f7e
 
45f17bf
 
5704f7e
 
 
 
 
bc46d8e
 
5704f7e
45f17bf
 
5704f7e
 
 
 
 
bc46d8e
 
 
5704f7e
 
 
 
 
 
 
 
bc46d8e
83157ea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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")


@app.get("/")
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

@app.post("/chat")
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
@app.post("/faq")
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"}