一、何时使用skills
1.在有大量上下文时使用技能,减少系统提示中的标记数量。
2.利用技能将能力整合成更大的行动,提供超越单一工具描述的额外背景。
3.如果代理无法访问文件系统,可以使用工具。
二、使用注意
1.提示词尽可能详细描述何时使用
2.skills定义的功能尽可能明确
三、代码示例
from deepagents import create_deep_agent
from langchain_openai import ChatOpenAI
from langgraph.checkpoint.memory import MemorySaver
from deepagents.backends.utils import create_file_data
import os
def llm():
# 大模型配置不变
api_key = os.getenv("ali_api_key")
model = "qwen-plus"
base_url = "https://dashscope.aliyuncs.com/compatible-mode/v1"
llm_model = ChatOpenAI(api_key=api_key, base_url=base_url, model=model)
return llm_model
llm = llm()
checkpointer = MemorySaver()
# 替换:读取本地的 SKILL.md(而非远程下载)
# 定义本地文件路径
local_skill_path = "./skills/langgraph/SKILL.md"
# 读取文件内容
if os.path.exists(local_skill_path):
with open(local_skill_path, "r", encoding="utf-8") as f:
skill_content = f.read() # 读取本地文件内容
else:
raise FileNotFoundError(f"未找到本地文件:{local_skill_path}")
# 核心作用
# 虚拟文件系统路径(远程),随便定义,将真实文件内容临时存储到里面,可以实现解耦
# create_file_data格式转换
skills_files = {
"/skills/langgraph/SKILL.md": create_file_data(skill_content)
}
# 最终是本地+虚拟,但相同的内容,虚拟优先级更高
# 更加高级的封装,甚至封装了langgraph
agent = create_deep_agent(
model=llm,
skills=["./skills/"], # 占位
system_prompt="""
你是一个严格的智能助手,必须遵守以下规则:
当用户询问langgraph相关问题时,必须严格按照SKILL.md文档内容进行执行并回答,不要自己添加任何回答内容
文件中的describe字段描述了该文档可以回答的问题,
输出的结果应该是整理好的最终答案。
""",
checkpointer=checkpointer
)
result = agent.invoke(
{
"messages": [ {"role": "user", "content": "langgraph?",}],
"files": skills_files # 虚拟的
},config={"configurable": {"thread_id": "12345"}},
)
# 打印智能体的回答
print(result)
print(result["messages"][-1].content)