File size: 1,094 Bytes
745d3f1
 
 
0125000
08606b0
5bf1b3e
745d3f1
5bf1b3e
 
 
eaff201
 
745d3f1
 
5bf1b3e
 
745d3f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI
from duckduckgo_search import DDGS
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware

app = FastAPI()
origins = ["*"] 

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"], 
    allow_headers=["*"], 
)

def chat_with_model(query: str, model: str) -> JSONResponse:
    results = None
    try:
        results = DDGS().chat(query, model=model)
    except Exception as e:
        return JSONResponse(content={"error": str(e)}, status_code=500)
    return JSONResponse(content={"results": results})

@app.get("/chat/")
async def chat(query: str) -> JSONResponse:
    results = None
    try:
        return chat_with_model(query, model='gpt-4o-mini')
    except Exception as e:
        try:
            return chat_with_model(query, model='claude-3-haiku')
        except Exception as e:
            return JSONResponse(content={"error": str(e)})

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860, log_level="info")