本页还没有译文
本页 16 段还没译成简体中文,下面显示的是英文原文。useCompletion
还没翻译译自英文(ai-sdk.dev)本页 16 段都还没译
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'import { Completion } from '@ai-sdk/svelte'import { useCompletion } from '@ai-sdk/vue'import { Completion } from '@ai-sdk/angular'API Signature
Type Parameters
BODY(object): The type of the additional request body passed through the hook options or the complete function. Defaults to object.
Parameters
api(string = '/api/completion'): The API endpoint that is called to generate text. It can be a relative path (starting with/) or an absolute URL.id(string): A unique identifier for the completion. If not provided, a random one will be generated. When provided, theuseCompletionhook with the sameidwill have shared states across components. This is useful when you have multiple components showing the same chat streaminitialInput(string): An optional string for the initial prompt input.initialCompletion(string): An optional string for the initial completion result.onFinish((prompt: string, completion: string) => void): An optional callback function that is called when the completion stream ends.onError((error: Error) => void): An optional callback that will be called when the chat stream encounters an error.headers(Record<string, string> | Headers): An optional object of headers to be passed to the API endpoint.body(BODY): An optional, additional body object to be passed to the API endpoint.credentials('omit' | 'same-origin' | 'include'): An optional literal that sets the mode of credentials to be used on the request. Defaults to same-origin.streamProtocol?('text' | 'data'): An optional literal that sets the type of stream to be used. Defaults todata. If set totext, the stream will be treated as a text stream. Note that with either protocol,useCompletiononly exposes text content via thecompletionfield — non-text parts (e.g.data-*parts) emitted by the server are ignored. UseuseChatif you need access to those parts.fetch?(FetchFunction): Optional. A custom fetch function to be used for the API call. Defaults to the global fetch function.throttle?(number): React only. Custom throttle wait time in milliseconds for the completion and data updates. When specified, throttles how often the UI updates during streaming. Default is undefined, which disables throttling.
Returns
completion(string): The current text completion.complete((prompt: string, options?: { headers?: Record<string, string> | Headers, body?: BODY }) => Promise<string | null | undefined>): Function to execute text completion based on the provided prompt. Returns the completion result when finished.error(undefined | Error): The error thrown during the completion process, if any.setCompletion((completion: string) => void): Function to update thecompletionstate.stop(() => void): Function to abort the current API request.input(string): The current value of the input field.setInput(React.Dispatch<React.SetStateAction<string>>): Function to update the input value.handleInputChange((event: any) => void): Handler for theonChangeevent of the input field to control the input's value.handleSubmit((event?: { preventDefault?: () => void }) => void): Form submission handler that automatically resets the input field and appends a user message.isLoading(boolean): Boolean flag indicating whether a fetch operation is currently in progress.
AI 翻译查看英文原文