File size: 5,964 Bytes
61fd574
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import os
from app.agents.base_agent import BaseAgent

MOCK_RESPONSE = {
    "rewrite_suggestions": [
        {
            "dimension": "技能匹配",
            "issue": "JD要求Python后端开发经验,简历未体现",
            "original": "熟悉Java、C++,了解数据结构与算法",
            "suggestion": "补充Python后端项目经历,例如\"使用FastAPI开发RESTful API,实现用户认证与数据持久化\"",
            "reason": "JD核心技术栈为Python后端,仅列举Java/C++无法满足岗位核心要求,需明确展示Python实战经验"
        },
        {
            "dimension": "经验匹配",
            "issue": "缺少企业级后端实习经历",
            "original": "在校课程项目:图书管理系统",
            "suggestion": "突出项目难点与高并发场景,例如\"优化数据库查询减少50%响应时间,支撑日均10万请求\"",
            "reason": "JD要求有后端实习经验,课程项目需用量化成果弥补实习缺失,展示工程化思维"
        },
        {
            "dimension": "项目契合",
            "issue": "项目方向与JD业务场景不匹配",
            "original": "基于Java Swing的桌面应用开发",
            "suggestion": "将项目描述重构为\"基于Python的Web全栈项目\",体现完整开发流程",
            "reason": "JD业务涉及Web后端服务,桌面应用项目与岗位技术栈差异大,需重新包装体现相关能力"
        },
    ],
    "interview_tips": [
        {
            "topic": "Python异步编程与FastAPI",
            "reason": "JD明确要求asyncio和FastAPI,简历中无相关经验",
            "tip": "准备asyncio核心概念(事件循环、协程、Task),结合FastAPI源码理解异步请求处理流程,准备一个异步爬虫或WebSocket demo"
        },
        {
            "topic": "数据库设计与ORM",
            "reason": "JD要求熟悉MySQL和Redis,简历仅提到\"了解数据库\"",
            "tip": "复习SQLAlchemy/FastAPI集成、索引优化、事务隔离级别,准备缓存穿透/雪崩的解决方案"
        },
        {
            "topic": "系统设计",
            "reason": "岗位为校招,可能会考察基础设计能力",
            "tip": "准备短URL服务、秒杀系统等常见设计题,重点练习Python伪代码表达能力"
        },
    ],
    "encouragement": "你的基础扎实,Java和C++的背景说明你有良好的编程思维。建议集中2-3周时间做一个完整的Python后端项目(如简易电商API),就能在简历上大幅提升匹配度。你的学习能力完全胜任这个方向,加油!"
}


class Advisor(BaseAgent):
    name = "advisor"

    def build_prompt(self, match_report: dict, jd_text: str) -> tuple[str, dict]:
        system = (
            "你是一个求职建议专家。你的任务是:\n"
            "1. 基于匹配报告,给出精准的简历修改建议。\n"
            "2. 建议必须包含:哪个维度、具体问题、原文、改写建议、原因。\n"
            "3. 每条建议必须做到\"HR一眼能看出改变\"的程度,不要泛泛而谈。\n"
            "4. 如果匹配度低于50分,必须在鼓励中包含'从匹配度角度不推荐'的提示。\n"
            "5. 提供不超过3个面试准备话题(每个话题包含 topic、reason、tip)。\n"
            "6. 最后输出一段鼓励语,要求真诚具体,不敷衍。\n\n"
            "请严格按以下JSON格式输出,字段名必须完全匹配:\n"
            '{\n'
            '  "rewrite_suggestions": [\n'
            '    {"dimension": "技能匹配", "issue": "...", "original": "...", "suggestion": "...", "reason": "..."}\n'
            '  ],\n'
            '  "interview_tips": [\n'
            '    {"topic": "...", "reason": "...", "tip": "..."}\n'
            '  ],\n'
            '  "encouragement": "..."\n'
            '}\n\n'
            "输出格式为JSON,不要加markdown代码块标记。"
        )
        user = {
            "match_report": match_report,
            "jd_text": jd_text
        }
        return system, user

    async def generate_suggestions(self, match_report: dict, jd_text: str) -> dict:
        api_key = os.environ.get("DEEPSEEK_API_KEY")
        if not api_key or os.environ.get("MOCK_MODE", "").lower() == "true":
            return MOCK_RESPONSE
        system, user_input = self.build_prompt(match_report, jd_text)
        result = await self.call_llm(system, user_input)
        raw_suggestions = result.get("rewrite_suggestions") or result.get("suggestions") or result.get("modifications") or []
        normalized = []
        for s in raw_suggestions:
            if isinstance(s, dict):
                normalized.append({
                    "dimension": s.get("dimension") or s.get("name") or "",
                    "issue": s.get("issue") or s.get("problem") or "",
                    "original": s.get("original") or s.get("original_text") or "",
                    "suggestion": s.get("suggestion") or s.get("rewrite") or "",
                    "reason": s.get("reason") or s.get("rationale") or "",
                })
        result["rewrite_suggestions"] = normalized
        raw_tips = result.get("interview_tips") or result.get("tips") or result.get("preparation") or []
        normalized_tips = []
        for t in raw_tips:
            if isinstance(t, dict):
                normalized_tips.append({
                    "topic": t.get("topic") or t.get("title") or "",
                    "reason": t.get("reason") or t.get("why") or "",
                    "tip": t.get("tip") or t.get("advice") or "",
                })
        result["interview_tips"] = normalized_tips
        if not result.get("encouragement"):
            result["encouragement"] = ""
        if "error" in result:
            return MOCK_RESPONSE
        return result