File size: 1,640 Bytes
9edc2f1
0125000
9edc2f1
 
08606b0
5bf1b3e
 
9edc2f1
5bf1b3e
 
9edc2f1
eaff201
9edc2f1
 
5bf1b3e
 
9edc2f1
 
745d3f1
 
9edc2f1
 
 
 
 
745d3f1
9edc2f1
 
 
 
745d3f1
9edc2f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
745d3f1
 
 
0733eda
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
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from duckai import DuckAI

app = FastAPI()

# Configure CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
    allow_headers=["*"],
)

class ChatQuery(BaseModel):
    query: str

@app.get("/chat/")
async def chat(query: str):
    if not query:
        raise HTTPException(status_code=400, detail="Query parameter is required")

    duck = DuckAI()
    try:
        results = duck.chat(query, model='gpt-4o-mini')
        return {"results": results}
    except Exception as e1:
        print(f"Primary model (gpt-4o-mini) failed: {e1}")
        try:
            results = duck.chat(query, model='claude-3-haiku')
            return {"results": results}
        except Exception as e2:
            print(f"Fallback model (claude-3-haiku) also failed: {e2}")
            raise HTTPException(
                status_code=500,
                detail={
                    "error": "Both models failed",
                    "primary_error": str(e1),
                    "fallback_error": str(e2)
                }
            )

async def chat_with_model(query: str, model: str):
    try:
        duck = DuckAI()
        results = duck.chat(query, model=model)
        return {"results": results}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))


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