leonsimon23 commited on
Commit
b586950
·
verified ·
1 Parent(s): f05e73d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -17
app.py CHANGED
@@ -33,7 +33,6 @@ API_KEY = "fastgpt-aleL83PzPiul9lZDJQFD4NHYfVIxP7mDIWFCyin2FuMhYgwlRC3NMkV2K5lxc
33
  import ijson # Install with: pip install ijson
34
 
35
 
36
-
37
  def chat_with_fastgpt(message, history):
38
  """
39
  与 FastGPT 进行对话的函数
@@ -70,28 +69,37 @@ def chat_with_fastgpt(message, history):
70
  )
71
  response.raise_for_status()
72
 
73
- # Use ijson to parse the stream
74
- parser = ijson.parse(response.raw) # Use response.raw for the raw byte stream
75
- current_content = ""
76
- for prefix, event, value in parser:
77
- if prefix == "choices.item.message.content" and event == "string":
78
- current_content += value
79
- elif current_content:
80
- # Yield the accumulated content
81
- for i in range(0, len(current_content), 10):
82
- yield current_content[i:i + 10]
83
- current_content = ""
84
-
85
- # Yield any remaining content
86
- if current_content:
87
- for i in range(0, len(current_content), 10):
88
- yield current_content[i:i + 10]
 
 
 
 
 
 
 
 
89
 
90
  except requests.exceptions.RequestException as e:
91
  print(f"请求错误: {e}")
92
  yield "请求 FastGPT API 出错,请检查网络或 API Key。"
93
  except Exception as e:
94
  print(f"未知错误: {e}")
 
95
  yield "发生未知错误。"
96
 
97
  # Gradio 界面
 
33
  import ijson # Install with: pip install ijson
34
 
35
 
 
36
  def chat_with_fastgpt(message, history):
37
  """
38
  与 FastGPT 进行对话的函数
 
69
  )
70
  response.raise_for_status()
71
 
72
+ buffer = ""
73
+ for chunk in response.iter_content(chunk_size=8192):
74
+ if chunk:
75
+ buffer += chunk.decode('utf-8')
76
+
77
+ # 尝试解析 JSON 对象
78
+ while True: # Continue processing the buffer until no more complete objects are found
79
+ try:
80
+ first_brace_index = buffer.find('{')
81
+ if first_brace_index == -1:
82
+ break
83
+
84
+ obj, end_index = json.JSONDecoder().raw_decode(buffer[first_brace_index:])
85
+
86
+ # 解析成功,处理 JSON 对象
87
+ if obj.get("choices"):
88
+ delta = obj["choices"][0].get("delta")
89
+ if delta and delta.get("content"):
90
+ yield delta["content"]
91
+
92
+ buffer = buffer[first_brace_index + end_index:]
93
+
94
+ except json.JSONDecodeError as e:
95
+ break
96
 
97
  except requests.exceptions.RequestException as e:
98
  print(f"请求错误: {e}")
99
  yield "请求 FastGPT API 出错,请检查网络或 API Key。"
100
  except Exception as e:
101
  print(f"未知错误: {e}")
102
+ print(f"错误信息: {e}")
103
  yield "发生未知错误。"
104
 
105
  # Gradio 界面