yym68686 commited on
Commit
eff878c
·
1 Parent(s): 93aef2a

Improve GPT response compatibility

Browse files
Files changed (2) hide show
  1. response.py +4 -2
  2. utils.py +1 -1
response.py CHANGED
@@ -80,11 +80,13 @@ async def fetch_gpt_response_stream(client, url, headers, payload):
80
  yield {"error": f"fetch_gpt_response_stream HTTP Error {response.status_code}", "details": error_message.decode('utf-8', errors='replace')}
81
  buffer = ""
82
  async for chunk in response.aiter_text():
83
- # print(chunk)
84
  buffer += chunk
85
  while "\n" in buffer:
86
  line, buffer = buffer.split("\n", 1)
87
- yield line + "\n"
 
 
88
 
89
  async def fetch_claude_response_stream(client, url, headers, payload, model):
90
  timestamp = datetime.timestamp(datetime.now())
 
80
  yield {"error": f"fetch_gpt_response_stream HTTP Error {response.status_code}", "details": error_message.decode('utf-8', errors='replace')}
81
  buffer = ""
82
  async for chunk in response.aiter_text():
83
+ # print("chunk", repr(chunk))
84
  buffer += chunk
85
  while "\n" in buffer:
86
  line, buffer = buffer.split("\n", 1)
87
+ # print("line", repr(line))
88
+ if line and line != "data: " and line != "data:":
89
+ yield line + "\n"
90
 
91
  async def fetch_claude_response_stream(client, url, headers, payload, model):
92
  timestamp = datetime.timestamp(datetime.now())
utils.py CHANGED
@@ -92,7 +92,7 @@ async def error_handling_wrapper(generator, status_code=200):
92
  try:
93
  first_item_str = json.loads(first_item_str)
94
  except json.JSONDecodeError:
95
- logger.error("error_handling_wrapper JSONDecodeError!", first_item_str)
96
  pass # 如果不是有效的JSON,保持原样
97
  if isinstance(first_item_str, dict) and 'error' in first_item_str:
98
  # 如果第一个 yield 的项是错误信息,抛出 HTTPException
 
92
  try:
93
  first_item_str = json.loads(first_item_str)
94
  except json.JSONDecodeError:
95
+ logger.error("error_handling_wrapper JSONDecodeError!" + repr(first_item_str))
96
  pass # 如果不是有效的JSON,保持原样
97
  if isinstance(first_item_str, dict) and 'error' in first_item_str:
98
  # 如果第一个 yield 的项是错误信息,抛出 HTTPException