84 lines
3.6 KiB
Python
84 lines
3.6 KiB
Python
"""Nova Chat Service
|
|
|
|
主要功能:查询简历数据 + 查询岗位(可选),调用 AI 模块完成对话。
|
|
依赖:resume_loader(简历统一查询)、nova_chat AI 模块
|
|
使用表:bg_user_resume + 5张子表(通过 resume_loader 查询)、bg_job(查岗位,可选)
|
|
"""
|
|
|
|
from typing import Optional
|
|
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.ai.nova_chat.chat import nova_chat
|
|
from app.models.job import Job
|
|
from app.services.resume_loader import ResumeDetail, load_resume_detail
|
|
|
|
|
|
class NovaChatService:
|
|
|
|
def __init__(self, session: AsyncSession):
|
|
self.session = session
|
|
|
|
async def chat(self, user_id: int, resume_id: int, message: str,
|
|
history: list[dict], job_id: Optional[int] = None) -> str:
|
|
"""Nova 对话:查简历 → 查岗位(可选) → 序列化 → 调 AI"""
|
|
detail = await load_resume_detail(self.session, resume_id, user_id)
|
|
resume_text = self._build_resume_text(detail)
|
|
job_context = await self._build_job_context(job_id) if job_id else "用户当前未浏览具体岗位"
|
|
return await nova_chat(resume_text, message, history, job_context)
|
|
|
|
async def _build_job_context(self, job_id: int) -> str:
|
|
"""查岗位并序列化为文本"""
|
|
result = await self.session.execute(select(Job).where(Job.id == job_id))
|
|
job = result.scalar_one_or_none()
|
|
if not job:
|
|
return "用户当前未浏览具体岗位"
|
|
parts = []
|
|
if job.title:
|
|
parts.append(f"岗位名称:{job.title}")
|
|
if job.description:
|
|
parts.append(f"岗位职责:{job.description}")
|
|
if job.requirement:
|
|
parts.append(f"任职要求:{job.requirement}")
|
|
if job.skill_tags:
|
|
parts.append(f"技能标签:{'、'.join(job.skill_tags)}")
|
|
return "\n".join(parts) if parts else "用户当前未浏览具体岗位"
|
|
|
|
@staticmethod
|
|
def _build_resume_text(detail: ResumeDetail) -> str:
|
|
"""将简历数据序列化为文本供 AI 使用"""
|
|
resume = detail.resume
|
|
parts = []
|
|
if resume.name:
|
|
parts.append(f"姓名:{resume.name}")
|
|
if resume.target_position:
|
|
parts.append(f"目标岗位:{resume.target_position}")
|
|
if resume.skills:
|
|
parts.append(f"技能:{'、'.join(resume.skills)}")
|
|
if resume.certificates:
|
|
parts.append(f"证书:{'、'.join(resume.certificates)}")
|
|
if resume.summary:
|
|
parts.append(f"个人概述:{resume.summary}")
|
|
if detail.education:
|
|
parts.append("教育经历:")
|
|
for r in detail.education:
|
|
parts.append(f" - {r.school or ''} {r.major or ''} {r.degree or ''}")
|
|
if detail.work:
|
|
parts.append("工作经历:")
|
|
for r in detail.work:
|
|
parts.append(f" - {r.company_name or ''} {r.position or ''}")
|
|
if detail.internship:
|
|
parts.append("实习经历:")
|
|
for r in detail.internship:
|
|
parts.append(f" - {r.company_name or ''} {r.position or ''}")
|
|
if detail.project:
|
|
parts.append("项目经历:")
|
|
for r in detail.project:
|
|
parts.append(f" - {r.project_name or ''} {r.role or ''}")
|
|
if detail.competition:
|
|
parts.append("竞赛经历:")
|
|
for r in detail.competition:
|
|
parts.append(f" - {r.competition_name or ''} {r.award or ''}")
|
|
return "\n".join(parts) if parts else "暂无简历信息"
|