跳到内容

如何在本地测试 LangGraph 应用

本指南假设你已正确设置 LangGraph 应用,包括适当的配置文件和相应的编译图,并且拥有有效的 LangChain API 密钥。

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

设置

安装 LangGraph CLI 包

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

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

LANGSMITH_API_KEY = *********

启动 API 服务器

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

langgraph dev

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

准备就绪!

内存模式

langgraph dev 命令以内存模式启动 LangGraph Server。此模式适用于开发和测试目的。对于生产环境,你应该部署 LangGraph Server 并访问持久化存储后端。

如果你想使用持久化存储后端测试应用,可以使用 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 参考文档。