Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,13 +13,19 @@ from dotenv import load_dotenv
|
|
| 13 |
load_dotenv()
|
| 14 |
|
| 15 |
# Initialize FastAPI app
|
| 16 |
-
app = FastAPI(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Configuration
|
| 19 |
API_KEY_NAME = "X-API-KEY"
|
| 20 |
API_KEYS = os.getenv("API_KEYS", "").split(",") # Comma-separated list from .env
|
| 21 |
|
| 22 |
-
# Configure CORS
|
| 23 |
app.add_middleware(
|
| 24 |
CORSMiddleware,
|
| 25 |
allow_origins=["*"],
|
|
@@ -33,15 +39,9 @@ api_key_header = APIKeyHeader(name=API_KEY_NAME, auto_error=False)
|
|
| 33 |
|
| 34 |
async def get_api_key(api_key: str = Security(api_key_header)):
|
| 35 |
if not api_key:
|
| 36 |
-
raise HTTPException(
|
| 37 |
-
status_code=401,
|
| 38 |
-
detail="API key is missing"
|
| 39 |
-
)
|
| 40 |
if api_key not in API_KEYS:
|
| 41 |
-
raise HTTPException(
|
| 42 |
-
status_code=401,
|
| 43 |
-
detail="Invalid API key"
|
| 44 |
-
)
|
| 45 |
return api_key
|
| 46 |
|
| 47 |
# Request models
|
|
@@ -56,9 +56,14 @@ class ChatCompletionRequest(BaseModel):
|
|
| 56 |
max_tokens: int = None
|
| 57 |
|
| 58 |
# Health check endpoint
|
| 59 |
-
@app.get("/")
|
| 60 |
async def health_check():
|
| 61 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
# Main API endpoint
|
| 64 |
@app.post("/v1/chat/completions")
|
|
@@ -85,26 +90,16 @@ async def chat_completion(
|
|
| 85 |
"customModelId": ""
|
| 86 |
}
|
| 87 |
|
| 88 |
-
headers = {
|
| 89 |
-
"accept": "application/json",
|
| 90 |
-
"content-type": "application/json",
|
| 91 |
-
}
|
| 92 |
-
|
| 93 |
# Call MultiChatAI API
|
| 94 |
response = requests.post(
|
| 95 |
"https://www.multichatai.com/api/chat/deepinfra",
|
| 96 |
-
headers=
|
| 97 |
json=multi_chat_body,
|
| 98 |
timeout=30
|
| 99 |
)
|
| 100 |
|
| 101 |
-
|
| 102 |
-
raise HTTPException(
|
| 103 |
-
status_code=response.status_code,
|
| 104 |
-
detail=f"MultiChatAI API error: {response.text}"
|
| 105 |
-
)
|
| 106 |
|
| 107 |
-
# Format response in OpenAI style
|
| 108 |
return JSONResponse({
|
| 109 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
| 110 |
"object": "chat.completion",
|
|
@@ -127,10 +122,29 @@ async def chat_completion(
|
|
| 127 |
|
| 128 |
except requests.Timeout:
|
| 129 |
raise HTTPException(status_code=504, detail="Upstream service timeout")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
except Exception as e:
|
| 131 |
raise HTTPException(status_code=500, detail=str(e))
|
| 132 |
|
| 133 |
# Add this if you need to support OPTIONS requests
|
| 134 |
@app.options("/v1/chat/completions")
|
| 135 |
async def options_handler():
|
| 136 |
-
return JSONResponse(content={}, status_code=200)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
load_dotenv()
|
| 14 |
|
| 15 |
# Initialize FastAPI app
|
| 16 |
+
app = FastAPI(
|
| 17 |
+
title="MultiChatAI to OpenAI API Wrapper",
|
| 18 |
+
description="API wrapper for MultiChatAI with OpenAI-compatible endpoints",
|
| 19 |
+
version="1.0.0",
|
| 20 |
+
docs_url="/docs",
|
| 21 |
+
redoc_url=None
|
| 22 |
+
)
|
| 23 |
|
| 24 |
# Configuration
|
| 25 |
API_KEY_NAME = "X-API-KEY"
|
| 26 |
API_KEYS = os.getenv("API_KEYS", "").split(",") # Comma-separated list from .env
|
| 27 |
|
| 28 |
+
# Configure CORS
|
| 29 |
app.add_middleware(
|
| 30 |
CORSMiddleware,
|
| 31 |
allow_origins=["*"],
|
|
|
|
| 39 |
|
| 40 |
async def get_api_key(api_key: str = Security(api_key_header)):
|
| 41 |
if not api_key:
|
| 42 |
+
raise HTTPException(status_code=401, detail="API key is missing")
|
|
|
|
|
|
|
|
|
|
| 43 |
if api_key not in API_KEYS:
|
| 44 |
+
raise HTTPException(status_code=401, detail="Invalid API key")
|
|
|
|
|
|
|
|
|
|
| 45 |
return api_key
|
| 46 |
|
| 47 |
# Request models
|
|
|
|
| 56 |
max_tokens: int = None
|
| 57 |
|
| 58 |
# Health check endpoint
|
| 59 |
+
@app.get("/", include_in_schema=False)
|
| 60 |
async def health_check():
|
| 61 |
+
return {
|
| 62 |
+
"status": "OK",
|
| 63 |
+
"service": "MultiChatAI Proxy",
|
| 64 |
+
"timestamp": datetime.now().isoformat(),
|
| 65 |
+
"environment": os.getenv("ENVIRONMENT", "development")
|
| 66 |
+
}
|
| 67 |
|
| 68 |
# Main API endpoint
|
| 69 |
@app.post("/v1/chat/completions")
|
|
|
|
| 90 |
"customModelId": ""
|
| 91 |
}
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
# Call MultiChatAI API
|
| 94 |
response = requests.post(
|
| 95 |
"https://www.multichatai.com/api/chat/deepinfra",
|
| 96 |
+
headers={"Content-Type": "application/json"},
|
| 97 |
json=multi_chat_body,
|
| 98 |
timeout=30
|
| 99 |
)
|
| 100 |
|
| 101 |
+
response.raise_for_status()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
|
|
|
| 103 |
return JSONResponse({
|
| 104 |
"id": f"chatcmpl-{uuid.uuid4()}",
|
| 105 |
"object": "chat.completion",
|
|
|
|
| 122 |
|
| 123 |
except requests.Timeout:
|
| 124 |
raise HTTPException(status_code=504, detail="Upstream service timeout")
|
| 125 |
+
except requests.RequestException as e:
|
| 126 |
+
raise HTTPException(
|
| 127 |
+
status_code=502,
|
| 128 |
+
detail=f"Upstream service error: {str(e)}"
|
| 129 |
+
)
|
| 130 |
except Exception as e:
|
| 131 |
raise HTTPException(status_code=500, detail=str(e))
|
| 132 |
|
| 133 |
# Add this if you need to support OPTIONS requests
|
| 134 |
@app.options("/v1/chat/completions")
|
| 135 |
async def options_handler():
|
| 136 |
+
return JSONResponse(content={}, status_code=200)
|
| 137 |
+
|
| 138 |
+
# For production deployment
|
| 139 |
+
def get_application():
|
| 140 |
+
return app
|
| 141 |
+
|
| 142 |
+
# For running locally
|
| 143 |
+
if __name__ == "__main__":
|
| 144 |
+
import uvicorn
|
| 145 |
+
uvicorn.run(
|
| 146 |
+
"app:app",
|
| 147 |
+
host="0.0.0.0",
|
| 148 |
+
port=int(os.getenv("PORT", 7860)),
|
| 149 |
+
reload=os.getenv("RELOAD", "false").lower() == "true"
|
| 150 |
+
)
|