Update app.py
Browse files
app.py
CHANGED
|
@@ -69,7 +69,7 @@ async def gpt_translate(text, target_lang):
|
|
| 69 |
)
|
| 70 |
return response.choices[0].message.content.strip()
|
| 71 |
|
| 72 |
-
|
| 73 |
history.append({"role": "user", "content": user_input})
|
| 74 |
|
| 75 |
if language == "":
|
|
@@ -89,7 +89,6 @@ async def respond(user_input, history, step, language, questions, followup_mode,
|
|
| 89 |
|
| 90 |
# ======== 追问阶段 ==========
|
| 91 |
if followup_mode:
|
| 92 |
-
# 获取针对用户追问回答的评论
|
| 93 |
comment_prompt = [
|
| 94 |
{"role": "system", "content": f"Write a short, empathetic comment in {language} responding to the user's last answer."},
|
| 95 |
history[-2],
|
|
@@ -102,9 +101,15 @@ async def respond(user_input, history, step, language, questions, followup_mode,
|
|
| 102 |
)
|
| 103 |
comment = comment_response.choices[0].message.content.strip()
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
if followup_stage == 1:
|
| 106 |
if random.random() < 0.1:
|
| 107 |
-
# 有第二次追问
|
| 108 |
followup_prompt = [
|
| 109 |
{"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."},
|
| 110 |
history[-2],
|
|
@@ -116,19 +121,13 @@ async def respond(user_input, history, step, language, questions, followup_mode,
|
|
| 116 |
temperature=0.7
|
| 117 |
)
|
| 118 |
followup_q = followup_response.choices[0].message.content.strip()
|
| 119 |
-
combined = comment + "\n\n" + followup_q
|
| 120 |
history.append({"role": "assistant", "content": ""})
|
| 121 |
-
for c in
|
| 122 |
history[-1]["content"] += c
|
| 123 |
await asyncio.sleep(0.03)
|
| 124 |
return history, "", step, language, questions, True, 2
|
| 125 |
else:
|
| 126 |
-
# 无第二次追问
|
| 127 |
-
history.append({"role": "assistant", "content": ""})
|
| 128 |
-
for c in comment:
|
| 129 |
-
history[-1]["content"] += c
|
| 130 |
-
await asyncio.sleep(0.03)
|
| 131 |
-
|
| 132 |
next_question = f"{step+1}. {questions[step]}"
|
| 133 |
history.append({"role": "assistant", "content": ""})
|
| 134 |
for c in next_question:
|
|
@@ -137,12 +136,7 @@ async def respond(user_input, history, step, language, questions, followup_mode,
|
|
| 137 |
return history, "", step + 1, language, questions, False, 0
|
| 138 |
|
| 139 |
elif followup_stage == 2:
|
| 140 |
-
# 第二次追问后
|
| 141 |
-
history.append({"role": "assistant", "content": ""})
|
| 142 |
-
for c in comment:
|
| 143 |
-
history[-1]["content"] += c
|
| 144 |
-
await asyncio.sleep(0.03)
|
| 145 |
-
|
| 146 |
next_question = f"{step+1}. {questions[step]}"
|
| 147 |
history.append({"role": "assistant", "content": ""})
|
| 148 |
for c in next_question:
|
|
@@ -182,14 +176,22 @@ async def respond(user_input, history, step, language, questions, followup_mode,
|
|
| 182 |
temperature=0.7
|
| 183 |
)
|
| 184 |
followup_q = followup_response.choices[0].message.content.strip()
|
| 185 |
-
|
|
|
|
| 186 |
history.append({"role": "assistant", "content": ""})
|
| 187 |
-
for c in
|
| 188 |
history[-1]["content"] += c
|
| 189 |
await asyncio.sleep(0.03)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
return history, "", step, language, questions, True, 1
|
| 191 |
|
| 192 |
-
# 无追问:评论一泡
|
| 193 |
history.append({"role": "assistant", "content": ""})
|
| 194 |
for c in comment:
|
| 195 |
history[-1]["content"] += c
|
|
@@ -200,6 +202,7 @@ async def respond(user_input, history, step, language, questions, followup_mode,
|
|
| 200 |
for c in next_question:
|
| 201 |
history[-1]["content"] += c
|
| 202 |
await asyncio.sleep(0.03)
|
|
|
|
| 203 |
return history, "", step + 1, language, questions, False, 0
|
| 204 |
|
| 205 |
def init():
|
|
|
|
| 69 |
)
|
| 70 |
return response.choices[0].message.content.strip()
|
| 71 |
|
| 72 |
+
aasync def respond(user_input, history, step, language, questions, followup_mode, followup_stage):
|
| 73 |
history.append({"role": "user", "content": user_input})
|
| 74 |
|
| 75 |
if language == "":
|
|
|
|
| 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],
|
|
|
|
| 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],
|
|
|
|
| 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:
|
|
|
|
| 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:
|
|
|
|
| 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
|
|
|
|
| 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():
|