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

onFinish:

(prompt: string, completion: string) => void

onError:

(error: Error) => void

headers:

Record<string, string> | Headers

body:

object

credentials:

'omit' | 'same-origin' | 'include'

streamProtocol?:

'text' | 'data'

fetch?:

FetchFunction

experimental_throttle?:

number

Returns

completion:

string

complete:

(prompt: string, options?: { headers?: Record<string, string> | Headers, body?: object }) => Promise<string | null | undefined>

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