AI SDK UIuseAssistant
useAssistant
Allows you to handle the client state when interacting with an OpenAI compatible assistant API. This hook is useful when you want to integrate assistant capibilities into your application, with the UI updated automatically as the assistant is streaming its execution.
This works in conjunction with AssistantResponse
in the backend.
Import
React
import { useAssistant } from "ai/react"
Object Parameters
api:
The API endpoint that accepts a threadId and message object and returns an AssistantResponse stream.
threadId?:
Represents the ID of an existing thread. If not provided, a new thread will be created.
credentials?:
Sets the mode of credentials to be used on the request.
headers?:
Headers to be passed to the API endpoint.
body?:
Additional body to be passed to the API endpoint.
onError?:
Callback that will be called when the assistant encounters an error
Response Object
messages:
The current array of chat messages.
setMessages:
Function to update the `messages` array.
threadId:
The current thread ID.
input:
The current value of the input field.
setInput:
Function to update the `input` value.
handleInputChange:
Handler for the `onChange` event of the input field to control the input's value.
submitMessage:
Form submission handler that automatically resets the input field and appends a user message.
status:
The current status of the assistant. This can be used to show a loading indicator.
error:
The error thrown during the assistant message processing, if any.