Update app.py
Browse files
app.py
CHANGED
|
@@ -429,8 +429,10 @@ def predict(message, history):
|
|
| 429 |
user_profile["mode"] = ans
|
| 430 |
if "是" in ans:
|
| 431 |
in_backward_flow = True
|
|
|
|
| 432 |
else:
|
| 433 |
in_forward_flow = True
|
|
|
|
| 434 |
else:
|
| 435 |
user_profile[key] = message.strip()
|
| 436 |
|
|
@@ -439,24 +441,26 @@ def predict(message, history):
|
|
| 439 |
current_q_index += 1
|
| 440 |
return nxt
|
| 441 |
|
| 442 |
-
#
|
| 443 |
-
|
|
|
|
| 444 |
|
|
|
|
| 445 |
# ~~~~~~~~~ Backward模式 ~~~~~~~~~
|
| 446 |
-
if
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
try:
|
| 461 |
api_key = os.environ.get("API_TOKEN")
|
| 462 |
if not api_key:
|
|
@@ -506,66 +510,66 @@ def predict(message, history):
|
|
| 506 |
|
| 507 |
except Exception as e:
|
| 508 |
return f"生成职业详情时出错: {str(e)}"
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
|
| 565 |
# ~~~~~~~~~ Forward模式 ~~~~~~~~~
|
| 566 |
-
|
| 567 |
# 前期处理:收集信息和推荐大方向
|
| 568 |
-
if
|
| 569 |
# 收集 4个基础问题
|
| 570 |
if forward_index > 0 and forward_index <= len(forward_additional_questions):
|
| 571 |
prev_key = forward_additional_questions[forward_index - 1][0]
|
|
|
|
| 429 |
user_profile["mode"] = ans
|
| 430 |
if "是" in ans:
|
| 431 |
in_backward_flow = True
|
| 432 |
+
in_forward_flow = False # 确保互斥
|
| 433 |
else:
|
| 434 |
in_forward_flow = True
|
| 435 |
+
in_backward_flow = False # 确保互斥
|
| 436 |
else:
|
| 437 |
user_profile[key] = message.strip()
|
| 438 |
|
|
|
|
| 441 |
current_q_index += 1
|
| 442 |
return nxt
|
| 443 |
|
| 444 |
+
# 打印调试信息
|
| 445 |
+
# print(f"Mode: {user_profile.get('mode')}, Backward: {in_backward_flow}, Forward: {in_forward_flow}")
|
| 446 |
+
# print(f"Current indexes - Backward: {backward_index}, Forward: {forward_index}")
|
| 447 |
|
| 448 |
+
# ======================== 分模式处理 =========================
|
| 449 |
# ~~~~~~~~~ Backward模式 ~~~~~~~~~
|
| 450 |
+
if in_backward_flow:
|
| 451 |
+
# 处理上一个问题的回答
|
| 452 |
+
if backward_index > 0 and backward_index <= len(backward_additional_questions):
|
| 453 |
+
prev_key = backward_additional_questions[backward_index - 1][0]
|
| 454 |
+
user_profile[prev_key] = message.strip()
|
| 455 |
+
|
| 456 |
+
# 提出下一个问题
|
| 457 |
+
if backward_index < len(backward_additional_questions):
|
| 458 |
+
k, prompt_text = backward_additional_questions[backward_index]
|
| 459 |
+
backward_index += 1
|
| 460 |
+
return prompt_text
|
| 461 |
+
else:
|
| 462 |
+
# 所有问题都已回答完毕
|
| 463 |
+
if not post_career_detail_asked:
|
| 464 |
try:
|
| 465 |
api_key = os.environ.get("API_TOKEN")
|
| 466 |
if not api_key:
|
|
|
|
| 510 |
|
| 511 |
except Exception as e:
|
| 512 |
return f"生成职业详情时出错: {str(e)}"
|
| 513 |
+
|
| 514 |
+
# 处理 A/B/C 答疑
|
| 515 |
+
elif post_career_detail_asked and not post_career_detail_done:
|
| 516 |
+
user_choice = message.strip().lower()
|
| 517 |
+
if user_choice in ["不需要", "no", "n"]:
|
| 518 |
+
post_career_detail_done = True
|
| 519 |
+
roadmap_offered = True
|
| 520 |
+
return "好的,不查看这三个专项。那需要一个时间轴式职业路线图吗?如需,则回复【要】,否则回复【不要】。"
|
| 521 |
+
else:
|
| 522 |
+
abc_text = answer_abc_questions(
|
| 523 |
+
selected=user_choice,
|
| 524 |
+
bg_info=user_profile.get("bg_info", ""),
|
| 525 |
+
wv=user_profile.get("work_value", "未提供"),
|
| 526 |
+
ps=user_profile.get("personality_summary", "未提供"),
|
| 527 |
+
dd=user_profile.get("dream_day", "未提供")
|
| 528 |
+
)
|
| 529 |
+
post_career_detail_done = True
|
| 530 |
+
roadmap_offered = True
|
| 531 |
+
return abc_text + "\n\n以上是你所选的A/B/C专项信息。需要一个时间轴式职业路线图吗?如需,则回复【要】,否则回复【不要】。"
|
| 532 |
+
|
| 533 |
+
# 处理是否需要时间轴路线图
|
| 534 |
+
elif roadmap_offered and not roadmap_done:
|
| 535 |
+
ans = message.strip().lower()
|
| 536 |
+
if ans in ["要", "yes", "y"]:
|
| 537 |
+
roadmap_done = True
|
| 538 |
+
backward_done = True
|
| 539 |
+
return do_time_roadmap()
|
| 540 |
+
else:
|
| 541 |
+
roadmap_done = True
|
| 542 |
+
backward_done = True
|
| 543 |
+
return "好的,不生成时间轴,本次规划结束。"
|
| 544 |
+
|
| 545 |
+
# 最终收尾 (仅在所有流程都完成时才会触发)
|
| 546 |
+
if backward_done:
|
| 547 |
+
try:
|
| 548 |
+
api_key = os.environ.get("API_TOKEN")
|
| 549 |
+
if not api_key:
|
| 550 |
+
return "错误:API_TOKEN 未设置"
|
| 551 |
+
client = OpenAI(api_key=api_key)
|
| 552 |
+
sprompt = generate_system_prompt()
|
| 553 |
+
msgs = [
|
| 554 |
+
{"role":"system","content":sprompt},
|
| 555 |
+
{"role":"user","content": f"以下是资料: {user_profile}. 若需要更多咨询可再次输入"}
|
| 556 |
+
]
|
| 557 |
+
resp = client.chat.completions.create(
|
| 558 |
+
model=model_default,
|
| 559 |
+
messages=msgs,
|
| 560 |
+
max_tokens=token_default,
|
| 561 |
+
temperature=temp_default,
|
| 562 |
+
top_p=top_p_default,
|
| 563 |
+
stream=False
|
| 564 |
+
)
|
| 565 |
+
return resp.choices[0].message.content
|
| 566 |
+
except Exception as e:
|
| 567 |
+
return f"发生错误: {str(e)}"
|
| 568 |
|
| 569 |
# ~~~~~~~~~ Forward模式 ~~~~~~~~~
|
| 570 |
+
elif in_forward_flow:
|
| 571 |
# 前期处理:收集信息和推荐大方向
|
| 572 |
+
if not direction_chosen:
|
| 573 |
# 收集 4个基础问题
|
| 574 |
if forward_index > 0 and forward_index <= len(forward_additional_questions):
|
| 575 |
prev_key = forward_additional_questions[forward_index - 1][0]
|