添加岗位简历诊断

This commit is contained in:
zk
2026-04-09 18:22:10 +08:00
parent fd675e05cc
commit c001ba8146
11 changed files with 1487 additions and 10 deletions
+1
View File
@@ -0,0 +1 @@
+137
View File
@@ -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
+115
View File
@@ -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"}] }]',
}
+74
View File
@@ -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
+2
View File
@@ -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__":
+22
View File
@@ -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="岗位要求")
+1
View File
@@ -0,0 +1 @@
+108
View File
@@ -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)
+355
View File
@@ -0,0 +1,355 @@
"""技能差距分析 + 定制简历 Service
岗位技能差距分析 → 定制简历生成/查询/编辑/回滚 → AI 对话式编辑。
依赖:skill_gap_analyzerAI引擎)
使用表: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)