Skip to main content

Documentation Index

Fetch the complete documentation index at: https://nvd-54.mintlify.app/llms.txt

Use this file to discover all available pages before exploring further.

在你完成 LangGraph 智能体的原型开发后,自然的下一步是添加测试。本指南涵盖了编写单元测试时可以使用的一些有用模式。 请注意,本指南是 LangGraph 特定的,涵盖了自定义结构图的场景——如果你刚刚入门,请查看使用 LangChain 内置 createAgent测试

前提条件

首先,确保你已安装 vitest
$ npm install -D vitest

入门

由于许多 LangGraph 智能体依赖于状态,一个有用的模式是在每个需要使用图的测试之前创建图,然后在测试中使用新的检查点实例进行编译。 下面的示例展示了这如何与一个简单的线性图配合使用,该图依次通过 node1node2。每个节点更新单个状态键 my_key
import { test, expect } from 'vitest';
import {
  StateGraph,
  StateSchema,
  START,
  END,
  MemorySaver,
} from '@langchain/langgraph';
import * as z from "zod";

const State = new StateSchema({
  my_key: z.string(),
});

const createGraph = () => {
  return new StateGraph(State)
    .addNode('node1', (state) => ({ my_key: 'hello from node1' }))
    .addNode('node2', (state) => ({ my_key: 'hello from node2' }))
    .addEdge(START, 'node1')
    .addEdge('node1', 'node2')
    .addEdge('node2', END);
};

test('basic agent execution', async () => {
  const uncompiledGraph = createGraph();
  const checkpointer = new MemorySaver();
  const compiledGraph = uncompiledGraph.compile({ checkpointer });
  const result = await compiledGraph.invoke(
    { my_key: 'initial_value' },
    { configurable: { thread_id: '1' } }
  );
  expect(result.my_key).toBe('hello from node2');
});

测试单个节点和边

编译后的 LangGraph 智能体通过 graph.nodes 暴露对每个单独节点的引用。你可以利用这一点来测试智能体中的单个节点。请注意,这将绕过编译图时传入的任何检查点:
import { test, expect } from 'vitest';
import {
  StateGraph,
  START,
  END,
  MemorySaver,
  StateSchema,
} from '@langchain/langgraph';
import * as z from "zod";

const State = new StateSchema({
  my_key: z.string(),
});

const createGraph = () => {
  return new StateGraph(State)
    .addNode('node1', (state) => ({ my_key: 'hello from node1' }))
    .addNode('node2', (state) => ({ my_key: 'hello from node2' }))
    .addEdge(START, 'node1')
    .addEdge('node1', 'node2')
    .addEdge('node2', END);
};

test('individual node execution', async () => {
  const uncompiledGraph = createGraph();
  // 在此示例中将被忽略
  const checkpointer = new MemorySaver();
  const compiledGraph = uncompiledGraph.compile({ checkpointer });
  // 仅调用节点 1
  const result = await compiledGraph.nodes['node1'].invoke(
    { my_key: 'initial_value' },
  );
  expect(result.my_key).toBe('hello from node1');
});

部分执行

对于由较大图组成的智能体,你可能希望测试智能体中的部分执行路径,而不是整个端到端流程。在某些情况下,将这些部分重构为子图可能在语义上更合理,你可以像正常方式一样单独调用它们。 但是,如果你不想更改智能体图的整体结构,可以使用 LangGraph 的持久化机制来模拟智能体在所需部分开始前暂停的状态,并在所需部分结束后再次暂停。步骤如下:
  1. 使用检查点编译你的智能体(内存检查点 MemorySaver 足以用于测试)。
  2. 调用智能体的 update_state 方法,将 asNode 参数设置为你想要开始测试的节点之前的节点名称。
  3. 使用与更新状态时相同的 thread_id 调用智能体,并将 interruptBefore 参数设置为你想要停止的节点名称。
以下是一个仅执行线性图中第二和第三个节点的示例:
import { test, expect } from 'vitest';
import {
  StateGraph,
  StateSchema,
  START,
  END,
  MemorySaver,
} from '@langchain/langgraph';
import * as z from "zod";

const State = new StateSchema({
  my_key: z.string(),
});

const createGraph = () => {
  return new StateGraph(State)
    .addNode('node1', (state) => ({ my_key: 'hello from node1' }))
    .addNode('node2', (state) => ({ my_key: 'hello from node2' }))
    .addNode('node3', (state) => ({ my_key: 'hello from node3' }))
    .addNode('node4', (state) => ({ my_key: 'hello from node4' }))
    .addEdge(START, 'node1')
    .addEdge('node1', 'node2')
    .addEdge('node2', 'node3')
    .addEdge('node3', 'node4')
    .addEdge('node4', END);
};

test('partial execution from node2 to node3', async () => {
  const uncompiledGraph = createGraph();
  const checkpointer = new MemorySaver();
  const compiledGraph = uncompiledGraph.compile({ checkpointer });
  await compiledGraph.updateState(
    { configurable: { thread_id: '1' } },
    // 传入节点 2 的状态——模拟节点 1 结束时的状态
    { my_key: 'initial_value' },
    // 更新已保存的状态,就像它来自节点 1
    // 执行将从节点 2 恢复
    'node1',
  );
  const result = await compiledGraph.invoke(
    // 通过传入 null 恢复执行
    null,
    {
      configurable: { thread_id: '1' },
      // 在节点 3 之后停止,这样节点 4 不会运行
      interruptAfter: ['node3']
    },
  );
  expect(result.my_key).toBe('hello from node3');
});