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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -4
app.py CHANGED
@@ -71,6 +71,8 @@ async def gpt_translate(text, target_lang):
71
 
72
  async def respond(user_input, history, step, language, questions, followup_mode):
73
  history.append({"role": "user", "content": user_input})
 
 
74
  if language == "":
75
  user_language = user_input.strip().capitalize()
76
  questions_en = generate_question_set()
@@ -78,17 +80,22 @@ async def respond(user_input, history, step, language, questions, followup_mode)
78
  welcome = await gpt_translate("Great! We will now continue in your selected language.", user_language)
79
  history.append({"role": "assistant", "content": f"{welcome}\n\n1. {translated_questions[0]}"})
80
  return history, "", 1, user_language, translated_questions, False
 
 
81
  if step >= len(questions):
82
  thank_you = await gpt_translate("Thank you for your response! It was a pleasure talking with you. I hope you enjoyed the conversation.", language)
83
  end_note = await gpt_translate("This concludes our questions. Please proceed with the rest of the survey. 📝", language)
84
  history.append({"role": "assistant", "content": thank_you})
85
  history.append({"role": "assistant", "content": end_note})
86
  return history, "", step, language, questions, False
 
 
87
  if followup_mode:
 
88
  comment_prompt = [
89
  {"role": "system", "content": f"Write a short, empathetic comment in {language} responding to the user's last answer."},
90
- history[-2],
91
- history[-1]
92
  ]
93
  comment_response = client.chat.completions.create(
94
  model="gpt-3.5-turbo",
@@ -96,19 +103,26 @@ async def respond(user_input, history, step, language, questions, followup_mode)
96
  temperature=0.7
97
  )
98
  comment = comment_response.choices[0].message.content.strip()
 
 
99
  history.append({"role": "assistant", "content": ""})
100
  for c in comment:
101
  history[-1]["content"] += c
102
  await asyncio.sleep(0.03)
 
 
103
  next_question = f"{step+1}. {questions[step]}"
104
  history.append({"role": "assistant", "content": ""})
105
  for c in next_question:
106
  history[-1]["content"] += c
107
  await asyncio.sleep(0.03)
 
108
  return history, "", step + 1, language, questions, False
109
 
 
 
110
  comment_prompt = [
111
- {"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."},
112
  history[-2],
113
  history[-1]
114
  ]
@@ -119,6 +133,7 @@ async def respond(user_input, history, step, language, questions, followup_mode)
119
  )
120
  bot_reply = comment_response.choices[0].message.content.strip()
121
 
 
122
  followup_count = 0
123
  rand = random.random()
124
  if rand < 0.1:
@@ -127,8 +142,9 @@ async def respond(user_input, history, step, language, questions, followup_mode)
127
  followup_count = 1
128
 
129
  if followup_count > 0:
 
130
  followup_prompt = [
131
- {"role": "system", "content": f"Ask {followup_count} open-ended follow-up question(s) based on the user's last answer in {language}."},
132
  history[-2],
133
  history[-1]
134
  ]
@@ -138,23 +154,30 @@ async def respond(user_input, history, step, language, questions, followup_mode)
138
  temperature=0.7
139
  )
140
  bot_reply += "\n\n" + followup_response.choices[0].message.content.strip()
 
141
  history.append({"role": "assistant", "content": ""})
142
  for c in bot_reply:
143
  history[-1]["content"] += c
144
  await asyncio.sleep(0.03)
 
 
145
  return history, "", step, language, questions, True
146
 
 
147
  history.append({"role": "assistant", "content": ""})
148
  for c in bot_reply:
149
  history[-1]["content"] += c
150
  await asyncio.sleep(0.03)
 
151
  next_question = f"{step+1}. {questions[step]}"
152
  history.append({"role": "assistant", "content": ""})
153
  for c in next_question:
154
  history[-1]["content"] += c
155
  await asyncio.sleep(0.03)
 
156
  return history, "", step + 1, language, questions, False
157
 
 
158
  def init():
159
  return [{"role": "assistant", "content": WELCOME_MESSAGE_EN}], 0, "", [], False
160
 
 
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()
 
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
+ # 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",
 
103
  temperature=0.7
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],
127
  history[-1]
128
  ]
 
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
  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],
149
  history[-1]
150
  ]
 
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
170
  await asyncio.sleep(0.03)
171
+
172
  next_question = f"{step+1}. {questions[step]}"
173
  history.append({"role": "assistant", "content": ""})
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