Spaces:
Runtime error
Runtime error
Eric Michael Martinez
commited on
Commit
•
9884bb9
1
Parent(s):
37a6d72
add batch embeddings API
Browse files- app/app.py +29 -0
app/app.py
CHANGED
@@ -273,6 +273,35 @@ async def openai_api_embeddings_passthrough(
|
|
273 |
return response.json()
|
274 |
|
275 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
276 |
@app.post("/v1/completions")
|
277 |
async def openai_api_completions_passthrough(
|
278 |
request: Request,
|
|
|
273 |
return response.json()
|
274 |
|
275 |
|
276 |
+
@app.post("/v1/engines/text-embedding-ada-002/embeddings")
|
277 |
+
async def openai_api_embeddings_passthrough(
|
278 |
+
request: Request,
|
279 |
+
user: User = Depends(fastapi_users.current_user()),
|
280 |
+
):
|
281 |
+
if not user:
|
282 |
+
raise HTTPException(status_code=401, detail="Unauthorized")
|
283 |
+
|
284 |
+
# Get the request data and headers
|
285 |
+
request_data = await request.json()
|
286 |
+
request_headers = request.headers
|
287 |
+
openai_api_key = os.getenv("OPENAI_API_KEY")
|
288 |
+
|
289 |
+
# Forward the request to the OpenAI API
|
290 |
+
async with AsyncClient() as client:
|
291 |
+
response = await client.post(
|
292 |
+
"https://api.openai.com/v1/engines/text-embedding-ada-002/embeddings",
|
293 |
+
json=request_data,
|
294 |
+
headers={
|
295 |
+
"Content-Type": request_headers.get("Content-Type"),
|
296 |
+
"Authorization": f"Bearer {openai_api_key}",
|
297 |
+
},
|
298 |
+
timeout=120.0,
|
299 |
+
)
|
300 |
+
|
301 |
+
# Return the OpenAI API response
|
302 |
+
return response.json()
|
303 |
+
|
304 |
+
|
305 |
@app.post("/v1/completions")
|
306 |
async def openai_api_completions_passthrough(
|
307 |
request: Request,
|