fix: 过滤 sse_starlette 返回的 ping 包,避免 `JSON Decoder error : ping -...` (#2585)

This commit is contained in:
liunux4odoo 2024-01-09 09:14:29 +08:00 committed by GitHub
parent 6f85119f03
commit 448c99f969
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 1 deletions

View File

@ -73,7 +73,7 @@ class MiniMaxWorker(ApiModelWorker):
with response as r: with response as r:
text = "" text = ""
for e in r.iter_text(): for e in r.iter_text():
if not e.startswith("data: "): # 真是优秀的返回 if not e.startswith("data: "):
data = { data = {
"error_code": 500, "error_code": 500,
"text": f"minimax返回错误的结果{e}", "text": f"minimax返回错误的结果{e}",

View File

@ -135,6 +135,8 @@ class ApiRequest:
try: try:
if chunk.startswith("data: "): if chunk.startswith("data: "):
data = json.loads(chunk[6:-2]) data = json.loads(chunk[6:-2])
elif chunk.startswith(":"): # skip sse comment line
continue
else: else:
data = json.loads(chunk) data = json.loads(chunk)
yield data yield data
@ -169,6 +171,8 @@ class ApiRequest:
try: try:
if chunk.startswith("data: "): if chunk.startswith("data: "):
data = json.loads(chunk[6:-2]) data = json.loads(chunk[6:-2])
elif chunk.startswith(":"): # skip sse comment line
continue
else: else:
data = json.loads(chunk) data = json.loads(chunk)
yield data yield data