Wanswan commited on
Commit
8e63755
·
verified ·
1 Parent(s): 08607d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -22
app.py CHANGED
@@ -91,11 +91,10 @@ async def respond(user_input, history, step, language, questions, followup_mode)
91
 
92
  # === 如果是追问回应 ===
93
  if followup_mode:
94
- # bot 对追问做出简短回应
95
  comment_prompt = [
96
  {"role": "system", "content": f"Write a short, empathetic comment in {language} responding to the user's last answer."},
97
- history[-2], # 用户上次回答
98
- history[-1] # 用户追问回答
99
  ]
100
  comment_response = client.chat.completions.create(
101
  model="gpt-3.5-turbo",
@@ -104,23 +103,23 @@ async def respond(user_input, history, step, language, questions, followup_mode)
104
  )
105
  comment = comment_response.choices[0].message.content.strip()
106
 
107
- # 输出回应
108
  history.append({"role": "assistant", "content": ""})
109
  for c in comment:
110
  history[-1]["content"] += c
111
  await asyncio.sleep(0.03)
112
 
113
- # 然后推进到下一个问题
114
- next_question = f"{step+1}. {questions[step]}"
115
- history.append({"role": "assistant", "content": ""})
116
- for c in next_question:
117
- history[-1]["content"] += c
118
- await asyncio.sleep(0.03)
119
-
120
- return history, "", step + 1, language, questions, False
121
-
122
- # === 正常问题回答阶段 ===
123
- # 生成简评 + 是否触发追问
 
124
  comment_prompt = [
125
  {"role": "system", "content": f"You are Robin, a warm and engaging conversational AI companion who chats naturally with the user, asks questions, and responds thoughtfully to their answers. Write 1–2 natural follow-up questions based on the user's answer. Do NOT number the questions. Just write them as natural, standalone questions. Write a short, personalized comment on the user's answer in {language}. Add a light emoji at the end."},
126
  history[-2],
@@ -133,7 +132,6 @@ async def respond(user_input, history, step, language, questions, followup_mode)
133
  )
134
  bot_reply = comment_response.choices[0].message.content.strip()
135
 
136
- # 控制追问次数
137
  followup_count = 0
138
  rand = random.random()
139
  if rand < 0.1:
@@ -142,7 +140,6 @@ async def respond(user_input, history, step, language, questions, followup_mode)
142
  followup_count = 1
143
 
144
  if followup_count > 0:
145
- # 生成追问
146
  followup_prompt = [
147
  {"role": "system", "content": f"Ask {followup_count} open-ended follow-up question(s) based on the user's last answer in {language}. Do NOT number them."},
148
  history[-2],
@@ -154,16 +151,12 @@ async def respond(user_input, history, step, language, questions, followup_mode)
154
  temperature=0.7
155
  )
156
  bot_reply += "\n\n" + followup_response.choices[0].message.content.strip()
157
-
158
  history.append({"role": "assistant", "content": ""})
159
  for c in bot_reply:
160
  history[-1]["content"] += c
161
  await asyncio.sleep(0.03)
162
-
163
- # 标记进入追问等待状态
164
  return history, "", step, language, questions, True
165
 
166
- # 若无追问,直接回应并进入下一题
167
  history.append({"role": "assistant", "content": ""})
168
  for c in bot_reply:
169
  history[-1]["content"] += c
@@ -174,10 +167,10 @@ async def respond(user_input, history, step, language, questions, followup_mode)
174
  for c in next_question:
175
  history[-1]["content"] += c
176
  await asyncio.sleep(0.03)
177
-
178
  return history, "", step + 1, language, questions, False
179
 
180
 
 
181
  def init():
182
  return [{"role": "assistant", "content": WELCOME_MESSAGE_EN}], 0, "", [], False
183
 
 
91
 
92
  # === 如果是追问回应 ===
93
  if followup_mode:
 
94
  comment_prompt = [
95
  {"role": "system", "content": f"Write a short, empathetic comment in {language} responding to the user's last answer."},
96
+ history[-2],
97
+ history[-1]
98
  ]
99
  comment_response = client.chat.completions.create(
100
  model="gpt-3.5-turbo",
 
103
  )
104
  comment = comment_response.choices[0].message.content.strip()
105
 
 
106
  history.append({"role": "assistant", "content": ""})
107
  for c in comment:
108
  history[-1]["content"] += c
109
  await asyncio.sleep(0.03)
110
 
111
+ # 确保只推进一个正确的问题(防止跳题)
112
+ if step < len(questions):
113
+ next_question = f"{step+1}. {questions[step]}"
114
+ history.append({"role": "assistant", "content": ""})
115
+ for c in next_question:
116
+ history[-1]["content"] += c
117
+ await asyncio.sleep(0.03)
118
+ return history, "", step + 1, language, questions, False
119
+ else:
120
+ return history, "", step, language, questions, False
121
+
122
+ # === 正常主问题回答流程 ===
123
  comment_prompt = [
124
  {"role": "system", "content": f"You are Robin, a warm and engaging conversational AI companion who chats naturally with the user, asks questions, and responds thoughtfully to their answers. Write 1–2 natural follow-up questions based on the user's answer. Do NOT number the questions. Just write them as natural, standalone questions. Write a short, personalized comment on the user's answer in {language}. Add a light emoji at the end."},
125
  history[-2],
 
132
  )
133
  bot_reply = comment_response.choices[0].message.content.strip()
134
 
 
135
  followup_count = 0
136
  rand = random.random()
137
  if rand < 0.1:
 
140
  followup_count = 1
141
 
142
  if followup_count > 0:
 
143
  followup_prompt = [
144
  {"role": "system", "content": f"Ask {followup_count} open-ended follow-up question(s) based on the user's last answer in {language}. Do NOT number them."},
145
  history[-2],
 
151
  temperature=0.7
152
  )
153
  bot_reply += "\n\n" + followup_response.choices[0].message.content.strip()
 
154
  history.append({"role": "assistant", "content": ""})
155
  for c in bot_reply:
156
  history[-1]["content"] += c
157
  await asyncio.sleep(0.03)
 
 
158
  return history, "", step, language, questions, True
159
 
 
160
  history.append({"role": "assistant", "content": ""})
161
  for c in bot_reply:
162
  history[-1]["content"] += c
 
167
  for c in next_question:
168
  history[-1]["content"] += c
169
  await asyncio.sleep(0.03)
 
170
  return history, "", step + 1, language, questions, False
171
 
172
 
173
+
174
  def init():
175
  return [{"role": "assistant", "content": WELCOME_MESSAGE_EN}], 0, "", [], False
176