The 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.
import { convertToModelMessages, streamText, UIMessage } from 'ai';import { openai } from '@ai-sdk/openai';export const maxDuration = 30;export async function POST(req: Request) { const { messages }: { messages: UIMessage[] } = await req.json(); const result = streamText({ model: openai('gpt-4-turbo'), system: 'You are a helpful assistant.', messages: await convertToModelMessages(messages), }); return result.toUIMessageStreamResponse();}
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.
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.
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:
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: