ai-sdk-ollama 4.5.0 → 4.6.0

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,22 @@
1
1
  # Changelog
2
2
 
3
+ ## 4.6.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 4fb2a92: Add `ollama.speech()` for Ollama text-to-speech models such as `phonon`, used with the AI SDK's `generateSpeech`.
8
+
9
+ - `OllamaSpeechModel` implements `SpeechModelV4` on `/v1/audio/speech`. It needs Ollama 0.40.9+ on Apple Silicon.
10
+ - `voice` defaults to `default` and `outputFormat` to `wav`, with `pcm` as the other choice.
11
+ - The model turns `speed`, `instructions`, `language` and other formats into warnings and keeps them out of the request.
12
+ - Ollama's error message reaches you as written, so an unknown voice lists the voices you can use.
13
+
14
+ ## 4.5.1
15
+
16
+ ### Patch Changes
17
+
18
+ - cc1d4f7: Report `finishReason: 'length'` when Ollama truncates a response that contains tool calls. Before this fix the provider reported `tool-calls`, so the AI SDK ran tools from a cut-off turn (#1092).
19
+
3
20
  ## 4.5.0
4
21
 
5
22
  ### Minor 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,
@@ -1702,7 +1703,7 @@ function cleanSchemaForOllama(schema) {
1702
1703
 
1703
1704
  // src/utils/map-ollama-finish-reason.ts
1704
1705
  function mapOllamaFinishReason(reason, hasToolCalls = false) {
1705
- if (hasToolCalls) {
1706
+ if (hasToolCalls && reason !== "length") {
1706
1707
  return {
1707
1708
  unified: "tool-calls",
1708
1709
  raw: reason || void 0
@@ -22719,6 +22720,93 @@ var OllamaImageModel = class {
22719
22720
  }
22720
22721
  };
22721
22722
 
22723
+ // src/models/speech-model.ts
22724
+ var import_provider_utils4 = require("@ai-sdk/provider-utils");
22725
+ var ollamaSpeechFailedResponseHandler = (0, import_provider_utils4.createJsonErrorResponseHandler)({
22726
+ errorSchema: external_exports.object({
22727
+ error: external_exports.union([external_exports.string(), external_exports.object({ message: external_exports.string() })])
22728
+ }),
22729
+ errorToMessage: (data) => typeof data.error === "string" ? data.error : data.error.message
22730
+ });
22731
+ var SUPPORTED_OUTPUT_FORMATS = ["wav", "pcm"];
22732
+ var OllamaSpeechModel = class {
22733
+ constructor(modelId, config2) {
22734
+ __publicField(this, "modelId", modelId);
22735
+ __publicField(this, "config", config2);
22736
+ __publicField(this, "specificationVersion", "v4");
22737
+ __publicField(this, "provider");
22738
+ this.provider = config2.provider;
22739
+ }
22740
+ async doGenerate({
22741
+ text,
22742
+ voice = "default",
22743
+ outputFormat = "wav",
22744
+ instructions,
22745
+ speed,
22746
+ language,
22747
+ abortSignal,
22748
+ headers
22749
+ }) {
22750
+ const warnings = [];
22751
+ let responseFormat = outputFormat;
22752
+ if (!SUPPORTED_OUTPUT_FORMATS.includes(outputFormat)) {
22753
+ responseFormat = "wav";
22754
+ warnings.push({
22755
+ type: "unsupported",
22756
+ feature: "outputFormat",
22757
+ details: `Ollama speech supports wav and pcm. Using wav instead of ${outputFormat}.`
22758
+ });
22759
+ }
22760
+ if (speed != null && speed !== 1) {
22761
+ warnings.push({
22762
+ type: "unsupported",
22763
+ feature: "speed",
22764
+ details: "Ollama speech only supports speed 1."
22765
+ });
22766
+ }
22767
+ if (instructions != null) {
22768
+ warnings.push({ type: "unsupported", feature: "instructions" });
22769
+ }
22770
+ if (language != null) {
22771
+ warnings.push({
22772
+ type: "unsupported",
22773
+ feature: "language",
22774
+ details: "Ollama speech has no language setting. Pick a model and voice for the language instead."
22775
+ });
22776
+ }
22777
+ const body = {
22778
+ model: this.modelId,
22779
+ input: text,
22780
+ voice,
22781
+ response_format: responseFormat
22782
+ };
22783
+ const {
22784
+ value: audio,
22785
+ responseHeaders,
22786
+ rawValue
22787
+ } = await (0, import_provider_utils4.postJsonToApi)({
22788
+ url: `${this.config.baseURL.replace(/\/$/, "")}/v1/audio/speech`,
22789
+ headers: (0, import_provider_utils4.combineHeaders)(this.config.headers(), headers),
22790
+ body,
22791
+ failedResponseHandler: ollamaSpeechFailedResponseHandler,
22792
+ successfulResponseHandler: (0, import_provider_utils4.createBinaryResponseHandler)(),
22793
+ abortSignal,
22794
+ fetch: this.config.fetch
22795
+ });
22796
+ return {
22797
+ audio,
22798
+ warnings,
22799
+ request: { body: JSON.stringify(body) },
22800
+ response: {
22801
+ timestamp: /* @__PURE__ */ new Date(),
22802
+ modelId: this.modelId,
22803
+ headers: responseHeaders,
22804
+ body: rawValue
22805
+ }
22806
+ };
22807
+ }
22808
+ };
22809
+
22722
22810
  // src/models/decision-model.ts
22723
22811
  var import_provider = require("@ai-sdk/provider");
22724
22812
  var isList = (input2) => Array.isArray(input2);
@@ -23163,6 +23251,13 @@ function createOllama(options = {}) {
23163
23251
  provider: "ollama.decision"
23164
23252
  });
23165
23253
  provider.evaluationModel = provider.decisionModel;
23254
+ provider.speechModel = (modelId) => new OllamaSpeechModel(modelId, {
23255
+ provider: "ollama.speech",
23256
+ baseURL,
23257
+ headers: () => normalizedHeaders,
23258
+ fetch: options.fetch
23259
+ });
23260
+ provider.speech = provider.speechModel;
23166
23261
  const toolsWithClient = {
23167
23262
  webSearch: (options2 = {}) => ollamaTools.webSearch({ ...options2, client }),
23168
23263
  webFetch: (options2 = {}) => ollamaTools.webFetch({ ...options2, client })