Update main.py
Browse files
main.py
CHANGED
@@ -367,7 +367,6 @@ class Blackbox:
|
|
367 |
except json.JSONDecodeError as je:
|
368 |
logger.error("Failed to parse search results JSON.")
|
369 |
raise je
|
370 |
-
break # Exit the retry loop if successful
|
371 |
except ClientError as ce:
|
372 |
logger.error(f"Client error occurred: {ce}. Retrying attempt {attempt + 1}/{retry_attempts}")
|
373 |
if attempt == retry_attempts - 1:
|
@@ -426,35 +425,35 @@ def create_response(content: str, model: str, finish_reason: Optional[str] = Non
|
|
426 |
@app.post("/v1/chat/completions", response_model=ChatCompletionResponse)
|
427 |
@limiter.limit("60/minute") # Example: 60 requests per minute per IP
|
428 |
async def chat_completions(
|
429 |
-
|
430 |
-
|
431 |
api_key: str = Depends(get_api_key)
|
432 |
):
|
433 |
-
logger.info(f"Received chat completions request: {
|
434 |
try:
|
435 |
-
messages = [{"role": msg.role, "content": msg.content} for msg in
|
436 |
-
prompt_tokens = count_tokens(messages,
|
437 |
|
438 |
async_generator = Blackbox.create_async_generator(
|
439 |
-
model=
|
440 |
messages=messages,
|
441 |
image=None, # Adjust if image handling is required
|
442 |
image_name=None,
|
443 |
-
webSearchMode=
|
444 |
)
|
445 |
|
446 |
-
if
|
447 |
async def generate():
|
448 |
try:
|
449 |
completion_tokens = 0
|
450 |
async for chunk in async_generator:
|
451 |
if isinstance(chunk, ImageResponse):
|
452 |
image_markdown = f""
|
453 |
-
response_chunk = create_response(image_markdown,
|
454 |
yield f"data: {json.dumps(response_chunk)}\n\n"
|
455 |
completion_tokens += len(image_markdown.split())
|
456 |
else:
|
457 |
-
response_chunk = create_response(chunk,
|
458 |
yield f"data: {json.dumps(response_chunk)}\n\n"
|
459 |
completion_tokens += len(chunk.split())
|
460 |
|
@@ -487,7 +486,7 @@ async def chat_completions(
|
|
487 |
id=f"chatcmpl-{uuid.uuid4()}",
|
488 |
object="chat.completion",
|
489 |
created=int(datetime.now().timestamp()),
|
490 |
-
model=
|
491 |
choices=[
|
492 |
ChatCompletionChoice(
|
493 |
index=0,
|
@@ -515,7 +514,7 @@ async def chat_completions(
|
|
515 |
@app.get("/v1/models", response_model=Dict[str, List[Dict[str, str]]])
|
516 |
@limiter.limit("60/minute")
|
517 |
async def get_models(
|
518 |
-
request: Request,
|
519 |
api_key: str = Depends(get_api_key)
|
520 |
):
|
521 |
logger.info("Fetching available models.")
|
@@ -526,7 +525,7 @@ async def get_models(
|
|
526 |
@limiter.limit("60/minute")
|
527 |
async def model_status(
|
528 |
model: str,
|
529 |
-
request: Request,
|
530 |
api_key: str = Depends(get_api_key)
|
531 |
):
|
532 |
"""Check if a specific model is available."""
|
@@ -541,7 +540,9 @@ async def model_status(
|
|
541 |
# Endpoint: Health Check
|
542 |
@app.get("/v1/health", response_model=Dict[str, str])
|
543 |
@limiter.limit("60/minute")
|
544 |
-
async def health_check(
|
|
|
|
|
545 |
"""Health check endpoint to verify the service is running."""
|
546 |
return {"status": "ok"}
|
547 |
|
|
|
367 |
except json.JSONDecodeError as je:
|
368 |
logger.error("Failed to parse search results JSON.")
|
369 |
raise je
|
|
|
370 |
except ClientError as ce:
|
371 |
logger.error(f"Client error occurred: {ce}. Retrying attempt {attempt + 1}/{retry_attempts}")
|
372 |
if attempt == retry_attempts - 1:
|
|
|
425 |
@app.post("/v1/chat/completions", response_model=ChatCompletionResponse)
|
426 |
@limiter.limit("60/minute") # Example: 60 requests per minute per IP
|
427 |
async def chat_completions(
|
428 |
+
chat_request: ChatRequest, # Renamed from 'request' to 'chat_request'
|
429 |
+
request: Request, # Added 'request: Request' parameter
|
430 |
api_key: str = Depends(get_api_key)
|
431 |
):
|
432 |
+
logger.info(f"Received chat completions request: {chat_request}")
|
433 |
try:
|
434 |
+
messages = [{"role": msg.role, "content": msg.content} for msg in chat_request.messages]
|
435 |
+
prompt_tokens = count_tokens(messages, chat_request.model)
|
436 |
|
437 |
async_generator = Blackbox.create_async_generator(
|
438 |
+
model=chat_request.model,
|
439 |
messages=messages,
|
440 |
image=None, # Adjust if image handling is required
|
441 |
image_name=None,
|
442 |
+
webSearchMode=chat_request.webSearchMode
|
443 |
)
|
444 |
|
445 |
+
if chat_request.stream:
|
446 |
async def generate():
|
447 |
try:
|
448 |
completion_tokens = 0
|
449 |
async for chunk in async_generator:
|
450 |
if isinstance(chunk, ImageResponse):
|
451 |
image_markdown = f""
|
452 |
+
response_chunk = create_response(image_markdown, chat_request.model)
|
453 |
yield f"data: {json.dumps(response_chunk)}\n\n"
|
454 |
completion_tokens += len(image_markdown.split())
|
455 |
else:
|
456 |
+
response_chunk = create_response(chunk, chat_request.model)
|
457 |
yield f"data: {json.dumps(response_chunk)}\n\n"
|
458 |
completion_tokens += len(chunk.split())
|
459 |
|
|
|
486 |
id=f"chatcmpl-{uuid.uuid4()}",
|
487 |
object="chat.completion",
|
488 |
created=int(datetime.now().timestamp()),
|
489 |
+
model=chat_request.model,
|
490 |
choices=[
|
491 |
ChatCompletionChoice(
|
492 |
index=0,
|
|
|
514 |
@app.get("/v1/models", response_model=Dict[str, List[Dict[str, str]]])
|
515 |
@limiter.limit("60/minute")
|
516 |
async def get_models(
|
517 |
+
request: Request, # Ensure 'request: Request' parameter is present
|
518 |
api_key: str = Depends(get_api_key)
|
519 |
):
|
520 |
logger.info("Fetching available models.")
|
|
|
525 |
@limiter.limit("60/minute")
|
526 |
async def model_status(
|
527 |
model: str,
|
528 |
+
request: Request, # Ensure 'request: Request' parameter is present
|
529 |
api_key: str = Depends(get_api_key)
|
530 |
):
|
531 |
"""Check if a specific model is available."""
|
|
|
540 |
# Endpoint: Health Check
|
541 |
@app.get("/v1/health", response_model=Dict[str, str])
|
542 |
@limiter.limit("60/minute")
|
543 |
+
async def health_check(
|
544 |
+
request: Request # Ensure 'request: Request' parameter is present
|
545 |
+
):
|
546 |
"""Health check endpoint to verify the service is running."""
|
547 |
return {"status": "ok"}
|
548 |
|