File size: 788 Bytes
196b20d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from g4f.client import Client
import uvicorn

app = FastAPI()
client = Client()

class ChatRequest(BaseModel):
    message: str
    model: str = "gpt-4.1"

@app.get("/")
def root():
    return {"status": "ok", "engine": "g4f"}

@app.post("/chat")
def chat(req: ChatRequest):
    try:
        response = client.chat.completions.create(
            model=req.model,
            messages=[{"role": "user", "content": req.message}],
            web_search=False
        )
        return {
            "reply": response.choices[0].message.content
        }
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

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