处理兼容问题
This commit is contained in:
@@ -1,8 +1,10 @@
|
|||||||
"""简历并行提取:将完整简历文本拆分为5个AI任务并行提取"""
|
"""简历并行提取:将完整简历文本拆分为5个AI任务并行提取"""
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import re
|
||||||
|
|
||||||
from langchain_core.output_parsers import JsonOutputParser
|
from json_repair import repair_json
|
||||||
|
from langchain_core.output_parsers import StrOutputParser
|
||||||
from langchain_core.prompts import ChatPromptTemplate
|
from langchain_core.prompts import ChatPromptTemplate
|
||||||
|
|
||||||
from app.ai.models import LLM
|
from app.ai.models import LLM
|
||||||
@@ -13,12 +15,19 @@ from app.ai.resume_extractor.prompts import (
|
|||||||
from app.core.logger import log
|
from app.core.logger import log
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_json(text: str) -> dict:
|
||||||
|
"""解析 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)
|
||||||
|
|
||||||
|
|
||||||
def _build_chain(prompt: str):
|
def _build_chain(prompt: str):
|
||||||
"""构建单个提取链:prompt → LLM → JSON解析"""
|
"""构建单个提取链:prompt → LLM → 文本输出"""
|
||||||
return (
|
return (
|
||||||
ChatPromptTemplate.from_messages([("system", prompt), ("human", "{text}")])
|
ChatPromptTemplate.from_messages([("system", prompt), ("human", "{text}")])
|
||||||
| LLM.JIAYU_CLAUDE_SONNET_4_5.create(temperature=0)
|
| LLM.JIAYU_CLAUDE_SONNET_4_5.create(temperature=0)
|
||||||
| JsonOutputParser()
|
| StrOutputParser()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -55,7 +64,8 @@ async def extract_all(text: str) -> dict:
|
|||||||
async def _safe_invoke(chain, inp: dict, label: str):
|
async def _safe_invoke(chain, inp: dict, label: str):
|
||||||
"""单个链调用,失败返回空"""
|
"""单个链调用,失败返回空"""
|
||||||
try:
|
try:
|
||||||
return await chain.ainvoke(inp)
|
raw = await chain.ainvoke(inp)
|
||||||
|
return _parse_json(raw)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.warning(f"AI提取[{label}]失败: {e}")
|
log.warning(f"AI提取[{label}]失败: {e}")
|
||||||
return {} if "个人信息" in label else []
|
return {} if "个人信息" in label else []
|
||||||
|
|||||||
Reference in New Issue
Block a user