跳到主要内容

文档索引

在以下地址获取完整的文档索引:https://docs.langchain.org.cn/llms.txt

在进一步探索之前,请使用此文件发现所有可用页面。

本指南将引导您创建一个具备规划、文件系统工具和子代理能力的深度代理。您将构建一个能够进行研究并撰写报告的研究代理。
正在使用 AI 编程助手?

先决条件

在开始之前,请确保您拥有模型提供商(例如 Gemini、Anthropic、OpenAI)的 API 密钥。
深度代理需要支持 工具调用 (tool calling) 的模型。请参阅 自定义 部分了解如何配置您的模型。

第 1 步:安装依赖项

npm install deepagents langchain @langchain/core @langchain/tavily
本指南以 Tavily 作为搜索提供商示例,但您也可以替换为任何搜索 API(例如 DuckDuckGo、SerpAPI、Brave Search)。

第 2 步:设置 API 密钥

export GOOGLE_API_KEY="your-api-key"
export TAVILY_API_KEY="your-tavily-api-key"

第 3 步:创建搜索工具

import { tool } from "langchain";
import { TavilySearch } from "@langchain/tavily";
import { z } from "zod";

const internetSearch = tool(
  async ({
    query,
    maxResults = 5,
    topic = "general",
    includeRawContent = false,
  }: {
    query: string;
    maxResults?: number;
    topic?: "general" | "news" | "finance";
    includeRawContent?: boolean;
  }) => {
    const tavilySearch = new TavilySearch({
      maxResults,
      tavilyApiKey: process.env.TAVILY_API_KEY,
      includeRawContent,
      topic,
    });
    return await tavilySearch._call({ query });
  },
  {
    name: "internet_search",
    description: "Run a web search",
    schema: z.object({
      query: z.string().describe("The search query"),
      maxResults: z
        .number()
        .optional()
        .default(5)
        .describe("Maximum number of results to return"),
      topic: z
        .enum(["general", "news", "finance"])
        .optional()
        .default("general")
        .describe("Search topic category"),
      includeRawContent: z
        .boolean()
        .optional()
        .default(false)
        .describe("Whether to include raw content"),
    }),
  },
);

第 4 步:创建深度代理 (Deep Agent)

import { createDeepAgent } from "deepagents";

// System prompt to steer the agent to be an expert researcher
const researchInstructions = `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_search\`

Use 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.
`;
从提供商处选择一个模型。默认情况下,createDeepAgent 使用 claude-sonnet-4-6。若要使用其他提供商,请传入 model 字符串 — 查看 建议模型 列表以获取完整详情。
const agent = createDeepAgent({
  model: "google-genai:gemini-3.1-pro-preview",
  tools: [internetSearch],
  systemPrompt: researchInstructions,
});

第 5 步:运行代理

const result = await agent.invoke({
  messages: [{ role: "user", content: "What is langgraph?" }],
});

// Print the agent's response
console.log(result.messages[result.messages.length - 1].content);

它是如何工作的?

您的深度代理会自动
  1. 规划其方案:使用内置的 write_todos 工具来拆解研究任务。
  2. 进行研究:通过调用 internet_search 工具来收集信息。
  3. 管理上下文:通过使用文件系统工具(write_file, read_file)来卸载大型搜索结果。
  4. 生成子智能体:根据需要将复杂的子任务委派给专门的子智能体。
  5. 综合报告:将调查结果汇编成连贯的回复。

示例

有关您可以使用深度代理构建的代理、模式和应用程序,请参阅 示例

流式处理

深度代理内置了 流式传输 (streaming) 功能,可通过 LangGraph 从代理执行中获取实时更新。这使您能够逐步观察输出,并检查和调试代理及子代理的工作,例如工具调用、工具结果和 LLM 响应。

后续步骤

现在您已经构建了第一个深度代理
  • 自定义您的代理:了解 自定义选项,包括自定义系统提示词、工具和子代理。
  • 添加长期记忆:启用跨对话的 持久化记忆
  • 部署到生产环境:了解深度代理的 部署选项

© . This site is unofficial and not affiliated with LangChain, Inc.