| from fastapi import FastAPI, Request, Response |
| import httpx |
|
|
| app = FastAPI() |
| BACKEND = "http://127.0.0.1:8000" |
|
|
| @app.get("/") |
| async def root(): |
| return { |
| "ok": True, |
| "message": "GGUF OpenAI-compatible API is running", |
| "chat_completions": "/v1/chat/completions" |
| } |
|
|
| @app.get("/health") |
| async def health(): |
| async with httpx.AsyncClient(timeout=10) as client: |
| r = await client.get(f"{BACKEND}/health") |
| return Response( |
| content=r.content, |
| status_code=r.status_code, |
| media_type=r.headers.get("content-type", "application/json"), |
| ) |
|
|
| @app.api_route("/v1/{path:path}", methods=["GET", "POST", "PUT", "PATCH", "DELETE"]) |
| async def proxy(path: str, request: Request): |
| url = f"{BACKEND}/v1/{path}" |
| body = await request.body() |
| headers = dict(request.headers) |
| headers.pop("host", None) |
|
|
| async with httpx.AsyncClient(timeout=None) as client: |
| r = await client.request( |
| method=request.method, |
| url=url, |
| headers=headers, |
| content=body, |
| params=request.query_params, |
| ) |
|
|
| return Response( |
| content=r.content, |
| status_code=r.status_code, |
| media_type=r.headers.get("content-type", "application/json"), |
| ) |