Skip to main content
React hook for building text completion interfaces with AI language models.

Parameters

string
A unique identifier for the completion. If not provided, a random ID will be generated. When provided, the useCompletion hook with the same id will share states across components.
string
default:"/api/completion"
The API endpoint that accepts a { prompt } object and returns a stream of tokens of the completion response.
string
default:"''"
Initial completion value to be used in the component.
string
default:"''"
Initial input value to be used in the component.
RequestCredentials
The credentials mode to be used for the fetch request. Possible values are: 'omit', 'same-origin', 'include'.
Record<string, string> | Headers
Additional headers to be sent with the request.
Record<string, unknown>
Additional body fields to be sent with the request.
'data' | 'text'
default:"'data'"
The stream protocol to use.
FetchFunction
Custom fetch implementation. You can use it as a middleware to intercept requests, or to provide a custom fetch implementation for e.g. testing.
(prompt: string, completion: string) => void
Callback function to be called when the completion is complete.
(error: Error) => void
Callback function to be called when an error occurs.
number
Custom throttle wait in milliseconds for the completion and data updates. Default is undefined, which disables throttling.

Returns

string
The current completion result.
(prompt: string, options?: CompletionRequestOptions) => Promise<string | null | undefined>
Send a new prompt to the API endpoint and update the completion state.
Error | undefined
The error object if an error occurred.
() => void
Abort the current API request but keep the generated tokens.
(completion: string) => void
Update the completion state locally.
string
The current value of the input.
React.Dispatch<React.SetStateAction<string>>
setState-powered method to update the input value.
(event: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => void
An input/textarea-ready onChange handler to control the value of the input.
(event?: { preventDefault?: () => void }) => void
Form submission handler to automatically reset input and trigger completion.
boolean
Whether the API request is in progress.

Examples

Basic completion

With form input

With custom options

Streaming with stop