LangChain Deep Agents + Qwen 实战教程(非常详细),从零打造专属AI助手,收藏这一篇就够了!
一文掌握 Skills 开发让你的 Agent 拥有超能力概述想让你的 AI Agent 更智能、更强大LangChain Deep Agents 是一个革命性的 AI Agent 框架它能够✨自主规划将复杂任务分解为可执行步骤智能记忆跨对话持久化上下文灵活扩展通过 Skills 机制无限扩展能力开箱即用完美适配阿里云 Qwen 系列模型本文将手把手教你如何开发和使用Skills技能让你的 Agent 从「能用」变成「好用」一、什么是 SkillsSkills 是一种扩展 Deep Agent 能力的机制它允许你将复杂的指令、上下文和资源打包成可复用的模块。Skills 的组成部分一个完整的 Skill 包含以下内容SKILL.md文件包含技能的指令和元数据必需附加脚本如 Python 脚本等可选参考文档如 API 文档、使用说明等可选资源文件如模板、配置文件等可选Skills 目录结构示例skills/├── langgraph-docs│ └── SKILL.md└── arxiv_search ├── SKILL.md └── arxiv_search.py # 用于搜索 arXiv 的代码二、SKILL.md 文件格式SKILL.md文件是 Skill 的核心它使用 YAML frontmatter 定义元数据后面跟随 Markdown 格式的指令内容。基础格式---name: skill-namedescription: 技能描述用于匹配用户请求---# 技能名称## Overview技能概述## Instructions详细的执行指令完整示例---name: langgraph-docsdescription: Use this skill for requests related to LangGraph in order to fetch relevant documentation to provide accurate, up-to-date guidance.license: MITcompatibility: Requires internet access for fetching documentation URLsmetadata: author: langchain version: 1.0 allowed-tools: fetch_url---# langgraph-docs## OverviewThis skill explains how to access LangGraph Python documentation to help answer questions and guide implementation.## Instructions### 1. Fetch the documentation indexUse the fetch_url tool to read the following URL:https://docs.langchain.com/llms.txtThis provides a structured list of all available documentation with descriptions.### 2. Select relevant documentationBased on the question, identify 2-4 most relevant documentation URLs from the index. Prioritize:- Specific how-to guides for implementation questions- Core concept pages for understanding questions- Tutorials for end-to-end examples- Reference docs for API details### 3. Fetch selected documentationUse the fetch_url tool to read the selected documentation URLs.### 4. Provide accurate guidanceAfter reading the documentation, complete the users request.元数据字段说明字段说明是否必需name技能名称用于标识是description技能描述Agent 用于匹配用户请求最大 1024 字符是license许可证信息否compatibility兼容性要求说明否metadata自定义元数据如 author、version、allowed-tools 等否限制条件description字段超过 1024 字符会被截断SKILL.md文件大小不能超过 10 MB超过会被跳过三、Skills 的工作原理Agent 使用 Skills 的流程如下匹配Match当用户请求到达时Agent 检查是否有 Skill 的description与任务匹配读取Read如果匹配成功Agent 读取完整的SKILL.md文件执行ExecuteAgent 按照 Skill 中的指令执行并根据需要访问支持文件脚本、模板、参考文档等四、在代码中使用 Skills环境变量配置使用阿里云 Qwen 模型export OPENAI_API_KEYyour-dashscope-api-keyexport OPENAI_API_BASEhttps://dashscope.aliyuncs.com/compatible-mode/v1import osfrom deepagents import create_deep_agentfrom langgraph.checkpoint.memory import MemorySaverfrom deepagents.backends.filesystem import FilesystemBackend# 配置阿里云 DashScope API如果未设置环境变量os.environ.setdefault(OPENAI_API_KEY, your-dashscope-api-key)os.environ.setdefault(OPENAI_API_BASE, https://dashscope.aliyuncs.com/compatible-mode/v1)# Checkpointer 是 human-in-the-loop 所必需的checkpointer MemorySaver()# 使用 FilesystemBackendSkills 需要存放在本地磁盘agent create_deep_agent( modelopenai:qwen3-max, # 使用阿里云 Qwen 模型 backendFilesystemBackend(root_dir/Users/user/project), skills[/Users/user/project/skills/], interrupt_on{ write_file: True, # 默认approve, edit, reject read_file: False, # 无需中断 edit_file: True # 默认approve, edit, reject }, checkpointercheckpointer, # 必需)result agent.invoke( {messages: [{role: user, content: What is langgraph?}]}, config{configurable: {thread_id: 12345}},)print(result)使用说明使用FilesystemBackend时Skills 从本地磁盘相对于root_dir的路径加载需要确保SKILL.md文件已存在于指定目录。五、Skills 优先级当在skills参数中指定多个路径时后加载的 Skill 优先级更高# 如果两个路径都包含名为 web-search 的 Skill# 来自 /skills/project/ 的版本将生效最后加载agent create_deep_agent( skills[/skills/user/, /skills/project/], ...)CLI 模式下的默认搜索顺序[ user-home/.deepagents/{agent}/skills/, user-home/.agents/skills/, project-root/.deepagents/skills/, project-root/.agents/skills/,]六、子代理Subagents中的 Skills通用子代理通用子代理会自动继承主 Agent 的 Skills无需额外配置。自定义子代理自定义子代理不会继承主 Agent 的 Skills需要单独指定from deepagents import create_deep_agent# 定义自定义子代理research_subagent { name: researcher, description: Research assistant with specialized skills, system_prompt: You are a researcher., tools: [web_search], skills: [/skills/research/, /skills/web-search/], # 子代理专属 Skills}agent create_deep_agent( modelopenai:qwen3-max, # 使用阿里云 Qwen 模型 skills[/skills/main/], # 主 Agent 和通用子代理使用这些 Skills subagents[research_subagent], # 研究员子代理只使用自己的 Skills)七、Skills vs. Memory特性SkillsMemory (AGENTS.md)用途扩展能力、提供指令存储偏好、记忆触发方式基于 description 匹配持久化存储内容类型指令、脚本、模板用户偏好、项目知识八、何时使用 Skills vs. Tools使用 Skills 的场景需要大量上下文信息以减少 system prompt 中的 token 数量需要将多个能力打包成更大的操作并提供超出单个工具描述的额外上下文需要提供详细的分步指令使用 Tools 的场景Agent 无法访问文件系统只需要简单的函数调用不需要复杂的上下文或指令九、快速开始示例安装依赖pip install deepagents tavily-python设置 API Keysexport OPENAI_API_KEYyour-dashscope-api-keyexport OPENAI_API_BASEhttps://dashscope.aliyuncs.com/compatible-mode/v1export TAVILY_API_KEYyour-tavily-api-key完整代码示例import osfrom typing import Literalfrom tavily import TavilyClientfrom deepagents import create_deep_agentfrom langgraph.checkpoint.memory import MemorySaverfrom deepagents.backends.filesystem import FilesystemBackend# 配置阿里云 DashScope API如果未设置环境变量os.environ.setdefault(OPENAI_API_KEY, your-dashscope-api-key)os.environ.setdefault(OPENAI_API_BASE, https://dashscope.aliyuncs.com/compatible-mode/v1)# 创建搜索工具tavily_client TavilyClient(api_keyos.environ[TAVILY_API_KEY])def internet_search( query: str, max_results: int 5, topic: Literal[general, news, finance] general, include_raw_content: bool False,): Run a web search return tavily_client.search( query, max_resultsmax_results, include_raw_contentinclude_raw_content, topictopic, )# 定义 system promptresearch_instructions You are an expert researcher. Your job is to conduct thorough research and then write a polished report.You have access to an internet search tool as your primary means of gathering information.## internet_searchUse this to run an internet search for a given query. You can specify the max number of results to return, the topic, and whether raw content should be included.# Checkpointer 用于支持 human-in-the-loopcheckpointer MemorySaver()# 创建 Deep Agent带 Skills 支持agent create_deep_agent( modelopenai:qwen3-max, # 使用阿里云 Qwen 模型 tools[internet_search], system_promptresearch_instructions, backendFilesystemBackend(root_dir./project), # 使用本地文件系统 skills[./project/skills/], # 指定 你自己的 Skills 目录 checkpointercheckpointer,)# 运行 Agentresult agent.invoke( {messages: [{role: user, content: What is langgraph?}]}, config{configurable: {thread_id: 12345}},)# 打印结果print(result[messages][-1].content)说明使用FilesystemBackend从本地文件系统加载 SkillsSkills 需要存放在./project/skills/目录下每个 Skill 是一个子目录包含SKILL.md文件例如./project/skills/langgraph-docs/SKILL.md十、Deep Agent 的核心能力能力说明规划与任务分解使用write_todos工具将复杂任务分解为可管理的步骤上下文管理通过ls、read_file、write_file、edit_file等工具管理大量上下文可插拔后端支持内存状态、本地磁盘、持久化存储、沙箱或自定义后端子代理生成使用task工具将复杂子任务委派给专门的子代理长期记忆通过 Memory Store 在对话和线程之间持久化记忆十一、支持的模型Deep Agents 支持多种 LLM 提供商推荐使用阿里云 Qwen 系列模型性价比高、中文友好提供商模型示例推荐度阿里云 DashScopeopenai:qwen3-maxopenai:qwen-turboopenai:qwen-plus⭐⭐⭐⭐⭐Anthropicanthropic:claude-sonnet-4-6⭐⭐⭐⭐OpenAIopenai:gpt-4o⭐⭐⭐⭐Googlegoogle_genai:gemini-3.1-pro-preview⭐⭐⭐Fireworksfireworks:accounts/fireworks/models/qwen3p5-397b-a17b⭐⭐⭐Ollamaollama:qwen2.5⭐⭐⭐配置阿里云 Qwen 模型export OPENAI_API_KEYyour-dashscope-api-keyexport OPENAI_API_BASEhttps://dashscope.aliyuncs.com/compatible-mode/v1学AI大模型的正确顺序千万不要搞错了2026年AI风口已来各行各业的AI渗透肉眼可见超多公司要么转型做AI相关产品要么高薪挖AI技术人才机遇直接摆在眼前有往AI方向发展或者本身有后端编程基础的朋友直接冲AI大模型应用开发转岗超合适就算暂时不打算转岗了解大模型、RAG、Prompt、Agent这些热门概念能上手做简单项目也绝对是求职加分王给大家整理了超全最新的AI大模型应用开发学习清单和资料手把手帮你快速入门学习路线:✅大模型基础认知—大模型核心原理、发展历程、主流模型GPT、文心一言等特点解析✅核心技术模块—RAG检索增强生成、Prompt工程实战、Agent智能体开发逻辑✅开发基础能力—Python进阶、API接口调用、大模型开发框架LangChain等实操✅应用场景开发—智能问答系统、企业知识库、AIGC内容生成工具、行业定制化大模型应用✅项目落地流程—需求拆解、技术选型、模型调优、测试上线、运维迭代✅面试求职冲刺—岗位JD解析、简历AI项目包装、高频面试题汇总、模拟面经以上6大模块看似清晰好上手实则每个部分都有扎实的核心内容需要吃透我把大模型的学习全流程已经整理好了抓住AI时代风口轻松解锁职业新可能希望大家都能把握机遇实现薪资/职业跃迁这份完整版的大模型 AI 学习资料已经上传CSDN朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费】