ai-sdk-ollama 4.5.1 → 4.6.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/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # Changelog
2
2
 
3
+ ## 4.6.1
4
+
5
+ ### Patch Changes
6
+
7
+ - f472ffd: Abort the Ollama request when the stream returned by `doStream` is cancelled, so the server stops generating once the consumer stops reading.
8
+ - 1645fac: Fall back to the default embedding reranking batch size when a supplied value would round down to zero, preventing repeated empty embedding requests.
9
+
10
+ ## 4.6.0
11
+
12
+ ### Minor Changes
13
+
14
+ - 4fb2a92: Add `ollama.speech()` for Ollama text-to-speech models such as `phonon`, used with the AI SDK's `generateSpeech`.
15
+
16
+ - `OllamaSpeechModel` implements `SpeechModelV4` on `/v1/audio/speech`. It needs Ollama 0.40.9+ on Apple Silicon.
17
+ - `voice` defaults to `default` and `outputFormat` to `wav`, with `pcm` as the other choice.
18
+ - The model turns `speed`, `instructions`, `language` and other formats into warnings and keeps them out of the request.
19
+ - Ollama's error message reaches you as written, so an unknown voice lists the voices you can use.
20
+
3
21
  ## 4.5.1
4
22
 
5
23
  ### Patch Changes
package/README.md CHANGED
@@ -155,6 +155,7 @@ export OLLAMA_API_KEY="your_api_key_here"
155
155
  - [Simple and Predictable](#simple-and-predictable)
156
156
  - [Reranking](#reranking)
157
157
  - [Decision Models](#decision-models)
158
+ - [Speech](#speech)
158
159
  - [Streaming Utilities](#streaming-utilities)
159
160
  - [Smooth Stream](#smooth-stream)
160
161
  - [Partial JSON Parsing](#partial-json-parsing)
@@ -522,6 +523,26 @@ answers.urgency.score; // 0 to 2, e.g. 0.8
522
523
 
523
524
  Ollama names boolean questions `noul`, and the provider maps them for you. Ollama's per-question `confidence` lands in `providerMetadata.ollama.confidence`. Pull a model first with `ollama pull nimble`.
524
525
 
526
+ ## Speech
527
+
528
+ Ollama 0.40.9 serves text-to-speech models such as [Phonon](https://ollama.com/library/phonon) on `/v1/audio/speech`. Use them with the AI SDK's `generateSpeech`:
529
+
530
+ ```typescript
531
+ import { writeFile } from 'node:fs/promises';
532
+ import { generateSpeech } from 'ai';
533
+ import { ollama } from 'ai-sdk-ollama';
534
+
535
+ const { audio } = await generateSpeech({
536
+ model: ollama.speech('phonon:40m-multilingual'),
537
+ text: 'Bonjour, voici votre résumé du matin.',
538
+ voice: 'Lucien',
539
+ });
540
+
541
+ await writeFile('morning.wav', audio.uint8Array); // 24 kHz mono WAV
542
+ ```
543
+
544
+ `voice` defaults to `default`. Voice names match in any case and with or without accents, and `ollama show phonon` lists them. An unknown voice fails with Ollama's list of available voices. `text` can be up to 4,096 characters per call, so split longer text at sentence breaks. `outputFormat` can be `wav` (default) or `pcm`, which is raw 16-bit little-endian samples with no header. Ollama writes the WAV header before it knows the length, so its size fields hold the maximum value. Players handle that, but use `pcm` if you need exact sizes. Phonon does not support `speed`, `instructions` or `language`, so the provider leaves them out of the request and returns a warning. Choose the language with the model tag and voice. Phonon needs Apple Silicon. Pull a model first with `ollama pull phonon`. The `phonon` (31M English) checkpoint is Apache 2.0, and the 40M checkpoints are CC BY-NC 4.0.
545
+
525
546
  ## Streaming Utilities
526
547
 
527
548
  ### Smooth Stream
@@ -26,6 +26,7 @@ __export(index_browser_exports, {
26
26
  OllamaError: () => OllamaError,
27
27
  OllamaEvaluationModel: () => OllamaEvaluationModel,
28
28
  OllamaRerankingModel: () => OllamaRerankingModel,
29
+ OllamaSpeechModel: () => OllamaSpeechModel,
29
30
  cosineSimilarity: () => cosineSimilarity,
30
31
  createOllama: () => createOllama,
31
32
  generateText: () => generateText,
@@ -2579,6 +2580,9 @@ var OllamaChatLanguageModel = class {
2579
2580
  } catch (error62) {
2580
2581
  controller.error(error62);
2581
2582
  }
2583
+ },
2584
+ cancel() {
2585
+ stream.abort();
2582
2586
  }
2583
2587
  });
2584
2588
  return {
@@ -22523,7 +22527,7 @@ var OllamaEmbeddingRerankingModel = class {
22523
22527
  */
22524
22528
  get embeddingBatchSize() {
22525
22529
  const { maxBatchSize } = this.settings;
22526
- if (typeof maxBatchSize === "number" && Number.isFinite(maxBatchSize) && maxBatchSize > 0) {
22530
+ if (typeof maxBatchSize === "number" && Number.isFinite(maxBatchSize) && maxBatchSize >= 1) {
22527
22531
  return Math.floor(maxBatchSize);
22528
22532
  }
22529
22533
  return 16;
@@ -22719,6 +22723,93 @@ var OllamaImageModel = class {
22719
22723
  }
22720
22724
  };
22721
22725
 
22726
+ // src/models/speech-model.ts
22727
+ var import_provider_utils4 = require("@ai-sdk/provider-utils");
22728
+ var ollamaSpeechFailedResponseHandler = (0, import_provider_utils4.createJsonErrorResponseHandler)({
22729
+ errorSchema: external_exports.object({
22730
+ error: external_exports.union([external_exports.string(), external_exports.object({ message: external_exports.string() })])
22731
+ }),
22732
+ errorToMessage: (data) => typeof data.error === "string" ? data.error : data.error.message
22733
+ });
22734
+ var SUPPORTED_OUTPUT_FORMATS = ["wav", "pcm"];
22735
+ var OllamaSpeechModel = class {
22736
+ constructor(modelId, config2) {
22737
+ __publicField(this, "modelId", modelId);
22738
+ __publicField(this, "config", config2);
22739
+ __publicField(this, "specificationVersion", "v4");
22740
+ __publicField(this, "provider");
22741
+ this.provider = config2.provider;
22742
+ }
22743
+ async doGenerate({
22744
+ text,
22745
+ voice = "default",
22746
+ outputFormat = "wav",
22747
+ instructions,
22748
+ speed,
22749
+ language,
22750
+ abortSignal,
22751
+ headers
22752
+ }) {
22753
+ const warnings = [];
22754
+ let responseFormat = outputFormat;
22755
+ if (!SUPPORTED_OUTPUT_FORMATS.includes(outputFormat)) {
22756
+ responseFormat = "wav";
22757
+ warnings.push({
22758
+ type: "unsupported",
22759
+ feature: "outputFormat",
22760
+ details: `Ollama speech supports wav and pcm. Using wav instead of ${outputFormat}.`
22761
+ });
22762
+ }
22763
+ if (speed != null && speed !== 1) {
22764
+ warnings.push({
22765
+ type: "unsupported",
22766
+ feature: "speed",
22767
+ details: "Ollama speech only supports speed 1."
22768
+ });
22769
+ }
22770
+ if (instructions != null) {
22771
+ warnings.push({ type: "unsupported", feature: "instructions" });
22772
+ }
22773
+ if (language != null) {
22774
+ warnings.push({
22775
+ type: "unsupported",
22776
+ feature: "language",
22777
+ details: "Ollama speech has no language setting. Pick a model and voice for the language instead."
22778
+ });
22779
+ }
22780
+ const body = {
22781
+ model: this.modelId,
22782
+ input: text,
22783
+ voice,
22784
+ response_format: responseFormat
22785
+ };
22786
+ const {
22787
+ value: audio,
22788
+ responseHeaders,
22789
+ rawValue
22790
+ } = await (0, import_provider_utils4.postJsonToApi)({
22791
+ url: `${this.config.baseURL.replace(/\/$/, "")}/v1/audio/speech`,
22792
+ headers: (0, import_provider_utils4.combineHeaders)(this.config.headers(), headers),
22793
+ body,
22794
+ failedResponseHandler: ollamaSpeechFailedResponseHandler,
22795
+ successfulResponseHandler: (0, import_provider_utils4.createBinaryResponseHandler)(),
22796
+ abortSignal,
22797
+ fetch: this.config.fetch
22798
+ });
22799
+ return {
22800
+ audio,
22801
+ warnings,
22802
+ request: { body: JSON.stringify(body) },
22803
+ response: {
22804
+ timestamp: /* @__PURE__ */ new Date(),
22805
+ modelId: this.modelId,
22806
+ headers: responseHeaders,
22807
+ body: rawValue
22808
+ }
22809
+ };
22810
+ }
22811
+ };
22812
+
22722
22813
  // src/models/decision-model.ts
22723
22814
  var import_provider = require("@ai-sdk/provider");
22724
22815
  var isList = (input2) => Array.isArray(input2);
@@ -23163,6 +23254,13 @@ function createOllama(options = {}) {
23163
23254
  provider: "ollama.decision"
23164
23255
  });
23165
23256
  provider.evaluationModel = provider.decisionModel;
23257
+ provider.speechModel = (modelId) => new OllamaSpeechModel(modelId, {
23258
+ provider: "ollama.speech",
23259
+ baseURL,
23260
+ headers: () => normalizedHeaders,
23261
+ fetch: options.fetch
23262
+ });
23263
+ provider.speech = provider.speechModel;
23166
23264
  const toolsWithClient = {
23167
23265
  webSearch: (options2 = {}) => ollamaTools.webSearch({ ...options2, client }),
23168
23266
  webFetch: (options2 = {}) => ollamaTools.webFetch({ ...options2, client })