Niansuh commited on
Commit
94e6f25
·
verified ·
1 Parent(s): 3908754

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +19 -9
main.py CHANGED
@@ -6,7 +6,6 @@ import uuid
6
  import json
7
  import logging
8
  import asyncio
9
- import time
10
  from aiohttp import ClientSession, ClientTimeout, ClientError
11
  from fastapi import FastAPI, HTTPException, Request, Depends, Header, status
12
  from fastapi.responses import StreamingResponse, JSONResponse
@@ -56,7 +55,7 @@ app.state.limiter = limiter
56
  app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
57
 
58
  # API Key Authentication
59
- API_KEYS = set(os.getenv("API_KEYS", "").split(",")) # Load API keys from environment variable
60
 
61
  async def get_api_key(authorization: Optional[str] = Header(None)):
62
  """
@@ -426,7 +425,11 @@ def create_response(content: str, model: str, finish_reason: Optional[str] = Non
426
  # Endpoint: Chat Completions
427
  @app.post("/v1/chat/completions", response_model=ChatCompletionResponse)
428
  @limiter.limit("60/minute") # Example: 60 requests per minute per IP
429
- async def chat_completions(request: ChatRequest, req: Request, api_key: str = Depends(get_api_key)):
 
 
 
 
430
  logger.info(f"Received chat completions request: {request}")
431
  try:
432
  messages = [{"role": msg.role, "content": msg.content} for msg in request.messages]
@@ -509,16 +512,23 @@ async def chat_completions(request: ChatRequest, req: Request, api_key: str = De
509
  raise HTTPException(status_code=500, detail=str(e))
510
 
511
  # Endpoint: List Models
512
- @app.get("/v1/models")
513
  @limiter.limit("60/minute")
514
- async def get_models(api_key: str = Depends(get_api_key)):
 
 
 
515
  logger.info("Fetching available models.")
516
  return {"data": [{"id": model} for model in Blackbox.models]}
517
 
518
  # Endpoint: Model Status
519
- @app.get("/v1/models/{model}/status")
520
  @limiter.limit("60/minute")
521
- async def model_status(model: str, api_key: str = Depends(get_api_key)):
 
 
 
 
522
  """Check if a specific model is available."""
523
  if model in Blackbox.models:
524
  return {"model": model, "status": "available"}
@@ -529,9 +539,9 @@ async def model_status(model: str, api_key: str = Depends(get_api_key)):
529
  raise HTTPException(status_code=404, detail="Model not found")
530
 
531
  # Endpoint: Health Check
532
- @app.get("/v1/health")
533
  @limiter.limit("60/minute")
534
- async def health_check():
535
  """Health check endpoint to verify the service is running."""
536
  return {"status": "ok"}
537
 
 
6
  import json
7
  import logging
8
  import asyncio
 
9
  from aiohttp import ClientSession, ClientTimeout, ClientError
10
  from fastapi import FastAPI, HTTPException, Request, Depends, Header, status
11
  from fastapi.responses import StreamingResponse, JSONResponse
 
55
  app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
56
 
57
  # API Key Authentication
58
+ API_KEYS = set(api_key.strip() for api_key in os.getenv("API_KEYS", "").split(",") if api_key.strip())
59
 
60
  async def get_api_key(authorization: Optional[str] = Header(None)):
61
  """
 
425
  # Endpoint: Chat Completions
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
+ request: ChatRequest,
430
+ req: Request,
431
+ api_key: str = Depends(get_api_key)
432
+ ):
433
  logger.info(f"Received chat completions request: {request}")
434
  try:
435
  messages = [{"role": msg.role, "content": msg.content} for msg in request.messages]
 
512
  raise HTTPException(status_code=500, detail=str(e))
513
 
514
  # Endpoint: List Models
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.")
522
  return {"data": [{"id": model} for model in Blackbox.models]}
523
 
524
  # Endpoint: Model Status
525
+ @app.get("/v1/models/{model}/status", response_model=Dict[str, str])
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."""
533
  if model in Blackbox.models:
534
  return {"model": model, "status": "available"}
 
539
  raise HTTPException(status_code=404, detail="Model not found")
540
 
541
  # Endpoint: Health Check
542
+ @app.get("/v1/health", response_model=Dict[str, str])
543
  @limiter.limit("60/minute")
544
+ async def health_check(request: Request):
545
  """Health check endpoint to verify the service is running."""
546
  return {"status": "ok"}
547