test3 / app.py
trackingsvg's picture
Create app.py
196b20d verified
raw
history blame contribute delete
788 Bytes
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)