🐛 Bug: Change the last character of the SSE message to \n\n
Browse files- response.py +13 -7
response.py
CHANGED
@@ -4,6 +4,12 @@ from datetime import datetime
|
|
4 |
|
5 |
from log_config import logger
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
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):
|
9 |
sample_data = {
|
@@ -36,7 +42,7 @@ async def generate_sse_response(timestamp, model, content=None, tools_id=None, f
|
|
36 |
json_data = json.dumps(sample_data, ensure_ascii=False)
|
37 |
|
38 |
# 构建SSE响应
|
39 |
-
sse_response = f"data: {json_data}
|
40 |
|
41 |
return sse_response
|
42 |
|
@@ -94,7 +100,7 @@ async def fetch_gemini_response_stream(client, url, headers, payload, model):
|
|
94 |
function_full_response = json.dumps(function_call["functionCall"]["args"])
|
95 |
sse_string = await generate_sse_response(timestamp, model, content=None, tools_id="chatcmpl-9inWv0yEtgn873CxMBzHeCeiHctTV", function_call_name=None, function_call_content=function_full_response)
|
96 |
yield sse_string
|
97 |
-
yield "data: [DONE]
|
98 |
|
99 |
async def fetch_vertex_claude_response_stream(client, url, headers, payload, model):
|
100 |
timestamp = int(datetime.timestamp(datetime.now()))
|
@@ -141,7 +147,7 @@ async def fetch_vertex_claude_response_stream(client, url, headers, payload, mod
|
|
141 |
function_full_response = json.dumps(function_call["input"])
|
142 |
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)
|
143 |
yield sse_string
|
144 |
-
yield "data: [DONE]
|
145 |
|
146 |
async def fetch_gpt_response_stream(client, url, headers, payload):
|
147 |
async with client.stream('POST', url, headers=headers, json=payload) as response:
|
@@ -157,7 +163,7 @@ async def fetch_gpt_response_stream(client, url, headers, payload):
|
|
157 |
line, buffer = buffer.split("\n", 1)
|
158 |
# logger.info("line: %s", repr(line))
|
159 |
if line and line != "data: " and line != "data:" and not line.startswith(": "):
|
160 |
-
yield line.strip() +
|
161 |
|
162 |
async def fetch_cloudflare_response_stream(client, url, headers, payload, model):
|
163 |
timestamp = int(datetime.timestamp(datetime.now()))
|
@@ -176,7 +182,7 @@ async def fetch_cloudflare_response_stream(client, url, headers, payload, model)
|
|
176 |
if line.startswith("data:"):
|
177 |
line = line.lstrip("data: ")
|
178 |
if line == "[DONE]":
|
179 |
-
yield "data: [DONE]
|
180 |
return
|
181 |
resp: dict = json.loads(line)
|
182 |
message = resp.get("response")
|
@@ -200,7 +206,7 @@ async def fetch_cohere_response_stream(client, url, headers, payload, model):
|
|
200 |
# logger.info("line: %s", repr(line))
|
201 |
resp: dict = json.loads(line)
|
202 |
if resp.get("is_finished") == True:
|
203 |
-
yield "data: [DONE]
|
204 |
return
|
205 |
if resp.get("event_type") == "text-generation":
|
206 |
message = resp.get("text")
|
@@ -266,7 +272,7 @@ async def fetch_claude_response_stream(client, url, headers, payload, model):
|
|
266 |
function_call_content = delta["partial_json"]
|
267 |
sse_string = await generate_sse_response(timestamp, model, None, None, None, function_call_content)
|
268 |
yield sse_string
|
269 |
-
yield "data: [DONE]
|
270 |
|
271 |
async def fetch_response(client, url, headers, payload):
|
272 |
response = None
|
|
|
4 |
|
5 |
from log_config import logger
|
6 |
|
7 |
+
# end_of_line = "\n\r\n"
|
8 |
+
# end_of_line = "\r\n"
|
9 |
+
# end_of_line = "\n\r"
|
10 |
+
end_of_line = "\n\n"
|
11 |
+
# end_of_line = "\r"
|
12 |
+
# end_of_line = "\n"
|
13 |
|
14 |
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):
|
15 |
sample_data = {
|
|
|
42 |
json_data = json.dumps(sample_data, ensure_ascii=False)
|
43 |
|
44 |
# 构建SSE响应
|
45 |
+
sse_response = f"data: {json_data}" + end_of_line
|
46 |
|
47 |
return sse_response
|
48 |
|
|
|
100 |
function_full_response = json.dumps(function_call["functionCall"]["args"])
|
101 |
sse_string = await generate_sse_response(timestamp, model, content=None, tools_id="chatcmpl-9inWv0yEtgn873CxMBzHeCeiHctTV", function_call_name=None, function_call_content=function_full_response)
|
102 |
yield sse_string
|
103 |
+
yield "data: [DONE]" + end_of_line
|
104 |
|
105 |
async def fetch_vertex_claude_response_stream(client, url, headers, payload, model):
|
106 |
timestamp = int(datetime.timestamp(datetime.now()))
|
|
|
147 |
function_full_response = json.dumps(function_call["input"])
|
148 |
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)
|
149 |
yield sse_string
|
150 |
+
yield "data: [DONE]" + end_of_line
|
151 |
|
152 |
async def fetch_gpt_response_stream(client, url, headers, payload):
|
153 |
async with client.stream('POST', url, headers=headers, json=payload) as response:
|
|
|
163 |
line, buffer = buffer.split("\n", 1)
|
164 |
# logger.info("line: %s", repr(line))
|
165 |
if line and line != "data: " and line != "data:" and not line.startswith(": "):
|
166 |
+
yield line.strip() + end_of_line
|
167 |
|
168 |
async def fetch_cloudflare_response_stream(client, url, headers, payload, model):
|
169 |
timestamp = int(datetime.timestamp(datetime.now()))
|
|
|
182 |
if line.startswith("data:"):
|
183 |
line = line.lstrip("data: ")
|
184 |
if line == "[DONE]":
|
185 |
+
yield "data: [DONE]" + end_of_line
|
186 |
return
|
187 |
resp: dict = json.loads(line)
|
188 |
message = resp.get("response")
|
|
|
206 |
# logger.info("line: %s", repr(line))
|
207 |
resp: dict = json.loads(line)
|
208 |
if resp.get("is_finished") == True:
|
209 |
+
yield "data: [DONE]" + end_of_line
|
210 |
return
|
211 |
if resp.get("event_type") == "text-generation":
|
212 |
message = resp.get("text")
|
|
|
272 |
function_call_content = delta["partial_json"]
|
273 |
sse_string = await generate_sse_response(timestamp, model, None, None, None, function_call_content)
|
274 |
yield sse_string
|
275 |
+
yield "data: [DONE]" + end_of_line
|
276 |
|
277 |
async def fetch_response(client, url, headers, payload):
|
278 |
response = None
|