Update main.py
Browse files
main.py
CHANGED
@@ -29,7 +29,7 @@ API_KEYS = os.getenv('API_KEYS', '').split(',') # Comma-separated API keys
|
|
29 |
RATE_LIMIT = int(os.getenv('RATE_LIMIT', '60')) # Requests per minute
|
30 |
|
31 |
if not API_KEYS or API_KEYS == ['']:
|
32 |
-
logger.error("No API keys found. Please set the API_KEYS environment variable.")
|
33 |
raise Exception("API_KEYS environment variable not set.")
|
34 |
|
35 |
# Simple in-memory rate limiter
|
@@ -295,8 +295,8 @@ class Blackbox:
|
|
295 |
logger.info(f"Image URL found.")
|
296 |
yield ImageResponse(image_url, alt=messages[-1]['content'])
|
297 |
else:
|
298 |
-
logger.error("Image URL not found in the response.")
|
299 |
-
raise Exception("Image URL not found in the response")
|
300 |
else:
|
301 |
full_response = ""
|
302 |
search_results_json = ""
|
@@ -313,7 +313,7 @@ class Blackbox:
|
|
313 |
yield decoded_chunk
|
314 |
logger.info("Finished streaming response chunks.")
|
315 |
except Exception as e:
|
316 |
-
logger.exception("Error while iterating over response chunks.")
|
317 |
raise e
|
318 |
if data["webSearchMode"] and search_results_json:
|
319 |
match = re.search(r'\$~~~\$(.*?)\$~~~\$', search_results_json, re.DOTALL)
|
@@ -326,7 +326,7 @@ class Blackbox:
|
|
326 |
logger.info("Formatted search results.")
|
327 |
yield formatted_results
|
328 |
except json.JSONDecodeError as je:
|
329 |
-
logger.error("Failed to parse search results JSON.")
|
330 |
raise je
|
331 |
break # Exit the retry loop if successful
|
332 |
except ClientError as ce:
|
@@ -382,7 +382,7 @@ async def chat_completions(request: ChatRequest, req: Request, api_key: str = De
|
|
382 |
# Validate that the requested model is available
|
383 |
if request.model not in Blackbox.models and request.model not in Blackbox.model_aliases:
|
384 |
logger.warning(f"Attempt to use unavailable model: {request.model}")
|
385 |
-
raise HTTPException(status_code=400, detail="Requested model is not available.")
|
386 |
|
387 |
# Process the request with actual message content, but don't log it
|
388 |
async_generator = Blackbox.create_async_generator(
|
@@ -410,7 +410,7 @@ async def chat_completions(request: ChatRequest, req: Request, api_key: str = De
|
|
410 |
error_response = {"error": he.detail}
|
411 |
yield f"data: {json.dumps(error_response)}\n\n"
|
412 |
except Exception as e:
|
413 |
-
logger.exception("Error during streaming response generation.")
|
414 |
error_response = {"error": str(e)}
|
415 |
yield f"data: {json.dumps(error_response)}\n\n"
|
416 |
|
@@ -452,7 +452,7 @@ async def chat_completions(request: ChatRequest, req: Request, api_key: str = De
|
|
452 |
logger.warning(f"HTTPException: {he.detail}")
|
453 |
raise he
|
454 |
except Exception as e:
|
455 |
-
logger.exception("An unexpected error occurred while processing the chat completions request.")
|
456 |
raise HTTPException(status_code=500, detail=str(e))
|
457 |
|
458 |
@app.get("/niansuhai/v1/models", dependencies=[Depends(rate_limiter)])
|
@@ -470,13 +470,13 @@ async def health_check(api_key: str = Depends(get_api_key)):
|
|
470 |
async def model_status(model: str, api_key: str = Depends(get_api_key)):
|
471 |
logger.info(f"Model status requested for '{model}' by API key: {api_key}")
|
472 |
if model in Blackbox.models:
|
473 |
-
return {"model": model, "status": "available"}
|
474 |
elif model in Blackbox.model_aliases:
|
475 |
actual_model = Blackbox.model_aliases[model]
|
476 |
-
return {"model": actual_model, "status": "available via alias"}
|
477 |
else:
|
478 |
logger.warning(f"Model not found: {model}")
|
479 |
-
raise HTTPException(status_code=404, detail="Model not found")
|
480 |
|
481 |
if __name__ == "__main__":
|
482 |
import uvicorn
|
|
|
29 |
RATE_LIMIT = int(os.getenv('RATE_LIMIT', '60')) # Requests per minute
|
30 |
|
31 |
if not API_KEYS or API_KEYS == ['']:
|
32 |
+
logger.error("No API keys found. Please set the API_KEYS environment variable. | NiansuhAI")
|
33 |
raise Exception("API_KEYS environment variable not set.")
|
34 |
|
35 |
# Simple in-memory rate limiter
|
|
|
295 |
logger.info(f"Image URL found.")
|
296 |
yield ImageResponse(image_url, alt=messages[-1]['content'])
|
297 |
else:
|
298 |
+
logger.error("Image URL not found in the response. | NiansuhAI")
|
299 |
+
raise Exception("Image URL not found in the response | NiansuhAI")
|
300 |
else:
|
301 |
full_response = ""
|
302 |
search_results_json = ""
|
|
|
313 |
yield decoded_chunk
|
314 |
logger.info("Finished streaming response chunks.")
|
315 |
except Exception as e:
|
316 |
+
logger.exception("Error while iterating over response chunks. | NiansuhAI")
|
317 |
raise e
|
318 |
if data["webSearchMode"] and search_results_json:
|
319 |
match = re.search(r'\$~~~\$(.*?)\$~~~\$', search_results_json, re.DOTALL)
|
|
|
326 |
logger.info("Formatted search results.")
|
327 |
yield formatted_results
|
328 |
except json.JSONDecodeError as je:
|
329 |
+
logger.error("Failed to parse search results JSON. | NiansuhAI")
|
330 |
raise je
|
331 |
break # Exit the retry loop if successful
|
332 |
except ClientError as ce:
|
|
|
382 |
# Validate that the requested model is available
|
383 |
if request.model not in Blackbox.models and request.model not in Blackbox.model_aliases:
|
384 |
logger.warning(f"Attempt to use unavailable model: {request.model}")
|
385 |
+
raise HTTPException(status_code=400, detail="Requested model is not available. | NiansuhAI")
|
386 |
|
387 |
# Process the request with actual message content, but don't log it
|
388 |
async_generator = Blackbox.create_async_generator(
|
|
|
410 |
error_response = {"error": he.detail}
|
411 |
yield f"data: {json.dumps(error_response)}\n\n"
|
412 |
except Exception as e:
|
413 |
+
logger.exception("Error during streaming response generation. | NiansuhAI")
|
414 |
error_response = {"error": str(e)}
|
415 |
yield f"data: {json.dumps(error_response)}\n\n"
|
416 |
|
|
|
452 |
logger.warning(f"HTTPException: {he.detail}")
|
453 |
raise he
|
454 |
except Exception as e:
|
455 |
+
logger.exception("An unexpected error occurred while processing the chat completions request. | NiansuhAI")
|
456 |
raise HTTPException(status_code=500, detail=str(e))
|
457 |
|
458 |
@app.get("/niansuhai/v1/models", dependencies=[Depends(rate_limiter)])
|
|
|
470 |
async def model_status(model: str, api_key: str = Depends(get_api_key)):
|
471 |
logger.info(f"Model status requested for '{model}' by API key: {api_key}")
|
472 |
if model in Blackbox.models:
|
473 |
+
return {"model": model, "status": "available | NiansuhAI"}
|
474 |
elif model in Blackbox.model_aliases:
|
475 |
actual_model = Blackbox.model_aliases[model]
|
476 |
+
return {"model": actual_model, "status": "available via alias | NiansuhAI"}
|
477 |
else:
|
478 |
logger.warning(f"Model not found: {model}")
|
479 |
+
raise HTTPException(status_code=404, detail="Model not found | NiansuhAI")
|
480 |
|
481 |
if __name__ == "__main__":
|
482 |
import uvicorn
|