页面简介
Learn how to use the useChat hook.
正文摘录
AI SDK UIMenuChatbotCopy for LLMView MarkdownAsk AI about this pageThe useChat hook makes it effortless to create a conversational user interface for your chatbot application. It enables the streaming of chat messages from your AI provider, manages the chat state, and updates the UI automatically as new messages arrive. To summarize, the useChat hook provides the following features: Message Streaming: All the messages from the AI provider are streamed to the chat UI in real-time. Managed States: The hook manages the states for input, messages, status, error and more for you. Seamless Integration: Easily integrate your chat AI into any design or layout with minimal effort. In this guide, you will learn how to use the useChat hook to create a chatbot application with real-time message streaming. Check out our chatbot with tools guide to learn how to use tools in your chatbot. Let's start with the following example first. Example app/page.tsx'use client'; import { useChat } from '@ai-sdk/react'; import { DefaultChatTransport } from 'ai'; import { useState } from 'react'; export default function Page() { const { messages, sendMessage, status } = useChat({ transport: new DefaultChatTransport({ api: '/api/chat', }), }); const [input, setInput] = useState(''); return ( <> {messages.map(message => ( <div key={message.id}> {message.role === 'user' ? 'User: ' : 'AI: '} {message.parts.map((part, index) => part.type === 'text' ? <span key={index}>{part.text}</span> : null, )} </div> ))} <form onSubmit={e => { e.preventDefault(); if (input.trim()) { sendMessage({ text: input }); setInput(''); } }} > <input value={input} onChange={e => setInput(e.target.value)} disabled={status !== 'ready'} placeholder="Say something..." /> <button type="submit" disabled={status !== 'ready'}> Submit </button> </form> </> ); } GatewayProviderCustomClaude Sonnet 5.5app/api/chat/route.tsimport { convertToModelMessages, createUIMessageStreamResponse, streamText, toUIMessageStream, UIMessage, } from 'ai'; // Allow streaming responses up to 30 seconds export const maxDuration = 30; export async function POST(req: Request) { const { messages }: { messages: UIMessage[] } = await req.json(); const result = streamText({ model: "anthropic/claude-sonnet-5.5", instructions: 'You are a helpful assistant.', messages: await convertToModelMessages(messages), }); return createUIMessageStreamResponse({ stream: toUIMessageStream({ stream: result.stream }), }); } The UI messages have a new parts property that contains the message parts. We recommend rendering the messages using the parts property instead of the content property. The parts property supports different message types, including text, tool invocation, and tool result, and allows for more flexible and complex chat UIs. In the Page component, the useChat hook will request to your AI provider endpoint whenever the user sends a message using sendMessage. The messages are then streamed back in real-time and displayed in the chat UI. This enables a seamless chat experience where the user can see the AI response as soon as it is available, without having to wait for the entire response to be received. Customized UI useChat also provides ways to manage the chat message states via code, show status, and update messages without being triggered by user interactions. Status The useChat hook returns a status. It has the following possible values: submitted: The message has been sent to the API and we're awaiting the start of the response stream. streaming: The response is actively streaming in from the API, receiving chunks of data. ready: The full response has been received and processed; a new user message can be submitted. error: An error occurred during the API request, preventing successful completion. You can use status for e.g. the following purposes: To show a loading spinner while the chatbot is processing the user's message. To show a "Stop" button to abort the current message. To disable the submit button. app/page.tsx'use client'; import { useChat } from '@ai-sdk/react'; import { DefaultChatTransport } from 'ai'; import { useState } from 'react'; export default function Page() { const { messages, sendMessage, status, stop } = useChat({ transport: new DefaultChatTransport({ api: '/api/chat', }), }); const [input, setInput] = useState(''); return ( <> {messages.map(message => ( <div key={message.id}> {message.role === 'user' ? 'User: ' : 'AI: '} {message.parts.map((part, index) => part.type === 'text' ? <span key={index}>{part.text}</span> : null, )} </div> ))} {(status === 'submitted' || status === 'streaming') && ( <div> {status === 'submitted' && <Spinner />} <button type="button" onClick={() => stop()}> Stop </button> </div> )} <form onSubmit={e => { e.preventDefault(); if (input.trim()) { sendMessage({ text: input }); setInput(''); } }} > <input value={input} onChange={e => setInput(e.target.value)} disabled={status !== 'ready'} placeholder="Say something..." /> <button type="submit" disabled={status !== 'ready'}> Submit </button> </form> </> ); } Error State Similarly, the error state reflects the error object thrown during the fetch request. It can be used to display an error message, disable the submit button, or show a retry button: We recommend showing a generic error message to the user, such as "Something went wrong." This is a good practice to avoid leaking information from the server. app/page.tsx'use client'; import { useChat } from '@ai-sdk/react'; import { DefaultChatTransport } from 'ai'; import { useState } from 'react'; export default function Chat() { const { messages, sendMessage, error, regenerate } = useChat({ transport: new DefaultChatTransport({ api: '/api/chat', }), }); const [input, setInput] = useState(''); return ( <div> {messages.map(m => ( <div key={m.id}> {m.role}:{' '} {m.parts.map((part, index) => part.type === 'text' ? <span key={index}>{part.text}</span> : null, )} </div> ))} {error && ( <> <div>An error occurred.</div> <button type="button" onClick={() => regenerate()}> Retry </button> </> )} <form onSubmit={e => { e.preventDefault(); if (input.trim()) { sendMessage({ text: input }); setInput(''); } }} > <input value={input} onChange={e => setInput(e.target.value)} disabled={error != null} /> </form> </div> ); } Please also see the error handling guide for more information. Modify messages Sometimes, you may want to directly modify some existing messages. For example, a delete button can be added to each message to allow users to remove them from the chat history. The setMessages function can help you achieve these tasks: const { messages, setMessages } = useChat() const handleDelete = (id) => { setMessages(messages.filter(message => message.id !== id)) } return <> {messages.map(message => ( <div key={message.id}> {message.role === 'user' ? 'User: ' : 'AI: '} {message.parts.map((part, index) => ( part.type === 'text' ? ( <span key={index}>{part.text}</span> ) : null ))} <button onClick={() => handleDelete(message.id)}>Delete</button> </div> ))} ... You can think of messages and setMessages as a pair of state and setState in React. Cancellation and regeneration It's also a common use case to abort the response message while it's still streaming back from the AI provider. You can do this by calling the stop function returned by the useChat hook. const { stop, status } = useChat() return <> <button onClick={stop} disabled={!(status === 'streaming' || status === 'submitted')}>Stop</button> ... When the user clicks the "Stop" button, the fetch request will be aborted. This avoids consuming unnecessary resources and improves the UX of your chatbot application. Similarly, you can also request the AI provider to reprocess the last message by calling the regenerate function returned by the useChat hook: const { regenerate, status } = useChat(); return ( <> <button onClick={regenerate} disabled={!(status === 'ready' || status === 'error')} > Regenerate </button> ... </> ); When the user clicks the "Regenerate" button, the AI provider will regenerate the last message and replace the current one correspondingly. Throttling UI Updates By default, useChat publishes message updates as each chunk is processed. React may batch multiple updates into a single render. React and Vue applications can throttle reactive message updates with the throttle option. Stream processing and event callbacks remain immediate, and the latest messages are published before the chat enters a terminal ready or error status. ReactVuepage.tsxconst { messages, ... } = useChat({ // Throttle reactive message updates to 50ms: throttle: 50, }); Queueing Messages Message queueing is experimental. By default, the input is disabled while a response is in progress. With experimental_queue, users can keep typing: messages sent while a response is in progress wait in experimental_queuedMessages and are sent one at a time after each response finishes. page.tsxconst { messages, experimental_queuedMessages, sendMessage, experimental_removeQueuedMessage, experimental_sendQueuedMessageNow, } = useChat({ experimental_queue: true, }); return ( <> {/* messages */} {experimental_queuedMessages.map(message => ( <div key={message.id}> {message.parts.map(part => (part.type === 'text' ? part.text : ''))} <button onClick={() => experimental_sendQueuedMessageNow(message.id)}> Send now </button> <button onClick={() => experimental_removeQueuedMessage(message.id)}> Remove </button> </div> ))} <form onSubmit={e => { e.preventDefault(); sendMessage({ text: input }); setInput(''); }} > {/* the input stays enabled while a response is in progress */} <input value={input} onChange={e => setInput(e.target.value)} /> </form> </> ); Queued messages are sent in the order they were sent. A new message never skips the queue. The queue also waits while the assistant waits for the user, e.g. a tool call that needs an approval or a client-side tool result, and continues once the result is added. Automatic follow-ups from sendAutomaticallyWhen are