Wanswan commited on
Commit
20aedc0
·
verified ·
1 Parent(s): a532bf2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -57
app.py CHANGED
@@ -55,6 +55,16 @@ SET_III = [
55
  "Think of a personal problem you're facing right now, big or small, and tell me about it. I'd love to hear how you're feeling about it — and if you'd like, I can share how I might deal with something similar."
56
  ]
57
 
 
 
 
 
 
 
 
 
 
 
58
  def generate_question_set():
59
  return random.sample(SET_I, 3) + random.sample(SET_II, 3) + random.sample(SET_III, 3)
60
 
@@ -69,6 +79,23 @@ 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, followup_stage):
73
  history.append({"role": "user", "content": user_input})
74
 
@@ -87,12 +114,17 @@ async def respond(user_input, history, step, language, questions, followup_mode,
87
  history.append({"role": "assistant", "content": end_note})
88
  return history, "", step, language, questions, False, 0
89
 
90
- # ======== 追问阶段 ==========
91
  if followup_mode:
 
 
 
 
 
 
 
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],
95
- history[-1]
96
  ]
97
  comment_response = client.chat.completions.create(
98
  model="gpt-3.5-turbo",
@@ -100,20 +132,13 @@ async def respond(user_input, history, step, language, questions, followup_mode,
100
  temperature=0.7
101
  )
102
  comment = comment_response.choices[0].message.content.strip()
103
-
104
- # 评论一泡
105
- history.append({"role": "assistant", "content": ""})
106
- for c in comment:
107
- history[-1]["content"] += c
108
- await asyncio.sleep(0.03)
109
 
110
  if followup_stage == 1:
111
  if random.random() < 0.1:
112
- # 有第二次追问 → 单独提问泡泡
113
  followup_prompt = [
114
- {"role": "system", "content": f"Ask 1 more open-ended follow-up question based on the user's last answer in {language}. Do NOT number it."},
115
- history[-2],
116
- history[-1]
117
  ]
118
  followup_response = client.chat.completions.create(
119
  model="gpt-3.5-turbo",
@@ -121,34 +146,21 @@ async def respond(user_input, history, step, language, questions, followup_mode,
121
  temperature=0.7
122
  )
123
  followup_q = followup_response.choices[0].message.content.strip()
124
- history.append({"role": "assistant", "content": ""})
125
- for c in followup_q:
126
- history[-1]["content"] += c
127
- await asyncio.sleep(0.03)
128
  return history, "", step, language, questions, True, 2
129
  else:
130
- # 无第二次追问 → 下一题
131
  next_question = f"{step+1}. {questions[step]}"
132
- history.append({"role": "assistant", "content": ""})
133
- for c in next_question:
134
- history[-1]["content"] += c
135
- await asyncio.sleep(0.03)
136
  return history, "", step + 1, language, questions, False, 0
137
 
138
  elif followup_stage == 2:
139
- # 第二次追问后 → 下一题
140
  next_question = f"{step+1}. {questions[step]}"
141
- history.append({"role": "assistant", "content": ""})
142
- for c in next_question:
143
- history[-1]["content"] += c
144
- await asyncio.sleep(0.03)
145
  return history, "", step + 1, language, questions, False, 0
146
 
147
- # ======== 主问题阶段 ==========
148
  comment_prompt = [
149
  {"role": "system", "content": f"Write a short, warm comment in {language} responding to the user's last answer."},
150
- history[-2],
151
- history[-1]
152
  ]
153
  comment_response = client.chat.completions.create(
154
  model="gpt-3.5-turbo",
@@ -164,11 +176,13 @@ async def respond(user_input, history, step, language, questions, followup_mode,
164
  elif rand < 0.5:
165
  followup_count = 1
166
 
 
 
 
167
  if followup_count > 0:
168
  followup_prompt = [
169
- {"role": "system", "content": f"Ask 1 open-ended follow-up question based on the user's last answer in {language}. Do NOT number it."},
170
- history[-2],
171
- history[-1]
172
  ]
173
  followup_response = client.chat.completions.create(
174
  model="gpt-3.5-turbo",
@@ -176,33 +190,13 @@ async def respond(user_input, history, step, language, questions, followup_mode,
176
  temperature=0.7
177
  )
178
  followup_q = followup_response.choices[0].message.content.strip()
179
-
180
- # 评论一泡
181
- history.append({"role": "assistant", "content": ""})
182
- for c in comment:
183
- history[-1]["content"] += c
184
- await asyncio.sleep(0.03)
185
-
186
- # 追问一泡
187
- history.append({"role": "assistant", "content": ""})
188
- for c in followup_q:
189
- history[-1]["content"] += c
190
- await asyncio.sleep(0.03)
191
-
192
  return history, "", step, language, questions, True, 1
193
 
194
- # 无追问:评论一泡 + 下一题一泡
195
- history.append({"role": "assistant", "content": ""})
196
- for c in comment:
197
- history[-1]["content"] += c
198
- await asyncio.sleep(0.03)
199
-
200
  next_question = f"{step+1}. {questions[step]}"
201
- history.append({"role": "assistant", "content": ""})
202
- for c in next_question:
203
- history[-1]["content"] += c
204
- await asyncio.sleep(0.03)
205
-
206
  return history, "", step + 1, language, questions, False, 0
207
 
208
  def init():
 
55
  "Think of a personal problem you're facing right now, big or small, and tell me about it. I'd love to hear how you're feeling about it — and if you'd like, I can share how I might deal with something similar."
56
  ]
57
 
58
+ BASE_TIRE_KEYWORDS = [
59
+ "you're asking too much",
60
+ "stop asking",
61
+ "i don't want to answer",
62
+ "enough",
63
+ "annoying",
64
+ "leave me alone",
65
+ "i'm tired of this"
66
+ ]
67
+
68
  def generate_question_set():
69
  return random.sample(SET_I, 3) + random.sample(SET_II, 3) + random.sample(SET_III, 3)
70
 
 
79
  )
80
  return response.choices[0].message.content.strip()
81
 
82
+ async def get_tired_keywords_in_lang(language: str):
83
+ if language == "English":
84
+ return [kw.lower() for kw in BASE_TIRE_KEYWORDS]
85
+ prompt = f"Translate the following expressions into {language}, separated by semicolons:\n" + "; ".join(BASE_TIRE_KEYWORDS)
86
+ response = client.chat.completions.create(
87
+ model="gpt-3.5-turbo",
88
+ messages=[{"role": "user", "content": prompt}],
89
+ temperature=0.3
90
+ )
91
+ translated_text = response.choices[0].message.content.strip()
92
+ return [kw.strip().lower() for kw in translated_text.split(";")]
93
+
94
+ async def user_is_tired(user_input: str, language: str) -> bool:
95
+ keywords = await get_tired_keywords_in_lang(language)
96
+ user_input_lower = user_input.lower()
97
+ return any(kw in user_input_lower for kw in keywords)
98
+
99
  async def respond(user_input, history, step, language, questions, followup_mode, followup_stage):
100
  history.append({"role": "user", "content": user_input})
101
 
 
114
  history.append({"role": "assistant", "content": end_note})
115
  return history, "", step, language, questions, False, 0
116
 
 
117
  if followup_mode:
118
+ if await user_is_tired(user_input, language):
119
+ skip_comment = await gpt_translate("了解了,我们来继续聊下一个问题:", language)
120
+ next_question = f"{step+1}. {questions[step]}"
121
+ combined = f"{skip_comment}\n\n{next_question}"
122
+ history.append({"role": "assistant", "content": combined})
123
+ return history, "", step + 1, language, questions, False, 0
124
+
125
  comment_prompt = [
126
  {"role": "system", "content": f"Write a short, empathetic comment in {language} responding to the user's last answer."},
127
+ history[-2], history[-1]
 
128
  ]
129
  comment_response = client.chat.completions.create(
130
  model="gpt-3.5-turbo",
 
132
  temperature=0.7
133
  )
134
  comment = comment_response.choices[0].message.content.strip()
135
+ history.append({"role": "assistant", "content": comment})
 
 
 
 
 
136
 
137
  if followup_stage == 1:
138
  if random.random() < 0.1:
 
139
  followup_prompt = [
140
+ {"role": "system", "content": f"Ask 1 follow-up question in {language}. Do NOT number it."},
141
+ history[-2], history[-1]
 
142
  ]
143
  followup_response = client.chat.completions.create(
144
  model="gpt-3.5-turbo",
 
146
  temperature=0.7
147
  )
148
  followup_q = followup_response.choices[0].message.content.strip()
149
+ history.append({"role": "assistant", "content": followup_q})
 
 
 
150
  return history, "", step, language, questions, True, 2
151
  else:
 
152
  next_question = f"{step+1}. {questions[step]}"
153
+ history.append({"role": "assistant", "content": next_question})
 
 
 
154
  return history, "", step + 1, language, questions, False, 0
155
 
156
  elif followup_stage == 2:
 
157
  next_question = f"{step+1}. {questions[step]}"
158
+ history.append({"role": "assistant", "content": next_question})
 
 
 
159
  return history, "", step + 1, language, questions, False, 0
160
 
 
161
  comment_prompt = [
162
  {"role": "system", "content": f"Write a short, warm comment in {language} responding to the user's last answer."},
163
+ history[-2], history[-1]
 
164
  ]
165
  comment_response = client.chat.completions.create(
166
  model="gpt-3.5-turbo",
 
176
  elif rand < 0.5:
177
  followup_count = 1
178
 
179
+ if await user_is_tired(user_input, language):
180
+ followup_count = 0
181
+
182
  if followup_count > 0:
183
  followup_prompt = [
184
+ {"role": "system", "content": f"Ask 1 follow-up question in {language}. Do NOT number it."},
185
+ history[-2], history[-1]
 
186
  ]
187
  followup_response = client.chat.completions.create(
188
  model="gpt-3.5-turbo",
 
190
  temperature=0.7
191
  )
192
  followup_q = followup_response.choices[0].message.content.strip()
193
+ history.append({"role": "assistant", "content": comment})
194
+ history.append({"role": "assistant", "content": followup_q})
 
 
 
 
 
 
 
 
 
 
 
195
  return history, "", step, language, questions, True, 1
196
 
197
+ history.append({"role": "assistant", "content": comment})
 
 
 
 
 
198
  next_question = f"{step+1}. {questions[step]}"
199
+ history.append({"role": "assistant", "content": next_question})
 
 
 
 
200
  return history, "", step + 1, language, questions, False, 0
201
 
202
  def init():