caobin commited on
Commit
7a68b64
·
verified ·
1 Parent(s): 968eb3d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -11
app.py CHANGED
@@ -21,22 +21,29 @@ model = AutoModelForCausalLM.from_pretrained(
21
  model.eval()
22
 
23
  # ===============================
24
- # 核心聊天逻辑(使用 tuple history
25
  # ===============================
26
  def chat_fn(message, history):
27
  """
28
- history: List[Tuple[user, assistant]]
29
  """
30
- # 只保留最近 3 轮
31
- history = history[-3:]
 
32
 
33
  prompt = ""
34
- for user, assistant in history:
35
- prompt += f"<|user|>{user}<|assistant|>{assistant}"
 
 
36
 
 
37
  prompt += f"<|user|>{message}<|assistant|>"
38
 
39
- inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
 
 
 
40
 
41
  with torch.no_grad():
42
  output_ids = model.generate(
@@ -53,7 +60,7 @@ def chat_fn(message, history):
53
  skip_special_tokens=True
54
  )
55
 
56
- # 只取 assistant 的新回答
57
  if "<|assistant|>" in output_text:
58
  output_text = output_text.split("<|assistant|>")[-1]
59
 
@@ -61,20 +68,36 @@ def chat_fn(message, history):
61
 
62
 
63
  # ===============================
64
- # Gradio UI
65
  # ===============================
66
  with gr.Blocks(title="caobin LLM Chatbot") as demo:
67
  gr.Markdown("# 🤖 caobin's AI Assistant")
68
 
69
- chatbot = gr.Chatbot(height=450)
 
 
 
 
70
  msg = gr.Textbox(
71
  label="输入你的问题",
72
  placeholder="请输入你的问题,支持多轮对话"
73
  )
74
 
75
  def respond(message, chat_history):
 
 
 
 
 
 
 
76
  response = chat_fn(message, chat_history)
77
- chat_history.append((message, response))
 
 
 
 
 
78
  return "", chat_history
79
 
80
  msg.submit(
 
21
  model.eval()
22
 
23
  # ===============================
24
+ # 聊天核心(messages schema
25
  # ===============================
26
  def chat_fn(message, history):
27
  """
28
+ history: List[Dict] with keys: role, content
29
  """
30
+
31
+ # 只保留最近 3 轮(6 条 message)
32
+ history = history[-6:]
33
 
34
  prompt = ""
35
+ for msg in history:
36
+ role = msg["role"]
37
+ content = msg["content"]
38
+ prompt += f"<|{role}|>{content}"
39
 
40
+ # 当前用户问题
41
  prompt += f"<|user|>{message}<|assistant|>"
42
 
43
+ inputs = tokenizer(
44
+ prompt,
45
+ return_tensors="pt"
46
+ ).to(model.device)
47
 
48
  with torch.no_grad():
49
  output_ids = model.generate(
 
60
  skip_special_tokens=True
61
  )
62
 
63
+ # 只取 assistant 新生成的部分
64
  if "<|assistant|>" in output_text:
65
  output_text = output_text.split("<|assistant|>")[-1]
66
 
 
68
 
69
 
70
  # ===============================
71
+ # Gradio UI(messages 模式)
72
  # ===============================
73
  with gr.Blocks(title="caobin LLM Chatbot") as demo:
74
  gr.Markdown("# 🤖 caobin's AI Assistant")
75
 
76
+ chatbot = gr.Chatbot(
77
+ type="messages",
78
+ height=450
79
+ )
80
+
81
  msg = gr.Textbox(
82
  label="输入你的问题",
83
  placeholder="请输入你的问题,支持多轮对话"
84
  )
85
 
86
  def respond(message, chat_history):
87
+ # 用户消息
88
+ chat_history.append({
89
+ "role": "user",
90
+ "content": message
91
+ })
92
+
93
+ # 模型回复
94
  response = chat_fn(message, chat_history)
95
+
96
+ chat_history.append({
97
+ "role": "assistant",
98
+ "content": response
99
+ })
100
+
101
  return "", chat_history
102
 
103
  msg.submit(