Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -46,6 +46,11 @@ class ChatRequest(BaseModel):
|
|
46 |
stream: Optional[bool] = False
|
47 |
|
48 |
|
|
|
|
|
|
|
|
|
|
|
49 |
async def verify_authorization(authorization: str = Header(None)):
|
50 |
if not authorization:
|
51 |
logger.error("Missing Authorization header")
|
@@ -152,6 +157,24 @@ async def chat_completion(request: ChatRequest, authorization: str = Header(None
|
|
152 |
raise HTTPException(status_code=500, detail=str(e))
|
153 |
|
154 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
155 |
@app.get("/health")
|
156 |
@app.get("/")
|
157 |
async def health_check():
|
|
|
46 |
stream: Optional[bool] = False
|
47 |
|
48 |
|
49 |
+
class EmbeddingRequest(BaseModel):
|
50 |
+
input: str
|
51 |
+
model: str = "text-embedding-004"
|
52 |
+
|
53 |
+
|
54 |
async def verify_authorization(authorization: str = Header(None)):
|
55 |
if not authorization:
|
56 |
logger.error("Missing Authorization header")
|
|
|
157 |
raise HTTPException(status_code=500, detail=str(e))
|
158 |
|
159 |
|
160 |
+
@app.post("/v1/embeddings")
|
161 |
+
@app.post("/hf/v1/embeddings")
|
162 |
+
async def embedding(request: EmbeddingRequest, authorization: str = Header(None)):
|
163 |
+
await verify_authorization(authorization)
|
164 |
+
async with key_lock:
|
165 |
+
api_key = next(key_cycle)
|
166 |
+
logger.info(f"Using API key: {api_key}")
|
167 |
+
|
168 |
+
try:
|
169 |
+
client = openai.OpenAI(api_key=api_key, base_url=config.settings.BASE_URL)
|
170 |
+
response = client.embeddings.create(input=request.input, model=request.model)
|
171 |
+
logger.info("Embedding successful")
|
172 |
+
return response
|
173 |
+
except Exception as e:
|
174 |
+
logger.error(f"Error in embedding: {str(e)}")
|
175 |
+
raise HTTPException(status_code=500, detail=str(e))
|
176 |
+
|
177 |
+
|
178 |
@app.get("/health")
|
179 |
@app.get("/")
|
180 |
async def health_check():
|