AI SDK UIuseChat

useChat

Allows you to easily create a conversational user interface for your chatbot application. It enables the streaming of chat messages from your AI provider, manages the state for chat input, and updates the UI automatically as new messages are received.

Import

React

import { useChat } from "ai/react"

Svelte

import { useChat } from "ai/svelte"

Vue

import { useChat } from "ai/vue"

Solid

import { useChat } from "ai/solid"

Object Parameters

api:

string = '/api/chat'
The chat completion API endpoint offered by the provider.

id:

string
An unique identifier for the chat. If not provided, a random one will be generated. When provided, the `useChat` hook with the same `id` will have shared states across components. This is useful when you have multiple components showing the same chat stream.

initialInput:

string = ''
An optional string for the initial prompt input.

initialMessages:

Messages[] = []
An optional array of initial chat messages

onResponse:

(response: Response) => void
An optional callback that will be called with the response from the API endpoint. Useful for throwing customized errors or logging

onFinish:

(message: Message) => void
An optional callback function that is called when the completion stream ends.

onError:

(error: Error) => void
An optional callback that will be called when the chat stream encounters an error.

headers:

Record<string, string> | Headers
An optional object of headers to be passed to the API endpoint.

body:

any
An optional, additional body object to be passed to the API endpoint.

credentials:

'omit' | 'same-origin' | 'include'
An optional literal that sets the mode of credentials to be used on the request. Defaults to same-origin.

sendExtraMessageFields:

boolean
An optional boolean that determines whether to send extra fields you've added to `messages`. Defaults to `false` and only the `content` and `role` fields will be sent to the API endpoint.

Response Object

messages:

Message[]
The current array of chat messages.

error:

Error | undefined
An error object returned by SWR, if any.

append:

(message: Message | CreateMessage, chatRequestOptions: { options: { headers, body } }) => Promise<string | undefined>
Function to append a message to the chat, triggering an API call for the AI response. It returns a promise that resolves to full response message content when the API call is successfully finished, or throws an error when the API call fails.

reload:

() => Promise<string | undefined>
Function to reload the last AI chat response for the given chat history. If the last message isn't from the assistant, it will request the API to generate a new response.

stop:

() => void
Function to abort the current API request.

setMessages:

(messages: Message[]) => void
Function to update the `messages` state locally without triggering an API call.

input:

string
The current value of the input field.

setInput:

React.Dispatch<React.SetStateAction<string>>
Function to update the `input` value.

handleInputChange:

(event: any) => void
Handler for the `onChange` event of the input field to control the input's value.

handleSubmit:

(event: React.FormEvent<HTMLFormElement>, chatRequestOptions?: ChatRequestOptions) => void
Form submission handler that automatically resets the input field and appends a user message. You can use the `options` parameter to send additional data, headers and more to the server.

isLoading:

boolean
Boolean flag indicating whether a request is currently in progress.

data:

JSONValue[]
Data returned from experimental_StreamData