如何将 LangGraph 集成到您的 React 应用程序中¶
useStream()
React hook 提供了一种将 LangGraph 无缝集成到您的 React 应用程序中的方式。它处理流式传输、状态管理和分支逻辑的所有复杂性,让您可以专注于构建出色的聊天体验。
主要特性
- 消息流式传输:处理消息块流以形成完整消息
- 对消息、中断、加载状态和错误进行自动状态管理
- 对话分支:从聊天历史中的任意点创建备选对话路径
- UI 无关设计:自带组件和样式
让我们探讨如何在 React 应用程序中使用 useStream()
。
useStream()
为创建定制聊天体验提供了坚实的基础。对于预构建的聊天组件和界面,我们还建议查看 CopilotKit 和 assistant-ui。
安装¶
示例¶
"use client";
import { useStream } from "@langchain/langgraph-sdk/react";
import type { Message } from "@langchain/langgraph-sdk";
export default function App() {
const thread = useStream<{ messages: Message[] }>({
apiUrl: "http://localhost:2024",
assistantId: "agent",
messagesKey: "messages",
});
return (
<div>
<div>
{thread.messages.map((message) => (
<div key={message.id}>{message.content as string}</div>
))}
</div>
<form
onSubmit={(e) => {
e.preventDefault();
const form = e.target as HTMLFormElement;
const message = new FormData(form).get("message") as string;
form.reset();
thread.submit({ messages: [{ type: "human", content: message }] });
}}
>
<input type="text" name="message" />
{thread.isLoading ? (
<button key="stop" type="button" onClick={() => thread.stop()}>
Stop
</button>
) : (
<button keytype="submit">Send</button>
)}
</form>
</div>
);
}
自定义您的 UI¶
useStream()
hook 处理所有复杂的幕后状态管理,为您提供简单的接口来构建您的 UI。以下是您可以直接获得的功能:
- 线程状态管理
- 加载和错误状态
- 中断
- 消息处理和更新
- 分支支持
以下是一些如何有效使用这些功能的示例
加载状态¶
isLoading
属性告诉您流何时处于活动状态,使您能够
- 显示加载指示器
- 在处理过程中禁用输入字段
- 显示取消按钮
export default function App() {
const { isLoading, stop } = useStream<{ messages: Message[] }>({
apiUrl: "http://localhost:2024",
assistantId: "agent",
messagesKey: "messages",
});
return (
<form>
{isLoading && (
<button key="stop" type="button" onClick={() => stop()}>
Stop
</button>
)}
</form>
);
}
线程管理¶
使用内置的线程管理功能跟踪对话。您可以访问当前线程 ID 并在创建新线程时收到通知
const [threadId, setThreadId] = useState<string | null>(null);
const thread = useStream<{ messages: Message[] }>({
apiUrl: "http://localhost:2024",
assistantId: "agent",
threadId: threadId,
onThreadId: setThreadId,
});
我们建议将 threadId
存储在 URL 的查询参数中,以便用户在刷新页面后可以恢复对话。
消息处理¶
useStream()
hook 将跟踪从服务器接收到的消息块,并将它们连接起来形成完整的消息。可以通过 messages
属性检索完成的消息块。
默认情况下,messagesKey
设置为 messages
,它会将新的消息块附加到 values["messages"]
。如果您将消息存储在不同的键中,可以更改 messagesKey
的值。
import type { Message } from "@langchain/langgraph-sdk";
import { useStream } from "@langchain/langgraph-sdk/react";
export default function HomePage() {
const thread = useStream<{ messages: Message[] }>({
apiUrl: "http://localhost:2024",
assistantId: "agent",
messagesKey: "messages",
});
return (
<div>
{thread.messages.map((message) => (
<div key={message.id}>{message.content as string}</div>
))}
</div>
);
}
在底层,useStream()
hook 将使用 streamMode: "messages-tuple"
从您的图节点内的任何 LangChain 聊天模型调用中接收消息流(即,单个 LLM 令牌)。在如何从您的图流式传输消息指南中了解更多关于消息流式传输的信息。
中断¶
useStream()
hook 暴露了 interrupt
属性,该属性将填充线程中的最后一个中断。您可以使用中断来
- 在执行节点之前渲染确认 UI
- 等待人工输入,允许代理向用户提出澄清问题
在如何处理中断指南中了解更多关于中断的信息。
const thread = useStream<{ messages: Message[] }, { InterruptType: string }>({
apiUrl: "http://localhost:2024",
assistantId: "agent",
messagesKey: "messages",
});
if (thread.interrupt) {
return (
<div>
Interrupted! {thread.interrupt.value}
<button
type="button"
onClick={() => {
// `resume` can be any value that the agent accepts
thread.submit(undefined, { command: { resume: true } });
}}
>
Resume
</button>
</div>
);
}
分支¶
对于每条消息,您可以使用 getMessagesMetadata()
获取消息首次出现的第一个检查点。然后,您可以从第一个出现检查点之前的检查点创建一个新的运行,从而在线程中创建一个新分支。
分支可以通过以下方式创建
- 编辑先前的用户消息。
- 请求重新生成先前的助手消息。
"use client";
import type { Message } from "@langchain/langgraph-sdk";
import { useStream } from "@langchain/langgraph-sdk/react";
import { useState } from "react";
function BranchSwitcher({
branch,
branchOptions,
onSelect,
}: {
branch: string | undefined;
branchOptions: string[] | undefined;
onSelect: (branch: string) => void;
}) {
if (!branchOptions || !branch) return null;
const index = branchOptions.indexOf(branch);
return (
<div className="flex items-center gap-2">
<button
type="button"
onClick={() => {
const prevBranch = branchOptions[index - 1];
if (!prevBranch) return;
onSelect(prevBranch);
}}
>
Prev
</button>
<span>
{index + 1} / {branchOptions.length}
</span>
<button
type="button"
onClick={() => {
const nextBranch = branchOptions[index + 1];
if (!nextBranch) return;
onSelect(nextBranch);
}}
>
Next
</button>
</div>
);
}
function EditMessage({
message,
onEdit,
}: {
message: Message;
onEdit: (message: Message) => void;
}) {
const [editing, setEditing] = useState(false);
if (!editing) {
return (
<button type="button" onClick={() => setEditing(true)}>
Edit
</button>
);
}
return (
<form
onSubmit={(e) => {
e.preventDefault();
const form = e.target as HTMLFormElement;
const content = new FormData(form).get("content") as string;
form.reset();
onEdit({ type: "human", content });
setEditing(false);
}}
>
<input name="content" defaultValue={message.content as string} />
<button type="submit">Save</button>
</form>
);
}
export default function App() {
const thread = useStream({
apiUrl: "http://localhost:2024",
assistantId: "agent",
messagesKey: "messages",
});
return (
<div>
<div>
{thread.messages.map((message) => {
const meta = thread.getMessagesMetadata(message);
const parentCheckpoint = meta?.firstSeenState?.parent_checkpoint;
return (
<div key={message.id}>
<div>{message.content as string}</div>
{message.type === "human" && (
<EditMessage
message={message}
onEdit={(message) =>
thread.submit(
{ messages: [message] },
{ checkpoint: parentCheckpoint }
)
}
/>
)}
{message.type === "ai" && (
<button
type="button"
onClick={() =>
thread.submit(undefined, { checkpoint: parentCheckpoint })
}
>
<span>Regenerate</span>
</button>
)}
<BranchSwitcher
branch={meta?.branch}
branchOptions={meta?.branchOptions}
onSelect={(branch) => thread.setBranch(branch)}
/>
</div>
);
})}
</div>
<form
onSubmit={(e) => {
e.preventDefault();
const form = e.target as HTMLFormElement;
const message = new FormData(form).get("message") as string;
form.reset();
thread.submit({ messages: [message] });
}}
>
<input type="text" name="message" />
{thread.isLoading ? (
<button key="stop" type="button" onClick={() => thread.stop()}>
Stop
</button>
) : (
<button key="submit" type="submit">
Send
</button>
)}
</form>
</div>
);
}
对于高级用例,您可以使用 experimental_branchTree
属性获取线程的树形表示,该表示可用于为非基于消息的图渲染分支控件。
乐观更新¶
您可以在向代理执行网络请求之前乐观地更新客户端状态,从而向用户提供即时反馈,例如在代理看到请求之前立即显示用户消息。
const stream = useStream({
apiUrl: "http://localhost:2024",
assistantId: "agent",
messagesKey: "messages",
});
const handleSubmit = (text: string) => {
const newMessage = { type: "human" as const, content: text };
stream.submit(
{ messages: [newMessage] },
{
optimisticValues(prev) {
const prevMessages = prev.messages ?? [];
const newMessages = [...prevMessages, newMessage];
return { ...prev, messages: newMessages };
},
}
);
};
TypeScript¶
useStream()
hook 对使用 TypeScript 编写的应用程序很友好,您可以为状态指定类型以获得更好的类型安全性和 IDE 支持。
// Define your types
type State = {
messages: Message[];
context?: Record<string, unknown>;
};
// Use them with the hook
const thread = useStream<State>({
apiUrl: "http://localhost:2024",
assistantId: "agent",
messagesKey: "messages",
});
您还可以选择为不同的场景指定类型,例如
ConfigurableType
:config.configurable
属性的类型(默认:Record<string, unknown>
)InterruptType
:中断值的类型 - 即interrupt(...)
函数的内容(默认:unknown
)CustomEventType
:自定义事件的类型(默认:unknown
)UpdateType
:提交函数的类型(默认:Partial<State>
)
const thread = useStream<
State,
{
UpdateType: {
messages: Message[] | Message;
context?: Record<string, unknown>;
};
InterruptType: string;
CustomEventType: {
type: "progress" | "debug";
payload: unknown;
};
ConfigurableType: {
model: string;
};
}
>({
apiUrl: "http://localhost:2024",
assistantId: "agent",
messagesKey: "messages",
});
如果您正在使用 LangGraph.js,您也可以重用您的图的注解类型。但是,请确保仅导入注解模式的类型,以避免导入整个 LangGraph.js 运行时(即,通过 import type { ... }
指令)。
import {
Annotation,
MessagesAnnotation,
type StateType,
type UpdateType,
} from "@langchain/langgraph/web";
const AgentState = Annotation.Root({
...MessagesAnnotation.spec,
context: Annotation<string>(),
});
const thread = useStream<
StateType<typeof AgentState.spec>,
{ UpdateType: UpdateType<typeof AgentState.spec> }
>({
apiUrl: "http://localhost:2024",
assistantId: "agent",
messagesKey: "messages",
});
事件处理¶
useStream()
hook 提供了几种回调选项,帮助您响应不同的事件
onError
:发生错误时调用。onFinish
:流完成时调用。onUpdateEvent
:接收到更新事件时调用。onCustomEvent
:接收到自定义事件时调用。请参阅自定义事件了解如何流式传输自定义事件。onMetadataEvent
:接收到元数据事件时调用,其中包含运行 ID 和线程 ID。