Cran-May commited on
Commit
4bc12e0
1 Parent(s): 4e9cd26

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -19
app.py CHANGED
@@ -32,27 +32,21 @@ def chat_fn(message, history, model, system_message, max_tokens, temperature, to
32
  response_generator = respond(message, history_list, model, system_message, max_tokens, temperature, top_p, top_k, repeat_penalty)
33
 
34
  for messages in response_generator:
35
- # 将历史消息直接转换为 Gradio Chatbot 格式
36
  chatbot_messages = []
37
- for user_msg, assistant_msg in messages:
38
- if user_msg: # 添加用户消息
39
- chatbot_messages.append([user_msg, False])
40
- if assistant_msg: # 添加助手消息
41
- chatbot_messages.append([assistant_msg, True])
 
 
 
 
42
  yield chatbot_messages, messages
43
 
44
 
45
- def respond(
46
- message,
47
- history: list[tuple[str, str]],
48
- model,
49
- system_message,
50
- max_tokens,
51
- temperature,
52
- top_p,
53
- top_k,
54
- repeat_penalty,
55
- ):
56
  global llm
57
  global llm_model
58
 
@@ -117,8 +111,18 @@ def respond(
117
  for output in stream:
118
  outputs += output
119
  token_count += len(output.split())
120
- current_history = history + [(message, outputs)]
121
- yield current_history
 
 
 
 
 
 
 
 
 
 
122
 
123
  end_time = time.time()
124
  latency = end_time - start_time
 
32
  response_generator = respond(message, history_list, model, system_message, max_tokens, temperature, top_p, top_k, repeat_penalty)
33
 
34
  for messages in response_generator:
 
35
  chatbot_messages = []
36
+ for msg in messages:
37
+ if isinstance(msg, tuple): # 如果是旧格式的元组
38
+ user_msg, assistant_msg = msg
39
+ if user_msg:
40
+ chatbot_messages.append({"role": "user", "content": user_msg})
41
+ if assistant_msg:
42
+ chatbot_messages.append({"role": "assistant", "content": assistant_msg})
43
+ else: # 如果已经是字典格式
44
+ chatbot_messages.append(msg)
45
  yield chatbot_messages, messages
46
 
47
 
48
+ def respond(message, history, model, system_message, max_tokens, temperature, top_p, top_k, repeat_penalty):
49
+
 
 
 
 
 
 
 
 
 
50
  global llm
51
  global llm_model
52
 
 
111
  for output in stream:
112
  outputs += output
113
  token_count += len(output.split())
114
+ current_messages = []
115
+
116
+ # 添加历史消息
117
+ for h in history:
118
+ current_messages.append({"role": "user", "content": h[0]})
119
+ current_messages.append({"role": "assistant", "content": h[1]})
120
+
121
+ # 添加当前对话
122
+ current_messages.append({"role": "user", "content": message})
123
+ current_messages.append({"role": "assistant", "content": outputs})
124
+
125
+ yield current_messages
126
 
127
  end_time = time.time()
128
  latency = end_time - start_time