Fix the bug of returning JSON parsing error for invalid request information.
Browse files- response.py +12 -8
response.py
CHANGED
@@ -70,14 +70,14 @@ async def fetch_gemini_response_stream(client, url, headers, payload, model):
|
|
70 |
|
71 |
async def fetch_gpt_response_stream(client, url, headers, payload):
|
72 |
async with client.stream('POST', url, headers=headers, json=payload) as response:
|
73 |
-
# print("response.status_code", response.status_code)
|
74 |
if response.status_code != 200:
|
75 |
-
# print("请求失败,状态码是", response.status_code)
|
76 |
error_message = await response.aread()
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
|
|
|
|
81 |
buffer = ""
|
82 |
async for chunk in response.aiter_text():
|
83 |
# print("chunk", repr(chunk))
|
@@ -94,14 +94,18 @@ async def fetch_claude_response_stream(client, url, headers, payload, model):
|
|
94 |
if response.status_code != 200:
|
95 |
error_message = await response.aread()
|
96 |
error_str = error_message.decode('utf-8', errors='replace')
|
97 |
-
|
|
|
|
|
|
|
98 |
yield {"error": f"fetch_claude_response_stream HTTP Error {response.status_code}", "details": error_json}
|
99 |
buffer = ""
|
100 |
async for chunk in response.aiter_text():
|
|
|
101 |
buffer += chunk
|
102 |
while "\n" in buffer:
|
103 |
line, buffer = buffer.split("\n", 1)
|
104 |
-
#
|
105 |
|
106 |
if line.startswith("data:"):
|
107 |
line = line[5:]
|
|
|
70 |
|
71 |
async def fetch_gpt_response_stream(client, url, headers, payload):
|
72 |
async with client.stream('POST', url, headers=headers, json=payload) as response:
|
|
|
73 |
if response.status_code != 200:
|
|
|
74 |
error_message = await response.aread()
|
75 |
+
error_str = error_message.decode('utf-8', errors='replace')
|
76 |
+
try:
|
77 |
+
error_json = json.loads(error_str)
|
78 |
+
except json.JSONDecodeError:
|
79 |
+
error_json = error_str
|
80 |
+
yield {"error": f"fetch_gpt_response_stream HTTP Error {response.status_code}", "details": error_json}
|
81 |
buffer = ""
|
82 |
async for chunk in response.aiter_text():
|
83 |
# print("chunk", repr(chunk))
|
|
|
94 |
if response.status_code != 200:
|
95 |
error_message = await response.aread()
|
96 |
error_str = error_message.decode('utf-8', errors='replace')
|
97 |
+
try:
|
98 |
+
error_json = json.loads(error_str)
|
99 |
+
except json.JSONDecodeError:
|
100 |
+
error_json = error_str
|
101 |
yield {"error": f"fetch_claude_response_stream HTTP Error {response.status_code}", "details": error_json}
|
102 |
buffer = ""
|
103 |
async for chunk in response.aiter_text():
|
104 |
+
logger.info(f"chunk: {repr(chunk)}")
|
105 |
buffer += chunk
|
106 |
while "\n" in buffer:
|
107 |
line, buffer = buffer.split("\n", 1)
|
108 |
+
# logger.info(line)
|
109 |
|
110 |
if line.startswith("data:"):
|
111 |
line = line[5:]
|