Skip to content

Commit

Permalink
🐛 Bug: Change the last character of the SSE message to \n\n
Browse files Browse the repository at this point in the history
  • Loading branch information
yym68686 committed Oct 3, 2024
1 parent 2d653d6 commit 0a30d42
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions response.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

from log_config import logger

# end_of_line = "\n\r\n"
# end_of_line = "\r\n"
# end_of_line = "\n\r"
end_of_line = "\n\n"
# end_of_line = "\r"
# end_of_line = "\n"

async def generate_sse_response(timestamp, model, content=None, tools_id=None, function_call_name=None, function_call_content=None, role=None, total_tokens=0, prompt_tokens=0, completion_tokens=0):
sample_data = {
Expand Down Expand Up @@ -36,7 +42,7 @@ async def generate_sse_response(timestamp, model, content=None, tools_id=None, f
json_data = json.dumps(sample_data, ensure_ascii=False)

# 构建SSE响应
sse_response = f"data: {json_data}\n\r\n"
sse_response = f"data: {json_data}" + end_of_line

return sse_response

Expand Down Expand Up @@ -94,7 +100,7 @@ async def fetch_gemini_response_stream(client, url, headers, payload, model):
function_full_response = json.dumps(function_call["functionCall"]["args"])
sse_string = await generate_sse_response(timestamp, model, content=None, tools_id="chatcmpl-9inWv0yEtgn873CxMBzHeCeiHctTV", function_call_name=None, function_call_content=function_full_response)
yield sse_string
yield "data: [DONE]\n\r\n"
yield "data: [DONE]" + end_of_line

async def fetch_vertex_claude_response_stream(client, url, headers, payload, model):
timestamp = int(datetime.timestamp(datetime.now()))
Expand Down Expand Up @@ -141,7 +147,7 @@ async def fetch_vertex_claude_response_stream(client, url, headers, payload, mod
function_full_response = json.dumps(function_call["input"])
sse_string = await generate_sse_response(timestamp, model, content=None, tools_id=function_call_id, function_call_name=None, function_call_content=function_full_response)
yield sse_string
yield "data: [DONE]\n\r\n"
yield "data: [DONE]" + end_of_line

async def fetch_gpt_response_stream(client, url, headers, payload):
async with client.stream('POST', url, headers=headers, json=payload) as response:
Expand All @@ -157,7 +163,7 @@ async def fetch_gpt_response_stream(client, url, headers, payload):
line, buffer = buffer.split("\n", 1)
# logger.info("line: %s", repr(line))
if line and line != "data: " and line != "data:" and not line.startswith(": "):
yield line.strip() + "\n\r\n"
yield line.strip() + end_of_line

async def fetch_cloudflare_response_stream(client, url, headers, payload, model):
timestamp = int(datetime.timestamp(datetime.now()))
Expand All @@ -176,7 +182,7 @@ async def fetch_cloudflare_response_stream(client, url, headers, payload, model)
if line.startswith("data:"):
line = line.lstrip("data: ")
if line == "[DONE]":
yield "data: [DONE]\n\r\n"
yield "data: [DONE]" + end_of_line
return
resp: dict = json.loads(line)
message = resp.get("response")
Expand All @@ -200,7 +206,7 @@ async def fetch_cohere_response_stream(client, url, headers, payload, model):
# logger.info("line: %s", repr(line))
resp: dict = json.loads(line)
if resp.get("is_finished") == True:
yield "data: [DONE]\n\r\n"
yield "data: [DONE]" + end_of_line
return
if resp.get("event_type") == "text-generation":
message = resp.get("text")
Expand Down Expand Up @@ -266,7 +272,7 @@ async def fetch_claude_response_stream(client, url, headers, payload, model):
function_call_content = delta["partial_json"]
sse_string = await generate_sse_response(timestamp, model, None, None, None, function_call_content)
yield sse_string
yield "data: [DONE]\n\r\n"
yield "data: [DONE]" + end_of_line

async def fetch_response(client, url, headers, payload):
response = None
Expand Down

0 comments on commit 0a30d42

Please sign in to comment.