Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -120,27 +120,55 @@ async def fetch_models():
|
|
120 |
@app.post("/chat/completions")
|
121 |
@app.post("/v1/chat/completions")
|
122 |
async def get_completion(payload: Payload, request: Request):
|
123 |
-
|
124 |
-
endpoint = secret_api_endpoint_2 if payload.model in alternate_models else secret_api_endpoint
|
125 |
-
|
126 |
-
# Use the payload directly as it includes stream and other user data
|
127 |
payload_dict = payload.dict()
|
128 |
-
|
129 |
-
|
|
|
|
|
|
|
|
|
130 |
|
131 |
async def stream_generator(payload_dict):
|
132 |
async with httpx.AsyncClient() as client:
|
133 |
try:
|
134 |
async with client.stream("POST", f"{endpoint}/v1/chat/completions", json=payload_dict, timeout=10) as response:
|
135 |
-
response.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
async for line in response.aiter_lines():
|
137 |
if line:
|
138 |
yield f"{line}\n"
|
139 |
except httpx.HTTPStatusError as status_err:
|
140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
except httpx.RequestError as req_err:
|
142 |
-
|
|
|
143 |
except Exception as e:
|
|
|
144 |
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {e}")
|
145 |
|
146 |
return StreamingResponse(stream_generator(payload_dict), media_type="application/json")
|
|
|
120 |
@app.post("/chat/completions")
|
121 |
@app.post("/v1/chat/completions")
|
122 |
async def get_completion(payload: Payload, request: Request):
|
123 |
+
model_to_use = "gpt-4o-mini" if payload.model == "gpt-4o" else payload.model
|
|
|
|
|
|
|
124 |
payload_dict = payload.dict()
|
125 |
+
payload_dict["model"] = model_to_use
|
126 |
+
|
127 |
+
# Select the appropriate endpoint
|
128 |
+
endpoint = secret_api_endpoint_2 if model_to_use in alternate_models else secret_api_endpoint
|
129 |
+
|
130 |
+
print(payload_dict)
|
131 |
|
132 |
async def stream_generator(payload_dict):
|
133 |
async with httpx.AsyncClient() as client:
|
134 |
try:
|
135 |
async with client.stream("POST", f"{endpoint}/v1/chat/completions", json=payload_dict, timeout=10) as response:
|
136 |
+
if response.status_code == 422:
|
137 |
+
# Handle unprocessable entity errors
|
138 |
+
raise HTTPException(status_code=422, detail="Unprocessable entity. Check your payload.")
|
139 |
+
elif response.status_code == 400:
|
140 |
+
# Handle bad request errors
|
141 |
+
raise HTTPException(status_code=400, detail="Bad request. Verify input data.")
|
142 |
+
elif response.status_code == 403:
|
143 |
+
# Handle forbidden access
|
144 |
+
raise HTTPException(status_code=403, detail="Forbidden. You do not have access to this resource.")
|
145 |
+
elif response.status_code == 404:
|
146 |
+
# Handle not found errors
|
147 |
+
raise HTTPException(status_code=404, detail="The requested resource was not found.")
|
148 |
+
elif response.status_code >= 500:
|
149 |
+
# Handle server errors
|
150 |
+
raise HTTPException(status_code=500, detail="Server error. Try again later.")
|
151 |
+
|
152 |
+
response.raise_for_status() # Raise HTTPStatusError for non-200 responses not explicitly handled
|
153 |
+
|
154 |
+
# Stream response to the client
|
155 |
async for line in response.aiter_lines():
|
156 |
if line:
|
157 |
yield f"{line}\n"
|
158 |
except httpx.HTTPStatusError as status_err:
|
159 |
+
# Catch specific HTTP errors
|
160 |
+
raise HTTPException(
|
161 |
+
status_code=status_err.response.status_code,
|
162 |
+
detail=f"HTTP error: {status_err.response.text}"
|
163 |
+
)
|
164 |
+
except httpx.TimeoutException:
|
165 |
+
# Handle timeout errors
|
166 |
+
raise HTTPException(status_code=504, detail="Request timed out. Please try again later.")
|
167 |
except httpx.RequestError as req_err:
|
168 |
+
# Handle generic request errors
|
169 |
+
raise HTTPException(status_code=500, detail=f"Request failed: {req_err}")
|
170 |
except Exception as e:
|
171 |
+
# Catch any unexpected exceptions
|
172 |
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {e}")
|
173 |
|
174 |
return StreamingResponse(stream_generator(payload_dict), media_type="application/json")
|