-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.py
More file actions
36 lines (29 loc) · 1.15 KB
/
Copy pathproxy.py
File metadata and controls
36 lines (29 loc) · 1.15 KB
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
from fastapi import FastAPI, Request, HTTPException
from fastapi.responses import StreamingResponse
import httpx
import asyncio
app = FastAPI()
OLLAMA_HOST = "http://localhost:11434"
@app.get("/")
async def root():
return {"message": "Proxy is running"}
@app.get("/api/tags")
async def get_tags():
async with httpx.AsyncClient() as client:
try:
r = await client.get(f"{OLLAMA_HOST}/api/tags")
r.raise_for_status()
return r.json()
except httpx.HTTPStatusError as e:
raise HTTPException(status_code=e.response.status_code, detail=str(e))
@app.post("/api/chat")
async def chat_proxy(request: Request):
data = await request.json()
data["think"] = False # ensure think is always false
async def event_generator():
async with httpx.AsyncClient(timeout=None) as client:
async with client.stream("POST", f"{OLLAMA_HOST}/api/chat", json=data) as response:
response.raise_for_status()
async for chunk in response.aiter_bytes():
yield chunk
return StreamingResponse(event_generator(), media_type="application/json")