52 lines
1.9 KiB
Python
52 lines
1.9 KiB
Python
"""求职助手 Agent 对话 AI 引擎
|
|
|
|
构造 prompt + 调 LLM + 解析返回。
|
|
依赖:LLM 枚举、job_agent/prompts、parse_llm_json
|
|
"""
|
|
|
|
from app.ai.job_agent.prompts import SYSTEM_PROMPT
|
|
from app.ai.model_config import JobAgentModel
|
|
from app.core.logger import log
|
|
from app.tool.json_helper import parse_llm_json
|
|
|
|
|
|
async def agent_chat(resume_text: str, message: str, history: list[dict],
|
|
job_categories: list[str], regions: list[str],
|
|
industries: list[str]) -> dict:
|
|
"""求职助手对话
|
|
1. 构造 system prompt 2. 拼 messages 3. 调 LLM 4. 解析返回
|
|
"""
|
|
# 1. 构造 system prompt
|
|
system_content = SYSTEM_PROMPT.format(
|
|
resume_text=resume_text,
|
|
job_categories="、".join(job_categories) if job_categories else "未设置",
|
|
regions="、".join(regions) if regions else "未设置",
|
|
industries="、".join(industries) if industries else "未设置",
|
|
)
|
|
|
|
# 2. 拼 messages
|
|
messages = [("system", system_content)]
|
|
for msg in history:
|
|
messages.append((msg["role"], msg["content"]))
|
|
messages.append(("human", message))
|
|
|
|
# 3. 调 LLM
|
|
try:
|
|
result = await JobAgentModel.CHAT.ainvoke(messages)
|
|
raw = result.content
|
|
except Exception as e:
|
|
log.error(f"求职助手AI调用失败: {e}")
|
|
return {"message": "抱歉,我暂时无法回复,请稍后再试。", "tool": None, "toolParams": None}
|
|
|
|
# 4. 解析返回
|
|
try:
|
|
parsed = parse_llm_json(raw)
|
|
return {
|
|
"message": parsed.get("message", ""),
|
|
"tool": parsed.get("tool"),
|
|
"toolParams": parsed.get("toolParams"),
|
|
}
|
|
except Exception as e:
|
|
log.warning(f"求职助手AI返回解析失败, raw={raw}, error={e}")
|
|
return {"message": raw.strip() if raw else "抱歉,我暂时无法回复。", "tool": None, "toolParams": None}
|