workers-ai-provider 3.0.5 → 3.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +102 -13
- package/dist/index.d.ts +143 -2
- package/dist/index.js +606 -235
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +85 -0
- package/src/streaming.ts +90 -24
- package/src/utils.ts +84 -2
- package/src/workersai-models.ts +27 -0
- package/src/workersai-reranking-model.ts +106 -0
- package/src/workersai-reranking-settings.ts +1 -0
- package/src/workersai-speech-model.ts +141 -0
- package/src/workersai-speech-settings.ts +1 -0
- package/src/workersai-transcription-model.ts +244 -0
- package/src/workersai-transcription-settings.ts +13 -0
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# workers-ai-provider
|
|
2
2
|
|
|
3
|
-
[Workers AI](https://developers.cloudflare.com/workers-ai/) provider for the [AI SDK](https://sdk.vercel.ai/).
|
|
3
|
+
[Workers AI](https://developers.cloudflare.com/workers-ai/) provider for the [AI SDK](https://sdk.vercel.ai/). Run Cloudflare's models for chat, embeddings, image generation, transcription, text-to-speech, reranking, and [AI Search](https://developers.cloudflare.com/ai-search/) — all from a single provider.
|
|
4
4
|
|
|
5
5
|
## Quick Start
|
|
6
6
|
|
|
@@ -71,13 +71,19 @@ Browse the full catalog at [developers.cloudflare.com/workers-ai/models](https:/
|
|
|
71
71
|
|
|
72
72
|
Some good defaults:
|
|
73
73
|
|
|
74
|
-
| Task
|
|
75
|
-
|
|
|
76
|
-
| Chat
|
|
77
|
-
| Chat
|
|
78
|
-
|
|
|
79
|
-
|
|
|
80
|
-
|
|
|
74
|
+
| Task | Model | Notes |
|
|
75
|
+
| -------------- | ------------------------------------------ | -------------------------------- |
|
|
76
|
+
| Chat | `@cf/meta/llama-4-scout-17b-16e-instruct` | Fast, strong tool calling |
|
|
77
|
+
| Chat | `@cf/meta/llama-3.3-70b-instruct-fp8-fast` | Largest Llama, best quality |
|
|
78
|
+
| Chat | `@cf/openai/gpt-oss-120b` | OpenAI open-weights, high reason |
|
|
79
|
+
| Reasoning | `@cf/qwen/qwq-32b` | Emits `reasoning_content` |
|
|
80
|
+
| Embeddings | `@cf/baai/bge-base-en-v1.5` | 768-dim, English |
|
|
81
|
+
| Embeddings | `@cf/google/embeddinggemma-300m` | 100+ languages, by Google |
|
|
82
|
+
| Images | `@cf/black-forest-labs/flux-1-schnell` | Fast image generation |
|
|
83
|
+
| Transcription | `@cf/openai/whisper-large-v3-turbo` | Best accuracy, multilingual |
|
|
84
|
+
| Transcription | `@cf/deepgram/nova-3` | Fast, high accuracy |
|
|
85
|
+
| Text-to-Speech | `@cf/deepgram/aura-2-en` | Context-aware, natural pacing |
|
|
86
|
+
| Reranking | `@cf/baai/bge-reranker-base` | Fast document reranking |
|
|
81
87
|
|
|
82
88
|
## Text Generation
|
|
83
89
|
|
|
@@ -169,6 +175,80 @@ const { images } = await generateImage({
|
|
|
169
175
|
// images[0].uint8Array contains the PNG bytes
|
|
170
176
|
```
|
|
171
177
|
|
|
178
|
+
## Transcription (Speech-to-Text)
|
|
179
|
+
|
|
180
|
+
Transcribe audio using Whisper or Deepgram Nova-3 models.
|
|
181
|
+
|
|
182
|
+
```ts
|
|
183
|
+
import { transcribe } from "ai";
|
|
184
|
+
import { readFile } from "node:fs/promises";
|
|
185
|
+
|
|
186
|
+
const { text, segments } = await transcribe({
|
|
187
|
+
model: workersai.transcription("@cf/openai/whisper-large-v3-turbo"),
|
|
188
|
+
audio: await readFile("./audio.mp3"),
|
|
189
|
+
mediaType: "audio/mpeg",
|
|
190
|
+
});
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
With language hints (Whisper only):
|
|
194
|
+
|
|
195
|
+
```ts
|
|
196
|
+
const { text } = await transcribe({
|
|
197
|
+
model: workersai.transcription("@cf/openai/whisper-large-v3-turbo", {
|
|
198
|
+
language: "fr",
|
|
199
|
+
}),
|
|
200
|
+
audio: audioBuffer,
|
|
201
|
+
mediaType: "audio/wav",
|
|
202
|
+
});
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
Deepgram Nova-3 is also supported and detects language automatically:
|
|
206
|
+
|
|
207
|
+
```ts
|
|
208
|
+
const { text } = await transcribe({
|
|
209
|
+
model: workersai.transcription("@cf/deepgram/nova-3"),
|
|
210
|
+
audio: audioBuffer,
|
|
211
|
+
mediaType: "audio/wav",
|
|
212
|
+
});
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
## Text-to-Speech
|
|
216
|
+
|
|
217
|
+
Generate spoken audio from text using Deepgram Aura-2.
|
|
218
|
+
|
|
219
|
+
```ts
|
|
220
|
+
import { speech } from "ai";
|
|
221
|
+
|
|
222
|
+
const { audio } = await speech({
|
|
223
|
+
model: workersai.speech("@cf/deepgram/aura-2-en"),
|
|
224
|
+
text: "Hello from Cloudflare Workers AI!",
|
|
225
|
+
voice: "asteria",
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
// audio is a Uint8Array of MP3 bytes
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
## Reranking
|
|
232
|
+
|
|
233
|
+
Reorder documents by relevance to a query — useful for RAG pipelines.
|
|
234
|
+
|
|
235
|
+
```ts
|
|
236
|
+
import { rerank } from "ai";
|
|
237
|
+
|
|
238
|
+
const { results } = await rerank({
|
|
239
|
+
model: workersai.reranking("@cf/baai/bge-reranker-base"),
|
|
240
|
+
query: "What is Cloudflare Workers?",
|
|
241
|
+
documents: [
|
|
242
|
+
"Cloudflare Workers lets you run JavaScript at the edge.",
|
|
243
|
+
"A cookie is a small piece of data stored in the browser.",
|
|
244
|
+
"Workers AI runs inference on Cloudflare's global network.",
|
|
245
|
+
],
|
|
246
|
+
topN: 2,
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
// results is sorted by relevance score
|
|
250
|
+
```
|
|
251
|
+
|
|
172
252
|
## AI Search
|
|
173
253
|
|
|
174
254
|
[AI Search](https://developers.cloudflare.com/ai-search/) is Cloudflare's managed RAG service. Connect your data and query it with natural language.
|
|
@@ -192,7 +272,7 @@ const { text } = await generateText({
|
|
|
192
272
|
});
|
|
193
273
|
```
|
|
194
274
|
|
|
195
|
-
Streaming works the same way
|
|
275
|
+
Streaming works the same way — use `streamText` instead of `generateText`.
|
|
196
276
|
|
|
197
277
|
> `createAutoRAG` still works but is deprecated. Use `createAISearch` instead.
|
|
198
278
|
|
|
@@ -207,18 +287,27 @@ Streaming works the same way -- use `streamText` instead of `generateText`.
|
|
|
207
287
|
| `apiKey` | `string` | Cloudflare API token. Required with `accountId`. |
|
|
208
288
|
| `gateway` | `GatewayOptions` | Optional [AI Gateway](https://developers.cloudflare.com/ai-gateway/) config. |
|
|
209
289
|
|
|
210
|
-
Returns a provider with model factories
|
|
290
|
+
Returns a provider with model factories:
|
|
211
291
|
|
|
212
292
|
```ts
|
|
213
|
-
//
|
|
293
|
+
// Chat — for generateText / streamText
|
|
214
294
|
workersai(modelId);
|
|
215
295
|
workersai.chat(modelId);
|
|
216
296
|
|
|
217
|
-
//
|
|
297
|
+
// Embeddings — for embedMany / embed
|
|
218
298
|
workersai.textEmbedding(modelId);
|
|
219
299
|
|
|
220
|
-
//
|
|
300
|
+
// Images — for generateImage
|
|
221
301
|
workersai.image(modelId);
|
|
302
|
+
|
|
303
|
+
// Transcription — for transcribe
|
|
304
|
+
workersai.transcription(modelId, settings?);
|
|
305
|
+
|
|
306
|
+
// Text-to-Speech — for speech
|
|
307
|
+
workersai.speech(modelId);
|
|
308
|
+
|
|
309
|
+
// Reranking — for rerank
|
|
310
|
+
workersai.reranking(modelId);
|
|
222
311
|
```
|
|
223
312
|
|
|
224
313
|
### `createAISearch(options)`
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { LanguageModelV3, EmbeddingModelV3, EmbeddingModelV3CallOptions, EmbeddingModelV3Result, ImageModelV3 } from '@ai-sdk/provider';
|
|
1
|
+
import { LanguageModelV3, EmbeddingModelV3, EmbeddingModelV3CallOptions, EmbeddingModelV3Result, ImageModelV3, TranscriptionModelV3, SpeechModelV3, RerankingModelV3 } from '@ai-sdk/provider';
|
|
2
2
|
|
|
3
3
|
type AISearchChatSettings = {
|
|
4
4
|
/**
|
|
@@ -21,6 +21,24 @@ type ImageGenerationModels = value2key<AiModels, BaseAiTextToImage>;
|
|
|
21
21
|
* The names of the BaseAiTextToEmbeddings models.
|
|
22
22
|
*/
|
|
23
23
|
type EmbeddingModels = value2key<AiModels, BaseAiTextEmbeddings>;
|
|
24
|
+
/**
|
|
25
|
+
* Workers AI models that support speech-to-text transcription.
|
|
26
|
+
*
|
|
27
|
+
* Includes Whisper variants from `@cloudflare/workers-types` plus
|
|
28
|
+
* Deepgram partner models that may not be in the typed interface yet.
|
|
29
|
+
*/
|
|
30
|
+
type TranscriptionModels = value2key<AiModels, BaseAiAutomaticSpeechRecognition> | "@cf/deepgram/nova-3";
|
|
31
|
+
/**
|
|
32
|
+
* Workers AI models that support text-to-speech.
|
|
33
|
+
*
|
|
34
|
+
* Includes models from `@cloudflare/workers-types` plus Deepgram partner
|
|
35
|
+
* models that may not be in the typed interface yet.
|
|
36
|
+
*/
|
|
37
|
+
type SpeechModels = value2key<AiModels, BaseAiTextToSpeech> | "@cf/deepgram/aura-1" | "@cf/deepgram/aura-2-en" | "@cf/deepgram/aura-2-es";
|
|
38
|
+
/**
|
|
39
|
+
* Workers AI models that support reranking.
|
|
40
|
+
*/
|
|
41
|
+
type RerankingModels = "@cf/baai/bge-reranker-base" | "@cf/baai/bge-reranker-v2-m3";
|
|
24
42
|
type value2key<T, V> = {
|
|
25
43
|
[K in keyof T]: T[K] extends V ? K : never;
|
|
26
44
|
}[keyof T];
|
|
@@ -141,6 +159,114 @@ declare class WorkersAIImageModel implements ImageModelV3 {
|
|
|
141
159
|
doGenerate({ prompt, n, size, aspectRatio, seed, }: Parameters<ImageModelV3["doGenerate"]>[0]): Promise<Awaited<ReturnType<ImageModelV3["doGenerate"]>>>;
|
|
142
160
|
}
|
|
143
161
|
|
|
162
|
+
type WorkersAITranscriptionSettings = {
|
|
163
|
+
/**
|
|
164
|
+
* Language of the audio, as an ISO-639-1 code (e.g. "en", "fr").
|
|
165
|
+
* Only supported by Whisper models. Nova-3 detects language automatically.
|
|
166
|
+
*/
|
|
167
|
+
language?: string;
|
|
168
|
+
/**
|
|
169
|
+
* Initial prompt / context to guide the transcription.
|
|
170
|
+
* Mapped to `initial_prompt` for Whisper models.
|
|
171
|
+
*/
|
|
172
|
+
prompt?: string;
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Parameters for configuring the Cloudflare-based AI runner.
|
|
177
|
+
*/
|
|
178
|
+
interface CreateRunConfig {
|
|
179
|
+
/** Your Cloudflare account identifier. */
|
|
180
|
+
accountId: string;
|
|
181
|
+
/** Cloudflare API token/key with appropriate permissions. */
|
|
182
|
+
apiKey: string;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
type WorkersAITranscriptionConfig = {
|
|
186
|
+
provider: string;
|
|
187
|
+
binding: Ai;
|
|
188
|
+
gateway?: GatewayOptions;
|
|
189
|
+
/**
|
|
190
|
+
* Whether the binding is a real `env.AI` binding (true) or a REST shim (false).
|
|
191
|
+
* Nova-3 uses different upload paths depending on this.
|
|
192
|
+
*/
|
|
193
|
+
isBinding: boolean;
|
|
194
|
+
/**
|
|
195
|
+
* REST credentials, only set when `isBinding` is false.
|
|
196
|
+
* Needed for Nova-3 which requires binary upload, bypassing the JSON-based REST shim.
|
|
197
|
+
*/
|
|
198
|
+
credentials?: CreateRunConfig;
|
|
199
|
+
};
|
|
200
|
+
/**
|
|
201
|
+
* Workers AI transcription model implementing the AI SDK's `TranscriptionModelV3` interface.
|
|
202
|
+
*
|
|
203
|
+
* Supports:
|
|
204
|
+
* - Whisper models (`@cf/openai/whisper`, `whisper-tiny-en`, `whisper-large-v3-turbo`)
|
|
205
|
+
* - Deepgram Nova-3 (`@cf/deepgram/nova-3`) — uses a different input/output format
|
|
206
|
+
*/
|
|
207
|
+
declare class WorkersAITranscriptionModel implements TranscriptionModelV3 {
|
|
208
|
+
readonly modelId: TranscriptionModels;
|
|
209
|
+
readonly settings: WorkersAITranscriptionSettings;
|
|
210
|
+
readonly config: WorkersAITranscriptionConfig;
|
|
211
|
+
readonly specificationVersion = "v3";
|
|
212
|
+
get provider(): string;
|
|
213
|
+
constructor(modelId: TranscriptionModels, settings: WorkersAITranscriptionSettings, config: WorkersAITranscriptionConfig);
|
|
214
|
+
doGenerate(options: Parameters<TranscriptionModelV3["doGenerate"]>[0]): Promise<Awaited<ReturnType<TranscriptionModelV3["doGenerate"]>>>;
|
|
215
|
+
private runWhisper;
|
|
216
|
+
private normalizeWhisperResponse;
|
|
217
|
+
private runNova3;
|
|
218
|
+
private normalizeNova3Response;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
type WorkersAISpeechSettings = {};
|
|
222
|
+
|
|
223
|
+
type WorkersAISpeechConfig = {
|
|
224
|
+
provider: string;
|
|
225
|
+
binding: Ai;
|
|
226
|
+
gateway?: GatewayOptions;
|
|
227
|
+
};
|
|
228
|
+
/**
|
|
229
|
+
* Workers AI speech (text-to-speech) model implementing the AI SDK's `SpeechModelV3` interface.
|
|
230
|
+
*
|
|
231
|
+
* Currently supports Deepgram Aura-1 (`@cf/deepgram/aura-1`).
|
|
232
|
+
* The model accepts `{ text, voice?, speed? }` and returns raw audio bytes.
|
|
233
|
+
*/
|
|
234
|
+
declare class WorkersAISpeechModel implements SpeechModelV3 {
|
|
235
|
+
readonly modelId: SpeechModels;
|
|
236
|
+
readonly settings: WorkersAISpeechSettings;
|
|
237
|
+
readonly config: WorkersAISpeechConfig;
|
|
238
|
+
readonly specificationVersion = "v3";
|
|
239
|
+
get provider(): string;
|
|
240
|
+
constructor(modelId: SpeechModels, settings: WorkersAISpeechSettings, config: WorkersAISpeechConfig);
|
|
241
|
+
doGenerate(options: Parameters<SpeechModelV3["doGenerate"]>[0]): Promise<Awaited<ReturnType<SpeechModelV3["doGenerate"]>>>;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
type WorkersAIRerankingSettings = {};
|
|
245
|
+
|
|
246
|
+
type WorkersAIRerankingConfig = {
|
|
247
|
+
provider: string;
|
|
248
|
+
binding: Ai;
|
|
249
|
+
gateway?: GatewayOptions;
|
|
250
|
+
};
|
|
251
|
+
/**
|
|
252
|
+
* Workers AI reranking model implementing the AI SDK's `RerankingModelV3` interface.
|
|
253
|
+
*
|
|
254
|
+
* Supports BGE reranker models (`@cf/baai/bge-reranker-base`, `bge-reranker-v2-m3`).
|
|
255
|
+
*
|
|
256
|
+
* Workers AI reranking API:
|
|
257
|
+
* - Input: `{ query, contexts: [{ text }], top_k? }`
|
|
258
|
+
* - Output: `{ response: [{ id, score }] }`
|
|
259
|
+
*/
|
|
260
|
+
declare class WorkersAIRerankingModel implements RerankingModelV3 {
|
|
261
|
+
readonly modelId: RerankingModels;
|
|
262
|
+
readonly settings: WorkersAIRerankingSettings;
|
|
263
|
+
readonly config: WorkersAIRerankingConfig;
|
|
264
|
+
readonly specificationVersion = "v3";
|
|
265
|
+
get provider(): string;
|
|
266
|
+
constructor(modelId: RerankingModels, settings: WorkersAIRerankingSettings, config: WorkersAIRerankingConfig);
|
|
267
|
+
doRerank(options: Parameters<RerankingModelV3["doRerank"]>[0]): Promise<Awaited<ReturnType<RerankingModelV3["doRerank"]>>>;
|
|
268
|
+
}
|
|
269
|
+
|
|
144
270
|
/**
|
|
145
271
|
* @deprecated Use `AISearchChatLanguageModel` instead. AutoRAG has been renamed to AI Search.
|
|
146
272
|
* @see https://developers.cloudflare.com/ai-search/
|
|
@@ -194,6 +320,21 @@ interface WorkersAI {
|
|
|
194
320
|
**/
|
|
195
321
|
image(modelId: ImageGenerationModels, settings?: WorkersAIImageSettings): WorkersAIImageModel;
|
|
196
322
|
imageModel(modelId: ImageGenerationModels, settings?: WorkersAIImageSettings): WorkersAIImageModel;
|
|
323
|
+
/**
|
|
324
|
+
* Creates a model for speech-to-text transcription.
|
|
325
|
+
**/
|
|
326
|
+
transcription(modelId: TranscriptionModels, settings?: WorkersAITranscriptionSettings): WorkersAITranscriptionModel;
|
|
327
|
+
transcriptionModel(modelId: TranscriptionModels, settings?: WorkersAITranscriptionSettings): WorkersAITranscriptionModel;
|
|
328
|
+
/**
|
|
329
|
+
* Creates a model for text-to-speech synthesis.
|
|
330
|
+
**/
|
|
331
|
+
speech(modelId: SpeechModels, settings?: WorkersAISpeechSettings): WorkersAISpeechModel;
|
|
332
|
+
speechModel(modelId: SpeechModels, settings?: WorkersAISpeechSettings): WorkersAISpeechModel;
|
|
333
|
+
/**
|
|
334
|
+
* Creates a model for document reranking.
|
|
335
|
+
**/
|
|
336
|
+
reranking(modelId: RerankingModels, settings?: WorkersAIRerankingSettings): WorkersAIRerankingModel;
|
|
337
|
+
rerankingModel(modelId: RerankingModels, settings?: WorkersAIRerankingSettings): WorkersAIRerankingModel;
|
|
197
338
|
}
|
|
198
339
|
/**
|
|
199
340
|
* Create a Workers AI provider instance.
|
|
@@ -234,4 +375,4 @@ type AutoRAGProvider = AISearchProvider;
|
|
|
234
375
|
*/
|
|
235
376
|
declare function createAutoRAG(options: AISearchSettings): AISearchProvider;
|
|
236
377
|
|
|
237
|
-
export { AISearchChatLanguageModel, type AISearchChatSettings, type AISearchProvider, type AISearchSettings, AutoRAGChatLanguageModel, type AutoRAGChatSettings, type AutoRAGProvider, type AutoRAGSettings, type WorkersAI, type WorkersAISettings, createAISearch, createAutoRAG, createWorkersAI };
|
|
378
|
+
export { AISearchChatLanguageModel, type AISearchChatSettings, type AISearchProvider, type AISearchSettings, AutoRAGChatLanguageModel, type AutoRAGChatSettings, type AutoRAGProvider, type AutoRAGSettings, type WorkersAI, WorkersAIRerankingModel, type WorkersAIRerankingSettings, type WorkersAISettings, WorkersAISpeechModel, type WorkersAISpeechSettings, WorkersAITranscriptionModel, type WorkersAITranscriptionSettings, createAISearch, createAutoRAG, createWorkersAI };
|