init
This commit is contained in:
+115
@@ -0,0 +1,115 @@
|
||||
"""
|
||||
客户端 AI 大模型封装(OpenAI 兼容接口)
|
||||
功能:
|
||||
1. 职位匹配度评分
|
||||
2. 简历内容整理
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
from openai import OpenAI
|
||||
from loguru import logger
|
||||
|
||||
from .config import client_settings
|
||||
|
||||
|
||||
class MatchResult:
|
||||
def __init__(self, score: int, reason: str):
|
||||
self.score = score
|
||||
self.reason = reason
|
||||
|
||||
def is_worth_applying(self, threshold: int = 60) -> bool:
|
||||
return self.score >= threshold
|
||||
|
||||
|
||||
class AIClient:
|
||||
"""OpenAI 兼容大模型客户端"""
|
||||
|
||||
def __init__(self, api_key: str | None = None, base_url: str | None = None,
|
||||
model: str | None = None):
|
||||
self.api_key = api_key or client_settings.AI_API_KEY or os.environ.get("AI_API_KEY", "")
|
||||
self.base_url = base_url or client_settings.AI_BASE_URL
|
||||
self.model = model or client_settings.AI_MODEL
|
||||
self.client = OpenAI(api_key=self.api_key, base_url=self.base_url)
|
||||
|
||||
def available(self) -> bool:
|
||||
return bool(self.api_key)
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 职位匹配度评分
|
||||
# ------------------------------------------------------------------
|
||||
def score_job(self, jd_text: str, resume_content: str) -> MatchResult:
|
||||
"""根据 JD 与主简历内容评估匹配度"""
|
||||
if not self.available():
|
||||
return MatchResult(50, "AI 未配置,默认放行")
|
||||
|
||||
system_prompt = (
|
||||
"你是专业招聘顾问。根据职位描述(JD)和候选人简历,输出 JSON 评估结果,"
|
||||
"不要输出 JSON 以外的内容。\n"
|
||||
'格式:{"score": <0-100整数>, "reason": "<一句话评价>"}\n'
|
||||
"评分标准:80-100 高度匹配;60-79 基本匹配;40-59 部分匹配;0-39 不匹配。"
|
||||
)
|
||||
user_prompt = f"【职位描述】\n{jd_text}\n\n【候选人简历】\n{resume_content}"
|
||||
|
||||
try:
|
||||
resp = self.client.chat.completions.create(
|
||||
model=self.model,
|
||||
messages=[
|
||||
{"role": "system", "content": system_prompt},
|
||||
{"role": "user", "content": user_prompt},
|
||||
],
|
||||
temperature=0.2,
|
||||
max_tokens=256,
|
||||
)
|
||||
raw = resp.choices[0].message.content.strip()
|
||||
if raw.startswith("```"):
|
||||
raw = raw.split("```")[1]
|
||||
if raw.startswith("json"):
|
||||
raw = raw[4:]
|
||||
data = json.loads(raw)
|
||||
return MatchResult(int(data.get("score", 50)), data.get("reason", ""))
|
||||
except Exception as e:
|
||||
logger.warning(f"AI 评分失败,默认放行:{e}")
|
||||
return MatchResult(50, f"AI 异常:{e}")
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 简历内容整理
|
||||
# ------------------------------------------------------------------
|
||||
def clean_resume(self, raw_text: str) -> str:
|
||||
"""用 AI 将抓取的杂乱简历文本整理为清晰的结构化文本"""
|
||||
if not self.available():
|
||||
return raw_text
|
||||
|
||||
system_prompt = (
|
||||
"你是简历整理助手。将下面抓取的杂乱简历文本整理为清晰、结构化的纯文本简历,"
|
||||
"保留所有关键信息(姓名可省略,联系方式可保留),去除广告和无关内容。"
|
||||
"直接输出整理后的文本,不要加任何解释。"
|
||||
)
|
||||
try:
|
||||
resp = self.client.chat.completions.create(
|
||||
model=self.model,
|
||||
messages=[
|
||||
{"role": "system", "content": system_prompt},
|
||||
{"role": "user", "content": raw_text},
|
||||
],
|
||||
temperature=0.3,
|
||||
max_tokens=2048,
|
||||
)
|
||||
return resp.choices[0].message.content.strip()
|
||||
except Exception as e:
|
||||
logger.warning(f"AI 整理简历失败,返回原文:{e}")
|
||||
return raw_text
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 连通测试
|
||||
# ------------------------------------------------------------------
|
||||
def ping(self) -> bool:
|
||||
try:
|
||||
resp = self.client.chat.completions.create(
|
||||
model=self.model,
|
||||
messages=[{"role": "user", "content": "回复1"}],
|
||||
max_tokens=5,
|
||||
)
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"AI 接口连通失败:{e}")
|
||||
return False
|
||||
Reference in New Issue
Block a user