Stone164 commited on
Commit
802f9cc
·
verified ·
1 Parent(s): 8127bae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -99
app.py CHANGED
@@ -443,27 +443,27 @@ def predict(message, history):
443
  mode = user_profile.get("mode") or ""
444
 
445
  # ~~~~~~~~~ Backward模式 ~~~~~~~~~
446
- if mode == "是":
447
- if in_backward_flow and not backward_done:
448
- if backward_index > 0 and backward_index <= len(backward_additional_questions):
449
- prev_key = backward_additional_questions[backward_index - 1][0]
450
- user_profile[prev_key] = message.strip()
451
-
452
- if backward_index < len(backward_additional_questions):
453
- k, prompt_text = backward_additional_questions[backward_index]
454
- backward_index += 1
455
- return prompt_text
456
- else:
457
- # 修改这里,不要直接设置backward_done为True
458
- # 而是需要生成职业详情并询问是否需要A/B/C专项信息
459
- try:
460
- api_key = os.environ.get("API_TOKEN")
461
- if not api_key:
462
- return "错误:API_TOKEN 未设置"
463
- client = OpenAI(api_key=api_key)
464
-
465
- # 先生成职业详情
466
- career_detail_prompt = f"""
467
  你是一位专业的职业规划顾问。
468
  学生目标职业: {user_profile.get('specific_career') or '未知'}
469
  学生背景: {user_profile.get('bg_info') or '未知'}
@@ -476,9 +476,83 @@ if mode == "是":
476
  6. 行业前景
477
  用分段Markdown格式,字数不少于300字。
478
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
479
  msgs = [
480
- {"role": "system", "content": "你是一位专业职业顾问,会为用户的目标职业提供详细分析。"},
481
- {"role": "user", "content": career_detail_prompt}
482
  ]
483
  resp = client.chat.completions.create(
484
  model=model_default,
@@ -488,83 +562,9 @@ if mode == "是":
488
  top_p=top_p_default,
489
  stream=False
490
  )
491
- detail_msg = resp.choices[0].message.content
492
-
493
- # 设置状态,与Forward模式保持一致
494
- user_profile["final_choice"] = user_profile.get("specific_career")
495
- job_chosen = True
496
- forward_deep_dive_done = True
497
- post_career_detail_asked = True
498
-
499
- # 返回职业详情并询问A/B/C
500
- return detail_msg + "\n\n我还可以针对以下三方面提供更深入的分析建议:" \
501
- "\n- A:专业/选课 & 求职方向" \
502
- "\n- B:本校资源利用建议" \
503
- "\n- C:实习 & networking 积累" \
504
- "\n如果你想查看其中一个或多个,请输入 A / B / C / AB / BC / AC / ABC" \
505
- "\n如果都不需要,请回复'不需要'。"
506
-
507
  except Exception as e:
508
- return f"生成职业详情时出错: {str(e)}"
509
-
510
- # 处理 A/B/C 答疑 (复用Forward模式的逻辑)
511
- elif post_career_detail_asked and not post_career_detail_done:
512
- user_choice = message.strip().lower()
513
- if user_choice in ["不需要", "no", "n"]:
514
- post_career_detail_done = True
515
- roadmap_offered = True
516
- return "好的,不查看这三个专项。那需要一个时间轴式职业路线图吗?如需,则回复【要】,否则回复【不要】。"
517
- else:
518
- abc_text = answer_abc_questions(
519
- selected=user_choice,
520
- bg_info=user_profile["bg_info"],
521
- wv=user_profile.get("work_value") or "未提供",
522
- ps=user_profile.get("personality_summary") or "未提供",
523
- dd=user_profile.get("dream_day") or "未提供"
524
- )
525
- post_career_detail_done = True
526
- roadmap_offered = True
527
- return abc_text + "\n\n以上是你所选的A/B/C专项信息。需要一个时间轴式职业路线图吗?如需,则回复【要】,否则回复【不要】。"
528
-
529
- # 处理是否需要时间轴路线图 (复用Forward模式的逻辑)
530
- elif job_chosen and not roadmap_offered and not roadmap_done:
531
- roadmap_offered = True
532
- return "需要一个时间轴式职业路线图吗?如需,则回复【要】,否则回复【不要】。"
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
  else:
 
443
  mode = user_profile.get("mode") or ""
444
 
445
  # ~~~~~~~~~ Backward模式 ~~~~~~~~~
446
+ if mode == "是":
447
+ if in_backward_flow and not backward_done:
448
+ if backward_index > 0 and backward_index <= len(backward_additional_questions):
449
+ prev_key = backward_additional_questions[backward_index - 1][0]
450
+ user_profile[prev_key] = message.strip()
451
+
452
+ if backward_index < len(backward_additional_questions):
453
+ k, prompt_text = backward_additional_questions[backward_index]
454
+ backward_index += 1
455
+ return prompt_text
456
+ else:
457
+ # 修改这里,不要直接设置backward_done为True
458
+ # 而是需要生成职业详情并询问是否需要A/B/C专项信息
459
+ try:
460
+ api_key = os.environ.get("API_TOKEN")
461
+ if not api_key:
462
+ return "错误:API_TOKEN 未设置"
463
+ client = OpenAI(api_key=api_key)
464
+
465
+ # 先生成职业详情
466
+ career_detail_prompt = f"""
467
  你是一位专业的职业规划顾问。
468
  学生目标职业: {user_profile.get('specific_career') or '未知'}
469
  学生背景: {user_profile.get('bg_info') or '未知'}
 
476
  6. 行业前景
477
  用分段Markdown格式,字数不少于300字。
478
  """
479
+ msgs = [
480
+ {"role": "system", "content": "你是一位专业职业顾问,会为用户的目标职业提供详细分析。"},
481
+ {"role": "user", "content": career_detail_prompt}
482
+ ]
483
+ resp = client.chat.completions.create(
484
+ model=model_default,
485
+ messages=msgs,
486
+ max_tokens=token_default,
487
+ temperature=temp_default,
488
+ top_p=top_p_default,
489
+ stream=False
490
+ )
491
+ detail_msg = resp.choices[0].message.content
492
+
493
+ # 设置状态,与Forward模式保持一致
494
+ user_profile["final_choice"] = user_profile.get("specific_career")
495
+ job_chosen = True
496
+ forward_deep_dive_done = True
497
+ post_career_detail_asked = True
498
+
499
+ # 返回职业详情并询问A/B/C
500
+ return detail_msg + "\n\n我还可以针对以下三方面提供更深入的分析建议:" \
501
+ "\n- A:专业/选课 & 求职方向" \
502
+ "\n- B:本校资源利用建议" \
503
+ "\n- C:实习 & networking 积累" \
504
+ "\n如果你想查看其中一个或多个,请输入 A / B / C / AB / BC / AC / ABC" \
505
+ "\n如果都不需要,请回复'不需要'。"
506
+
507
+ except Exception as e:
508
+ return f"生成职业详情时出错: {str(e)}"
509
+
510
+ # 处理 A/B/C 答疑 (复用Forward模式的逻辑)
511
+ elif post_career_detail_asked and not post_career_detail_done:
512
+ user_choice = message.strip().lower()
513
+ if user_choice in ["不需要", "no", "n"]:
514
+ post_career_detail_done = True
515
+ roadmap_offered = True
516
+ return "好的,不查看这三个专项。那需要一个时间轴式职业路线图吗?如需,则回复【要】,否则回复【不要】。"
517
+ else:
518
+ abc_text = answer_abc_questions(
519
+ selected=user_choice,
520
+ bg_info=user_profile["bg_info"],
521
+ wv=user_profile.get("work_value") or "未提供",
522
+ ps=user_profile.get("personality_summary") or "未提供",
523
+ dd=user_profile.get("dream_day") or "未提供"
524
+ )
525
+ post_career_detail_done = True
526
+ roadmap_offered = True
527
+ return abc_text + "\n\n以上是你所选的A/B/C专项信息。需要一个时间轴式职业路线图吗?如需,则回复【要】,否则回复【不要】。"
528
+
529
+ # 处理是否需要时间轴路线图 (复用Forward模式的逻辑)
530
+ elif job_chosen and not roadmap_offered and not roadmap_done:
531
+ roadmap_offered = True
532
+ return "需要一个时间轴式职业路线图吗?如需,则回复【要】,否则回复【不要】。"
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,
 
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
  else: