添加AI 优化接口
This commit is contained in:
@@ -9,7 +9,7 @@ from langchain_core.output_parsers import StrOutputParser
|
||||
from langchain_core.prompts import ChatPromptTemplate
|
||||
|
||||
from app.ai.models import LLM
|
||||
from app.ai.resume_diagnoser.prompts import DIAGNOSE_MODULE_PROMPT, SUMMARY_PROMPT
|
||||
from app.ai.resume_diagnoser.prompts import DIAGNOSE_MODULE_PROMPT, SUMMARY_PROMPT, POLISH_PROMPT
|
||||
from app.core.logger import log
|
||||
|
||||
|
||||
@@ -62,6 +62,45 @@ async def generate_summary(grade: str, urgent_total: int, important_total: int,
|
||||
return "简历诊断已完成,请查看各模块的详细诊断结果。"
|
||||
|
||||
|
||||
_polish_chain = (
|
||||
ChatPromptTemplate.from_messages([("system", POLISH_PROMPT), ("human", "请开始优化。")])
|
||||
| LLM.CLAUDE_SONNET_4.create(temperature=0.3)
|
||||
| StrOutputParser()
|
||||
)
|
||||
|
||||
|
||||
async def polish_content(module_type: str, reference_content: list[dict] | str | None,
|
||||
user_content: list[str], is_summary: bool) -> list[str]:
|
||||
"""润色用户编辑后的文本"""
|
||||
ref_text = ""
|
||||
if reference_content:
|
||||
if isinstance(reference_content, list):
|
||||
ref_text = "\n".join(
|
||||
item.get("text", "") if isinstance(item, dict) else str(item)
|
||||
for item in reference_content
|
||||
)
|
||||
else:
|
||||
ref_text = str(reference_content)
|
||||
if not ref_text:
|
||||
ref_text = "无"
|
||||
|
||||
inp = {
|
||||
"module_type": module_type,
|
||||
"reference_content": ref_text,
|
||||
"user_content": "\n".join(user_content),
|
||||
"summary_constraint": "- 注意:此模块只能输出一个段落,数组只能有一个元素" if is_summary else "",
|
||||
}
|
||||
try:
|
||||
raw = await _polish_chain.ainvoke(inp)
|
||||
result = _parse_json(raw)
|
||||
if isinstance(result, list):
|
||||
return [str(item) for item in result]
|
||||
return [str(result)]
|
||||
except Exception as e:
|
||||
log.warning(f"AI润色失败: {e}")
|
||||
return user_content
|
||||
|
||||
|
||||
async def _safe_invoke(task: dict) -> dict:
|
||||
"""单条记录诊断,失败返回空结果"""
|
||||
raw = ""
|
||||
|
||||
Reference in New Issue
Block a user