useCompletion()
Allows you to create text completion based capabilities for your application. It enables the streaming of text completions from your AI provider, manages the state for chat input, and updates the UI automatically as new messages are received.
Import
import { useCompletion } from '@ai-sdk/react'API Signature
Parameters
api:
string = '/api/completion'
id:
string
initialInput:
string
initialCompletion:
string
onResponse:
(response: Response) => void
onFinish:
(prompt: string, completion: string) => void
onError:
(error: Error) => void
headers:
Record<string, string> | Headers
body:
any
credentials:
'omit' | 'same-origin' | 'include'
sendExtraMessageFields:
boolean
streamProtocol?:
'text' | 'data'
fetch?:
FetchFunction
experimental_throttle?:
number
Returns
completion:
string
complete:
(prompt: string, options: { headers, body }) => void
error:
undefined | Error
setCompletion:
(completion: string) => void
stop:
() => void
input:
string
setInput:
React.Dispatch<React.SetStateAction<string>>
handleInputChange:
(event: any) => void
handleSubmit:
(event?: { preventDefault?: () => void }) => void
isLoading:
boolean