File size: 1,443 Bytes
791d677
5014fbd
e26fa15
 
 
791d677
 
 
5014fbd
 
 
 
 
 
2388163
5014fbd
 
 
 
 
 
 
 
 
 
 
e26fa15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
791d677
e26fa15
5014fbd
 
 
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
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from functools import lru_cache
from clients.groq_client import GroqClient

app = FastAPI()


# ✅ Allowed origins
origins = [
    "http://localhost:4200",
    "https://mosesmangabo.vercel.app",
    "https://chatbot-bridge-service.onrender.com"
    "https://eng-musa-chatbotbridge.hf.space"
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],  # allow all HTTP methods (GET, POST, etc.)
    allow_headers=["*"],  # allow all headers
)


class QueryRequest(BaseModel):
    query: str

# Lazy singleton for GroqClient
@lru_cache(maxsize=1)
def get_groq_client():
    return GroqClient()

@app.post("/chat")
async def chat_endpoint(request: QueryRequest):
    try:
        groq_client = get_groq_client()
        response = groq_client.ask(request.query)
        if response.startswith("Error while"):
            return {"status_code": 500, "response": response, "next_questions": []}
        return {
            "status_code": 200,
            "response": response,
            "next_questions": groq_client.get_next_questions(),
        }
    except Exception as e:
        return {"status_code": 500, "response": f"Internal error: {e}", "next_questions": []}

@app.get("/")
def healthcheck():
    return {"status": "ok"}

#python -m uvicorn main:app --reload