React hook for building text completion interfaces with AI language models.
import { useCompletion } from 'ai/react';
export default function Completion() {
const { completion, complete, isLoading } = useCompletion();
return (
<div>
<button onClick={() => complete('Write a poem about')}>
Generate
</button>
{isLoading && <div>Loading...</div>}
<div>{completion}</div>
</div>
);
}
Parameters
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.
api
string
default:"/api/completion"
The API endpoint that accepts a { prompt } object and returns a stream of tokens of the completion response.
Initial completion value to be used in the component.
Initial input value to be used in the component.
The credentials mode to be used for the fetch request.
Possible values are: 'omit', 'same-origin', 'include'.
Additional headers to be sent with the request.
Additional body fields to be sent with the request.
streamProtocol
'data' | 'text'
default:"'data'"
The stream protocol to use.
Custom fetch implementation. You can use it as a middleware to intercept requests,
or to provide a custom fetch implementation for e.g. testing.
onFinish
(prompt: string, completion: string) => void
Callback function to be called when the completion is complete.
Callback function to be called when an error occurs.
Custom throttle wait in milliseconds for the completion and data updates.
Default is undefined, which disables throttling.
Returns
The current completion result.
complete
(prompt: string, options?: CompletionRequestOptions) => Promise<string | null | undefined>
Send a new prompt to the API endpoint and update the completion state.
The error object if an error occurred.
Abort the current API request but keep the generated tokens.
setCompletion
(completion: string) => void
Update the completion state locally.
The current value of the input.
setInput
React.Dispatch<React.SetStateAction<string>>
setState-powered method to update the input value.
handleInputChange
(event: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => void
An input/textarea-ready onChange handler to control the value of the input.
handleSubmit
(event?: { preventDefault?: () => void }) => void
Form submission handler to automatically reset input and trigger completion.
Whether the API request is in progress.
Examples
Basic completion
import { useCompletion } from 'ai/react';
export default function Completion() {
const { completion, complete, isLoading } = useCompletion();
return (
<div>
<button
onClick={() => complete('Write a short poem about spring')}
disabled={isLoading}
>
{isLoading ? 'Generating...' : 'Generate Poem'}
</button>
<div>{completion}</div>
</div>
);
}
import { useCompletion } from 'ai/react';
export default function Completion() {
const {
completion,
input,
handleInputChange,
handleSubmit,
isLoading,
} = useCompletion();
return (
<div>
<form onSubmit={handleSubmit}>
<input
value={input}
onChange={handleInputChange}
placeholder="Enter a prompt..."
/>
<button type="submit" disabled={isLoading}>
{isLoading ? 'Generating...' : 'Generate'}
</button>
</form>
<div>{completion}</div>
</div>
);
}
With custom options
import { useCompletion } from 'ai/react';
export default function Completion() {
const { completion, complete, isLoading } = useCompletion({
api: '/api/custom-completion',
onFinish: (prompt, completion) => {
console.log('Finished:', completion);
},
onError: (error) => {
console.error('Error:', error);
},
});
return (
<div>
<button
onClick={() =>
complete('Write a story', {
body: { temperature: 0.8 },
})
}
disabled={isLoading}
>
Generate
</button>
<div>{completion}</div>
</div>
);
}
Streaming with stop
import { useCompletion } from 'ai/react';
export default function Completion() {
const { completion, complete, stop, isLoading } = useCompletion();
return (
<div>
<button onClick={() => complete('Write a long story')}>
Start
</button>
<button onClick={stop} disabled={!isLoading}>
Stop
</button>
<div>{completion}</div>
</div>
);
}