跳到内容

如何本地测试 LangGraph 应用

本指南假定您已正确设置 LangGraph 应用,并具有正确的配置文件和相应的已编译图,并且您拥有正确的 LangChain API 密钥。

本地测试确保没有 Python 依赖项的错误或冲突,并确认配置文件已正确指定。

设置

安装 LangGraph CLI 包

pip install -U "langgraph-cli[inmem]"

确保您拥有 API 密钥,您可以从 LangSmith UI(设置 > API 密钥)创建密钥。 这是验证您拥有 LangGraph Cloud 访问权限所必需的。 在将密钥保存到安全位置后,将以下行放在您的 .env 文件中

LANGSMITH_API_KEY = *********

启动 API 服务器

安装 CLI 后,您可以运行以下命令来启动 API 服务器以进行本地测试

langgraph dev

这将本地启动 LangGraph API 服务器。 如果运行成功,您应该看到类似以下内容

准备就绪!

内存模式

langgraph dev 命令以内存模式启动 LangGraph 服务器。 此模式适用于开发和测试目的。 对于生产用途,您应该部署可以访问持久存储后端的 LangGraph 服务器。

如果您想使用持久存储后端测试您的应用程序,您可以使用 langgraph up 命令而不是 langgraph dev。 您需要在您的机器上安装 docker 才能使用此命令。

与服务器交互

我们现在可以使用 LangGraph SDK 与 API 服务器交互。 首先,我们需要启动我们的客户端,选择我们的助手(在本例中是我们称为“agent”的图,请确保选择您希望测试的正确助手)。

您可以通过传递身份验证或设置环境变量来初始化。

使用身份验证初始化

from langgraph_sdk import get_client

# only pass the url argument to get_client() if you changed the default port when calling langgraph dev
client = get_client(url=<DEPLOYMENT_URL>,api_key=<LANGSMITH_API_KEY>)
# Using the graph deployed with the name "agent"
assistant_id = "agent"
thread = await client.threads.create()
import { Client } from "@langchain/langgraph-sdk";

// only set the apiUrl if you changed the default port when calling langgraph dev
const client = new Client({ apiUrl: <DEPLOYMENT_URL>, apiKey: <LANGSMITH_API_KEY> });
// Using the graph deployed with the name "agent"
const assistantId = "agent";
const thread = await client.threads.create();
curl --request POST \
  --url <DEPLOYMENT_URL>/threads \
  --header 'Content-Type: application/json'
  --header 'x-api-key: <LANGSMITH_API_KEY>'

使用环境变量初始化

如果您在您的环境中设置了 LANGSMITH_API_KEY,则无需显式传递身份验证到客户端

from langgraph_sdk import get_client

# only pass the url argument to get_client() if you changed the default port when calling langgraph dev
client = get_client()
# Using the graph deployed with the name "agent"
assistant_id = "agent"
thread = await client.threads.create()
import { Client } from "@langchain/langgraph-sdk";

// only set the apiUrl if you changed the default port when calling langgraph dev
const client = new Client();
// Using the graph deployed with the name "agent"
const assistantId = "agent";
const thread = await client.threads.create();
curl --request POST \
  --url <DEPLOYMENT_URL>/threads \
  --header 'Content-Type: application/json'

现在我们可以调用我们的图以确保它正常工作。 确保更改输入以匹配您图的正确架构。

input = {"messages": [{"role": "user", "content": "what's the weather in sf"}]}
async for chunk in client.runs.stream(
    thread["thread_id"],
    assistant_id,
    input=input,
    stream_mode="updates",
):
    print(f"Receiving new event of type: {chunk.event}...")
    print(chunk.data)
    print("\n\n")
const input = { "messages": [{ "role": "user", "content": "what's the weather in sf"}] }

const streamResponse = client.runs.stream(
  thread["thread_id"],
  assistantId,
  {
    input: input,
    streamMode: "updates",
  }
);
for await (const chunk of streamResponse) {
  console.log(`Receiving new event of type: ${chunk.event}...`);
  console.log(chunk.data);
  console.log("\n\n");
}
curl --request POST \
 --url <DEPLOYMENT_URL>/threads/<THREAD_ID>/runs/stream \
 --header 'Content-Type: application/json' \
 --data "{
   \"assistant_id\": \"agent\",
   \"input\": {\"messages\": [{\"role\": \"human\", \"content\": \"what's the weather in sf\"}]},
   \"stream_mode\": [
     \"events\"
   ]
 }" | \
 sed 's/\r$//' | \
 awk '
 /^event:/ {
     if (data_content != "") {
         print data_content "\n"
     }
     sub(/^event: /, "Receiving event of type: ", $0)
     printf "%s...\n", $0
     data_content = ""
 }
 /^data:/ {
     sub(/^data: /, "", $0)
     data_content = $0
 }
 END {
     if (data_content != "") {
         print data_content "\n"
     }
 }
 ' 

如果您的图正常工作,您应该在控制台中看到您的图输出。 当然,您可能需要以更多方式测试您的图,有关您可以使用 SDK 发送的完整命令列表,请参阅 PythonJS/TS 参考文档。