Stone164 commited on
Commit
007c39c
·
verified ·
1 Parent(s): ed20cbe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -86
app.py CHANGED
@@ -429,10 +429,8 @@ def predict(message, history):
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,38 +439,35 @@ def predict(message, history):
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:
467
  return "错误:API_TOKEN 未设置"
468
  client = OpenAI(api_key=api_key)
469
 
470
- # 生成职业详情
471
  career_detail_prompt = f"""
472
  你是一位专业的职业规划顾问。
473
  学生目标职业: {user_profile.get('specific_career') or '未知'}
474
  学生背景: {user_profile.get('bg_info') or '未知'}
475
- 为这位想要从事 {user_profile.get('specific_career')} 职业的学生写一份详细的职业说明,至少包含:
476
  1. 行业平均薪资(初级/中级/高级)
477
  2. 工作环境(远程/混合/办公室,团队规模等)
478
  3. 晋升难度(需要什么资历或条件)
@@ -495,81 +490,86 @@ def predict(message, history):
495
  )
496
  detail_msg = resp.choices[0].message.content
497
 
498
- # 设置状态
499
  user_profile["final_choice"] = user_profile.get("specific_career")
500
- job_chosen = 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 答疑
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]
 
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
  current_q_index += 1
440
  return nxt
441
 
 
 
 
 
442
  # ======================== 分模式处理 =========================
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 '未知'}
470
+ 请写一份详细的职业说明,至少包含:
471
  1. 行业平均薪资(初级/中级/高级)
472
  2. 工作环境(远程/混合/办公室,团队规模等)
473
  3. 晋升难度(需要什么资历或条件)
 
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.get("bg_info", ""),
521
+ wv=user_profile.get("work_value", "未提供"),
522
+ ps=user_profile.get("personality_summary", "未提供"),
523
+ dd=user_profile.get("dream_day", "未提供")
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:
571
  # 前期处理:收集信息和推荐大方向
572
+ if in_forward_flow and 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]