Skip to main content
本指南将引导您创建一个具有规划、文件系统工具和子 agent 能力的 deep agent。您将构建一个可以进行研究并编写报告的研究 agent。

先决条件

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

步骤 1:安装依赖

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

步骤 2:设置 API 密钥

export ANTHROPIC_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.
`;

const agent = createDeepAgent({
  tools: [internetSearch],
  systemPrompt: researchInstructions,
});

步骤 5:运行 agent

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);

它是如何工作的?

您的 deep agent 自动:
  1. 规划其方法:使用内置的 write_todos 工具分解研究任务。
  2. 进行研究:通过调用 internet_search 工具收集信息。
  3. 管理上下文:使用文件系统工具(write_fileread_file)卸载大型搜索结果。
  4. 生成子 agent:根据需要将复杂的子任务委派给专门的子 agent。
  5. 综合报告:将发现编译成连贯的响应。

示例

有关您可以使用 Deep Agents 构建的 agent、模式和应用程序,请参阅 示例

流式传输

Deep agents 具有内置的 流式传输 功能,用于通过 LangGraph 获取 agent 执行的实时更新。 这允许您逐步观察输出,并审查和调试 agent 和子 agent 的工作,例如工具调用、工具结果和 LLM 响应。

下一步

现在您已经构建了第一个 deep agent:
  • 自定义您的 agent:了解 自定义选项,包括自定义系统提示、工具和子 agent。
  • 添加长期记忆:启用跨对话的 持久记忆
  • 部署到生产:了解 LangGraph 应用程序的 部署选项