Mono / main.py
AIMaster7's picture
fixed streaming issue
a6877b8 verified
raw
history blame
3.1 kB
@app.post("/v1/chat/completions")
async def chat_completion(request: ChatRequest):
headers = {
'accept': 'text/event-stream',
'content-type': 'application/json',
'origin': 'https://www.chatwithmono.xyz',
'referer': 'https://www.chatwithmono.xyz/',
'user-agent': 'Mozilla/5.0',
}
payload = {
"messages": [{"role": msg.role, "content": msg.content} for msg in request.messages],
"model": request.model
}
if request.stream:
def event_stream():
sent_done = False
with requests.post(
"https://www.chatwithmono.xyz/api/chat",
headers=headers,
json=payload,
stream=True,
timeout=120
) as response:
for line in response.iter_lines(decode_unicode=True):
if line.startswith("0:"):
try:
content_piece = json.loads(line[2:])
yield f'data: {json.dumps({"choices": [{"delta": {"content": content_piece}, "finish_reason": None}]})}\n\n'
except:
continue
elif line.startswith(("e:", "d:")) and not sent_done:
sent_done = True
yield "data: [DONE]\n\n"
return StreamingResponse(event_stream(), media_type="text/event-stream")
else:
assistant_response = ""
usage_info = {}
with requests.post(
"https://www.chatwithmono.xyz/api/chat",
headers=headers,
json=payload,
stream=True,
timeout=120
) as response:
for chunk in response.iter_lines(decode_unicode=True):
if chunk.startswith("0:"):
try:
piece = json.loads(chunk[2:])
assistant_response += piece # this is just a string fragment
except:
continue
elif chunk.startswith(("e:", "d:")):
try:
data = json.loads(chunk[2:])
usage_info = data.get("usage", {})
except:
continue
return JSONResponse(content={
"id": f"chatcmpl-{unix_id()}",
"object": "chat.completion",
"created": int(time.time()),
"model": request.model,
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": assistant_response # correctly concatenated string
},
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": usage_info.get("promptTokens", 0),
"completion_tokens": usage_info.get("completionTokens", 0),
"total_tokens": usage_info.get("promptTokens", 0) + usage_info.get("completionTokens", 0),
}
})