ValueFX9507 commited on
Commit
b6f2df7
·
verified ·
1 Parent(s): 650c6df

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -9
app.py CHANGED
@@ -64,9 +64,14 @@ def format_message(content):
64
 
65
 
66
  def user_input(user_message, history):
67
- return "", history # 不再主动添加消息到历史记录
 
 
 
 
68
 
69
  def predict(message, chat_history, system_msg, temperature, top_p, repetition_penalty):
 
70
  headers = {
71
  "Content-Type": "application/json",
72
  "Authorization": f"Bearer {API_KEY}"
@@ -74,10 +79,13 @@ def predict(message, chat_history, system_msg, temperature, top_p, repetition_pe
74
 
75
  if chat_history is None:
76
  chat_history = []
 
 
 
77
 
78
  # 构造对话历史
79
  messages = [{"role": "system", "content": system_msg}]
80
- for msg in chat_history:
81
  messages.append({"role": "user", "content": msg[0]})
82
  if msg[1]:
83
  messages.append({"role": "assistant", "content": msg[1]})
@@ -99,23 +107,21 @@ def predict(message, chat_history, system_msg, temperature, top_p, repetition_pe
99
  )
100
 
101
  buffer = ""
102
- history = chat_history.copy() + [[message, None]] # 初始化新对话
103
 
104
- # 流式处理关键修改点
105
  for chunk in response.iter_lines():
106
  if chunk:
107
- chunk_str = chunk.decode("utf-8").replace("data: ", "")
108
- try:
109
  chunk_data = json.loads(chunk_str)
110
  if "choices" in chunk_data:
111
  delta = chunk_data["choices"][0].get("delta", {})
112
  if "content" in delta:
113
- # 增量更新缓冲区
114
  buffer += delta["content"]
115
 
116
- # 只更新当前消息的最新状态
117
  new_history = history.copy()
118
- new_history[-1][1] = format_message(buffer) # 直接使用字符串
119
  yield new_history
120
  except Exception as e:
121
  print(f"Error in processing chunk: {e}")
@@ -129,6 +135,7 @@ def predict(message, chat_history, system_msg, temperature, top_p, repetition_pe
129
 
130
 
131
 
 
132
  # JavaScript to ensure the details tag is working properly
133
  js_script = """
134
  <script>
 
64
 
65
 
66
  def user_input(user_message, history):
67
+ """处理用户输入"""
68
+ if history is None:
69
+ history = []
70
+ history = history + [[user_message, None]] # 只添加用户消息
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
 
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
  )
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").:
 
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}")
 
135
 
136
 
137
 
138
+
139
  # JavaScript to ensure the details tag is working properly
140
  js_script = """
141
  <script>