Eric Michael Martinez commited on
Commit
37a6d72
1 Parent(s): 2010315

add embeddings endpoint

Browse files
Files changed (1) hide show
  1. app/app.py +29 -0
app/app.py CHANGED
@@ -244,6 +244,35 @@ async def authenticated_route(user: User = Depends(current_active_user)):
244
  return {"message": f"Hello {user.email}!"}
245
 
246
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
247
  @app.post("/v1/completions")
248
  async def openai_api_completions_passthrough(
249
  request: Request,
 
244
  return {"message": f"Hello {user.email}!"}
245
 
246
 
247
+ @app.post("/v1/embeddings")
248
+ async def openai_api_embeddings_passthrough(
249
+ request: Request,
250
+ user: User = Depends(fastapi_users.current_user()),
251
+ ):
252
+ if not user:
253
+ raise HTTPException(status_code=401, detail="Unauthorized")
254
+
255
+ # Get the request data and headers
256
+ request_data = await request.json()
257
+ request_headers = request.headers
258
+ openai_api_key = os.getenv("OPENAI_API_KEY")
259
+
260
+ # Forward the request to the OpenAI API
261
+ async with AsyncClient() as client:
262
+ response = await client.post(
263
+ "https://api.openai.com/v1/embeddings",
264
+ json=request_data,
265
+ headers={
266
+ "Content-Type": request_headers.get("Content-Type"),
267
+ "Authorization": f"Bearer {openai_api_key}",
268
+ },
269
+ timeout=120.0,
270
+ )
271
+
272
+ # Return the OpenAI API response
273
+ return response.json()
274
+
275
+
276
  @app.post("/v1/completions")
277
  async def openai_api_completions_passthrough(
278
  request: Request,