翻篇收录说明
翻篇网页快照

Cartesia | AI SDK

https://ai-sdk.dev/providers/ai-sdk-providers/cartesia

最近抓取:2026-12-04 03:45:36(北京时间)

最近内容变化:2026-12-04 03:45:36(北京时间)

这是最近一次有效抓取的文本快照,不含网页脚本、图片和动态交互内容。实际网页可能已更新。

访问原网页

页面简介

Learn how to set up the Cartesia provider and check AI Gateway availability.

正文摘录

AI SDK ProvidersMenuCartesiaCopy for LLMView MarkdownAsk AI about this pageThe Cartesia provider contains Sonic speech generation, Ink-Whisper batch transcription, and Ink 2 realtime and streaming transcription support. Cartesia models are not currently available through AI Gateway, so the opening example uses @ai-sdk/cartesia directly. See Using AI Gateway below for supported alternatives. import { generateSpeech } from 'ai'; import { cartesia } from '@ai-sdk/cartesia'; import { writeFile } from 'node:fs/promises'; const { audio } = await generateSpeech({ model: cartesia.speech('sonic-3.5'), text: 'Hello, world!', outputFormat: 'mp3', voice: '694f9389-aac1-45b6-b726-9d9369183238', }); await writeFile('speech.mp3', audio.uint8Array); Using AI Gateway The current AI Gateway model catalog does not list Cartesia models. Use @ai-sdk/cartesia for the APIs on this page. To use other speech models through Gateway, install ai and configure a Gateway API key: pnpmnpmyarnbun$ pnpm add ai Set an AI Gateway API key in your environment: export AI_GATEWAY_API_KEY=your_api_key_here Vercel deployments can also use OIDC authentication. See the Gateway documentation for supported models and APIs. Using @ai-sdk/cartesia For direct access to Cartesia, install ai and @ai-sdk/cartesia: pnpmnpmyarnbun$ pnpm add ai @ai-sdk/cartesia Set your Cartesia API credential in the CARTESIA_API_KEY environment variable: export CARTESIA_API_KEY=your_api_key_here The following sections document the direct @ai-sdk/cartesia provider, including its model factories and settings. Gateway model and feature availability can differ. Provider Instance You can import the default provider instance cartesia from @ai-sdk/cartesia: import { cartesia } from '@ai-sdk/cartesia'; If you need a customized setup, you can import createCartesia from @ai-sdk/cartesia and create a provider instance with your settings: import { createCartesia } from '@ai-sdk/cartesia'; const cartesia = createCartesia({ // custom settings, e.g. fetch: customFetch, }); You can use the following optional settings to customize the Cartesia provider instance: apiKey string | PromiseLike<string> | (() => string | PromiseLike<string>) Pass a synchronous or asynchronous callback to resolve the key before each request. The callback is responsible for caching and refreshing credentials. API key that is being sent using the Authorization header. It defaults to the CARTESIA_API_KEY environment variable. version string The Cartesia API version to use (sent via the Cartesia-Version header). headers Record<string,string> Custom headers to include in the requests. fetch (input: RequestInfo, init?: RequestInit) => Promise<Response> Custom fetch implementation. Defaults to the global fetch function. You can use it as a middleware to intercept requests, or to provide a custom fetch implementation for e.g. testing. webSocket WebSocketConstructor Custom WebSocket implementation for Ink 2 streaming transcription. Defaults to the global WebSocket constructor. Speech Models You can create models that call the Cartesia text-to-speech API using the .speech() factory method. The first argument is the model id, e.g. sonic-3.5. const model = cartesia.speech('sonic-3.5'); You can use the model with the generateSpeech function. Cartesia requires a voice id: import { generateSpeech } from 'ai'; import { cartesia } from '@ai-sdk/cartesia'; const result = await generateSpeech({ model: cartesia.speech('sonic-3.5'), text: 'Hello, world!', voice: '694f9389-aac1-45b6-b726-9d9369183238', }); You can also pass additional provider-specific options using the providerOptions argument: import { generateSpeech } from 'ai'; import { cartesia, type CartesiaSpeechModelOptions } from '@ai-sdk/cartesia'; const result = await generateSpeech({ model: cartesia.speech('sonic-3.5'), text: 'Hello, world!', voice: '694f9389-aac1-45b6-b726-9d9369183238', providerOptions: { cartesia: { container: 'wav', encoding: 'pcm_s16le', sampleRate: 24000, } satisfies CartesiaSpeechModelOptions, }, }); The following provider options are available: container string Container format for the output audio. Supported values: 'raw', 'wav', 'mp3'. Optional. encoding string Encoding type for the audio output. Supported values: 'pcm_f32le', 'pcm_s16le', 'pcm_mulaw', 'pcm_alaw'. Optional. sampleRate number Sample rate for the output audio in Hz (e.g. 8000, 16000, 22050, 24000, 44100, 48000). Optional. bitRate number Bitrate for mp3 output in bits per second (e.g. 32000, 64000, 128000, 192000). Optional. speed number Controls the speed of the generated speech between 0.6 and 1.5. Optional. volume number Volume of the generated speech between 0.5 and 2. Defaults to 1. Requires Sonic 3 or newer. Optional. emotion string Emotion of the generated speech, for example calm or sad. This option is in beta and works best with English. Requires Sonic 3 or newer. See the Cartesia docs for the list of values. Optional. language string The language to generate speech in (ISO 639-1 code). Optional. locale string A regional locale such as en-GB or es-MX. Regional locales require Sonic 3.6 or later. Optional. Cannot be combined with providerOptions.cartesia.language. When a generic language call option is also set, locale takes precedence and the ignored generic option produces an unsupported warning. normalization string Controls how written text is prepared for speech. Use auto (the default), off, or a language or locale code such as en-IN to select reading conventions independently of the speech language. Optional. Cartesia validates the supported values. See the text normalization guide. pronunciationDictionaryId string ID of a pronunciation dictionary to apply to the text. Dictionaries are created with the Cartesia API, not with this provider. Sonic 3 and newer. Optional. Model Capabilities Use cartesiaModelCapabilities(modelId) from @ai-sdk/cartesia to look up a model's declared capabilities and provider options, grouped by endpoint. ModelInstructionssonic-3.5sonic-3sonic-2sonic-turbosonic-latest Realtime Transcription Models Realtime is an experimental feature. You can create a model for Cartesia's realtime Ink 2 API using the .experimental_realtime() factory method: import { cartesia } from '@ai-sdk/cartesia'; const model = cartesia.experimental_realtime('ink-2'); Realtime sessions run in the browser and require a short-lived access token created on your server with cartesia.experimental_realtime.getToken(): const token = await cartesia.experimental_realtime.getToken({ model: 'ink-2', sessionConfig: { inputAudioFormat: { type: 'audio/pcm', rate: 24000 }, inputAudioTranscription: { language: 'en' }, turnDetection: { type: 'server-vad' }, }, }); Ink 2 produces input transcription events and supports English audio. Turn detection is enabled by default. To use manual finalization instead, set turnDetection to null in the session config and call commitAudio() when the current input is complete. See Realtime for the complete browser session setup. Model Capabilities ModelStreaming TranscriptionTurn Detectionink-2 Streaming Transcription Models Ink 2 is also available through the transcription streaming API. Create the model with .transcription() and pass it to streamTranscription: import { cartesia } from '@ai-sdk/cartesia'; import { streamTranscription } from 'ai'; const result = streamTranscription({ model: cartesia.transcription('ink-2'), audio, // ReadableStream<Uint8Array | string> containing raw audio chunks inputAudioFormat: { type: 'audio/pcm', rate: 24000 }, providerOptions: { cartesia: { language: 'en', }, }, }); for await (const part of result.fullStream) { if (part.type === 'transcript-partial') { console.log('partial:', part.text); } if (part.type === 'transcript-final') { console.log('final:', part.text); } } The provider creates a short-lived, STT-scoped Cartesia access token before opening the WebSocket, so the API key is not included in the WebSocket URL. Send audio chunks at approximately their playback rate; Cartesia's realtime endpoint is intended for live audio rather than bulk file upload. Ink 2 supports English audio and uses Cartesia's native turn detection by default. To finalize only when the input audio stream ends, pass providerOptions.cartesia.streaming.turnDetection: false. By default, the provider maps audio/pcm, audio/pcmu, and audio/pcma to 16-bit PCM, G.711 μ-law, and G.711 A-law respectively. To stream another raw PCM representation, set providerOptions.cartesia.streaming.encoding to pcm_s32le, pcm_f16le, or pcm_f32le. For example: const result = streamTranscription({ model: cartesia.transcription('ink-2'), audio, inputAudioFormat: { type: 'audio/pcm', rate: 48000 }, providerOptions: { cartesia: { streaming: { encoding: 'pcm_f32le', }, }, }, }); To bias recognition toward product names or jargon, pass up to 100 terms (up to 1,200 characters in total) in providerOptions.cartesia.streaming.keyterms (keyterm prompting). Turn detection can be tuned with turnStartThreshold, turnEagerEndThreshold, turnEndThreshold and turnEndTimeoutMs; these are ignored, with a warning, when turnDetection is false. See the Cartesia turn detection docs for the accepted ranges. const result = streamTranscription({ model: cartesia.transcription('ink-2'), audio, inputAudioFormat: { type: 'audio/pcm', rate: 24000 }, providerOptions: { cartesia: { streaming: { keyterms: ['Cartesia', 'Sonic'], turnEndTimeoutMs: 800, }, }, }, }); See Transcription for more information about streaming transcription. Model Capabilities ModelStreaming TranscriptionTurn DetectionKeytermsink-2 Batch Transcription Models You can create models that call the Cartesia transcription API using the .transcription() factory method. The first argument is the model id e.g. ink-whisper. const model = cartesia.transcription('ink-whisper'); You can use the model with the transcribe function: import { transcribe } from 'ai'; import { cartesia } from '@ai-sdk/cartesia'; import { readFile } from 'fs/promises'; con