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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -11
app.py CHANGED
@@ -69,28 +69,26 @@ async def gpt_translate(text, target_lang):
69
  )
70
  return response.choices[0].message.content.strip()
71
 
72
- async def respond(user_input, history, step, language, questions, followup_mode):
73
  history.append({"role": "user", "content": user_input})
74
 
75
- # === 用户选择语言 ===
76
  if language == "":
77
  user_language = user_input.strip().capitalize()
78
  questions_en = generate_question_set()
79
  translated_questions = [await gpt_translate(q, user_language) for q in questions_en]
80
  welcome = await gpt_translate("Great! We will now continue in your selected language.", user_language)
81
  history.append({"role": "assistant", "content": f"{welcome}\n\n1. {translated_questions[0]}"})
82
- return history, "", 1, user_language, translated_questions, False
83
 
84
- # === 所有问题问完 ===
85
  if step >= len(questions):
86
  thank_you = await gpt_translate("Thank you for your response! It was a pleasure talking with you. I hope you enjoyed the conversation.", language)
87
  end_note = await gpt_translate("This concludes our questions. Please proceed with the rest of the survey. 📝", language)
88
  history.append({"role": "assistant", "content": thank_you})
89
  history.append({"role": "assistant", "content": end_note})
90
- return history, "", step, language, questions, False
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],
@@ -108,16 +106,94 @@ async def respond(user_input, history, step, language, questions, followup_mode)
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 = [
 
69
  )
70
  return response.choices[0].message.content.strip()
71
 
72
+ async def respond(user_input, history, step, language, questions, followup_mode, followup_stage):
73
  history.append({"role": "user", "content": user_input})
74
 
 
75
  if language == "":
76
  user_language = user_input.strip().capitalize()
77
  questions_en = generate_question_set()
78
  translated_questions = [await gpt_translate(q, user_language) for q in questions_en]
79
  welcome = await gpt_translate("Great! We will now continue in your selected language.", user_language)
80
  history.append({"role": "assistant", "content": f"{welcome}\n\n1. {translated_questions[0]}"})
81
+ return history, "", 1, user_language, translated_questions, False, 0
82
 
 
83
  if step >= len(questions):
84
  thank_you = await gpt_translate("Thank you for your response! It was a pleasure talking with you. I hope you enjoyed the conversation.", language)
85
  end_note = await gpt_translate("This concludes our questions. Please proceed with the rest of the survey. 📝", language)
86
  history.append({"role": "assistant", "content": thank_you})
87
  history.append({"role": "assistant", "content": end_note})
88
+ return history, "", step, language, questions, False, 0
89
 
 
90
  if followup_mode:
91
+ # bot 回复用户的追问回答
92
  comment_prompt = [
93
  {"role": "system", "content": f"Write a short, empathetic comment in {language} responding to the user's last answer."},
94
  history[-2],
 
106
  history[-1]["content"] += c
107
  await asyncio.sleep(0.03)
108
 
109
+ if followup_stage == 1:
110
+ # 可能还有第2次追问
111
+ if random.random() < 0.1:
112
+ followup_prompt = [
113
+ {"role": "system", "content": f"Ask 1 more follow-up question based on the user's last answer in {language}. Do NOT number it."},
114
+ history[-2],
115
+ history[-1]
116
+ ]
117
+ followup_response = client.chat.completions.create(
118
+ model="gpt-3.5-turbo",
119
+ messages=followup_prompt,
120
+ temperature=0.7
121
+ )
122
+ followup_q = followup_response.choices[0].message.content.strip()
123
+ history.append({"role": "assistant", "content": ""})
124
+ for c in followup_q:
125
+ history[-1]["content"] += c
126
+ await asyncio.sleep(0.03)
127
+ return history, "", step, language, questions, True, 2
128
+ else:
129
+ # 无第二次追问,进入下一题
130
+ next_question = f"{step+1}. {questions[step]}"
131
+ history.append({"role": "assistant", "content": ""})
132
+ for c in next_question:
133
+ history[-1]["content"] += c
134
+ await asyncio.sleep(0.03)
135
+ return history, "", step + 1, language, questions, False, 0
136
+
137
+ elif followup_stage == 2:
138
+ # 回答完第二次追问,直接进入下一主问题
139
  next_question = f"{step+1}. {questions[step]}"
140
  history.append({"role": "assistant", "content": ""})
141
  for c in next_question:
142
  history[-1]["content"] += c
143
  await asyncio.sleep(0.03)
144
+ return history, "", step + 1, language, questions, False, 0
145
+
146
+ # 正常主问题回答逻辑
147
+ comment_prompt = [
148
+ {"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."},
149
+ history[-2],
150
+ history[-1]
151
+ ]
152
+ comment_response = client.chat.completions.create(
153
+ model="gpt-3.5-turbo",
154
+ messages=comment_prompt,
155
+ temperature=0.8
156
+ )
157
+ bot_reply = comment_response.choices[0].message.content.strip()
158
+
159
+ followup_count = 0
160
+ rand = random.random()
161
+ if rand < 0.1:
162
+ followup_count = 2
163
+ elif rand < 0.5:
164
+ followup_count = 1
165
+
166
+ if followup_count > 0:
167
+ followup_prompt = [
168
+ {"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."},
169
+ history[-2],
170
+ history[-1]
171
+ ]
172
+ followup_response = client.chat.completions.create(
173
+ model="gpt-3.5-turbo",
174
+ messages=followup_prompt,
175
+ temperature=0.7
176
+ )
177
+ bot_reply += "\n\n" + followup_response.choices[0].message.content.strip()
178
+
179
+ history.append({"role": "assistant", "content": ""})
180
+ for c in bot_reply:
181
+ history[-1]["content"] += c
182
+ await asyncio.sleep(0.03)
183
+ return history, "", step, language, questions, True, 1
184
+
185
+ # 无追问,直接进入下一题
186
+ history.append({"role": "assistant", "content": ""})
187
+ for c in bot_reply:
188
+ history[-1]["content"] += c
189
+ await asyncio.sleep(0.03)
190
+
191
+ next_question = f"{step+1}. {questions[step]}"
192
+ history.append({"role": "assistant", "content": ""})
193
+ for c in next_question:
194
+ history[-1]["content"] += c
195
+ await asyncio.sleep(0.03)
196
+ return history, "", step + 1, language, questions, False, 0
197
 
198
  # === 正常主问题回答流程 ===
199
  comment_prompt = [