ValueFX9507 commited on
Commit
f0cbed9
·
verified ·
1 Parent(s): 0e0b334

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -9
app.py CHANGED
@@ -71,7 +71,6 @@ def user_input(user_message, history):
71
  return "", history
72
 
73
  def predict(message, chat_history, system_msg, temperature, top_p, repetition_penalty):
74
- """处理AI回复"""
75
  headers = {
76
  "Content-Type": "application/json",
77
  "Authorization": f"Bearer {API_KEY}"
@@ -79,13 +78,10 @@ def predict(message, chat_history, system_msg, temperature, top_p, repetition_pe
79
 
80
  if chat_history is None:
81
  chat_history = []
82
-
83
- # 不需要再添加用户消息,因为已经在user_input中添加了
84
- history = chat_history.copy()
85
 
86
  # 构造对话历史
87
  messages = [{"role": "system", "content": system_msg}]
88
- for msg in history:
89
  messages.append({"role": "user", "content": msg[0]})
90
  if msg[1]:
91
  messages.append({"role": "assistant", "content": msg[1]})
@@ -107,21 +103,22 @@ def predict(message, chat_history, system_msg, temperature, top_p, repetition_pe
107
  )
108
 
109
  buffer = ""
110
- first_chunk = True
111
 
112
- # 流式处理
113
  for chunk in response.iter_lines():
114
  if chunk:
115
  chunk_str = chunk.decode("utf-8").replace("data: ", "")
 
116
  chunk_data = json.loads(chunk_str)
117
  if "choices" in chunk_data:
118
  delta = chunk_data["choices"][0].get("delta", {})
119
  if "content" in delta:
 
120
  buffer += delta["content"]
121
 
122
- # 更新最后一条消息的回复
123
  new_history = history.copy()
124
- new_history[-1][1] = format_message(buffer)
125
  yield new_history
126
  except Exception as e:
127
  print(f"Error in processing chunk: {e}")
@@ -136,6 +133,7 @@ def predict(message, chat_history, system_msg, temperature, top_p, repetition_pe
136
 
137
 
138
 
 
139
  # JavaScript to ensure the details tag is working properly
140
  js_script = """
141
  <script>
 
71
  return "", history
72
 
73
  def predict(message, chat_history, system_msg, temperature, top_p, repetition_penalty):
 
74
  headers = {
75
  "Content-Type": "application/json",
76
  "Authorization": f"Bearer {API_KEY}"
 
78
 
79
  if chat_history is None:
80
  chat_history = []
 
 
 
81
 
82
  # 构造对话历史
83
  messages = [{"role": "system", "content": system_msg}]
84
+ for msg in chat_history:
85
  messages.append({"role": "user", "content": msg[0]})
86
  if msg[1]:
87
  messages.append({"role": "assistant", "content": msg[1]})
 
103
  )
104
 
105
  buffer = ""
 
106
 
107
+ # 流式处理关键修改点
108
  for chunk in response.iter_lines():
109
  if chunk:
110
  chunk_str = chunk.decode("utf-8").replace("data: ", "")
111
+ try:
112
  chunk_data = json.loads(chunk_str)
113
  if "choices" in chunk_data:
114
  delta = chunk_data["choices"][0].get("delta", {})
115
  if "content" in delta:
116
+ # 增量更新缓冲区
117
  buffer += delta["content"]
118
 
119
+ # 只更新当前消息的最新状态
120
  new_history = history.copy()
121
+ new_history[-1][1] = format_message(buffer) # 直接使用字符串
122
  yield new_history
123
  except Exception as e:
124
  print(f"Error in processing chunk: {e}")
 
133
 
134
 
135
 
136
+
137
  # JavaScript to ensure the details tag is working properly
138
  js_script = """
139
  <script>