HelenaXH commited on
Commit
0b6301b
·
verified ·
1 Parent(s): cd9b24d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -35
app.py CHANGED
@@ -67,41 +67,81 @@ token_default = 2000
67
  temp_default = 0.7
68
  top_p_default = 0.95
69
 
70
-
71
- # ~~~~~~~~~~~~~~~~~~~~~~~ 函数:answer_abc_questions ~~~~~~~~~~~~~~~~~~~~~~~
 
72
  def answer_abc_questions(selected, bg_info, wv, ps, dd):
73
  """
74
- 根据用户 A/B/C/AB/ABC 等,返回相应的额外建议内容。
75
- 你可以自定义下面段文案以符合你的需要
 
 
 
76
  """
77
- text_A = f"""
78
- **A: 专业/选课方向 & 求职入门建议**
79
- - (这里可结合bg_info, wv, ps, dd给一些更针对的建议)
80
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
 
82
- text_B = f"""
83
- **B: 本校资源利用建议**
84
- - (这里介绍如何利用所在学校的Career Center、社团、实验室资源、校友网等)
85
- """
 
 
86
 
87
- text_C = f"""
88
- **C: 实习 & Experience 积累 (networking)**
89
- - (这里可提到怎么找实习、如何做networking、如何累积实操项目等)
90
  """
91
 
92
- sel = selected.lower()
93
- response_parts = []
94
- if "a" in sel:
95
- response_parts.append(text_A)
96
- if "b" in sel:
97
- response_parts.append(text_B)
98
- if "c" in sel:
99
- response_parts.append(text_C)
100
-
101
- if not response_parts:
102
- return "好的,你暂时不需要查看这三个专项。"
103
 
104
- return "\n".join(response_parts)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
 
106
 
107
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 提供具体职业后,再深入介绍 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -560,12 +600,12 @@ def predict(message, history):
560
  # 关键:现在我们“询问 A/B/C”
561
  post_career_detail_asked = True
562
 
563
- return detail_msg + "\n\n我还可以针对以下三方面提供建议:\n" \
564
- "A:专业/选课 & 求职方向\n" \
565
- "B:本校资源利用建议\n" \
566
- "C:实习 & networking 积累\n" \
567
- "如需要其中一个或多个,请输入 A / B / C / AB / BC / AC / ABC\n" \
568
- "如果都不需要,请回复“不需要”。"
569
 
570
  else:
571
  return "无效的选项,请重新输入1/2/3或'换'"
@@ -739,7 +779,6 @@ footer {
739
  global roadmap_offered, roadmap_done
740
 
741
  global direction_chosen, jobs_recommended, job_chosen
742
-
743
  global post_career_detail_asked, post_career_detail_done
744
 
745
  current_q_index = 0
@@ -762,7 +801,6 @@ footer {
762
  direction_chosen = False
763
  jobs_recommended = False
764
  job_chosen = False
765
-
766
  post_career_detail_asked = False
767
  post_career_detail_done = False
768
 
@@ -775,3 +813,4 @@ footer {
775
 
776
  demo.load(auto_first, outputs=chatbot)
777
  demo.launch()
 
 
67
  temp_default = 0.7
68
  top_p_default = 0.95
69
 
70
+ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
71
+ # 新的动态函数: 根据用户选择的 A/B/C,调用 OpenAI 生成相应的分析
72
+ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
73
  def answer_abc_questions(selected, bg_info, wv, ps, dd):
74
  """
75
+ 动态地向OpenAI发起请求,让AI针对“专业/课方向 & 求职入门(A)”、“本校资源利用(B)”、
76
+ “实习 & networking(C)”个模块基于用户背景信息做个性化分析
77
+
78
+ selected 可能是 "a", "b", "c", "ab", "bc", "abc"等,
79
+ 我们用 prompt 让模型只回答用户选中的部分。
80
  """
81
+ # 先把用户选择的模块整理好
82
+ desired_parts = []
83
+ # 我们会在 prompt 里告诉模型,于选中每个部分,都写一段分析
84
+ if "a" in selected:
85
+ desired_parts.append("A")
86
+ if "b" in selected:
87
+ desired_parts.append("B")
88
+ if "c" in selected:
89
+ desired_parts.append("C")
90
+
91
+ # 如果用户啥都没选,就直接返回空
92
+ if not desired_parts:
93
+ return "好的,你暂时不需要查看A/B/C的专项信息。"
94
+
95
+ # 构造给模型的描述
96
+ # 让模型只输出用户所选模块的内容
97
+ # 这里可以再根据你的需求,告诉模型每个模块需要写什么要点
98
+ parts_description = {
99
+ "A": "A: 专业/选课方向 & 求职入门建议",
100
+ "B": "B: 本校资源利用建议",
101
+ "C": "C: 实习 & Experience积累(networking)",
102
+ }
103
+
104
+ # 在Prompt中,告诉模型这个人的背景,并要求对每个选中的模块输出详细分析
105
+ # 你可以视情况再加一些具体指令,比如段落格式等
106
+ prompt_text = f"""
107
+ 你是一位专业的职业规划顾问。以下是学生背景信息,请根据他选择的模块(A/B/C)给出详细分析和建议。
108
+ 学生背景:
109
+ - 学术背景: {bg_info}
110
+ - 工作意义: {wv}
111
+ - 性格: {ps}
112
+ - 理想工作: {dd}
113
 
114
+ 学生目前想要的额外信息模块:{', '.join(desired_parts)}
115
+ 请分别按顺序为每个模块写一段分析和建议,可以使用Markdown分段形式。
116
+ 模块说明:
117
+ A: 专业/选课方向 & 求职入门建议
118
+ B: 本校资源利用建议(如Career Center、实习机会、校友资源)
119
+ C: 实习 & Experience积累(networking)
120
 
121
+ 请只回答用户所选模块,不要回答未选的模块。
 
 
122
  """
123
 
124
+ try:
125
+ api_key = os.environ.get("API_TOKEN")
126
+ if not api_key:
127
+ return "错误:API_TOKEN 未设置"
128
+ client = OpenAI(api_key=api_key)
 
 
 
 
 
 
129
 
130
+ msgs = [
131
+ {"role": "system", "content": "你是一位专业职业顾问,能够根据用户选择的A/B/C模块做深度分析。"},
132
+ {"role": "user", "content": prompt_text}
133
+ ]
134
+ resp = client.chat.completions.create(
135
+ model=model_default,
136
+ messages=msgs,
137
+ max_tokens=token_default,
138
+ temperature=temp_default,
139
+ top_p=top_p_default,
140
+ stream=False
141
+ )
142
+ return resp.choices[0].message.content
143
+ except Exception as e:
144
+ return f"生成A/B/C专项信息时出错: {str(e)}"
145
 
146
 
147
  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 提供具体职业后,再深入介绍 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
600
  # 关键:现在我们“询问 A/B/C”
601
  post_career_detail_asked = True
602
 
603
+ return detail_msg + "\n\n我还可以针对以下三方面提供**更深入**的分析建议:" \
604
+ "\n- A:专业/选课 & 求职方向" \
605
+ "\n- B:本校资源利用建议" \
606
+ "\n- C:实习 & networking 积累" \
607
+ "\n果你想查看其中一个或多个,请输入 A / B / C / AB / BC / AC / ABC" \
608
+ "\n如果都不需要,请回复“不需要”。"
609
 
610
  else:
611
  return "无效的选项,请重新输入1/2/3或'换'"
 
779
  global roadmap_offered, roadmap_done
780
 
781
  global direction_chosen, jobs_recommended, job_chosen
 
782
  global post_career_detail_asked, post_career_detail_done
783
 
784
  current_q_index = 0
 
801
  direction_chosen = False
802
  jobs_recommended = False
803
  job_chosen = False
 
804
  post_career_detail_asked = False
805
  post_career_detail_done = False
806
 
 
813
 
814
  demo.load(auto_first, outputs=chatbot)
815
  demo.launch()
816
+