Skip to content

如何使用 RemoteGraph 与部署进行交互

RemoteGraph 是一个接口,允许您像与常规的本地定义的 LangGraph 图(例如 CompiledGraph)一样与您的 LangGraph 平台部署进行交互。本指南将向您展示如何初始化 RemoteGraph 并与之交互。

初始化图形

在初始化 RemoteGraph 时,您必须始终指定:

  • name:您想要交互的图形名称。这与您在部署的 langgraph.json 配置文件中使用的图形名称相同。
  • api_key:有效的 LangSmith API 密钥。可以设为环境变量(LANGSMITH_API_KEY)或通过 api_key 参数直接传递。如果 LangGraphClient / SyncLangGraphClient 是通过 api_key 参数初始化的,则也可以通过 client / sync_client 参数提供 API 密钥。

此外,您还必须提供以下之一:

  • url:您想要交互的部署的 URL。如果您传递 url 参数,则会使用提供的 URL、头部(如果有提供)和默认配置值(例如超时等)创建同步和异步客户端。
  • client:用于异步与部署交互的 LangGraphClient 实例(例如使用 .astream().ainvoke().aget_state().aupdate_state() 等)。
  • sync_client:用于同步与部署交互的 SyncLangGraphClient 实例(例如使用 .stream().invoke().get_state().update_state() 等)。

注意

如果同时传递 clientsync_client 以及 url 参数,则它们将优先于 url 参数。如果未提供 client / sync_client / url 参数,RemoteGraph 将在运行时抛出 ValueError

使用 URL

from langgraph.pregel.remote import RemoteGraph

url = <DEPLOYMENT_URL>
graph_name = "agent"
remote_graph = RemoteGraph(graph_name, url=url)
import { RemoteGraph } from "@langchain/langgraph/remote";

const url = `<DEPLOYMENT_URL>`;
const graphName = "agent";
const remoteGraph = new RemoteGraph({ graphId: graphName, url });

使用客户端

from langgraph_sdk import get_client, get_sync_client
from langgraph.pregel.remote import RemoteGraph

url = <DEPLOYMENT_URL>
graph_name = "agent"
client = get_client(url=url)
sync_client = get_sync_client(url=url)
remote_graph = RemoteGraph(graph_name, client=client, sync_client=sync_client)
import { Client } from "@langchain/langgraph-sdk";
import { RemoteGraph } from "@langchain/langgraph/remote";

const client = new Client({ apiUrl: `<DEPLOYMENT_URL>` });
const graphName = "agent";
const remoteGraph = new RemoteGraph({ graphId: graphName, client });

调用图形

由于 RemoteGraph 是一个实现与 CompiledGraph 相同方法的 Runnable,您可以像通常与编译图形一样与之交互,即通过调用 .invoke().stream().get_state().update_state() 等(以及它们的异步对应方法)。

异步调用

注意

要异步使用图形,您必须在初始化 RemoteGraph 时提供 urlclient

# 调用图形
result = await remote_graph.ainvoke({
    "messages": [{"role": "user", "content": "旧金山的天气怎么样"}]
})

# 从图形中流式输出
async for chunk in remote_graph.astream({
    "messages": [{"role": "user", "content": "洛杉矶的天气怎么样"}]
}):
    print(chunk)
// 调用图形
const result = await remoteGraph.invoke({
    messages: [{role: "user", content: "旧金山的天气怎么样"}]
})

// 从图形中流式输出
for await (const chunk of await remoteGraph.stream({
    messages: [{role: "user", content: "洛杉矶的天气怎么样"}]
})):
    console.log(chunk)

同步调用

注意

要同步使用图形,您必须在初始化 RemoteGraph 时提供 urlsync_client

# 调用图形
result = remote_graph.invoke({
    "messages": [{"role": "user", "content": "旧金山的天气怎么样"}]
})

# 从图形中流式输出
for chunk in remote_graph.stream({
    "messages": [{"role": "user", "content": "洛杉矶的天气怎么样"}]
}):
    print(chunk)

线程级持久性

默认情况下,图形运行(即 .invoke().stream() 调用)是无状态的 - 检查点和图形的最终状态不会被持久化。如果您想持久化图形运行的输出(例如,启用人机交互功能),您可以创建一个线程并通过 config 参数提供线程 ID,方法与常规编译图形相同:

from langgraph_sdk import get_sync_client
url = <DEPLOYMENT_URL>
graph_name = "agent"
sync_client = get_sync_client(url=url)
remote_graph = RemoteGraph(graph_name, url=url)

# 创建一个线程(或使用现有线程)
thread = sync_client.threads.create()

# 使用线程配置调用图形
config = {"configurable": {"thread_id": thread["thread_id"]}}
result = remote_graph.invoke({
    "messages": [{"role": "user", "content": "旧金山的天气怎么样"}]
}, config=config)

# 验证状态已保存到线程
thread_state = remote_graph.get_state(config)
print(thread_state)
import { Client } from "@langchain/langgraph-sdk";
import { RemoteGraph } from "@langchain/langgraph/remote";

const url = `<DEPLOYMENT_URL>`;
const graphName = "agent";
const client = new Client({ apiUrl: url });
const remoteGraph = new RemoteGraph({ graphId: graphName, url });

// 创建一个线程(或使用现有线程)
const thread = await client.threads.create();

// 使用线程配置调用图形
const config = { configurable: { thread_id: thread.thread_id }};
const result = await remoteGraph.invoke({
  messages: [{ role: "user", content: "旧金山的天气怎么样" }],
}, config);

// 验证状态已保存到线程
const threadState = await remoteGraph.getState(config);
console.log(threadState);

用作子图

注意

如果您需要将 checkpointer 与具有 RemoteGraph 子图节点的图形一起使用,请确保将 UUID 用作线程 ID。

由于 RemoteGraph 的行为与常规的 CompiledGraph 相同,因此它也可以作为另一个图形中的子图使用。例如:

from langgraph_sdk import get_sync_client
from langgraph.graph import StateGraph, MessagesState, START
from typing import TypedDict

url = <DEPLOYMENT_URL>
graph_name = "agent"
remote_graph = RemoteGraph(graph_name, url=url)

# 定义父图
builder = StateGraph(MessagesState)
# 将远程图直接添加为节点
builder.add_node("child", remote_graph)
builder.add_edge(START, "child")
graph = builder.compile()

# 调用父图
result = graph.invoke({
    "messages": [{"role": "user", "content": "旧金山的天气怎么样"}]
})
print(result)

# 从父图和子图流式输出
for chunk in graph.stream({
    "messages": [{"role": "user", "content": "旧金山的天气怎么样"}]
}, subgraphs=True):
    print(chunk)
import { MessagesAnnotation, StateGraph, START } from "@langchain/langgraph";
import { RemoteGraph } from "@langchain/langgraph/remote";

const url = `<DEPLOYMENT_URL>`;
const graphName = "agent";
const remoteGraph = new RemoteGraph({ graphId: graphName, url });

// 定义父图并将远程图直接添加为节点
const graph = new StateGraph(MessagesAnnotation)
  .addNode("child", remoteGraph)
  .addEdge(START, "child")
  .compile()

// 调用父图
const result = await graph.invoke({
  messages: [{ role: "user", content: "旧金山的天气怎么样" }]
});
console.log(result);

// 从父图和子图流式输出
for await (const chunk of await graph.stream({
  messages: [{ role: "user", content: "洛杉矶的天气怎么样" }]
}, { subgraphs: true })) {
  console.log(chunk);
}
优云智算