Skip to content

测试(Test) ​

在为 LangGraph 智能体构建原型后,自然的下一步是添加测试。本指南介绍了编写单元测试时可以使用的一些实用模式。

请注意,本指南特定于 LangGraph,并涵盖具有自定义结构的图的场景——如果你刚刚入门,请查阅使用 LangChain 内置 createAgent 的测试指南。

前置准备 ​

首先,确保你已安装 vitest:

bash
npm install -D vitest

入门 ​

因为许多 LangGraph 智能体依赖状态,一个有用的模式是在使用它的每个测试之前创建你的图,然后在测试内使用新的检查点保存器实例对其进行编译。

以下示例展示了这如何与通过 node1 和 node2 进行的简单线性图配合工作。每个节点更新单个状态键 my_key:

typescript
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。你可以利用这一点来测试智能体内的单个节点。请注意,这将绕过编译图时传递的任何检查点保存器:

typescript
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');
});

部分执行(Partial execution) ​

对于由较大图组成的智能体,你可能希望测试智能体内的部分执行路径,而不是整个端到端流程。在某些情况下,在语义上将这些部分重构为子图可能是有意义的,你可以照常单独调用它们。

但是,如果你不想更改智能体图的整体结构,可以使用 LangGraph 的持久化机制来模拟一种状态:即你的智能体在所需部分的开始之前暂停,并在所需部分的结尾处再次暂停。步骤如下:

  1. 使用检查点保存器编译你的智能体(内存中检查点保存器 MemorySaver 足以进行测试)。
  2. 调用智能体的 update_state 方法,将 asNode 参数设置为你想要开始测试的节点之前的节点名称。
  3. 使用更新状态时使用的相同 thread_id 和设置为要停止的节点名称的 interruptAfter 参数来调用智能体。

以下示例仅执行线性图中的第二个和第三个节点:

typescript
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');
});