File size: 1,831 Bytes
71a7938
 
 
df0b0dd
71a7938
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df0b0dd
 
8e9388d
df0b0dd
 
 
 
 
8e9388d
df0b0dd
 
 
 
 
 
 
 
 
 
 
8e9388d
df0b0dd
71a7938
df0b0dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import os
from fastapi import HTTPException
from agent import agent_with_db
from schemas import request
from dotenv import load_dotenv
load_dotenv()

app = FastAPI()
allowed_origins = os.getenv("ALLOWED_ORIGINS").split(',')

app.add_middleware(
    CORSMiddleware,
    allow_origins=allowed_origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)
global agent
agent = agent_with_db()

async def parse_user_data(user_data):
    
    if user_data is None:
        return "No user data available"
    
    # Ensure input is a dictionary
    if not isinstance(user_data, dict):
        raise ValueError("Input must be a dictionary or None")
    
    # Create user info dictionary with default values
    user_info = {
        "state_user_belongs_to": (
            user_data.get("state") if user_data.get("state") is not None 
            else "No state information available"
        ),
        "sex_of_user": (
            user_data.get("gender") if user_data.get("gender") is not None 
            else "Information not available"
        )
    }
    
    return user_info

@app.post("/retrieve", status_code=200)
async def retrieve(request:request):
    try:
        prev_conv = request.previous_state
        user_info = await parse_user_data(request.user_data)
        
        if prev_conv is None:
            prev_conv = "No previous conversation available, first time"
        query = request.query
        prev_conv = str(prev_conv)
        user_info = str(user_info)
        response = agent({"query": query, "previous_conversation": prev_conv, "user_data": user_info})

        return {"response": response["result"]}
    
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))