页面简介
Learn how to upgrade AI SDK 3.4 to 4.0.
正文摘录
Migration GuidesMenuMigrate AI SDK 3.4 to 4.0Copy for LLMView MarkdownAsk AI about this pageCheck out the AI SDK 4.0 release blog post for more information about the release. Recommended Migration Process Backup your project. If you use a versioning control system, make sure all previous versions are committed. Migrate to AI SDK 3.4. Upgrade to AI SDK 4.0. Automatically migrate your code using codemods. If you don't want to use codemods, we recommend resolving all deprecation warnings before upgrading to AI SDK 4.0. Follow the breaking changes guide below. Verify your project is working as expected. Commit your changes. AI SDK 4.0 package versions You need to update the following packages to the following versions in your package.json file(s): ai package: 4.0.* @ai-sdk/provider-utils package: 2.0.* @ai-sdk/* packages: 1.0.* (other @ai-sdk/* packages) Codemods The AI SDK provides Codemod transformations to help upgrade your codebase when a feature is deprecated, removed, or otherwise changed. Codemods are transformations that run on your codebase programmatically. They allow you to easily apply many changes without having to manually go through every file. Codemods are intended as a tool to help you with the upgrade process. They may not cover all of the changes you need to make. You may need to make additional changes manually. You can run all codemods provided as part of the 4.0 upgrade process by running the following command from the root of your project: npx @ai-sdk/codemod upgrade To run only the v4 codemods: npx @ai-sdk/codemod v4 Individual codemods can be run by specifying the name of the codemod: npx @ai-sdk/codemod <codemod-name> <path> For example, to run a specific v4 codemod: npx @ai-sdk/codemod v4/replace-baseurl src/ See also the table of codemods. In addition, the latest set of codemods can be found in the @ai-sdk/codemod repository. Provider Changes Removed baseUrl option The baseUrl option has been removed from all providers. Please use the baseURL option instead. AI SDK 3.4const perplexity = createOpenAI({ // ... baseUrl: 'https://api.perplexity.ai/', }); AI SDK 4.0const perplexity = createOpenAI({ // ... baseURL: 'https://api.perplexity.ai/', }); Anthropic Provider Removed Anthropic facade The Anthropic facade has been removed from the Anthropic provider. Please use the anthropic object or the createAnthropic function instead. AI SDK 3.4const anthropic = new Anthropic({ // ... }); AI SDK 4.0const anthropic = createAnthropic({ // ... }); Removed topK setting There is no codemod available for this change. Please review and update your code manually. The model specific topK setting has been removed from the Anthropic provider. You can use the standard topK setting instead. AI SDK 3.4const result = await generateText({ model: anthropic('claude-3-5-sonnet-latest', { topK: 0.5, }), }); AI SDK 4.0const result = await generateText({ model: anthropic('claude-3-5-sonnet-latest'), topK: 0.5, }); Google Generative AI Provider Removed Google facade The Google facade has been removed from the Google Generative AI provider. Please use the google object or the createGoogleGenerativeAI function instead. AI SDK 3.4const google = new Google({ // ... }); AI SDK 4.0const google = createGoogleGenerativeAI({ // ... }); Removed topK setting There is no codemod available for this change. Please review and update your code manually. The model-specific topK setting has been removed from the Google Generative AI provider. You can use the standard topK setting instead. AI SDK 3.4const result = await generateText({ model: google('gemini-1.5-flash', { topK: 0.5, }), }); AI SDK 4.0const result = await generateText({ model: google('gemini-1.5-flash'), topK: 0.5, }); Google Vertex Provider Removed topK setting There is no codemod available for this change. Please review and update your code manually. The model-specific topK setting has been removed from the Google Vertex provider. You can use the standard topK setting instead. AI SDK 3.4const result = await generateText({ model: vertex('gemini-1.5-flash', { topK: 0.5, }), }); AI SDK 4.0const result = await generateText({ model: vertex('gemini-1.5-flash'), topK: 0.5, }); Mistral Provider Removed Mistral facade The Mistral facade has been removed from the Mistral provider. Please use the mistral object or the createMistral function instead. AI SDK 3.4const mistral = new Mistral({ // ... }); AI SDK 4.0const mistral = createMistral({ // ... }); OpenAI Provider Removed OpenAI facade The OpenAI facade has been removed from the OpenAI provider. Please use the openai object or the createOpenAI function instead. AI SDK 3.4const openai = new OpenAI({ // ... }); AI SDK 4.0const openai = createOpenAI({ // ... }); LangChain Adapter Removed toAIStream The toAIStream function has been removed from the LangChain adapter. Please use the toDataStream function instead. AI SDK 3.4LangChainAdapter.toAIStream(stream); AI SDK 4.0LangChainAdapter.toDataStream(stream); AI SDK Core Changes streamText returns immediately Instead of returning a Promise, the streamText function now returns immediately. It is not necessary to await the result of streamText. AI SDK 3.4const result = await streamText({ // ... }); AI SDK 4.0const result = streamText({ // ... }); streamObject returns immediately Instead of returning a Promise, the streamObject function now returns immediately. It is not necessary to await the result of streamObject. AI SDK 3.4const result = await streamObject({ // ... }); AI SDK 4.0const result = streamObject({ // ... }); Remove roundtrips The maxToolRoundtrips and maxAutomaticRoundtrips options have been removed from the generateText and streamText functions. Please use the maxSteps option instead. The roundtrips property has been removed from the GenerateTextResult type. Please use the steps property instead. AI SDK 3.4const { text, roundtrips } = await generateText({ maxToolRoundtrips: 1, // or maxAutomaticRoundtrips // ... }); AI SDK 4.0const { text, steps } = await generateText({ maxSteps: 2, // ... }); Removed nanoid export The nanoid export has been removed. Please use generateId instead. AI SDK 3.4import { nanoid } from 'ai'; AI SDK 4.0import { generateId } from 'ai'; Increased default size of generated IDs There is no codemod available for this change. Please review and update your code manually. The generateId function now generates 16-character IDs. The previous default was 7 characters. This might e.g. require updating your database schema if you limit the length of IDs. AI SDK 4.0import { generateId } from 'ai'; const id = generateId(); // now 16 characters Removed ExperimentalMessage types The following types have been removed: ExperimentalMessage (use ModelMessage instead) ExperimentalUserMessage (use CoreUserMessage instead) ExperimentalAssistantMessage (use CoreAssistantMessage instead) ExperimentalToolMessage (use CoreToolMessage instead) AI SDK 3.4import { ExperimentalMessage, ExperimentalUserMessage, ExperimentalAssistantMessage, ExperimentalToolMessage, } from 'ai'; AI SDK 4.0import { ModelMessage, CoreUserMessage, CoreAssistantMessage, CoreToolMessage, } from 'ai'; Removed ExperimentalTool type The ExperimentalTool type has been removed. Please use the CoreTool type instead. AI SDK 3.4import { ExperimentalTool } from 'ai'; AI SDK 4.0import { CoreTool } from 'ai'; Removed experimental AI function exports The following exports have been removed: experimental_generateText (use generateText instead) experimental_streamText (use streamText instead) experimental_generateObject (use generateObject instead) experimental_streamObject (use streamObject instead) AI SDK 3.4import { experimental_generateText, experimental_streamText, experimental_generateObject, experimental_streamObject, } from 'ai'; AI SDK 4.0import { generateText, streamText, generateObject, streamObject } from 'ai'; Removed AI-stream related methods from streamText The following methods have been removed from the streamText result: toAIStream pipeAIStreamToResponse toAIStreamResponse Use the toDataStream, pipeDataStreamToResponse, and toDataStreamResponse functions instead. AI SDK 3.4const result = await streamText({ // ... }); result.toAIStream(); result.pipeAIStreamToResponse(response); result.toAIStreamResponse(); AI SDK 4.0const result = streamText({ // ... }); result.toDataStream(); result.pipeDataStreamToResponse(response); result.toDataStreamResponse(); Renamed "formatStreamPart" to "formatDataStreamPart" The formatStreamPart function has been renamed to formatDataStreamPart. AI SDK 3.4formatStreamPart('text', 'Hello, world!'); AI SDK 4.0formatDataStreamPart('text', 'Hello, world!'); Renamed "parseStreamPart" to "parseDataStreamPart" The parseStreamPart function has been renamed to parseDataStreamPart. AI SDK 3.4const part = parseStreamPart(line); AI SDK 4.0const part = parseDataStreamPart(line); Renamed TokenUsage, CompletionTokenUsage and EmbeddingTokenUsage types The TokenUsage, CompletionTokenUsage and EmbeddingTokenUsage types have been renamed to LanguageModelUsage (for the first two) and EmbeddingModelUsage (for the last). AI SDK 3.4import { TokenUsage, CompletionTokenUsage, EmbeddingTokenUsage } from 'ai'; AI SDK 4.0import { LanguageModelUsage, EmbeddingModelUsage } from 'ai'; Removed deprecated telemetry data There is no codemod available for this change. Please review and update your code manually. The following telemetry data values have been removed: ai.finishReason (now in ai.response.finishReason) ai.result.object (now in ai.response.object) ai.result.text (now in ai.response.text) ai.result.toolCalls (now in ai.response.toolCalls) ai.stream.msToFirstChunk (now in ai.response.msToFirstChunk) This change will apply to observability providers and any scripts or automation that you use for processing telemetry data. Provider Registry Removed experimental_Provider, experimental_ProviderRegistry, and experimental_ModelRegistry The experimental_Provider interface, experimental_ProviderRegistry i