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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -99
app.py CHANGED
@@ -443,27 +443,34 @@ 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,83 +483,9 @@ def predict(message, history):
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,9 +495,83 @@ def predict(message, history):
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
+ # 更新状态变量:保存用户的回答到对应的键
449
+ if backward_index > 0 and backward_index <= len(backward_additional_questions):
450
+ prev_key = backward_additional_questions[backward_index - 1][0]
451
+ user_profile[prev_key] = message.strip()
452
+
453
+ # 如果还有问题要问,继续问
454
+ if backward_index < len(backward_additional_questions):
455
+ k, prompt_text = backward_additional_questions[backward_index]
456
+ backward_index += 1
457
+ return prompt_text
458
+
459
+ # 问完了所有问题后,提供职业详情和A/B/C选项
460
+ else:
461
+ # 打印调试信息确认我们进入了这个分支
462
+ print("进入Backward模式的详情生成分支")
463
+ print(f"职业方向: {user_profile.get('specific_career')}")
464
+ print(f"背景信息: {user_profile.get('bg_info')}")
465
+
466
+ try:
467
+ api_key = os.environ.get("API_TOKEN")
468
+ if not api_key:
469
+ return "错误:API_TOKEN 未设置"
470
+ client = OpenAI(api_key=api_key)
471
+
472
+ # 先生成职业详情
473
+ career_detail_prompt = f"""
474
  你是一位专业的职业规划顾问。
475
  学生目标职业: {user_profile.get('specific_career') or '未知'}
476
  学生背景: {user_profile.get('bg_info') or '未知'}
 
483
  6. 行业前景
484
  用分段Markdown格式,字数不少于300字。
485
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
486
  msgs = [
487
+ {"role": "system", "content": "你是一位专业职业顾问,会为用户的目标职业提供详细分析。"},
488
+ {"role": "user", "content": career_detail_prompt}
489
  ]
490
  resp = client.chat.completions.create(
491
  model=model_default,
 
495
  top_p=top_p_default,
496
  stream=False
497
  )
498
+ detail_msg = resp.choices[0].message.content
499
+
500
+ # 设置状态,与Forward模式保持一致
501
+ user_profile["final_choice"] = user_profile.get("specific_career")
502
+ job_chosen = True
503
+ forward_deep_dive_done = True
504
+ post_career_detail_asked = True
505
+
506
+ # 返回职业详情并询问A/B/C
507
+ return detail_msg + "\n\n我还可以针对以下三方面提供更深入的分析建议:" \
508
+ "\n- A:专业/选课 & 求职方向" \
509
+ "\n- B:本校资源利用建议" \
510
+ "\n- C:实习 & networking 积累" \
511
+ "\n如果你想查看其中一个或多个,请输入 A / B / C / AB / BC / AC / ABC" \
512
+ "\n如果都不需要,请回复'不需要'。"
513
+
514
  except Exception as e:
515
+ return f"生成职业详情时出错: {str(e)}"
516
+
517
+ # 处理 A/B/C 答疑 (复用Forward模式的逻辑)
518
+ elif post_career_detail_asked and not post_career_detail_done:
519
+ user_choice = message.strip().lower()
520
+ if user_choice in ["不需要", "no", "n"]:
521
+ post_career_detail_done = True
522
+ roadmap_offered = True
523
+ return "好的,不查看这三个专项。那需要一个时间轴式职业路线图吗?如需,则回复【要】,否则回复【不要】。"
524
+ else:
525
+ abc_text = answer_abc_questions(
526
+ selected=user_choice,
527
+ bg_info=user_profile.get("bg_info", ""),
528
+ wv=user_profile.get("work_value", "未提供"),
529
+ ps=user_profile.get("personality_summary", "未提供"),
530
+ dd=user_profile.get("dream_day", "未提供")
531
+ )
532
+ post_career_detail_done = True
533
+ roadmap_offered = True
534
+ return abc_text + "\n\n以上是你所选的A/B/C专项信息。需要一个时间轴式职业路线图吗?如需,则回复【要】,否则回复【不要】。"
535
+
536
+ # 处理是否需要时间轴路线图 (复用Forward模式的逻辑)
537
+ elif job_chosen and not roadmap_offered and not roadmap_done:
538
+ roadmap_offered = True
539
+ return "需要一个时间轴式职业路线图吗?如需,则回复【要】,否则回复【不要】。"
540
+
541
+ elif roadmap_offered and not roadmap_done:
542
+ ans = message.strip().lower()
543
+ if ans in ["要", "yes", "y"]:
544
+ roadmap_done = True
545
+ backward_done = True
546
+ return do_time_roadmap()
547
+ else:
548
+ roadmap_done = True
549
+ backward_done = True
550
+ return "好的,不生成时间轴,本次规划结束。"
551
+
552
+ # 最终收尾 (仅在所有流程都完成时才会触发)
553
+ if backward_done:
554
+ try:
555
+ api_key = os.environ.get("API_TOKEN")
556
+ if not api_key:
557
+ return "错误:API_TOKEN 未设置"
558
+ client = OpenAI(api_key=api_key)
559
+ sprompt = generate_system_prompt()
560
+ msgs = [
561
+ {"role":"system","content":sprompt},
562
+ {"role":"user","content": f"以下是资料: {user_profile}. 若需要更多咨询可再次输入"}
563
+ ]
564
+ resp = client.chat.completions.create(
565
+ model=model_default,
566
+ messages=msgs,
567
+ max_tokens=token_default,
568
+ temperature=temp_default,
569
+ top_p=top_p_default,
570
+ stream=False
571
+ )
572
+ return resp.choices[0].message.content
573
+ except Exception as e:
574
+ return f"发生错误: {str(e)}"
575
 
576
  # ~~~~~~~~~ Forward模式 ~~~~~~~~~
577
  else: