Files
ailine/app/agent/prompts.py

38 lines
1.8 KiB
Python
Raw Normal View History

2026-04-20 14:05:57 +08:00
# app/prompts.py
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
2026-04-20 14:05:57 +08:00
from langchain_core.tools import BaseTool
2026-04-20 14:05:57 +08:00
def create_system_prompt(tools: list = None) -> ChatPromptTemplate:
"""
2026-04-20 14:05:57 +08:00
创建系统提示模板可选择动态注入工具描述
"""
2026-04-20 14:05:57 +08:00
tools_section = ""
if tools:
tool_descs = []
for tool in tools:
# 提取工具名称和描述的第一行
name = getattr(tool, 'name', tool.__name__)
desc = (tool.description or "").split('\n')[0]
tool_descs.append(f"- {name}: {desc}")
tools_section = "\n".join(tool_descs)
system_template = (
"你是一个个人生活助手和数据分析助手,请使用中文交流。\n\n"
"【用户背景信息】\n"
"以下是对当前用户的已知信息和长期记忆,你必须优先采纳并在回答中体现:\n"
"{memory_context}\n"
"若包含姓名、偏好等个人信息,请自然融入回应(例如称呼名字、提及偏好)。\n\n"
"【可用工具与使用规则】\n"
2026-04-20 14:05:57 +08:00
f"{tools_section}\n"
"工具调用时请直接返回所需参数,无需额外说明。\n\n"
"【回答要求(必须遵守)】\n"
2026-04-17 01:26:05 +08:00
"1. 回答必须简洁、直接。\n"
2026-04-20 14:05:57 +08:00
"2. 如果你认为该问题需要进行深入的推理或思考,请务必将你的思维链或推理过程用 `<think>` 和 `</think>` 标签包裹起来,放在回答的最前面。\n"
2026-04-17 01:26:05 +08:00
"3. 优先利用已知用户信息进行个性化回复。\n"
"4. 若无信息可依,礼貌询问或提供通用帮助。"
)
return ChatPromptTemplate.from_messages([
("system", system_template),
MessagesPlaceholder(variable_name="messages")
2026-04-20 14:05:57 +08:00
])