From c001ba8146671334b6278d1b959052dedd817624 Mon Sep 17 00:00:00 2001 From: zk Date: Thu, 9 Apr 2026 18:22:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=B2=97=E4=BD=8D=E7=AE=80?= =?UTF-8?q?=E5=8E=86=E8=AF=8A=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .kiro/specs/skill-gap-analysis.md | 653 ++++++++++++++++++++++++++ .kiro/steering/项目结构说明.md | 29 +- app/ai/skill_gap_analyzer/__init__.py | 1 + app/ai/skill_gap_analyzer/analyzer.py | 137 ++++++ app/ai/skill_gap_analyzer/prompts.py | 115 +++++ app/api/skill_gap.py | 74 +++ app/main.py | 2 + app/models/job.py | 22 + app/schemas/__init__.py | 1 + app/schemas/skill_gap.py | 108 +++++ app/services/skill_gap_service.py | 355 ++++++++++++++ 11 files changed, 1487 insertions(+), 10 deletions(-) create mode 100644 .kiro/specs/skill-gap-analysis.md create mode 100644 app/ai/skill_gap_analyzer/__init__.py create mode 100644 app/ai/skill_gap_analyzer/analyzer.py create mode 100644 app/ai/skill_gap_analyzer/prompts.py create mode 100644 app/api/skill_gap.py create mode 100644 app/models/job.py create mode 100644 app/schemas/__init__.py create mode 100644 app/schemas/skill_gap.py create mode 100644 app/services/skill_gap_service.py diff --git a/.kiro/specs/skill-gap-analysis.md b/.kiro/specs/skill-gap-analysis.md new file mode 100644 index 0000000..592d926 --- /dev/null +++ b/.kiro/specs/skill-gap-analysis.md @@ -0,0 +1,653 @@ +# 岗位简历技能差距分析 + 定制简历 — 完整方案 + +## 一、需求概述 + +三步流程: +1. **差距分析**:根据岗位技能标签和用户简历,AI 判断缺失技能,纯计算匹配分 +2. **定制简历**:用户选择要优化的模块和要新增的技能,AI 生成优化后的简历内容 +3. **预览 + AI 对话编辑**:前端渲染定制简历,用户可通过 AI 对话继续编辑,用于投递时使用(不写回原简历) + +定制简历存 Redis,过期时间 12 小时,不落库。一个用户同时只有一份定制简历。 + +--- + +## 二、接口总览 + +| 序号 | 路径 | 方法 | 说明 | +|------|------|------|------| +| 1 | `/api/job/skill-gap` | POST | 差距分析 | +| 2 | `/api/job/customize-resume` | POST | 生成定制简历 | +| 3 | `/api/job/customize-resume` | GET | 查询定制简历 | +| 4 | `/api/job/customize-resume` | PUT | 手动编辑定制简历 | +| 5 | `/api/job/customize-resume/rollback` | POST | 回滚定制简历 | +| 6 | `/api/job/customize-resume/ai-edit` | POST | AI 对话式编辑定制简历 | + +--- + +## 三、接口一:差距分析 + +### 接口信息 + +| 项目 | 值 | +|------|-----| +| 路径 | `POST /api/job/skill-gap` | +| 入参 | `{ "jobId": Long }` | +| 鉴权 | 需要登录态,从 token 取 userId | + +### 处理流程 + +1. 从 token 取 userId +2. 查简历(自动选择,不传 resumeId): + - 先查 `bg_user_resume` 中 `user_id=userId AND is_default=1`,按 `update_time DESC` 取第一条 + - 没有默认简历 → 查 `user_id=userId`,按 `update_time DESC` 取第一条 + - 没有任何简历 → 报错"请先创建简历" +3. 查岗位: + - 查 `bg_job` 拿 id、title、skill_tags + - 岗位不存在 → 报错 + - skill_tags 为空 → 直接返回满分 10,missingSkills 为空数组 +4. 查简历子表(拼 AI 输入): + - `bg_user_resume_education` + - `bg_user_resume_work` + - `bg_user_resume_internship` + - `bg_user_resume_project` + - `bg_user_resume_competition` +5. 调 AI(一次): + - 输入:岗位 skill_tags 列表 + 简历 skills 字段 + 各子表经历描述 + - 输出:缺失技能的 JSON 数组,必须是 skill_tags 的子集 +6. 计算匹配分:`score = (skill_tags总数 - missingSkills数) / skill_tags总数 × 10`,保留一位小数 + +### 返回 + +```json +{ + "score": 2.5, + "job": { + "jobId": "1234567890", + "title": "数据产品经理", + "skillTags": ["Python", "SQL", "项目管理", "团队协作", "数据分析", "跨部门沟通"] + }, + "resume": { + "resumeId": "1234567890", + "resumeName": "李华_产品经理", + "targetPosition": "电商产品经理" + }, + "missingSkills": ["Python", "SQL", "数据分析", "跨部门沟通"] +} +``` + +### 边界处理 + +| 场景 | 处理 | +|------|------| +| 用户无简历 | 报错"请先创建简历" | +| 岗位不存在 | 报错 | +| skill_tags 为空 | 满分 10,missingSkills 为空数组 | +| AI 调用失败 | 降级:全部标记为缺失,分数 0 | + +### AI Prompt + +``` +你是一个技能匹配助手。给定岗位要求的技能标签列表和用户简历信息,判断用户简历中未覆盖的技能。 + +【岗位技能标签】 +{skill_tags} + +【用户简历】 +{resume_json} + +规则: +1. 逐个判断岗位技能标签,用户简历中是否体现了该技能(包括直接提及、经历中隐含的技能) +2. 只输出用户简历未覆盖的技能,必须是岗位技能标签的子集,原文输出不要修改 +3. 返回 JSON 数组格式,如:["Python", "SQL"] +4. 如果全部覆盖,返回空数组 [] +5. 只返回 JSON 数组,不要其他内容 +``` + +--- + +## 四、接口二:生成定制简历 + +### 接口信息 + +| 项目 | 值 | +|------|-----| +| 路径 | `POST /api/job/customize-resume` | +| 入参 | 见下方 | +| 鉴权 | 需要登录态,从 token 取 userId | + +### 入参 + +```json +{ + "jobId": "Long", + "resumeId": "Long", + "optimizeModules": ["summary", "skills", "experience"], + "addSkills": ["Python", "SQL"] +} +``` + +- `resumeId`:以哪份简历为模板(来自差距分析返回的 resumeId,用户可能切换过简历) +- `optimizeModules`:用户勾选要优化的模块,可选值:summary(个人概述)、skills(技能)、experience(过往经历) +- `addSkills`:用户勾选要新增的技能关键词(来自差距分析的 missingSkills) + +### 处理流程 + +1. 查简历主表 + 所有子表(完整简历数据) +2. 查岗位信息(title、description、requirement) +3. 按用户选择的模块分别处理(各模块并发执行,最后合并): + +**summary(个人概述)**: +- 调 AI,根据岗位信息微调 summary,融入选中的技能关键词 +- 避免过度优化,保持原文风格,只做轻微润色 + +**skills(技能)**: +- 把 addSkills 追加到现有 skills 列表,不调 AI,纯内存操作 + +**experience(过往经历)**: +- 按子表(education/work/internship/project/competition)为单位,每个子表一个 AI 调用,传入该子表的完整数据 +- 让描述更贴合岗位方向,避免过度优化,基本保持原文不变 +- 不融入 addSkills,经历描述不硬塞技能关键词 + +**addSkills 影响范围**:只影响 skills(直接追加)和 summary(自然融入),不影响 experience。 + +**并发策略**:summary 优化 和 各子表优化 全部并发执行(asyncio.gather),skills 纯内存操作不需要等待。最终合并所有结果。 + +4. 未勾选的模块保持原数据不动 +5. 组装完整的定制简历数据,存 Redis(key:`customize:resume:{userId}`,过期 12 小时,重新生成会覆盖) +6. 返回成功标识,不返回简历数据(前端通过 GET 接口查询) + +### 返回 + +```json +{ + "success": true +} +``` + +说明:简历数据前端通过 `GET /api/job/customize-resume` 查询。子表记录的 id 使用随机 8 位字符串作为标识(从数据库查出时生成),不使用数据库原始 id。 + +### 边界处理 + +| 场景 | 处理 | +|------|------| +| 简历不存在 | 报错 | +| 岗位不存在 | 报错 | +| optimizeModules 为空 | 报错"请至少选择一个优化模块" | +| AI 调用失败 | 该模块保持原数据不动,不影响其他模块 | + +--- + +## 五、接口三:查询定制简历 + +### 接口信息 + +| 项目 | 值 | +|------|-----| +| 路径 | `GET /api/job/customize-resume` | +| 入参 | 无 | +| 鉴权 | 需要登录态,从 token 取 userId | + +### 处理流程 + +1. 从 Redis 取定制简历数据(key:`customize:resume:{userId}`) +2. 不存在 → 返回 null +3. 返回完整简历 JSON + +--- + +## 六、接口四:修改定制简历(手动编辑) + +### 接口信息 + +| 项目 | 值 | +|------|-----| +| 路径 | `PUT /api/job/customize-resume` | +| 入参 | 完整简历 JSON(整体覆盖) | +| 鉴权 | 需要登录态,从 token 取 userId | + +### 处理流程 + +1. 校验入参 +2. 整体覆盖 Redis 中的定制简历数据(key:`customize:resume:{userId}`) +3. 刷新过期时间为 12 小时 +4. 不存在时也直接写入 + +--- + +## 七、接口五:回滚定制简历 + +### 接口信息 + +| 项目 | 值 | +|------|-----| +| 路径 | `POST /api/job/customize-resume/rollback` | +| 入参 | 无 | +| 鉴权 | 需要登录态,从 token 取 userId | + +### 处理流程 + +1. 从 Redis 取回滚数据(key:`customize:resume:rollback:{userId}`) +2. 不存在 → 报错"没有可回滚的版本" +3. 用回滚数据覆盖当前定制简历(key:`customize:resume:{userId}`) +4. 删除回滚数据 +5. 刷新定制简历过期时间为 12 小时 + +--- + +## 七、接口五:AI 对话式编辑定制简历 + +### 接口信息 + +| 项目 | 值 | +|------|-----| +| 路径 | `POST /api/job/customize-resume/ai-edit` | +| 入参 | 见下方 | +| 鉴权 | 需要登录态,从 token 取 userId | + +### 入参 + +```json +{ + "jobId": "Long", + "instruction": "精简一下第一段工作经历", + "chatHistory": [ + { "role": "user", "content": "优化描述" }, + { "role": "assistant", "content": "你想优化哪一部分?" } + ] +} +``` + +- `instruction`:用户当前输入的指令 +- `chatHistory`:之前的对话历史,前端维护,每次请求带上 + +### 消息类型 + +返回两种消息类型: + +**message(普通对话)**:AI 追问、引导,不修改简历 +```json +{ + "type": "message", + "message": "你想优化哪一部分的描述?是最新的实习还是所有工作经历?" +} +``` + +**updated(修改通知)**:AI 修改了简历,返回新版本 +```json +{ + "type": "updated", + "message": "完成!已更新:个人简介、技能、工作经验" +} +``` + +### 处理流程(两步走) + +#### 第一步:准备数据 + +1. 从 Redis 取当前定制简历(不存在则报错) +2. 查 `bg_job` 拿 title、description + +#### 第二步:规划 AI(意图识别) + +输入:用户指令 + 对话历史 + 当前完整简历内容 + 岗位 title + +输出两种结果: + +**对话(指令不明确)**: +```json +{ "action": "chat", "message": "你想优化哪一部分?" } +``` +→ 直接返回 `{ "type": "message", "message": "..." }`,结束。 + +**修改计划(指令明确)**: +```json +{ + "action": "modify", + "modules": [ + { "module": "resume", "instruction": "在 summary 中融入数据分析技能,skills 添加 Python" }, + { "module": "work", "instruction": "精简第一段工作经历,突出量化成果" } + ], + "updatedModulesLabel": "个人简介、工作经验" +} +``` + +模块划分(按表结构,共 6 个): + +| 模块名 | 对应表 | 可修改字段 | +|--------|--------|-----------| +| `resume` | `bg_user_resume` | avatarUrl、name、email、mobileNumber、city、wechatNumber、portfolioUrl、skills、certificates、summary | +| `education` | `bg_user_resume_education` | 全部字段 | +| `work` | `bg_user_resume_work` | 全部字段 | +| `internship` | `bg_user_resume_internship` | 全部字段 | +| `project` | `bg_user_resume_project` | 全部字段 | +| `competition` | `bg_user_resume_competition` | 全部字段 | + +#### 第三步:按模块并发执行修改 + +根据修改计划,对每个模块并发调 AI(asyncio.gather): +- 每个模块传入该模块的完整数据(如 work 传 `[{工作1}, {工作2}]` 整体) +- AI 返回修改后的该模块完整数据 +- 所有模块统一走 AI,包括 skills 等简单操作(因为需要 AI 理解用户自然语言指令) + +#### 第四步:合并 + +- AI 调用失败的模块保持原数据不动 +- 成功的模块直接用 AI 返回结果替换 + +把所有模块结果合并回完整简历。 + +#### 第五步:保存 + 返回 + +1. 当前简历存为回滚数据(key:`customize:resume:rollback:{userId}`,过期 30 分钟) +2. 新简历覆盖 Redis(key:`customize:resume:{userId}`),刷新过期时间 12 小时 +3. 返回 `type: updated` + 消息(前端通过 GET 接口查询新简历,通过回滚接口恢复) + +### description 字段处理 + +子表的 description 字段格式为 `[{id, text}, {id, text}]`,AI 操作规则(通过 prompt 约束): +- **修改**:保留原 id,只改 text +- **新增**:AI 自行生成随机 8 位字符串作为 id +- **删除**:直接从数组中移除 + +不做后端校验,完全依靠 prompt 约束 AI 行为。 + +### 边界处理 + +| 场景 | 处理 | +|------|------| +| 定制简历不存在 | 报错"定制简历不存在,请先生成" | +| 规划 AI 失败 | 返回 `type: message`,提示重试 | +| 某个模块修改 AI 失败 | 该模块保持原数据,其他模块正常返回 | + +--- + +## 八、AI Prompt 汇总 + +### 1. 差距分析 Prompt + +``` +你是一个技能匹配助手。给定岗位要求的技能标签列表和用户简历信息,判断用户简历中未覆盖的技能。 + +【岗位技能标签】 +{skill_tags} + +【用户简历】 +{resume_json} + +规则: +1. 逐个判断岗位技能标签,用户简历中是否体现了该技能(包括直接提及、经历中隐含的技能) +2. 只输出用户简历未覆盖的技能,必须是岗位技能标签的子集,原文输出不要修改 +3. 返回 JSON 数组格式,如:["Python", "SQL"] +4. 如果全部覆盖,返回空数组 [] +5. 只返回 JSON 数组,不要其他内容 +``` + +### 2. 定制简历 - summary 优化 Prompt + +``` +你是一个简历优化助手。根据目标岗位信息,微调用户的个人概述。 + +【目标岗位】 +{job_title} + +【需要融入的技能关键词】 +{add_skills} + +【原始个人概述】 +{original_summary} + +规则: +1. 保持原文风格和主体内容不变 +2. 只做轻微润色,让概述更贴合目标岗位方向 +3. 自然融入需要新增的技能关键词,不要生硬堆砌 +4. 避免过度优化,改动越少越好 +5. 直接输出优化后的文本,不要其他内容 +``` + +### 3. 定制简历 - experience 优化 Prompt + +``` +你是一个简历优化助手。根据目标岗位信息,微调用户的经历描述。 + +【目标岗位】 +{job_title} +{job_description} + +【原始经历数据】 +{original_module_data} + +规则: +1. 基本保持原文不变,只在可以优化的地方做轻微调整 +2. 让描述更贴合目标岗位方向,但不要编造内容 +3. 避免过度优化,改动越少越好 +4. description 字段是 [{id, text}] 格式:修改时保留原 id 只改 text,新增段落生成随机8位字符串作为 id,删除段落直接移除 +5. 返回修改后的完整模块数据(JSON 格式,与输入格式一致) +``` + +### 4. Agent - 规划 Prompt + +``` +你是一个简历编辑助手。分析用户的指令,决定需要修改简历的哪些模块。 + +【目标岗位】 +{job_title} + +【当前简历】 +{resume_json} + +【对话历史】 +{chat_history} + +【用户指令】 +{instruction} + +如果用户指令明确,返回修改计划 JSON: +{"action": "modify", "modules": [{"module": "模块名", "instruction": "具体修改要求"}], "updatedModulesLabel": "中文模块名列表"} + +如果用户指令不明确或需要澄清,返回对话 JSON: +{"action": "chat", "message": "你的追问内容"} + +模块名可选: +- resume:主表(个人信息,包含 name、email、mobileNumber、city、wechatNumber、portfolioUrl、skills、certificates、summary、avatarUrl) +- education:教育经历 +- work:工作经历 +- internship:实习经历 +- project:项目经历 +- competition:竞赛经历 +只返回 JSON,不要其他内容。 +``` + +### 5. Agent - 模块修改 Prompt + +``` +你是一个简历编辑助手。根据修改要求,修改简历的指定模块。 + +【目标岗位】 +{job_title} + +【修改要求】 +{module_instruction} + +【模块数据结构】 +{module_schema} + +【当前模块数据】 +{module_data} + +规则: +1. 严格按照修改要求操作,可以增删改 +2. 未要求修改的记录保持不变 +3. 不要编造用户简历中不存在的内容 +4. 保持原文格式和结构 +5. description 字段是 [{id, text}] 格式:修改时保留原 id 只改 text,新增段落生成随机8位字符串作为 id,删除段落直接从数组中移除 +6. 新增记录时按照模块数据结构生成完整字段,id 使用随机8位字符串 +7. 返回修改后的完整模块数据(JSON 格式,与输入格式一致) +``` + +### 各模块数据结构定义(传入 prompt 的 module_schema) + +**resume(主表)**: +```json +{ "avatarUrl": "string", "name": "string", "email": "string", "mobileNumber": "string", "city": "string", "wechatNumber": "string", "portfolioUrl": "string", "skills": ["string"], "certificates": ["string"], "summary": "string" } +``` + +**education**: +```json +[{ "id": "string(8位)", "school": "string", "major": "string", "degree": "大专/本科/硕士/博士", "studyType": "全日制/非全日制", "startDate": "2023.09", "endDate": "2024.06", "description": [{"id": "string(8位)", "text": "string"}] }] +``` + +**work**: +```json +[{ "id": "string(8位)", "companyName": "string", "position": "string", "startDate": "2023.06", "endDate": "2023.09", "description": [{"id": "string(8位)", "text": "string"}] }] +``` + +**internship**: +```json +[{ "id": "string(8位)", "companyName": "string", "position": "string", "startDate": "2023.06", "endDate": "2023.09", "description": [{"id": "string(8位)", "text": "string"}] }] +``` + +**project**: +```json +[{ "id": "string(8位)", "companyName": "string", "projectName": "string", "role": "string", "startDate": "2023.06", "endDate": "2023.09", "description": [{"id": "string(8位)", "text": "string"}] }] +``` + +**competition**: +```json +[{ "id": "string(8位)", "competitionName": "string", "award": "string", "awardDate": "2023.07", "description": [{"id": "string(8位)", "text": "string"}] }] +``` + +--- + +## 九、Redis 设计 + +### Key 格式 + +- 定制简历:`customize:resume:{userId}` +- 回滚数据:`customize:resume:rollback:{userId}` + +### Value 结构 + +```python +class CustomizeResume: + """定制简历缓存结构""" + resume: ResumeProfile # 主表信息 + education: list[Education] # 教育经历 + work: list[Work] # 工作经历 + internship: list[Internship] # 实习经历 + project: list[Project] # 项目经历 + competition: list[Competition] # 竞赛经历 + +class ResumeProfile: + """主表可修改字段""" + avatarUrl: str + name: str + email: str + mobileNumber: str + city: str + wechatNumber: str + portfolioUrl: str + skills: list[str] + certificates: list[str] + summary: str + +class Education: + id: str # 随机8位标识 + school: str + major: str + degree: str # 大专/本科/硕士/博士 + studyType: str # 全日制/非全日制 + startDate: str # 格式:2023.09 + endDate: str # 格式:2024.06 + description: list[Paragraph] + +class Work: + id: str + companyName: str + position: str + startDate: str + endDate: str + description: list[Paragraph] + +class Internship: + id: str + companyName: str + position: str + startDate: str + endDate: str + description: list[Paragraph] + +class Project: + id: str + companyName: str + projectName: str + role: str + startDate: str + endDate: str + description: list[Paragraph] + +class Competition: + id: str + competitionName: str + award: str + awardDate: str # 格式:2023.07 + description: list[Paragraph] + +class Paragraph: + id: str # 随机8位标识 + text: str +``` + +定制简历和回滚数据使用相同的 `CustomizeResume` 结构。代码实现时使用 Pydantic model,存取 Redis 通过 `model_dump_json()` / `model_validate_json()`。 + +### 常量 + +```python +CUSTOMIZE_RESUME_KEY_PREFIX = "customize:resume:" +CUSTOMIZE_RESUME_EXPIRE = 12 * 60 * 60 # 12小时 +CUSTOMIZE_RESUME_ROLLBACK_KEY_PREFIX = "customize:resume:rollback:" +CUSTOMIZE_RESUME_ROLLBACK_EXPIRE = 30 * 60 # 30分钟 +``` + +### 过期时间 + +- 定制简历:12 小时,每次写入/修改时刷新 +- 回滚数据:30 分钟,每次 AI 编辑时覆盖 + +--- + +## 十、数据依赖 + +| 表 | 读写 | 用途 | +|----|------|------| +| `bg_job` | 读 | 取岗位信息(title、description、requirement、skill_tags) | +| `bg_user_resume` | 读 | 取简历主表数据 | +| `bg_user_resume_education` | 读 | 取教育经历 | +| `bg_user_resume_work` | 读 | 取工作经历 | +| `bg_user_resume_internship` | 读 | 取实习经历 | +| `bg_user_resume_project` | 读 | 取项目经历 | +| `bg_user_resume_competition` | 读 | 取竞赛经历 | +| Redis | 写/读 | 存取定制简历,过期 12 小时 | + +无新建表,无数据库写操作。 + +--- + +## 十一、文件规划 + +### 新建文件 + +| 文件 | 职责 | +|------|------| +| `app/models/job.py` | Job 表 ORM 模型(bg_job,只读) | +| `app/core/schemas/skill_gap.py` | Pydantic Schema(请求参数 Param + 响应 Dto + Redis 缓存模型 CustomizeResume) | +| `app/ai/skill_gap_analyzer/__init__.py` | 模块初始化 | +| `app/ai/skill_gap_analyzer/prompts.py` | 所有 AI prompt 模板 | +| `app/ai/skill_gap_analyzer/analyzer.py` | AI 调用逻辑(差距分析 + 定制简历优化 + Agent 规划/执行) | +| `app/services/skill_gap_service.py` | 业务逻辑层(含 Redis 常量、简历查询、Redis 读写) | +| `app/api/skill_gap.py` | 路由层(6 个接口) | + +### 修改文件 + +| 文件 | 改动 | +|------|------| +| `app/main.py` | 注册 skill_gap 路由 | diff --git a/.kiro/steering/项目结构说明.md b/.kiro/steering/项目结构说明.md index 535c607..9dc2f99 100644 --- a/.kiro/steering/项目结构说明.md +++ b/.kiro/steering/项目结构说明.md @@ -35,14 +35,18 @@ offerpie_python_ai/ │ ├─ resume_extractor/ # 简历 AI 提取模块 │ │ ├─ prompts.py # 5 个提取任务的 System Prompt(个人信息/教育/工作+实习/项目/竞赛) │ │ └─ extractor.py # AI 并行提取(extract_all 入口,asyncio.gather 5 路并行) - │ └─ resume_diagnoser/ # 简历 AI 诊断模块 - │ ├─ prompts.py # 诊断 Prompt 模板(分模块诊断 + 汇总评价 + 润色优化) - │ └─ diagnoser.py # AI 并行诊断(diagnose_all 入口 + generate_summary 汇总评价 + polish_content 润色优化) + │ ├─ resume_diagnoser/ # 简历 AI 诊断模块 + │ │ ├─ prompts.py # 诊断 Prompt 模板(分模块诊断 + 汇总评价 + 润色优化) + │ │ └─ diagnoser.py # AI 并行诊断(diagnose_all 入口 + generate_summary 汇总评价 + polish_content 润色优化) + │ └─ skill_gap_analyzer/ # 技能差距分析 + 定制简历 AI 模块 + │ ├─ prompts.py # 差距分析 + 简历优化 + Agent 规划/执行 Prompt 模板 + MODULE_SCHEMAS + │ └─ analyzer.py # AI 调用逻辑(差距分析 + summary优化 + 经历优化 + Agent规划 + Agent模块修改) │ ├─ api/ # **路由层**(REST API 接口) │ ├─ health.py # 健康检查接口 GET /health/ │ ├─ resume.py # 简历接口 POST /resume/upload(上传文件AI解析) - │ └─ resume_diagnose.py # 简历诊断接口(POST 触发诊断 / GET 查询报告 / PUT 标记处理+用户评价 / POST 润色优化) + │ ├─ resume_diagnose.py # 简历诊断接口(POST 触发诊断 / GET 查询报告 / PUT 标记处理+用户评价 / POST 润色优化) + │ └─ skill_gap.py # 技能差距分析 + 定制简历接口(差距分析 / 生成定制简历 / 查询 / 编辑 / 回滚 / AI对话编辑) │ ├─ models/ # **ORM 模型层**(SQLAlchemy 声明式映射) │ ├─ func_permission.py # 功能权限定义表(bg_func_permission) @@ -55,16 +59,21 @@ offerpie_python_ai/ │ ├─ user_resume_project.py # 简历-项目经历表(bg_user_resume_project) │ ├─ user_resume_competition.py # 简历-竞赛经历表(bg_user_resume_competition) │ ├─ resume_diagnosis_report.py # 简历诊断报告表(bg_resume_diagnosis_report) - │ └─ resume_diagnosis_issue.py # 简历诊断问题表(bg_resume_diagnosis_issue) + │ ├─ resume_diagnosis_issue.py # 简历诊断问题表(bg_resume_diagnosis_issue) + │ └─ job.py # 岗位表(bg_job,只读,用于技能差距分析) │ ├─ tool/ # **工具层**(无状态、无业务依赖的通用工具) │ ├─ file_parser.py # 文件解析工具(PDF/Word/TXT → 纯文本,parse_to_text 入口方法) │ └─ snowflake.py # 雪花 ID 生成工具(next_id) │ + ├─ schemas/ # **Schema 层**(Pydantic 请求/响应/缓存模型) + │ └─ skill_gap.py # 技能差距分析 Schema(SkillGapParam、CustomizeResumeParam、AiEditParam、CustomizeResume 等) + │ └─ services/ # **业务逻辑层** ├─ func_permission_service.py # 功能权限服务(校验+扣减+回退,逻辑与Java端一致) ├─ resume_parse_service.py # 简历解析服务(文件解析→AI结构化→写入主表+5张子表) - └─ resume_diagnose_service.py # 简历诊断服务(加载简历→AI并行诊断→统计评级→写入报告) + ├─ resume_diagnose_service.py # 简历诊断服务(加载简历→AI并行诊断→统计评级→写入报告) + └─ skill_gap_service.py # 技能差距分析服务(差距分析→定制简历生成/查询/编辑/回滚→AI对话编辑) ``` ## 2️⃣ 各层模块职责 @@ -72,11 +81,11 @@ offerpie_python_ai/ |------|----------|-------------| | **config** | 统一配置管理,基于 Pydantic Settings,支持 .env 文件加载 | `Settings`(数据库、Redis、LLM供应商、JWT、CORS、日志等全部配置项) | | **core** | 核心基础设施:数据库连接、Redis连接、鉴权、日志、中间件、异常处理、统一响应 | `database.py`、`redis.py`、`auth.py`、`middleware.py`、`exceptions.py`、`logger.py`、`StandardResponse` | -| **ai** | AI 模型管理 + 业务 AI 能力 | `LLM` 枚举、`resume_extractor/`(简历并行提取:5路 AI 同时提取个人信息/教育/工作+实习/项目/竞赛)、`resume_diagnoser/`(简历诊断:并行诊断各模块描述 + 汇总评价) | -| **api** | REST API 路由定义 | `health.py`(健康检查)、`resume.py`(简历上传解析)、`resume_diagnose.py`(简历诊断) | -| **models** | SQLAlchemy ORM 模型,与 Java 端共享同一数据库 | `FuncPermission`、`UserFuncPermissionStock`、`UserFuncUsageLog`、`UserResume`、`UserResumeEducation`/`Work`/`Internship`/`Project`/`Competition`、`ResumeDiagnosisReport`、`ResumeDiagnosisIssue` | +| **ai** | AI 模型管理 + 业务 AI 能力 | `LLM` 枚举、`resume_extractor/`(简历并行提取)、`resume_diagnoser/`(简历诊断)、`skill_gap_analyzer/`(技能差距分析 + 定制简历优化 + Agent 规划/执行) | +| **api** | REST API 路由定义 | `health.py`(健康检查)、`resume.py`(简历上传解析)、`resume_diagnose.py`(简历诊断)、`skill_gap.py`(技能差距分析 + 定制简历) | +| **models** | SQLAlchemy ORM 模型,与 Java 端共享同一数据库 | `FuncPermission`、`UserFuncPermissionStock`、`UserFuncUsageLog`、`UserResume`、`UserResumeEducation`/`Work`/`Internship`/`Project`/`Competition`、`ResumeDiagnosisReport`、`ResumeDiagnosisIssue`、`Job`(只读) | | **tool** | 无状态通用工具,不依赖数据库/Redis/用户上下文 | `file_parser.py`(PDF/Word/TXT 文件解析为纯文本)、`snowflake.py`(雪花ID生成) | -| **services** | 业务逻辑实现 | `FuncPermissionService`(功能权限校验、扣减、回退)、`ResumeParseService`(简历文件解析→AI结构化→入库)、`ResumeDiagnoseService`(简历诊断→AI并行分析→评级→入库) | +| **services** | 业务逻辑实现 | `FuncPermissionService`(功能权限校验、扣减、回退)、`ResumeParseService`(简历文件解析→AI结构化→入库)、`ResumeDiagnoseService`(简历诊断→AI并行分析→评级→入库)、`SkillGapService`(技能差距分析→定制简历生成/查询/编辑/回滚→AI对话编辑) | ## 3️⃣ 技术栈 | 类别 | 技术选型 | 说明 | diff --git a/app/ai/skill_gap_analyzer/__init__.py b/app/ai/skill_gap_analyzer/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/app/ai/skill_gap_analyzer/__init__.py @@ -0,0 +1 @@ + diff --git a/app/ai/skill_gap_analyzer/analyzer.py b/app/ai/skill_gap_analyzer/analyzer.py new file mode 100644 index 0000000..80bb60a --- /dev/null +++ b/app/ai/skill_gap_analyzer/analyzer.py @@ -0,0 +1,137 @@ +"""技能差距分析 AI 引擎 + +差距分析 + 定制简历优化 + Agent 规划/执行。 +依赖:LLM 枚举、skill_gap_analyzer/prompts +""" + +import asyncio +import re + +from json_repair import repair_json +from langchain_core.output_parsers import StrOutputParser +from langchain_core.prompts import ChatPromptTemplate + +from app.ai.models import LLM +from app.ai.skill_gap_analyzer.prompts import ( + SKILL_GAP_PROMPT, SUMMARY_OPTIMIZE_PROMPT, EXPERIENCE_OPTIMIZE_PROMPT, + AGENT_PLAN_PROMPT, AGENT_MODULE_EDIT_PROMPT, MODULE_SCHEMAS, +) +from app.core.logger import log + + +def _parse_json(text: str): + """解析 AI 输出的 JSON,自动去除 markdown 代码块包裹,容错处理""" + cleaned = re.sub(r"^```(?:json)?\s*\n?", "", text.strip()) + cleaned = re.sub(r"\n?```\s*$", "", cleaned) + return repair_json(cleaned, return_objects=True) + + +# ===== 差距分析 ===== + +_skill_gap_chain = ( + ChatPromptTemplate.from_messages([("system", SKILL_GAP_PROMPT), ("human", "请开始分析。")]) + | LLM.DOUBAO_PRO_32K.create(temperature=0) + | StrOutputParser() +) + + +async def analyze_skill_gap(skill_tags: list[str], resume_json: str) -> list[str]: + """分析技能差距,返回缺失技能列表""" + try: + raw = await _skill_gap_chain.ainvoke({"skill_tags": str(skill_tags), "resume_json": resume_json}) + result = _parse_json(raw) + if isinstance(result, list): + return [s for s in result if isinstance(s, str) and s in skill_tags] + return skill_tags # 解析异常降级:全部标记缺失 + except Exception as e: + log.warning(f"AI技能差距分析失败: {e}") + return skill_tags # 降级:全部标记缺失 + + +# ===== 定制简历 - summary 优化 ===== + +_summary_optimize_chain = ( + ChatPromptTemplate.from_messages([("system", SUMMARY_OPTIMIZE_PROMPT), ("human", "请开始优化。")]) + | LLM.CLAUDE_SONNET_4.create(temperature=0.3) + | StrOutputParser() +) + + +async def optimize_summary(job_title: str, add_skills: list[str], original_summary: str) -> str: + """优化个人概述,融入技能关键词""" + try: + return await _summary_optimize_chain.ainvoke({ + "job_title": job_title, "add_skills": "、".join(add_skills) if add_skills else "无", + "original_summary": original_summary or "暂无", + }) + except Exception as e: + log.warning(f"AI优化summary失败: {e}") + return original_summary + + +# ===== 定制简历 - experience 优化 ===== + +_experience_optimize_chain = ( + ChatPromptTemplate.from_messages([("system", EXPERIENCE_OPTIMIZE_PROMPT), ("human", "请开始优化。")]) + | LLM.CLAUDE_SONNET_4.create(temperature=0.3) + | StrOutputParser() +) + + +async def optimize_module(job_title: str, job_description: str, module_data: str) -> list | dict | None: + """优化子表模块经历描述,返回修改后的完整模块数据""" + try: + raw = await _experience_optimize_chain.ainvoke({ + "job_title": job_title, "job_description": job_description or "", + "original_module_data": module_data, + }) + return _parse_json(raw) + except Exception as e: + log.warning(f"AI优化经历模块失败: {e}") + return None + + +# ===== Agent - 规划 ===== + +_plan_chain = ( + ChatPromptTemplate.from_messages([("system", AGENT_PLAN_PROMPT), ("human", "请分析用户指令。")]) + | LLM.DOUBAO_PRO_32K.create(temperature=0) + | StrOutputParser() +) + + +async def plan_edit(job_title: str, resume_json: str, chat_history: str, instruction: str) -> dict | None: + """Agent 规划:分析用户指令,返回修改计划或对话回复""" + try: + raw = await _plan_chain.ainvoke({ + "job_title": job_title, "resume_json": resume_json, + "chat_history": chat_history, "instruction": instruction, + }) + result = _parse_json(raw) + return result if isinstance(result, dict) else None + except Exception as e: + log.warning(f"AI规划失败: {e}") + return None + + +# ===== Agent - 模块修改 ===== + +_module_edit_chain = ( + ChatPromptTemplate.from_messages([("system", AGENT_MODULE_EDIT_PROMPT), ("human", "请执行修改。")]) + | LLM.CLAUDE_SONNET_4.create(temperature=0.3) + | StrOutputParser() +) + + +async def execute_module_edit(job_title: str, module_instruction: str, + module_schema: str, module_data: str) -> dict | list | None: + """Agent 模块修改:根据指令修改指定模块数据""" + try: + raw = await _module_edit_chain.ainvoke({ + "job_title": job_title, "module_instruction": module_instruction, + "module_schema": module_schema, "module_data": module_data, + }) + return _parse_json(raw) + except Exception as e: + log.warning(f"AI模块修改失败: {e}") + return None diff --git a/app/ai/skill_gap_analyzer/prompts.py b/app/ai/skill_gap_analyzer/prompts.py new file mode 100644 index 0000000..8fe36a6 --- /dev/null +++ b/app/ai/skill_gap_analyzer/prompts.py @@ -0,0 +1,115 @@ +"""技能差距分析 + 定制简历 Prompt 模板 + +注意:prompt 中的 JSON 示例花括号必须用 {{ }} 转义,避免被 ChatPromptTemplate 当作变量。 +""" + +SKILL_GAP_PROMPT = """你是一个技能匹配助手。给定岗位要求的技能标签列表和用户简历信息,判断用户简历中未覆盖的技能。 + +【岗位技能标签】 +{skill_tags} + +【用户简历】 +{resume_json} + +规则: +1. 逐个判断岗位技能标签,用户简历中是否体现了该技能(包括直接提及、经历中隐含的技能) +2. 只输出用户简历未覆盖的技能,必须是岗位技能标签的子集,原文输出不要修改 +3. 返回 JSON 数组格式,如:["Python", "SQL"] +4. 如果全部覆盖,返回空数组 [] +5. 只返回 JSON 数组,不要其他内容""" + +SUMMARY_OPTIMIZE_PROMPT = """你是一个简历优化助手。根据目标岗位信息,微调用户的个人概述。 + +【目标岗位】 +{job_title} + +【需要融入的技能关键词】 +{add_skills} + +【原始个人概述】 +{original_summary} + +规则: +1. 保持原文风格和主体内容不变 +2. 只做轻微润色,让概述更贴合目标岗位方向 +3. 自然融入需要新增的技能关键词,不要生硬堆砌 +4. 避免过度优化,改动越少越好 +5. 直接输出优化后的文本,不要其他内容""" + +EXPERIENCE_OPTIMIZE_PROMPT = """你是一个简历优化助手。根据目标岗位信息,微调用户的经历描述。 + +【目标岗位】 +{job_title} +{job_description} + +【原始经历数据】 +{original_module_data} + +规则: +1. 基本保持原文不变,只在可以优化的地方做轻微调整 +2. 让描述更贴合目标岗位方向,但不要编造内容 +3. 避免过度优化,改动越少越好 +4. description 字段是 [{{"id": "xxx", "text": "xxx"}}] 格式:修改时保留原 id 只改 text,新增段落生成随机8位字符串作为 id,删除段落直接移除 +5. 返回修改后的完整模块数据(JSON 格式,与输入格式一致)""" + +AGENT_PLAN_PROMPT = """你是一个简历编辑助手。分析用户的指令,决定需要修改简历的哪些模块。 + +【目标岗位】 +{job_title} + +【当前简历】 +{resume_json} + +【对话历史】 +{chat_history} + +【用户指令】 +{instruction} + +如果用户指令明确,返回修改计划 JSON: +{{"action": "modify", "modules": [{{"module": "模块名", "instruction": "具体修改要求"}}], "updatedModulesLabel": "中文模块名列表"}} + +如果用户指令不明确或需要澄清,返回对话 JSON: +{{"action": "chat", "message": "你的追问内容"}} + +模块名可选: +- resume:主表(个人信息,包含 name、email、mobileNumber、city、wechatNumber、portfolioUrl、skills、certificates、summary、avatarUrl) +- education:教育经历 +- work:工作经历 +- internship:实习经历 +- project:项目经历 +- competition:竞赛经历 +只返回 JSON,不要其他内容。""" + +AGENT_MODULE_EDIT_PROMPT = """你是一个简历编辑助手。根据修改要求,修改简历的指定模块。 + +【目标岗位】 +{job_title} + +【修改要求】 +{module_instruction} + +【模块数据结构】 +{module_schema} + +【当前模块数据】 +{module_data} + +规则: +1. 严格按照修改要求操作,可以增删改 +2. 未要求修改的记录保持不变 +3. 不要编造用户简历中不存在的内容 +4. 保持原文格式和结构 +5. description 字段是 [{{"id": "xxx", "text": "xxx"}}] 格式:修改时保留原 id 只改 text,新增段落生成随机8位字符串作为 id,删除段落直接从数组中移除 +6. 新增记录时按照模块数据结构生成完整字段,id 使用随机8位字符串 +7. 返回修改后的完整模块数据(JSON 格式,与输入格式一致)""" + +# 各模块数据结构定义(传入 prompt 的 module_schema) +MODULE_SCHEMAS: dict[str, str] = { + "resume": '{ "avatarUrl": "string", "name": "string", "email": "string", "mobileNumber": "string", "city": "string", "wechatNumber": "string", "portfolioUrl": "string", "skills": ["string"], "certificates": ["string"], "summary": "string" }', + "education": '[{ "id": "string(8位)", "school": "string", "major": "string", "degree": "大专/本科/硕士/博士", "studyType": "全日制/非全日制", "startDate": "2023.09", "endDate": "2024.06", "description": [{"id": "string(8位)", "text": "string"}] }]', + "work": '[{ "id": "string(8位)", "companyName": "string", "position": "string", "startDate": "2023.06", "endDate": "2023.09", "description": [{"id": "string(8位)", "text": "string"}] }]', + "internship": '[{ "id": "string(8位)", "companyName": "string", "position": "string", "startDate": "2023.06", "endDate": "2023.09", "description": [{"id": "string(8位)", "text": "string"}] }]', + "project": '[{ "id": "string(8位)", "companyName": "string", "projectName": "string", "role": "string", "startDate": "2023.06", "endDate": "2023.09", "description": [{"id": "string(8位)", "text": "string"}] }]', + "competition": '[{ "id": "string(8位)", "competitionName": "string", "award": "string", "awardDate": "2023.07", "description": [{"id": "string(8位)", "text": "string"}] }]', +} diff --git a/app/api/skill_gap.py b/app/api/skill_gap.py new file mode 100644 index 0000000..eb47498 --- /dev/null +++ b/app/api/skill_gap.py @@ -0,0 +1,74 @@ +"""岗位简历技能差距分析 + 定制简历接口""" + +from fastapi import APIRouter + +from app.core.context import RequestContext +from app.core.database import get_db +from app.schemas.skill_gap import SkillGapParam, CustomizeResumeParam, AiEditParam, CustomizeResume +from app.services.skill_gap_service import SkillGapService + +router = APIRouter(prefix="/job", tags=["岗位简历"]) + + +@router.post("/skill-gap", summary="技能差距分析") +async def skill_gap(param: SkillGapParam): + """根据岗位技能标签和用户简历,分析技能差距""" + user_id = RequestContext.user_id.get() + async for session in get_db(): + service = SkillGapService(session) + result = await service.analyze_skill_gap(user_id, param.job_id) + return result + + +@router.post("/customize-resume", summary="生成定制简历") +async def generate_customize_resume(param: CustomizeResumeParam): + """根据差距分析结果,AI 生成定制简历""" + user_id = RequestContext.user_id.get() + async for session in get_db(): + service = SkillGapService(session) + await service.generate_customize_resume( + user_id, param.job_id, param.resume_id, + param.optimize_modules, param.add_skills, + ) + return {"success": True} + + +@router.get("/customize-resume", summary="查询定制简历") +async def get_customize_resume(): + """查询当前用户的定制简历""" + user_id = RequestContext.user_id.get() + async for session in get_db(): + service = SkillGapService(session) + result = await service.get_customize_resume(user_id) + return result + + +@router.put("/customize-resume", summary="修改定制简历") +async def update_customize_resume(data: CustomizeResume): + """手动编辑定制简历(整体覆盖)""" + user_id = RequestContext.user_id.get() + async for session in get_db(): + service = SkillGapService(session) + await service.update_customize_resume(user_id, data.model_dump(by_alias=True)) + + +@router.post("/customize-resume/rollback", summary="回滚定制简历") +async def rollback_customize_resume(): + """回滚到上一版本的定制简历""" + user_id = RequestContext.user_id.get() + async for session in get_db(): + service = SkillGapService(session) + await service.rollback_customize_resume(user_id) + + +@router.post("/customize-resume/ai-edit", summary="AI对话编辑定制简历") +async def ai_edit_customize_resume(param: AiEditParam): + """AI 对话式编辑定制简历""" + user_id = RequestContext.user_id.get() + # 短事务:查岗位信息 + async for session in get_db(): + service = SkillGapService(session) + result = await service.ai_edit_customize_resume( + user_id, param.job_id, param.instruction, param.chat_history, + ) + return result diff --git a/app/main.py b/app/main.py index 66aaa9e..dc203e2 100644 --- a/app/main.py +++ b/app/main.py @@ -33,10 +33,12 @@ app.add_middleware( from app.api.health import router as health_router from app.api.resume import router as resume_router from app.api.resume_diagnose import router as resume_diagnose_router +from app.api.skill_gap import router as skill_gap_router app.include_router(health_router) app.include_router(resume_router) app.include_router(resume_diagnose_router) +app.include_router(skill_gap_router) # ============================== if __name__ == "__main__": diff --git a/app/models/job.py b/app/models/job.py new file mode 100644 index 0000000..8ad2832 --- /dev/null +++ b/app/models/job.py @@ -0,0 +1,22 @@ +"""岗位表(bg_job,只读) + +Python 端仅读取岗位信息用于技能差距分析,表结构由 Java 端管理。 +""" + +from typing import Optional + +from sqlalchemy import BigInteger, String, Text, JSON +from sqlalchemy.orm import Mapped, mapped_column + +from app.core.database import Base + + +class Job(Base): + """岗位表 bg_job(只读)""" + __tablename__ = "bg_job" + + id: Mapped[int] = mapped_column(BigInteger, primary_key=True) + title: Mapped[Optional[str]] = mapped_column(String(128), nullable=True, comment="岗位名称") + skill_tags: Mapped[Optional[list]] = mapped_column(JSON, nullable=True, comment="技能标签列表") + description: Mapped[Optional[str]] = mapped_column(Text, nullable=True, comment="岗位描述") + requirement: Mapped[Optional[str]] = mapped_column(Text, nullable=True, comment="岗位要求") diff --git a/app/schemas/__init__.py b/app/schemas/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/app/schemas/__init__.py @@ -0,0 +1 @@ + diff --git a/app/schemas/skill_gap.py b/app/schemas/skill_gap.py new file mode 100644 index 0000000..c7a76e3 --- /dev/null +++ b/app/schemas/skill_gap.py @@ -0,0 +1,108 @@ +"""技能差距分析 + 定制简历 Schema + +请求参数 Param、响应 Dto、Redis 缓存模型。 +字段命名使用 camelCase alias,与前端 JSON 对齐。 +""" + +from pydantic import BaseModel, Field + + +# ===== 请求参数 ===== + +class SkillGapParam(BaseModel): + job_id: int = Field(..., alias="jobId") + + +class CustomizeResumeParam(BaseModel): + job_id: int = Field(..., alias="jobId") + resume_id: int = Field(..., alias="resumeId") + optimize_modules: list[str] = Field(..., alias="optimizeModules") + add_skills: list[str] = Field(default_factory=list, alias="addSkills") + + +class AiEditParam(BaseModel): + job_id: int = Field(..., alias="jobId") + instruction: str = Field(...) + chat_history: list[dict] = Field(default_factory=list, alias="chatHistory") + + +# ===== Redis 缓存子模型 ===== + +class _AliasModel(BaseModel): + """带 alias 的基类,允许同时用 alias 和字段名赋值""" + model_config = {"populate_by_name": True} + + +class Paragraph(_AliasModel): + id: str = Field(...) + text: str = Field(default="") + + +class ResumeProfile(_AliasModel): + avatar_url: str = Field(default="", alias="avatarUrl") + name: str = Field(default="") + email: str = Field(default="") + mobile_number: str = Field(default="", alias="mobileNumber") + city: str = Field(default="") + wechat_number: str = Field(default="", alias="wechatNumber") + portfolio_url: str = Field(default="", alias="portfolioUrl") + skills: list[str] = Field(default_factory=list) + certificates: list[str] = Field(default_factory=list) + summary: str = Field(default="") + + +class Education(_AliasModel): + id: str = Field(default="") + school: str = Field(default="") + major: str = Field(default="") + degree: str = Field(default="") + study_type: str = Field(default="", alias="studyType") + start_date: str = Field(default="", alias="startDate") + end_date: str = Field(default="", alias="endDate") + description: list[Paragraph] = Field(default_factory=list) + + +class Work(_AliasModel): + id: str = Field(default="") + company_name: str = Field(default="", alias="companyName") + position: str = Field(default="") + start_date: str = Field(default="", alias="startDate") + end_date: str = Field(default="", alias="endDate") + description: list[Paragraph] = Field(default_factory=list) + + +class Internship(_AliasModel): + id: str = Field(default="") + company_name: str = Field(default="", alias="companyName") + position: str = Field(default="") + start_date: str = Field(default="", alias="startDate") + end_date: str = Field(default="", alias="endDate") + description: list[Paragraph] = Field(default_factory=list) + + +class Project(_AliasModel): + id: str = Field(default="") + company_name: str = Field(default="", alias="companyName") + project_name: str = Field(default="", alias="projectName") + role: str = Field(default="") + start_date: str = Field(default="", alias="startDate") + end_date: str = Field(default="", alias="endDate") + description: list[Paragraph] = Field(default_factory=list) + + +class Competition(_AliasModel): + id: str = Field(default="") + competition_name: str = Field(default="", alias="competitionName") + award: str = Field(default="") + award_date: str = Field(default="", alias="awardDate") + description: list[Paragraph] = Field(default_factory=list) + + +class CustomizeResume(_AliasModel): + """定制简历 Redis 缓存结构""" + resume: ResumeProfile = Field(default_factory=ResumeProfile) + education: list[Education] = Field(default_factory=list) + work: list[Work] = Field(default_factory=list) + internship: list[Internship] = Field(default_factory=list) + project: list[Project] = Field(default_factory=list) + competition: list[Competition] = Field(default_factory=list) diff --git a/app/services/skill_gap_service.py b/app/services/skill_gap_service.py new file mode 100644 index 0000000..60c8eaa --- /dev/null +++ b/app/services/skill_gap_service.py @@ -0,0 +1,355 @@ +"""技能差距分析 + 定制简历 Service + +岗位技能差距分析 → 定制简历生成/查询/编辑/回滚 → AI 对话式编辑。 +依赖:skill_gap_analyzer(AI引擎) +使用表:bg_job(读)、bg_user_resume + 5张子表(读) +存储:Redis(定制简历 + 回滚数据) +""" + +import asyncio +import json +import random +import string + +from sqlalchemy import select, desc +from sqlalchemy.ext.asyncio import AsyncSession + +from app.ai.skill_gap_analyzer.analyzer import ( + analyze_skill_gap, optimize_summary, optimize_module, + plan_edit, execute_module_edit, +) +from app.ai.skill_gap_analyzer.prompts import MODULE_SCHEMAS +from app.core.logger import log +from app.core.redis import redis_client +from app.schemas.skill_gap import ( + CustomizeResume, ResumeProfile, Education, Work, Internship, Project, Competition, Paragraph, +) +from app.models.job import Job +from app.models.user_resume import UserResume +from app.models.user_resume_competition import UserResumeCompetition +from app.models.user_resume_education import UserResumeEducation +from app.models.user_resume_internship import UserResumeInternship +from app.models.user_resume_project import UserResumeProject +from app.models.user_resume_work import UserResumeWork + +# Redis 常量 +CUSTOMIZE_RESUME_KEY_PREFIX = "customize:resume:" +CUSTOMIZE_RESUME_EXPIRE = 12 * 60 * 60 # 12小时 +CUSTOMIZE_RESUME_ROLLBACK_KEY_PREFIX = "customize:resume:rollback:" +CUSTOMIZE_RESUME_ROLLBACK_EXPIRE = 30 * 60 # 30分钟 + +_CHARS = string.ascii_letters + string.digits + + +def _rand_id() -> str: + """生成随机8位字符串标识""" + return "".join(random.choices(_CHARS, k=8)) + + +def _build_paragraphs(description: list[dict] | None) -> list[Paragraph]: + """将数据库 description [{id, text}] 转为 Paragraph 列表,id 用随机8位替换""" + if not description: + return [] + return [Paragraph(id=_rand_id(), text=item.get("text", "")) for item in description if isinstance(item, dict)] + + +def _build_resume_json(resume: UserResume, edu_list, work_list, intern_list, proj_list, comp_list) -> str: + """拼装简历 JSON 字符串供 AI 使用""" + data = { + "skills": resume.skills or [], + "certificates": resume.certificates or [], + "summary": resume.summary or "", + "targetPosition": resume.target_position or "", + } + if edu_list: + data["education"] = [{"school": r.school, "major": r.major, "degree": r.degree, "description": r.description} for r in edu_list] + if work_list: + data["work"] = [{"companyName": r.company_name, "position": r.position, "description": r.description} for r in work_list] + if intern_list: + data["internship"] = [{"companyName": r.company_name, "position": r.position, "description": r.description} for r in intern_list] + if proj_list: + data["project"] = [{"companyName": r.company_name, "projectName": r.project_name, "role": r.role, "description": r.description} for r in proj_list] + if comp_list: + data["competition"] = [{"competitionName": r.competition_name, "award": r.award, "description": r.description} for r in comp_list] + return json.dumps(data, ensure_ascii=False) + + +class SkillGapService: + + def __init__(self, session: AsyncSession): + self.session = session + + # ===== 差距分析 ===== + + async def analyze_skill_gap(self, user_id: int, job_id: int) -> dict: + """差距分析完整流程:查简历 → 查岗位 → AI分析 → 计算匹配分""" + # 1. 自动选择简历 + resume = await self._pick_resume(user_id) + # 2. 查岗位 + job = await self._get_job(job_id) + skill_tags: list[str] = job.skill_tags or [] + # 3. skill_tags 为空 → 满分 + if not skill_tags: + return self._gap_result(10.0, job, resume, []) + # 4. 查子表拼 AI 输入 + edu, work, intern, proj, comp = await self._load_sub_tables(resume.id) + resume_json = _build_resume_json(resume, edu, work, intern, proj, comp) + # 5. AI 分析 + missing = await analyze_skill_gap(skill_tags, resume_json) + # 6. 计算匹配分 + score = round((len(skill_tags) - len(missing)) / len(skill_tags) * 10, 1) + return self._gap_result(score, job, resume, missing) + + @staticmethod + def _gap_result(score: float, job: Job, resume: UserResume, missing: list[str]) -> dict: + return { + "score": score, + "job": {"jobId": str(job.id), "title": job.title, "skillTags": job.skill_tags or []}, + "resume": {"resumeId": str(resume.id), "resumeName": resume.resume_name or "", "targetPosition": resume.target_position or ""}, + "missingSkills": missing, + } + + # ===== 生成定制简历 ===== + + async def generate_customize_resume(self, user_id: int, job_id: int, resume_id: int, + optimize_modules: list[str], add_skills: list[str]) -> None: + """生成定制简历:查数据 → 并发AI优化 → 存Redis""" + if not optimize_modules: + raise ValueError("请至少选择一个优化模块") + # 1. 查简历 + 岗位 + resume = await self._get_resume(resume_id, user_id) + job = await self._get_job(job_id) + edu_rows, work_rows, intern_rows, proj_rows, comp_rows = await self._load_sub_tables(resume.id) + # 2. 组装基础定制简历 + cr = self._build_customize_resume(resume, edu_rows, work_rows, intern_rows, proj_rows, comp_rows) + # 3. 并发 AI 优化 + tasks = [] + job_desc = f"{job.description or ''}\n{job.requirement or ''}" + if "summary" in optimize_modules: + tasks.append(("summary", optimize_summary(job.title or "", add_skills, resume.summary or ""))) + if "experience" in optimize_modules: + for module_name, rows_json in self._experience_tasks(cr, job.title or "", job_desc): + tasks.append((module_name, optimize_module(job.title or "", job_desc, rows_json))) + # 执行并发 + if tasks: + keys = [t[0] for t in tasks] + results = await asyncio.gather(*[t[1] for t in tasks], return_exceptions=True) + for key, result in zip(keys, results): + if isinstance(result, Exception): + log.warning(f"定制简历优化[{key}]失败: {result}") + continue + self._apply_optimize_result(cr, key, result) + # 4. skills 追加(纯内存操作) + if "skills" in optimize_modules and add_skills: + existing = set(cr.resume.skills) + cr.resume.skills.extend([s for s in add_skills if s not in existing]) + # 5. 存 Redis + await self._save_customize_resume(user_id, cr) + + @staticmethod + def _experience_tasks(cr: CustomizeResume, job_title: str, job_desc: str) -> list[tuple[str, str]]: + """构建各子表的 AI 优化任务列表""" + result = [] + for name, items in [("education", cr.education), ("work", cr.work), ("internship", cr.internship), + ("project", cr.project), ("competition", cr.competition)]: + if items: + result.append((name, json.dumps([item.model_dump(by_alias=True) for item in items], ensure_ascii=False))) + return result + + @staticmethod + def _apply_optimize_result(cr: CustomizeResume, key: str, result) -> None: + """将 AI 优化结果应用到定制简历""" + if key == "summary" and isinstance(result, str): + cr.resume.summary = result + elif key == "education" and isinstance(result, list): + cr.education = [Education.model_validate(item) for item in result] + elif key == "work" and isinstance(result, list): + cr.work = [Work.model_validate(item) for item in result] + elif key == "internship" and isinstance(result, list): + cr.internship = [Internship.model_validate(item) for item in result] + elif key == "project" and isinstance(result, list): + cr.project = [Project.model_validate(item) for item in result] + elif key == "competition" and isinstance(result, list): + cr.competition = [Competition.model_validate(item) for item in result] + + # ===== 查询 / 修改 / 回滚 ===== + + async def get_customize_resume(self, user_id: int) -> dict | None: + """查询定制简历""" + key = f"{CUSTOMIZE_RESUME_KEY_PREFIX}{user_id}" + data = await redis_client.get(key) + if not data: + return None + return CustomizeResume.model_validate_json(data).model_dump(by_alias=True) + + async def update_customize_resume(self, user_id: int, data: dict) -> None: + """手动编辑定制简历(整体覆盖)""" + cr = CustomizeResume.model_validate(data) + await self._save_customize_resume(user_id, cr) + + async def rollback_customize_resume(self, user_id: int) -> None: + """回滚定制简历""" + rollback_key = f"{CUSTOMIZE_RESUME_ROLLBACK_KEY_PREFIX}{user_id}" + data = await redis_client.get(rollback_key) + if not data: + raise ValueError("没有可回滚的版本") + key = f"{CUSTOMIZE_RESUME_KEY_PREFIX}{user_id}" + await redis_client.set(key, data, ex=CUSTOMIZE_RESUME_EXPIRE) + await redis_client.delete(rollback_key) + + # ===== AI 对话编辑 ===== + + async def ai_edit_customize_resume(self, user_id: int, job_id: int, + instruction: str, chat_history: list[dict]) -> dict: + """AI 对话式编辑定制简历""" + # 1. 取当前定制简历 + key = f"{CUSTOMIZE_RESUME_KEY_PREFIX}{user_id}" + raw = await redis_client.get(key) + if not raw: + raise ValueError("定制简历不存在,请先生成") + cr = CustomizeResume.model_validate_json(raw) + resume_json = cr.model_dump_json(by_alias=True) + # 2. 查岗位 + job = await self._get_job(job_id) + # 3. 规划 AI + history_str = json.dumps(chat_history, ensure_ascii=False) if chat_history else "无" + plan = await plan_edit(job.title or "", resume_json, history_str, instruction) + if not plan: + return {"type": "message", "message": "抱歉,我没有理解你的意思,请再描述一下。"} + if plan.get("action") == "chat": + return {"type": "message", "message": plan.get("message", "请再描述一下你的需求。")} + # 4. 按模块并发执行修改 + modules = plan.get("modules", []) + if not modules: + return {"type": "message", "message": plan.get("message", "请再描述一下你的需求。")} + edit_tasks = [] + for m in modules: + mod_name = m.get("module", "") + mod_instr = m.get("instruction", "") + schema = MODULE_SCHEMAS.get(mod_name, "") + mod_data = self._get_module_data(cr, mod_name) + edit_tasks.append((mod_name, execute_module_edit(job.title or "", mod_instr, schema, mod_data))) + keys = [t[0] for t in edit_tasks] + results = await asyncio.gather(*[t[1] for t in edit_tasks], return_exceptions=True) + # 5. 合并结果 + for mod_key, result in zip(keys, results): + if isinstance(result, Exception): + log.warning(f"AI编辑模块[{mod_key}]失败: {result}") + continue + if result is None: + continue + self._apply_edit_result(cr, mod_key, result) + # 6. 保存回滚 + 新版本 + rollback_key = f"{CUSTOMIZE_RESUME_ROLLBACK_KEY_PREFIX}{user_id}" + await redis_client.set(rollback_key, raw, ex=CUSTOMIZE_RESUME_ROLLBACK_EXPIRE) + await self._save_customize_resume(user_id, cr) + label = plan.get("updatedModulesLabel", "简历内容") + return {"type": "updated", "message": f"完成!已更新:{label}"} + + @staticmethod + def _get_module_data(cr: CustomizeResume, mod_name: str) -> str: + """获取指定模块的 JSON 数据""" + if mod_name == "resume": + return cr.resume.model_dump_json(by_alias=True) + mapping = {"education": cr.education, "work": cr.work, "internship": cr.internship, + "project": cr.project, "competition": cr.competition} + items = mapping.get(mod_name, []) + return json.dumps([item.model_dump(by_alias=True) for item in items], ensure_ascii=False) + + @staticmethod + def _apply_edit_result(cr: CustomizeResume, mod_name: str, result) -> None: + """将 AI 编辑结果应用到定制简历""" + try: + if mod_name == "resume" and isinstance(result, dict): + cr.resume = ResumeProfile.model_validate(result) + elif mod_name == "education" and isinstance(result, list): + cr.education = [Education.model_validate(item) for item in result] + elif mod_name == "work" and isinstance(result, list): + cr.work = [Work.model_validate(item) for item in result] + elif mod_name == "internship" and isinstance(result, list): + cr.internship = [Internship.model_validate(item) for item in result] + elif mod_name == "project" and isinstance(result, list): + cr.project = [Project.model_validate(item) for item in result] + elif mod_name == "competition" and isinstance(result, list): + cr.competition = [Competition.model_validate(item) for item in result] + except Exception as e: + log.warning(f"应用AI编辑结果[{mod_name}]失败: {e}") + + # ===== 内部工具方法 ===== + + async def _pick_resume(self, user_id: int) -> UserResume: + """自动选择简历:先查默认,再查最新""" + result = await self.session.execute( + select(UserResume).where(UserResume.user_id == user_id, UserResume.is_default == 1) + .order_by(desc(UserResume.update_time)).limit(1)) + resume = result.scalar_one_or_none() + if not resume: + result = await self.session.execute( + select(UserResume).where(UserResume.user_id == user_id) + .order_by(desc(UserResume.update_time)).limit(1)) + resume = result.scalar_one_or_none() + if not resume: + raise ValueError("请先创建简历") + return resume + + async def _get_resume(self, resume_id: int, user_id: int) -> UserResume: + """查指定简历""" + result = await self.session.execute( + select(UserResume).where(UserResume.id == resume_id, UserResume.user_id == user_id)) + resume = result.scalar_one_or_none() + if not resume: + raise ValueError("简历不存在") + return resume + + async def _get_job(self, job_id: int) -> Job: + """查岗位""" + result = await self.session.execute(select(Job).where(Job.id == job_id)) + job = result.scalar_one_or_none() + if not job: + raise ValueError("岗位不存在") + return job + + async def _load_sub_tables(self, resume_id: int): + """查询简历5张子表""" + edu = (await self.session.execute(select(UserResumeEducation).where(UserResumeEducation.resume_id == resume_id))).scalars().all() + work = (await self.session.execute(select(UserResumeWork).where(UserResumeWork.resume_id == resume_id))).scalars().all() + intern = (await self.session.execute(select(UserResumeInternship).where(UserResumeInternship.resume_id == resume_id))).scalars().all() + proj = (await self.session.execute(select(UserResumeProject).where(UserResumeProject.resume_id == resume_id))).scalars().all() + comp = (await self.session.execute(select(UserResumeCompetition).where(UserResumeCompetition.resume_id == resume_id))).scalars().all() + return edu, work, intern, proj, comp + + def _build_customize_resume(self, resume: UserResume, edu_rows, work_rows, + intern_rows, proj_rows, comp_rows) -> CustomizeResume: + """从数据库记录组装 CustomizeResume""" + profile = ResumeProfile( + avatarUrl=resume.avatar_url or "", name=resume.name or "", email=resume.email or "", + mobileNumber=resume.mobile_number or "", city=resume.city or "", + wechatNumber=resume.wechat_number or "", portfolioUrl=resume.portfolio_url or "", + skills=resume.skills or [], certificates=resume.certificates or [], + summary=resume.summary or "", + ) + return CustomizeResume( + resume=profile, + education=[Education(id=_rand_id(), school=r.school or "", major=r.major or "", + degree=r.degree or "", studyType=r.study_type or "", + startDate=r.start_date or "", endDate=r.end_date or "", + description=_build_paragraphs(r.description)) for r in edu_rows], + work=[Work(id=_rand_id(), companyName=r.company_name or "", position=r.position or "", + startDate=r.start_date or "", endDate=r.end_date or "", + description=_build_paragraphs(r.description)) for r in work_rows], + internship=[Internship(id=_rand_id(), companyName=r.company_name or "", position=r.position or "", + startDate=r.start_date or "", endDate=r.end_date or "", + description=_build_paragraphs(r.description)) for r in intern_rows], + project=[Project(id=_rand_id(), companyName=r.company_name or "", projectName=r.project_name or "", + role=r.role or "", startDate=r.start_date or "", endDate=r.end_date or "", + description=_build_paragraphs(r.description)) for r in proj_rows], + competition=[Competition(id=_rand_id(), competitionName=r.competition_name or "", award=r.award or "", + awardDate=r.award_date or "", + description=_build_paragraphs(r.description)) for r in comp_rows], + ) + + @staticmethod + async def _save_customize_resume(user_id: int, cr: CustomizeResume) -> None: + """存定制简历到 Redis""" + key = f"{CUSTOMIZE_RESUME_KEY_PREFIX}{user_id}" + await redis_client.set(key, cr.model_dump_json(by_alias=True), ex=CUSTOMIZE_RESUME_EXPIRE)