Update main.py
Browse files
main.py
CHANGED
@@ -199,23 +199,31 @@ async def chat_completions(
|
|
199 |
|
200 |
|
201 |
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
211 |
yield f"data: {json.dumps(create_chat_completion_data('', request.model, 'stop'))}\n\n"
|
212 |
yield "data: [DONE]\n\n"
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
|
|
219 |
|
220 |
if request.stream:
|
221 |
logger.info("Streaming response")
|
|
|
199 |
|
200 |
|
201 |
|
202 |
+
async def generate():
|
203 |
+
async with httpx.AsyncClient() as client:
|
204 |
+
try:
|
205 |
+
async with client.stream('POST', 'https://sider.ai/api/v2/completion/text', headers=headers, json=json_data, timeout=120.0) as response:
|
206 |
+
response.raise_for_status()
|
207 |
+
async for line in response.aiter_lines():
|
208 |
+
if line and ("[DONE]" not in line):
|
209 |
+
data = json.loads(line[5:])
|
210 |
+
content = data.get("data")
|
211 |
+
|
212 |
+
# Check if content is None or does not contain the text key
|
213 |
+
if content and 'text' in content:
|
214 |
+
yield f"data: {json.dumps(create_chat_completion_data(content['text'], request.model))}\n\n"
|
215 |
+
else:
|
216 |
+
logger.error(f"Unexpected content format: {data}")
|
217 |
+
yield f"data: {json.dumps(create_chat_completion_data('Error: Unexpected content format', request.model, 'stop'))}\n\n"
|
218 |
yield f"data: {json.dumps(create_chat_completion_data('', request.model, 'stop'))}\n\n"
|
219 |
yield "data: [DONE]\n\n"
|
220 |
+
except httpx.HTTPStatusError as e:
|
221 |
+
logger.error(f"HTTP error occurred: {e}")
|
222 |
+
raise HTTPException(status_code=e.response.status_code, detail=str(e))
|
223 |
+
except httpx.RequestError as e:
|
224 |
+
logger.error(f"An error occurred while requesting: {e}")
|
225 |
+
raise HTTPException(status_code=500, detail=str(e))
|
226 |
+
|
227 |
|
228 |
if request.stream:
|
229 |
logger.info("Streaming response")
|