from fastapi import FastAPI, Request from fastapi.responses import JSONResponse, Response import httpx app = FastAPI() OPENROUTER_KEY = "sk-9cfed30eb6894df5b9a3ffb4b1fb956d" @app.post("/v1/chat/completions") async def proxy(req: Request): body = await req.json() async with httpx.AsyncClient() as client: try: r = await client.post( "https://api.deepseek.com/chat/completions", headers={ "Authorization": f"Bearer {OPENROUTER_KEY}", "HTTP-Referer": "http://localhost", "model": "deepseek-v4-flash", "X-Title": "rag-engine" }, json=body, timeout=300, ) except httpx.HTTPError as e: # Ошибка сети / таймаут return JSONResponse( status_code=502, content={"error": "Bad gateway", "details": str(e)}, ) print(r.text) # Если бэкенд всегда отвечает JSON: # return JSONResponse(status_code=r.status_code, content=r.json()) # Универсальный вариант — пробрасываем «как есть» return Response( content=r.content, status_code=r.status_code, media_type=r.headers.get("content-type", "application/json"), )