-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.py
More file actions
167 lines (137 loc) · 4.49 KB
/
Copy pathproxy.py
File metadata and controls
167 lines (137 loc) · 4.49 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python3
from typing import Any, Dict
import httpx
import uvicorn
from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import JSONResponse
MODEL_NAME = "nomic-ai/nomic-embed-text-v1.5"
UPSTREAM_MODEL = "embed"
PORT = 5055
UPSTREAM_EMBEDDINGS_URL = "http://127.0.0.1:8005/v1/embeddings"
UPSTREAM_MODELS_URL = "http://127.0.0.1:8005/v1/models"
app = FastAPI(title="Embedding Proxy", version="1.0.0")
@app.get("/health")
async def health() -> Dict[str, str]:
return {"status": "ok"}
@app.get("/health/model")
async def health_model() -> Dict[str, Any]:
try:
async with httpx.AsyncClient(timeout=10.0) as client:
response = await client.get(UPSTREAM_MODELS_URL)
except httpx.RequestError as exc:
raise HTTPException(
status_code=502,
detail={
"status": "error",
"message": "Could not reach upstream model server",
"upstream_url": UPSTREAM_MODELS_URL,
"error": str(exc),
},
)
if response.status_code != 200:
raise HTTPException(
status_code=502,
detail={
"status": "error",
"message": "Upstream model server returned non-200 status",
"upstream_status_code": response.status_code,
"upstream_body": response.text,
},
)
try:
data = response.json()
except Exception as exc:
raise HTTPException(
status_code=502,
detail={
"status": "error",
"message": "Upstream model server returned invalid JSON",
"error": str(exc),
"upstream_body": response.text,
},
)
models = data.get("data", [])
model_ids = {
model.get("id")
for model in models
if isinstance(model, dict)
}
upstream_available = UPSTREAM_MODEL in model_ids
return {
"status": "ok" if upstream_available else "degraded",
"public_model": MODEL_NAME,
"upstream_model": UPSTREAM_MODEL,
"upstream_url": UPSTREAM_MODELS_URL,
"upstream_available": upstream_available,
"available_upstream_models": sorted(model_ids),
}
@app.get("/v1/models")
async def list_models() -> Dict[str, Any]:
return {
"object": "list",
"data": [
{
"id": MODEL_NAME,
"object": "model",
"owned_by": "local",
}
],
}
@app.post("/v1/embeddings")
async def proxy_embeddings(request: Request) -> JSONResponse:
try:
payload = await request.json()
except Exception as exc:
raise HTTPException(status_code=400, detail=f"Invalid JSON: {exc}")
if not isinstance(payload, dict):
raise HTTPException(status_code=400, detail="Request body must be a JSON object")
requested_model = payload.get("model")
if requested_model not in {MODEL_NAME, UPSTREAM_MODEL, None}:
raise HTTPException(
status_code=400,
detail=f"Unsupported model '{requested_model}'. Expected '{MODEL_NAME}'.",
)
upstream_payload = dict(payload)
upstream_payload["model"] = UPSTREAM_MODEL
headers = {}
auth = request.headers.get("authorization")
if auth:
headers["authorization"] = auth
try:
async with httpx.AsyncClient(timeout=120.0) as client:
upstream_response = await client.post(
UPSTREAM_EMBEDDINGS_URL,
json=upstream_payload,
headers=headers,
)
except httpx.RequestError as exc:
raise HTTPException(
status_code=502,
detail=f"Could not reach upstream embedding server: {exc}",
)
try:
data = upstream_response.json()
except Exception:
return JSONResponse(
status_code=upstream_response.status_code,
content={
"error": {
"message": upstream_response.text,
"type": "upstream_non_json_response",
}
},
)
return JSONResponse(
status_code=upstream_response.status_code,
content=data,
)
@app.post("/embeddings")
async def proxy_embeddings_short(request: Request) -> JSONResponse:
return await proxy_embeddings(request)
if __name__ == "__main__":
uvicorn.run(
"proxy:app",
host="0.0.0.0",
port=PORT,
reload=False,
)