Can you provide an example using deepagentsjs?

HEAD
945b362build(deps): bump fast-xml-builder from 1.1.5 to 1.2.0 (#539)
This post might have stale content, as HEAD is 40 commits ahead.
Avatar of glimis2
glimis2
posted

给一个使用deepagentjs的例子

Avatar of anthropic/claude-haiku-4.5
Claude Haiku 4.5
commented

完美!现在我有足够的信息了。让我给用户一个全面的回答,包括几个不同复杂度的例子。

##DeepAgents 使用例子

我给你整理了几个 deepagentsjs 的使用例子,从简单到复杂:

###1. 最简单的例子 - 基本用法

import { createDeepAgent } from "deepagents";

const agent = createDeepAgent();

const result = await agent.invoke({
  messages: [
    {
      role: "user",
      content: "Research LangGraph and write a summary in summary.md",
    },
  ],
});

这个例子展示了 deepagents 的核心特性:

  • 开箱即用 - 不需要配置就能运行
  • 内置工具 - 自动包含文件操作(read_file, write_file)、计划管理(write_todos)等

###2. 自定义配置例子

import { ChatOpenAI } from "@langchain/openai";
import { createDeepAgent } from "deepagents";

const agent = createDeepAgent({
  model: new ChatOpenAI({ model: "gpt-4", temperature: 0 }),
  tools: [myCustomTool],           // 添加自定义工具
  systemPrompt: "You are a research assistant.",  // 自定义系统提示词
});

const result = await agent.invoke({
  messages: [
    {
      role: "user",
      content: "Do some research and write a report",
    },
  ],
});

###3. 带流式输出的例子

import { createDeepAgent } from "deepagents";

const agent = createDeepAgent({
  systemPrompt: "You are a helpful research assistant",
  subagents: [
    {
      name: "researcher",
      description: "Researches a topic in depth",
      systemPrompt: "You are a thorough researcher.",
    },
  ],
});

// 流式处理响应
for await (const [namespace, chunk] of await agent.stream(
  {
    messages: [
      { role: "user", content: "Research quantum computing advances" },
    ],
  },
  {
    streamMode: "updates",
    subgraphs: true,
  },
)) {
  if (namespace.length > 0) {
    console.log(`[subagent: ${namespace.join("|")}]`);
  } else {
    console.log("[main agent]");
  }
  console.log(chunk);
}

###4. 分层代理例子 - DeepAgent as SubAgent

这个例子演示了多层级代理架构,一个 DeepAgent 可以作为另一个 DeepAgent 的子代理:

import { createDeepAgent, type CompiledSubAgent } from "deepagents";
import { tool } from "langchain";
import { z } from "zod";

// 定义工具
const getWeather = tool(
  (input) => `Weather in ${input.location} is sunny and 72°F.`,
  {
    name: "get_weather",
    description: "Get the current weather",
    schema: z.object({
      location: z.string().describe("The city or location"),
    }),
  },
);

const getNews = tool(
  (input) =>
    `Latest news for "${input.topic}":\n` +
    `1. Major breakthrough announced today\n` +
    `2. Experts weigh in on the future`,
  {
    name: "get_news",
    description: "Search for latest news",
    schema: z.object({
      topic: z.string().describe("The topic to search"),
    }),
  },
);

// 第二层:研究 DeepAgent
const researchDeepAgent = createDeepAgent({
  systemPrompt: "You are a research specialist.",
  tools: [getNews],
});

// 第一层:主代理,包含研究代理作为子代理
const mainAgent = createDeepAgent({
  systemPrompt: "You are a helpful assistant that coordinates different capabilities.",
  tools: [getWeather],
  subagents: [
    {
      name: "research-specialist",
      description: "A specialized research agent with news tools",
      runnable: researchDeepAgent,
    } satisfies CompiledSubAgent,
  ],
});

// 调用
const result = await mainAgent.invoke({
  messages: [
    { role: "user", content: "What's the weather in San Francisco?" },
  ],
});

###5. 高级例子 - 研究代理

完整的研究代理例子,包含:

  • 🔍 网络搜索工具 (Tavily)
  • 📝 文件管理
  • ✅ 任务规划
  • 🤖 子代理(研究和评论)

参考文件:examples/research/research-agent.ts

这个例子展示了如何:

  • 创建复杂的多代理工作流
  • 使用文件系统进行工作记录
  • 实现代理之间的协作和批评反馈

###DeepAgents 的核心特性

特性说明
Planningwrite_todos 任务规划和进度追踪
Filesystemread_file, write_file, edit_file, ls, glob, grep
Sub-agents通过 task 工具委托工作,隔离上下文
Smart defaults开箱即用的提示词和中间件
Context management基于文件的工作流管理长期任务
LangGraph Native支持流式处理、持久化等 LangGraph 特性

###快速开始

npm install deepagents
# 或
pnpm add deepagents

你想要深入探索某个特定例子吗?


END OF POST

Can you provide an example using deepagentsjs? — langchain-ai/deepagentsjs