HelenaXH commited on
Commit
2d84166
·
verified ·
1 Parent(s): 2656af6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +124 -121
app.py CHANGED
@@ -70,6 +70,43 @@ top_p_default = 0.95
70
 
71
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
72
  # 动态函数:根据用户选择的 A/B/C,让 OpenAI 生成相应分析
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
74
  def answer_abc_questions(selected, bg_info, wv, ps, dd):
75
  desired_parts = []
@@ -447,128 +484,94 @@ def predict(message, history):
447
  mode = user_profile.get("mode") or ""
448
 
449
  # ~~~~~~~~~ Backward模式 ~~~~~~~~~
450
- if mode == "是":
451
- if in_backward_flow and not backward_done:
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
- if backward_index < len(backward_additional_questions):
457
- k, prompt_text = backward_additional_questions[backward_index]
458
- backward_index += 1
459
- return prompt_text
460
- else:
461
- # 修改这里不要直接设置backward_done为True
462
- # 而是需要生成职业详情并询问是否需要A/B/C专项信息
463
- try:
464
- api_key = os.environ.get("API_TOKEN")
465
- if not api_key:
466
- return "错误:API_TOKEN 未设置"
467
- client = OpenAI(api_key=api_key)
468
-
469
- # 先生成职业详情
470
- career_detail_prompt = f"""
471
- 你是一位专业的职业规划顾问。
472
- 学生目标职业: {user_profile.get('specific_career') or '未知'}
473
- 学生背景: {user_profile.get('bg_info') or '未知'}
474
- 请写一份详细的职业说明,至少包含
475
- 1. 行业平均薪资(初级/中级/高级)
476
- 2. 工作环境(远程/混合/办公室,团队规模等)
477
- 3. 晋升难度(需要什么资历或条件)
478
- 4. 关键技能和考证
479
- 5. 日常工作节奏
480
- 6. 行业前景
481
- 用分段Markdown格式,字数不少于300字。
482
- """
483
- msgs = [
484
- {"role": "system", "content": "你是一位专业职业顾问,会为用户的目标职业提供详细分析。"},
485
- {"role": "user", "content": career_detail_prompt}
486
- ]
487
- resp = client.chat.completions.create(
488
- model=model_default,
489
- messages=msgs,
490
- max_tokens=token_default,
491
- temperature=temp_default,
492
- top_p=top_p_default,
493
- stream=False
494
- )
495
- detail_msg = resp.choices[0].message.content
496
-
497
- # 设置状态,与Forward模式保持一致
498
- user_profile["final_choice"] = user_profile.get("specific_career")
499
- job_chosen = True
500
- forward_deep_dive_done = True
501
- post_career_detail_asked = True
502
-
503
- # 返回职业详情并询问A/B/C
504
- return detail_msg + "\n\n我还可以针对以下三方面提供更深入的分析建议:" \
505
- "\n- A:专业/选课 & 求职方向" \
506
- "\n- B:本校资源利用建议" \
507
- "\n- C:实习 & networking 积累" \
508
- "\n如果你想查看其中一个或多个,请输�� A / B / C / AB / BC / AC / ABC" \
509
- "\n如果都不需要,请回复'不需要'。"
510
-
511
- except Exception as e:
512
- return f"生成职业详情时出错: {str(e)}"
513
-
514
- # 处理 A/B/C 答疑 (复用Forward模式的逻辑)
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
- # 处理是否需要时间轴路线图 (复用Forward模式的逻辑)
534
- elif job_chosen and not roadmap_offered and not roadmap_done:
535
  roadmap_offered = True
536
- return "需要一个时间轴式职业路线图吗?如需,则回复【要】,否则回复【不要】。"
537
-
538
- elif roadmap_offered and not roadmap_done:
539
- ans = message.strip().lower()
540
- if ans in ["", "yes", "y"]:
541
- roadmap_done = True
542
- backward_done = True
543
- return do_time_roadmap()
544
- else:
545
- roadmap_done = True
546
- backward_done = True
547
- return ",不生成时间轴,本次规划结束。"
548
-
549
- # 最终收尾 (仅在所有流程都完成时才会触发)
550
- if backward_done:
551
- try:
552
- api_key = os.environ.get("API_TOKEN")
553
- if not api_key:
554
- return "错误:API_TOKEN 未设置"
555
- client = OpenAI(api_key=api_key)
556
- sprompt = generate_system_prompt()
557
- msgs = [
558
- {"role":"system","content":sprompt},
559
- {"role":"user","content": f"以下是资料: {user_profile}. 若需要更多咨询可再次输入"}
560
- ]
561
- resp = client.chat.completions.create(
562
- model=model_default,
563
- messages=msgs,
564
- max_tokens=token_default,
565
- temperature=temp_default,
566
- top_p=top_p_default,
567
- stream=False
568
- )
569
- return resp.choices[0].message.content
570
- except Exception as e:
571
- return f"发生错误: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
572
 
573
  # ~~~~~~~~~ Forward模式 ~~~~~~~~~
574
  else:
 
70
 
71
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
72
  # 动态函数:根据用户选择的 A/B/C,让 OpenAI 生成相应分析
73
+ def backward_strategy_plan(specific_career, bg_info):
74
+ prompt = f"""
75
+ 你是一位专业的职业规划顾问。
76
+ 现在有一位学生,他的目标职业是:{specific_career}
77
+ 他的当前背景是:{bg_info}
78
+
79
+ 请根据这个学生的背景和目标,帮他做一个清晰的达成路径规划,内容要包括:
80
+ 1. 是否需要考虑转专业或辅修?
81
+ 2. 是否需要补哪些课?课程关键词有哪些?
82
+ 3. 推荐的证书/考试(如CFA等)和学习建议
83
+ 4. 推荐的实习方向(基于他的背景)
84
+ 5. 如何利用本校资源(career center、club、networking等)
85
+ 6. 如果他背景不匹配(比如机械去金融),如何填补Gap?
86
+
87
+ 请使用 Markdown 分段格式,不少于300字,内容务必紧扣他目前的背景和目标。
88
+ """
89
+ try:
90
+ api_key = os.environ.get("API_TOKEN")
91
+ if not api_key:
92
+ return "错误:API_TOKEN 未设置"
93
+ client = OpenAI(api_key=api_key)
94
+ msgs = [
95
+ {"role": "system", "content": "你是一位经验丰富的职业顾问,擅长根据用户背景制定路径规划"},
96
+ {"role": "user", "content": prompt}
97
+ ]
98
+ resp = client.chat.completions.create(
99
+ model=model_default,
100
+ messages=msgs,
101
+ max_tokens=token_default,
102
+ temperature=temp_default,
103
+ top_p=top_p_default,
104
+ stream=False
105
+ )
106
+ return resp.choices[0].message.content
107
+ except Exception as e:
108
+ return f"生成职业路径建议时出错: {str(e)}"
109
+
110
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
111
  def answer_abc_questions(selected, bg_info, wv, ps, dd):
112
  desired_parts = []
 
484
  mode = user_profile.get("mode") or ""
485
 
486
  # ~~~~~~~~~ Backward模式 ~~~~~~~~~
487
+ if mode == "是":
488
+ if in_backward_flow and not backward_done:
489
+ if backward_index > 0 and backward_index <= len(backward_additional_questions):
490
+ prev_key = backward_additional_questions[backward_index - 1][0]
491
+ user_profile[prev_key] = message.strip()
492
+
493
+ if backward_index < len(backward_additional_questions):
494
+ k, prompt_text = backward_additional_questions[backward_index]
495
+ backward_index += 1
496
+ return prompt_text
497
+ else:
498
+ # 已完成背景收集生成路径分析
499
+ strategy = backward_strategy_plan(
500
+ specific_career=user_profile.get("specific_career"),
501
+ bg_info=user_profile.get("bg_info")
502
+ )
503
+
504
+ user_profile["final_choice"] = user_profile.get("specific_career")
505
+ job_chosen = True
506
+ forward_deep_dive_done = True
507
+ post_career_detail_asked = True
508
+
509
+ return strategy + "\n\n我还可以针对以下三方面提供更深入的分析建议:" \
510
+ "\n- A:专业/选课 & 求职方向" \
511
+ "\n- B本校资源利用建议" \
512
+ "\n- C:实习 & networking 积累" \
513
+ "\n如果你想查看其中一个或多个,请输入 A / B / C / AB / BC / AC / ABC" \
514
+ "\n如果都不需要,请回复'不需要'。"
515
+
516
+ # 处理 A/B/C 答疑
517
+ elif post_career_detail_asked and not post_career_detail_done:
518
+ user_choice = message.strip().lower()
519
+ if user_choice in ["不需要", "no", "n"]:
520
+ post_career_detail_done = True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
521
  roadmap_offered = True
522
+ return "好的,不查看这三个专项。那需要一个时间轴式职业路线图吗?如需,则回复【要】,否则回复【不要】。"
523
+ else:
524
+ abc_text = answer_abc_questions(
525
+ selected=user_choice,
526
+ bg_info=user_profile.get("bg_info", ""),
527
+ wv=user_profile.get("work_value", "未提供"),
528
+ ps=user_profile.get("personality_summary", "未提供"),
529
+ dd=user_profile.get("dream_day", "未提供")
530
+ )
531
+ post_career_detail_done = True
532
+ roadmap_offered = True
533
+ return abc_text + "\n\n以上是你所选A/B/C专项信息。需要一个时间轴式职业路线图吗?如需则回复【要】,否则回复【不要】。"
534
+
535
+ # 路线图询问
536
+ elif job_chosen and not roadmap_offered and not roadmap_done:
537
+ roadmap_offered = True
538
+ return "需要一个时间轴式职业路线图吗?如需,则回复【要】,否则回复【不要】。"
539
+
540
+ elif roadmap_offered and not roadmap_done:
541
+ ans = message.strip().lower()
542
+ if ans in ["要", "yes", "y"]:
543
+ roadmap_done = True
544
+ backward_done = True
545
+ return do_time_roadmap()
546
+ else:
547
+ roadmap_done = True
548
+ backward_done = True
549
+ return "好的,不生成时间轴,本次规划结束。"
550
+
551
+ # Backward 最终总结
552
+ if backward_done:
553
+ try:
554
+ api_key = os.environ.get("API_TOKEN")
555
+ if not api_key:
556
+ return "错误:API_TOKEN 未设置"
557
+ client = OpenAI(api_key=api_key)
558
+ sprompt = generate_system_prompt()
559
+ msgs = [
560
+ {"role": "system", "content": sprompt},
561
+ {"role": "user", "content": f"以下是资料: {user_profile}. 若需要更多咨询可再次输入"}
562
+ ]
563
+ resp = client.chat.completions.create(
564
+ model=model_default,
565
+ messages=msgs,
566
+ max_tokens=token_default,
567
+ temperature=temp_default,
568
+ top_p=top_p_default,
569
+ stream=False
570
+ )
571
+ return resp.choices[0].message.content
572
+ except Exception as e:
573
+ return f"发生错误: {str(e)}"
574
+
575
 
576
  # ~~~~~~~~~ Forward模式 ~~~~~~~~~
577
  else: