抽象json提取风格
This commit is contained in:
@@ -5,9 +5,7 @@
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import re
|
||||
|
||||
from json_repair import repair_json
|
||||
from langchain_core.output_parsers import StrOutputParser
|
||||
from langchain_core.prompts import ChatPromptTemplate
|
||||
|
||||
@@ -17,13 +15,7 @@ from app.ai.skill_gap_analyzer.prompts import (
|
||||
AGENT_PLAN_PROMPT, AGENT_MODULE_EDIT_PROMPT, AGENT_MODULE_ADD_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)
|
||||
from app.tool.json_helper import parse_llm_json
|
||||
|
||||
|
||||
# ===== 差距分析 =====
|
||||
@@ -39,7 +31,7 @@ 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)
|
||||
result = parse_llm_json(raw)
|
||||
if isinstance(result, list):
|
||||
return [s for s in result if isinstance(s, str) and s in skill_tags]
|
||||
return skill_tags # 解析异常降级:全部标记缺失
|
||||
@@ -85,7 +77,7 @@ async def optimize_module(job_title: str, job_description: str, module_data: str
|
||||
"job_title": job_title, "job_description": job_description or "",
|
||||
"original_module_data": module_data,
|
||||
})
|
||||
return _parse_json(raw)
|
||||
return parse_llm_json(raw)
|
||||
except Exception as e:
|
||||
log.warning(f"AI优化经历模块失败: {e}")
|
||||
return None
|
||||
@@ -109,7 +101,7 @@ async def plan_edit(job_title: str, job_description: str, resume_json: str,
|
||||
"resume_json": resume_json,
|
||||
"chat_history": chat_history, "instruction": instruction,
|
||||
})
|
||||
result = _parse_json(raw)
|
||||
result = parse_llm_json(raw)
|
||||
return result if isinstance(result, dict) else None
|
||||
except Exception as e:
|
||||
log.warning(f"AI规划失败: {e}")
|
||||
@@ -135,7 +127,7 @@ async def execute_record_edit(job_title: str, job_description: str, instruction:
|
||||
"instruction": instruction, "chat_history": chat_history,
|
||||
"module_schema": module_schema, "record_data": record_data,
|
||||
})
|
||||
return _parse_json(raw)
|
||||
return parse_llm_json(raw)
|
||||
except Exception as e:
|
||||
log.warning(f"AI单条记录修改失败: {e}")
|
||||
return None
|
||||
@@ -159,7 +151,7 @@ async def execute_record_add(job_title: str, job_description: str, instruction:
|
||||
"instruction": instruction, "chat_history": chat_history,
|
||||
"module_schema": module_schema,
|
||||
})
|
||||
result = _parse_json(raw)
|
||||
result = parse_llm_json(raw)
|
||||
return result if isinstance(result, dict) else None
|
||||
except Exception as e:
|
||||
log.warning(f"AI新增记录失败: {e}")
|
||||
|
||||
Reference in New Issue
Block a user