Chatbot
TheuseChat 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.
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
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.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
TheuseChat 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.
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.
Error State
Similarly, theerror 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.
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. ThesetMessages function can help you achieve these tasks:
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 thestop function returned by the useChat hook.
regenerate function returned by the useChat hook:
Next Steps
Tool Usage
Learn how to add tool calling to your chatbot
Message Persistence
Store and load chat messages from a database
Resume Streams
Handle disconnects with resumable streams
Streaming Data
Stream custom data alongside messages