modelfusion 0.26.0 → 0.27.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.
Files changed (128) hide show
  1. package/index.cjs +1 -0
  2. package/index.d.ts +1 -0
  3. package/index.js +1 -0
  4. package/model-function/ApiConfiguration.d.ts +14 -0
  5. package/model-function/BasicApiConfiguration.cjs +35 -0
  6. package/model-function/BasicApiConfiguration.d.ts +15 -0
  7. package/model-function/BasicApiConfiguration.js +31 -0
  8. package/model-function/index.cjs +2 -0
  9. package/model-function/index.d.ts +2 -0
  10. package/model-function/index.js +2 -0
  11. package/model-provider/automatic1111/Automatic1111ApiConfiguration.cjs +14 -0
  12. package/model-provider/automatic1111/Automatic1111ApiConfiguration.d.ts +10 -0
  13. package/model-provider/automatic1111/Automatic1111ApiConfiguration.js +10 -0
  14. package/model-provider/automatic1111/Automatic1111ImageGenerationModel.cjs +10 -7
  15. package/model-provider/automatic1111/Automatic1111ImageGenerationModel.d.ts +2 -5
  16. package/model-provider/automatic1111/Automatic1111ImageGenerationModel.js +10 -7
  17. package/model-provider/automatic1111/index.cjs +1 -0
  18. package/model-provider/automatic1111/index.d.ts +1 -0
  19. package/model-provider/automatic1111/index.js +1 -0
  20. package/model-provider/cohere/CohereApiConfiguration.cjs +29 -0
  21. package/model-provider/cohere/CohereApiConfiguration.d.ts +13 -0
  22. package/model-provider/cohere/CohereApiConfiguration.js +25 -0
  23. package/model-provider/cohere/CohereTextEmbeddingModel.cjs +11 -38
  24. package/model-provider/cohere/CohereTextEmbeddingModel.d.ts +2 -11
  25. package/model-provider/cohere/CohereTextEmbeddingModel.js +11 -38
  26. package/model-provider/cohere/CohereTextGenerationModel.cjs +7 -36
  27. package/model-provider/cohere/CohereTextGenerationModel.d.ts +2 -11
  28. package/model-provider/cohere/CohereTextGenerationModel.js +7 -36
  29. package/model-provider/cohere/CohereTokenizer.cjs +11 -33
  30. package/model-provider/cohere/CohereTokenizer.d.ts +4 -9
  31. package/model-provider/cohere/CohereTokenizer.js +11 -33
  32. package/model-provider/cohere/index.cjs +1 -0
  33. package/model-provider/cohere/index.d.ts +1 -0
  34. package/model-provider/cohere/index.js +1 -0
  35. package/model-provider/elevenlabs/ElevenLabsApiConfiguration.cjs +29 -0
  36. package/model-provider/elevenlabs/ElevenLabsApiConfiguration.d.ts +13 -0
  37. package/model-provider/elevenlabs/ElevenLabsApiConfiguration.js +25 -0
  38. package/model-provider/elevenlabs/ElevenLabsSpeechSynthesisModel.cjs +22 -32
  39. package/model-provider/elevenlabs/ElevenLabsSpeechSynthesisModel.d.ts +8 -8
  40. package/model-provider/elevenlabs/ElevenLabsSpeechSynthesisModel.js +22 -32
  41. package/model-provider/elevenlabs/index.cjs +1 -0
  42. package/model-provider/elevenlabs/index.d.ts +1 -0
  43. package/model-provider/elevenlabs/index.js +1 -0
  44. package/model-provider/huggingface/HuggingFaceApiConfiguration.cjs +29 -0
  45. package/model-provider/huggingface/HuggingFaceApiConfiguration.d.ts +13 -0
  46. package/model-provider/huggingface/HuggingFaceApiConfiguration.js +25 -0
  47. package/model-provider/huggingface/HuggingFaceError.cjs +3 -2
  48. package/model-provider/huggingface/HuggingFaceError.d.ts +3 -3
  49. package/model-provider/huggingface/HuggingFaceError.js +3 -2
  50. package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.cjs +15 -21
  51. package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.d.ts +3 -8
  52. package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.js +15 -21
  53. package/model-provider/huggingface/HuggingFaceTextGenerationModel.cjs +7 -36
  54. package/model-provider/huggingface/HuggingFaceTextGenerationModel.d.ts +2 -7
  55. package/model-provider/huggingface/HuggingFaceTextGenerationModel.js +7 -36
  56. package/model-provider/huggingface/index.cjs +1 -0
  57. package/model-provider/huggingface/index.d.ts +1 -0
  58. package/model-provider/huggingface/index.js +1 -0
  59. package/model-provider/llamacpp/LlamaCppApiConfiguration.cjs +14 -0
  60. package/model-provider/llamacpp/LlamaCppApiConfiguration.d.ts +10 -0
  61. package/model-provider/llamacpp/LlamaCppApiConfiguration.js +10 -0
  62. package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.cjs +11 -12
  63. package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.d.ts +2 -9
  64. package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.js +11 -12
  65. package/model-provider/llamacpp/LlamaCppTextGenerationModel.cjs +9 -10
  66. package/model-provider/llamacpp/LlamaCppTextGenerationModel.d.ts +2 -9
  67. package/model-provider/llamacpp/LlamaCppTextGenerationModel.js +9 -10
  68. package/model-provider/llamacpp/LlamaCppTokenizer.cjs +10 -8
  69. package/model-provider/llamacpp/LlamaCppTokenizer.d.ts +4 -10
  70. package/model-provider/llamacpp/LlamaCppTokenizer.js +10 -8
  71. package/model-provider/llamacpp/index.cjs +1 -0
  72. package/model-provider/llamacpp/index.d.ts +1 -0
  73. package/model-provider/llamacpp/index.js +1 -0
  74. package/model-provider/lmnt/LmntApiConfiguration.cjs +29 -0
  75. package/model-provider/lmnt/LmntApiConfiguration.d.ts +13 -0
  76. package/model-provider/lmnt/LmntApiConfiguration.js +25 -0
  77. package/model-provider/lmnt/LmntSpeechSynthesisModel.cjs +18 -34
  78. package/model-provider/lmnt/LmntSpeechSynthesisModel.d.ts +8 -8
  79. package/model-provider/lmnt/LmntSpeechSynthesisModel.js +18 -34
  80. package/model-provider/lmnt/index.cjs +1 -0
  81. package/model-provider/lmnt/index.d.ts +1 -0
  82. package/model-provider/lmnt/index.js +1 -0
  83. package/model-provider/openai/AzureOpenAIApiConfiguration.cjs +62 -0
  84. package/model-provider/openai/AzureOpenAIApiConfiguration.d.ts +26 -0
  85. package/model-provider/openai/AzureOpenAIApiConfiguration.js +58 -0
  86. package/model-provider/openai/OpenAIApiConfiguration.cjs +31 -0
  87. package/model-provider/openai/OpenAIApiConfiguration.d.ts +13 -0
  88. package/model-provider/openai/OpenAIApiConfiguration.js +27 -0
  89. package/model-provider/openai/OpenAIImageGenerationModel.cjs +16 -20
  90. package/model-provider/openai/OpenAIImageGenerationModel.d.ts +4 -4
  91. package/model-provider/openai/OpenAIImageGenerationModel.js +16 -20
  92. package/model-provider/openai/OpenAITextEmbeddingModel.cjs +16 -35
  93. package/model-provider/openai/OpenAITextEmbeddingModel.d.ts +2 -7
  94. package/model-provider/openai/OpenAITextEmbeddingModel.js +16 -35
  95. package/model-provider/openai/OpenAITextGenerationModel.cjs +6 -33
  96. package/model-provider/openai/OpenAITextGenerationModel.d.ts +4 -5
  97. package/model-provider/openai/OpenAITextGenerationModel.js +6 -33
  98. package/model-provider/openai/OpenAITranscriptionModel.cjs +16 -39
  99. package/model-provider/openai/OpenAITranscriptionModel.d.ts +4 -11
  100. package/model-provider/openai/OpenAITranscriptionModel.js +16 -39
  101. package/model-provider/openai/chat/OpenAIChatModel.cjs +9 -17
  102. package/model-provider/openai/chat/OpenAIChatModel.d.ts +4 -5
  103. package/model-provider/openai/chat/OpenAIChatModel.js +9 -17
  104. package/model-provider/openai/index.cjs +2 -1
  105. package/model-provider/openai/index.d.ts +2 -1
  106. package/model-provider/openai/index.js +2 -1
  107. package/model-provider/stability/StabilityApiConfiguration.cjs +31 -0
  108. package/model-provider/stability/StabilityApiConfiguration.d.ts +13 -0
  109. package/model-provider/stability/StabilityApiConfiguration.js +27 -0
  110. package/model-provider/stability/StabilityImageGenerationModel.cjs +12 -46
  111. package/model-provider/stability/StabilityImageGenerationModel.d.ts +2 -7
  112. package/model-provider/stability/StabilityImageGenerationModel.js +12 -46
  113. package/model-provider/stability/index.cjs +1 -0
  114. package/model-provider/stability/index.d.ts +1 -0
  115. package/model-provider/stability/index.js +1 -0
  116. package/observability/helicone/HeliconeOpenAIApiConfiguration.cjs +45 -0
  117. package/observability/helicone/HeliconeOpenAIApiConfiguration.d.ts +15 -0
  118. package/observability/helicone/HeliconeOpenAIApiConfiguration.js +41 -0
  119. package/observability/index.cjs +17 -0
  120. package/observability/index.d.ts +1 -0
  121. package/observability/index.js +1 -0
  122. package/package.json +1 -1
  123. package/util/api/loadApiKey.cjs +11 -0
  124. package/util/api/loadApiKey.d.ts +6 -0
  125. package/util/api/loadApiKey.js +7 -0
  126. package/model-provider/openai/OpenAIModelSettings.d.ts +0 -8
  127. /package/{model-provider/openai/OpenAIModelSettings.cjs → model-function/ApiConfiguration.cjs} +0 -0
  128. /package/{model-provider/openai/OpenAIModelSettings.js → model-function/ApiConfiguration.js} +0 -0
@@ -12,6 +12,7 @@ const countTokens_js_1 = require("../../model-function/tokenize-text/countTokens
12
12
  const PromptFormatTextGenerationModel_js_1 = require("../../prompt/PromptFormatTextGenerationModel.cjs");
13
13
  const callWithRetryAndThrottle_js_1 = require("../../util/api/callWithRetryAndThrottle.cjs");
14
14
  const postToApi_js_1 = require("../../util/api/postToApi.cjs");
15
+ const CohereApiConfiguration_js_1 = require("./CohereApiConfiguration.cjs");
15
16
  const CohereError_js_1 = require("./CohereError.cjs");
16
17
  const CohereTokenizer_js_1 = require("./CohereTokenizer.cjs");
17
18
  exports.COHERE_TEXT_GENERATION_MODELS = {
@@ -69,23 +70,13 @@ class CohereTextGenerationModel extends AbstractModel_js_1.AbstractModel {
69
70
  this.contextWindowSize =
70
71
  exports.COHERE_TEXT_GENERATION_MODELS[this.settings.model].contextWindowSize;
71
72
  this.tokenizer = new CohereTokenizer_js_1.CohereTokenizer({
72
- baseUrl: this.settings.baseUrl,
73
- apiKey: this.settings.apiKey,
73
+ api: this.settings.api,
74
74
  model: this.settings.model,
75
- retry: this.settings.tokenizerSettings?.retry,
76
- throttle: this.settings.tokenizerSettings?.throttle,
77
75
  });
78
76
  }
79
77
  get modelName() {
80
78
  return this.settings.model;
81
79
  }
82
- get apiKey() {
83
- const apiKey = this.settings.apiKey ?? process.env.COHERE_API_KEY;
84
- if (apiKey == null) {
85
- throw new Error("No Cohere API key provided. Pass an API key to the constructor or set the COHERE_API_KEY environment variable.");
86
- }
87
- return apiKey;
88
- }
89
80
  async countPromptTokens(input) {
90
81
  return (0, countTokens_js_1.countTokens)(this.tokenizer, input);
91
82
  }
@@ -96,7 +87,6 @@ class CohereTextGenerationModel extends AbstractModel_js_1.AbstractModel {
96
87
  settings,
97
88
  };
98
89
  const callSettings = {
99
- apiKey: this.apiKey,
100
90
  ...combinedSettings,
101
91
  // use endSequences instead of stopSequences
102
92
  // to exclude stop tokens from the generated text
@@ -109,8 +99,8 @@ class CohereTextGenerationModel extends AbstractModel_js_1.AbstractModel {
109
99
  responseFormat,
110
100
  };
111
101
  return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
112
- retry: this.settings.retry,
113
- throttle: this.settings.throttle,
102
+ retry: callSettings.api?.retry,
103
+ throttle: callSettings.api?.throttle,
114
104
  call: async () => callCohereTextGenerationAPI(callSettings),
115
105
  });
116
106
  }
@@ -118,7 +108,6 @@ class CohereTextGenerationModel extends AbstractModel_js_1.AbstractModel {
118
108
  const eventSettingProperties = [
119
109
  "maxCompletionTokens",
120
110
  "stopSequences",
121
- "baseUrl",
122
111
  "numGenerations",
123
112
  "temperature",
124
113
  "k",
@@ -179,28 +168,10 @@ const cohereTextGenerationResponseSchema = zod_1.z.object({
179
168
  })
180
169
  .optional(),
181
170
  });
182
- /**
183
- * Call the Cohere Co.Generate API to generate a text completion for the given prompt.
184
- *
185
- * @see https://docs.cohere.com/reference/generate
186
- *
187
- * @example
188
- * const response = await callCohereTextGenerationAPI({
189
- * apiKey: COHERE_API_KEY,
190
- * model: "command-nightly",
191
- * prompt: "Write a short story about a robot learning to love:\n\n",
192
- * temperature: 0.7,
193
- * maxTokens: 500,
194
- * });
195
- *
196
- * console.log(response.generations[0].text);
197
- */
198
- async function callCohereTextGenerationAPI({ baseUrl = "https://api.cohere.ai/v1", abortSignal, responseFormat, apiKey, model, prompt, numGenerations, maxTokens, temperature, k, p, frequencyPenalty, presencePenalty, endSequences, stopSequences, returnLikelihoods, logitBias, truncate, }) {
171
+ async function callCohereTextGenerationAPI({ api = new CohereApiConfiguration_js_1.CohereApiConfiguration(), abortSignal, responseFormat, model, prompt, numGenerations, maxTokens, temperature, k, p, frequencyPenalty, presencePenalty, endSequences, stopSequences, returnLikelihoods, logitBias, truncate, }) {
199
172
  return (0, postToApi_js_1.postJsonToApi)({
200
- url: `${baseUrl}/generate`,
201
- headers: {
202
- Authorization: `Bearer ${apiKey}`,
203
- },
173
+ url: api.assembleUrl(`/generate`),
174
+ headers: api.headers,
204
175
  body: {
205
176
  stream: responseFormat.stream,
206
177
  model,
@@ -1,12 +1,11 @@
1
1
  import { z } from "zod";
2
2
  import { AbstractModel } from "../../model-function/AbstractModel.js";
3
+ import { ApiConfiguration } from "../../model-function/ApiConfiguration.js";
3
4
  import { ModelFunctionOptions } from "../../model-function/ModelFunctionOptions.js";
4
5
  import { DeltaEvent } from "../../model-function/generate-text/DeltaEvent.js";
5
6
  import { TextGenerationModel, TextGenerationModelSettings } from "../../model-function/generate-text/TextGenerationModel.js";
6
7
  import { PromptFormat } from "../../prompt/PromptFormat.js";
7
8
  import { PromptFormatTextGenerationModel } from "../../prompt/PromptFormatTextGenerationModel.js";
8
- import { RetryFunction } from "../../util/api/RetryFunction.js";
9
- import { ThrottleFunction } from "../../util/api/ThrottleFunction.js";
10
9
  import { ResponseHandler } from "../../util/api/postToApi.js";
11
10
  import { CohereTokenizer } from "./CohereTokenizer.js";
12
11
  export declare const COHERE_TEXT_GENERATION_MODELS: {
@@ -25,15 +24,8 @@ export declare const COHERE_TEXT_GENERATION_MODELS: {
25
24
  };
26
25
  export type CohereTextGenerationModelType = keyof typeof COHERE_TEXT_GENERATION_MODELS;
27
26
  export interface CohereTextGenerationModelSettings extends TextGenerationModelSettings {
27
+ api?: ApiConfiguration;
28
28
  model: CohereTextGenerationModelType;
29
- baseUrl?: string;
30
- apiKey?: string;
31
- retry?: RetryFunction;
32
- throttle?: ThrottleFunction;
33
- tokenizerSettings?: {
34
- retry?: RetryFunction;
35
- throttle?: ThrottleFunction;
36
- };
37
29
  numGenerations?: number;
38
30
  temperature?: number;
39
31
  k?: number;
@@ -68,7 +60,6 @@ export declare class CohereTextGenerationModel extends AbstractModel<CohereTextG
68
60
  get modelName(): "command" | "command-nightly" | "command-light" | "command-light-nightly";
69
61
  readonly contextWindowSize: number;
70
62
  readonly tokenizer: CohereTokenizer;
71
- private get apiKey();
72
63
  countPromptTokens(input: string): Promise<number>;
73
64
  callAPI<RESPONSE>(prompt: string, options: {
74
65
  responseFormat: CohereTextGenerationResponseFormatType<RESPONSE>;
@@ -6,6 +6,7 @@ import { countTokens } from "../../model-function/tokenize-text/countTokens.js";
6
6
  import { PromptFormatTextGenerationModel } from "../../prompt/PromptFormatTextGenerationModel.js";
7
7
  import { callWithRetryAndThrottle } from "../../util/api/callWithRetryAndThrottle.js";
8
8
  import { createJsonResponseHandler, postJsonToApi, } from "../../util/api/postToApi.js";
9
+ import { CohereApiConfiguration } from "./CohereApiConfiguration.js";
9
10
  import { failedCohereCallResponseHandler } from "./CohereError.js";
10
11
  import { CohereTokenizer } from "./CohereTokenizer.js";
11
12
  export const COHERE_TEXT_GENERATION_MODELS = {
@@ -63,23 +64,13 @@ export class CohereTextGenerationModel extends AbstractModel {
63
64
  this.contextWindowSize =
64
65
  COHERE_TEXT_GENERATION_MODELS[this.settings.model].contextWindowSize;
65
66
  this.tokenizer = new CohereTokenizer({
66
- baseUrl: this.settings.baseUrl,
67
- apiKey: this.settings.apiKey,
67
+ api: this.settings.api,
68
68
  model: this.settings.model,
69
- retry: this.settings.tokenizerSettings?.retry,
70
- throttle: this.settings.tokenizerSettings?.throttle,
71
69
  });
72
70
  }
73
71
  get modelName() {
74
72
  return this.settings.model;
75
73
  }
76
- get apiKey() {
77
- const apiKey = this.settings.apiKey ?? process.env.COHERE_API_KEY;
78
- if (apiKey == null) {
79
- throw new Error("No Cohere API key provided. Pass an API key to the constructor or set the COHERE_API_KEY environment variable.");
80
- }
81
- return apiKey;
82
- }
83
74
  async countPromptTokens(input) {
84
75
  return countTokens(this.tokenizer, input);
85
76
  }
@@ -90,7 +81,6 @@ export class CohereTextGenerationModel extends AbstractModel {
90
81
  settings,
91
82
  };
92
83
  const callSettings = {
93
- apiKey: this.apiKey,
94
84
  ...combinedSettings,
95
85
  // use endSequences instead of stopSequences
96
86
  // to exclude stop tokens from the generated text
@@ -103,8 +93,8 @@ export class CohereTextGenerationModel extends AbstractModel {
103
93
  responseFormat,
104
94
  };
105
95
  return callWithRetryAndThrottle({
106
- retry: this.settings.retry,
107
- throttle: this.settings.throttle,
96
+ retry: callSettings.api?.retry,
97
+ throttle: callSettings.api?.throttle,
108
98
  call: async () => callCohereTextGenerationAPI(callSettings),
109
99
  });
110
100
  }
@@ -112,7 +102,6 @@ export class CohereTextGenerationModel extends AbstractModel {
112
102
  const eventSettingProperties = [
113
103
  "maxCompletionTokens",
114
104
  "stopSequences",
115
- "baseUrl",
116
105
  "numGenerations",
117
106
  "temperature",
118
107
  "k",
@@ -172,28 +161,10 @@ const cohereTextGenerationResponseSchema = z.object({
172
161
  })
173
162
  .optional(),
174
163
  });
175
- /**
176
- * Call the Cohere Co.Generate API to generate a text completion for the given prompt.
177
- *
178
- * @see https://docs.cohere.com/reference/generate
179
- *
180
- * @example
181
- * const response = await callCohereTextGenerationAPI({
182
- * apiKey: COHERE_API_KEY,
183
- * model: "command-nightly",
184
- * prompt: "Write a short story about a robot learning to love:\n\n",
185
- * temperature: 0.7,
186
- * maxTokens: 500,
187
- * });
188
- *
189
- * console.log(response.generations[0].text);
190
- */
191
- async function callCohereTextGenerationAPI({ baseUrl = "https://api.cohere.ai/v1", abortSignal, responseFormat, apiKey, model, prompt, numGenerations, maxTokens, temperature, k, p, frequencyPenalty, presencePenalty, endSequences, stopSequences, returnLikelihoods, logitBias, truncate, }) {
164
+ async function callCohereTextGenerationAPI({ api = new CohereApiConfiguration(), abortSignal, responseFormat, model, prompt, numGenerations, maxTokens, temperature, k, p, frequencyPenalty, presencePenalty, endSequences, stopSequences, returnLikelihoods, logitBias, truncate, }) {
192
165
  return postJsonToApi({
193
- url: `${baseUrl}/generate`,
194
- headers: {
195
- Authorization: `Bearer ${apiKey}`,
196
- },
166
+ url: api.assembleUrl(`/generate`),
167
+ headers: api.headers,
197
168
  body: {
198
169
  stream: responseFormat.stream,
199
170
  model,
@@ -8,6 +8,7 @@ const zod_1 = __importDefault(require("zod"));
8
8
  const callWithRetryAndThrottle_js_1 = require("../../util/api/callWithRetryAndThrottle.cjs");
9
9
  const postToApi_js_1 = require("../../util/api/postToApi.cjs");
10
10
  const CohereError_js_1 = require("./CohereError.cjs");
11
+ const CohereApiConfiguration_js_1 = require("./CohereApiConfiguration.cjs");
11
12
  /**
12
13
  * Tokenizer for the Cohere models. It uses the Co.Tokenize and Co.Detokenize APIs.
13
14
  *
@@ -34,20 +35,12 @@ class CohereTokenizer {
34
35
  });
35
36
  this.settings = settings;
36
37
  }
37
- get apiKey() {
38
- const apiKey = this.settings.apiKey ?? process.env.COHERE_API_KEY;
39
- if (apiKey == null) {
40
- throw new Error("No Cohere API key provided. Pass an API key to the constructor or set the COHERE_API_KEY environment variable.");
41
- }
42
- return apiKey;
43
- }
44
38
  async callTokenizeAPI(text, context) {
45
39
  return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
46
- retry: this.settings.retry,
47
- throttle: this.settings.throttle,
40
+ retry: this.settings.api?.retry,
41
+ throttle: this.settings.api?.throttle,
48
42
  call: async () => callCohereTokenizeAPI({
49
43
  abortSignal: context?.abortSignal,
50
- apiKey: this.apiKey,
51
44
  text,
52
45
  ...this.settings,
53
46
  }),
@@ -55,11 +48,10 @@ class CohereTokenizer {
55
48
  }
56
49
  async callDeTokenizeAPI(tokens, context) {
57
50
  return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
58
- retry: this.settings.retry,
59
- throttle: this.settings.throttle,
51
+ retry: this.settings.api?.retry,
52
+ throttle: this.settings.api?.throttle,
60
53
  call: async () => callCohereDetokenizeAPI({
61
54
  abortSignal: context?.abortSignal,
62
- apiKey: this.apiKey,
63
55
  tokens,
64
56
  ...this.settings,
65
57
  }),
@@ -89,17 +81,10 @@ const cohereDetokenizationResponseSchema = zod_1.default.object({
89
81
  }),
90
82
  }),
91
83
  });
92
- /**
93
- * Call the Cohere Co.Detokenize API to detokenize a text.
94
- *
95
- * https://docs.cohere.com/reference/detokenize-1
96
- */
97
- async function callCohereDetokenizeAPI({ baseUrl = "https://api.cohere.ai/v1", abortSignal, apiKey, model, tokens, }) {
84
+ async function callCohereDetokenizeAPI({ api = new CohereApiConfiguration_js_1.CohereApiConfiguration(), abortSignal, model, tokens, }) {
98
85
  return (0, postToApi_js_1.postJsonToApi)({
99
- url: `${baseUrl}/detokenize`,
100
- headers: {
101
- Authorization: `Bearer ${apiKey}`,
102
- },
86
+ url: api.assembleUrl(`/detokenize`),
87
+ headers: api.headers,
103
88
  body: {
104
89
  model,
105
90
  tokens,
@@ -118,17 +103,10 @@ const cohereTokenizationResponseSchema = zod_1.default.object({
118
103
  }),
119
104
  }),
120
105
  });
121
- /**
122
- * Call the Cohere Co.Tokenize API to tokenize a text.
123
- *
124
- * https://docs.cohere.com/reference/tokenize
125
- */
126
- async function callCohereTokenizeAPI({ baseUrl = "https://api.cohere.ai/v1", abortSignal, apiKey, model, text, }) {
106
+ async function callCohereTokenizeAPI({ api = new CohereApiConfiguration_js_1.CohereApiConfiguration(), abortSignal, model, text, }) {
127
107
  return (0, postToApi_js_1.postJsonToApi)({
128
- url: `${baseUrl}/tokenize`,
129
- headers: {
130
- Authorization: `Bearer ${apiKey}`,
131
- },
108
+ url: api.assembleUrl(`/tokenize`),
109
+ headers: api.headers,
132
110
  body: {
133
111
  model,
134
112
  text,
@@ -1,17 +1,13 @@
1
1
  import z from "zod";
2
- import { FullTokenizer } from "../../model-function/tokenize-text/Tokenizer.js";
3
2
  import { Run } from "../../core/Run.js";
4
- import { RetryFunction } from "../../util/api/RetryFunction.js";
5
- import { ThrottleFunction } from "../../util/api/ThrottleFunction.js";
6
- import { CohereTextGenerationModelType } from "./CohereTextGenerationModel.js";
3
+ import { ApiConfiguration } from "../../model-function/ApiConfiguration.js";
4
+ import { FullTokenizer } from "../../model-function/tokenize-text/Tokenizer.js";
7
5
  import { CohereTextEmbeddingModelType } from "./CohereTextEmbeddingModel.js";
6
+ import { CohereTextGenerationModelType } from "./CohereTextGenerationModel.js";
8
7
  export type CohereTokenizerModelType = CohereTextGenerationModelType | CohereTextEmbeddingModelType;
9
8
  export interface CohereTokenizerSettings {
9
+ api?: ApiConfiguration;
10
10
  model: CohereTokenizerModelType;
11
- baseUrl?: string;
12
- apiKey?: string;
13
- retry?: RetryFunction;
14
- throttle?: ThrottleFunction;
15
11
  }
16
12
  /**
17
13
  * Tokenizer for the Cohere models. It uses the Co.Tokenize and Co.Detokenize APIs.
@@ -32,7 +28,6 @@ export interface CohereTokenizerSettings {
32
28
  export declare class CohereTokenizer implements FullTokenizer {
33
29
  readonly settings: CohereTokenizerSettings;
34
30
  constructor(settings: CohereTokenizerSettings);
35
- private get apiKey();
36
31
  callTokenizeAPI(text: string, context?: Run): Promise<CohereTokenizationResponse>;
37
32
  callDeTokenizeAPI(tokens: number[], context?: Run): Promise<CohereDetokenizationResponse>;
38
33
  tokenize(text: string): Promise<number[]>;
@@ -2,6 +2,7 @@ import z from "zod";
2
2
  import { callWithRetryAndThrottle } from "../../util/api/callWithRetryAndThrottle.js";
3
3
  import { createJsonResponseHandler, postJsonToApi, } from "../../util/api/postToApi.js";
4
4
  import { failedCohereCallResponseHandler } from "./CohereError.js";
5
+ import { CohereApiConfiguration } from "./CohereApiConfiguration.js";
5
6
  /**
6
7
  * Tokenizer for the Cohere models. It uses the Co.Tokenize and Co.Detokenize APIs.
7
8
  *
@@ -28,20 +29,12 @@ export class CohereTokenizer {
28
29
  });
29
30
  this.settings = settings;
30
31
  }
31
- get apiKey() {
32
- const apiKey = this.settings.apiKey ?? process.env.COHERE_API_KEY;
33
- if (apiKey == null) {
34
- throw new Error("No Cohere API key provided. Pass an API key to the constructor or set the COHERE_API_KEY environment variable.");
35
- }
36
- return apiKey;
37
- }
38
32
  async callTokenizeAPI(text, context) {
39
33
  return callWithRetryAndThrottle({
40
- retry: this.settings.retry,
41
- throttle: this.settings.throttle,
34
+ retry: this.settings.api?.retry,
35
+ throttle: this.settings.api?.throttle,
42
36
  call: async () => callCohereTokenizeAPI({
43
37
  abortSignal: context?.abortSignal,
44
- apiKey: this.apiKey,
45
38
  text,
46
39
  ...this.settings,
47
40
  }),
@@ -49,11 +42,10 @@ export class CohereTokenizer {
49
42
  }
50
43
  async callDeTokenizeAPI(tokens, context) {
51
44
  return callWithRetryAndThrottle({
52
- retry: this.settings.retry,
53
- throttle: this.settings.throttle,
45
+ retry: this.settings.api?.retry,
46
+ throttle: this.settings.api?.throttle,
54
47
  call: async () => callCohereDetokenizeAPI({
55
48
  abortSignal: context?.abortSignal,
56
- apiKey: this.apiKey,
57
49
  tokens,
58
50
  ...this.settings,
59
51
  }),
@@ -82,17 +74,10 @@ const cohereDetokenizationResponseSchema = z.object({
82
74
  }),
83
75
  }),
84
76
  });
85
- /**
86
- * Call the Cohere Co.Detokenize API to detokenize a text.
87
- *
88
- * https://docs.cohere.com/reference/detokenize-1
89
- */
90
- async function callCohereDetokenizeAPI({ baseUrl = "https://api.cohere.ai/v1", abortSignal, apiKey, model, tokens, }) {
77
+ async function callCohereDetokenizeAPI({ api = new CohereApiConfiguration(), abortSignal, model, tokens, }) {
91
78
  return postJsonToApi({
92
- url: `${baseUrl}/detokenize`,
93
- headers: {
94
- Authorization: `Bearer ${apiKey}`,
95
- },
79
+ url: api.assembleUrl(`/detokenize`),
80
+ headers: api.headers,
96
81
  body: {
97
82
  model,
98
83
  tokens,
@@ -111,17 +96,10 @@ const cohereTokenizationResponseSchema = z.object({
111
96
  }),
112
97
  }),
113
98
  });
114
- /**
115
- * Call the Cohere Co.Tokenize API to tokenize a text.
116
- *
117
- * https://docs.cohere.com/reference/tokenize
118
- */
119
- async function callCohereTokenizeAPI({ baseUrl = "https://api.cohere.ai/v1", abortSignal, apiKey, model, text, }) {
99
+ async function callCohereTokenizeAPI({ api = new CohereApiConfiguration(), abortSignal, model, text, }) {
120
100
  return postJsonToApi({
121
- url: `${baseUrl}/tokenize`,
122
- headers: {
123
- Authorization: `Bearer ${apiKey}`,
124
- },
101
+ url: api.assembleUrl(`/tokenize`),
102
+ headers: api.headers,
125
103
  body: {
126
104
  model,
127
105
  text,
@@ -15,6 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.CohereError = void 0;
18
+ __exportStar(require("./CohereApiConfiguration.cjs"), exports);
18
19
  var CohereError_js_1 = require("./CohereError.cjs");
19
20
  Object.defineProperty(exports, "CohereError", { enumerable: true, get: function () { return CohereError_js_1.CohereError; } });
20
21
  __exportStar(require("./CohereTextEmbeddingModel.cjs"), exports);
@@ -1,3 +1,4 @@
1
+ export * from "./CohereApiConfiguration.js";
1
2
  export { CohereError, CohereErrorData } from "./CohereError.js";
2
3
  export * from "./CohereTextEmbeddingModel.js";
3
4
  export * from "./CohereTextGenerationModel.js";
@@ -1,3 +1,4 @@
1
+ export * from "./CohereApiConfiguration.js";
1
2
  export { CohereError } from "./CohereError.js";
2
3
  export * from "./CohereTextEmbeddingModel.js";
3
4
  export * from "./CohereTextGenerationModel.js";
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ElevenLabsApiConfiguration = void 0;
4
+ const BasicApiConfiguration_js_1 = require("../../model-function/BasicApiConfiguration.cjs");
5
+ const loadApiKey_js_1 = require("../../util/api/loadApiKey.cjs");
6
+ class ElevenLabsApiConfiguration extends BasicApiConfiguration_js_1.BasicApiConfiguration {
7
+ constructor({ baseUrl = "https://api.elevenlabs.io/v1", apiKey, retry, throttle, } = {}) {
8
+ super({
9
+ baseUrl,
10
+ retry,
11
+ throttle,
12
+ });
13
+ Object.defineProperty(this, "apiKey", {
14
+ enumerable: true,
15
+ configurable: true,
16
+ writable: true,
17
+ value: void 0
18
+ });
19
+ this.apiKey = (0, loadApiKey_js_1.loadApiKey)({
20
+ apiKey,
21
+ environmentVariableName: "ELEVENLABS_API_KEY",
22
+ description: "ElevenLabs",
23
+ });
24
+ }
25
+ get headers() {
26
+ return { "xi-api-key": this.apiKey };
27
+ }
28
+ }
29
+ exports.ElevenLabsApiConfiguration = ElevenLabsApiConfiguration;
@@ -0,0 +1,13 @@
1
+ import { BasicApiConfiguration } from "../../model-function/BasicApiConfiguration.js";
2
+ import { RetryFunction } from "../../util/api/RetryFunction.js";
3
+ import { ThrottleFunction } from "../../util/api/ThrottleFunction.js";
4
+ export declare class ElevenLabsApiConfiguration extends BasicApiConfiguration {
5
+ readonly apiKey: string;
6
+ constructor({ baseUrl, apiKey, retry, throttle, }?: {
7
+ baseUrl?: string;
8
+ apiKey?: string;
9
+ retry?: RetryFunction;
10
+ throttle?: ThrottleFunction;
11
+ });
12
+ get headers(): Record<string, string>;
13
+ }
@@ -0,0 +1,25 @@
1
+ import { BasicApiConfiguration } from "../../model-function/BasicApiConfiguration.js";
2
+ import { loadApiKey } from "../../util/api/loadApiKey.js";
3
+ export class ElevenLabsApiConfiguration extends BasicApiConfiguration {
4
+ constructor({ baseUrl = "https://api.elevenlabs.io/v1", apiKey, retry, throttle, } = {}) {
5
+ super({
6
+ baseUrl,
7
+ retry,
8
+ throttle,
9
+ });
10
+ Object.defineProperty(this, "apiKey", {
11
+ enumerable: true,
12
+ configurable: true,
13
+ writable: true,
14
+ value: void 0
15
+ });
16
+ this.apiKey = loadApiKey({
17
+ apiKey,
18
+ environmentVariableName: "ELEVENLABS_API_KEY",
19
+ description: "ElevenLabs",
20
+ });
21
+ }
22
+ get headers() {
23
+ return { "xi-api-key": this.apiKey };
24
+ }
25
+ }
@@ -4,7 +4,13 @@ exports.ElevenLabsSpeechSynthesisModel = void 0;
4
4
  const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
5
5
  const callWithRetryAndThrottle_js_1 = require("../../util/api/callWithRetryAndThrottle.cjs");
6
6
  const postToApi_js_1 = require("../../util/api/postToApi.cjs");
7
+ const ElevenLabsApiConfiguration_js_1 = require("./ElevenLabsApiConfiguration.cjs");
7
8
  const ElevenLabsError_js_1 = require("./ElevenLabsError.cjs");
9
+ /**
10
+ * Synthesize speech using the ElevenLabs Text to Speech API.
11
+ *
12
+ * @see https://api.elevenlabs.io/docs#/text-to-speech/Text_to_speech_v1_text_to_speech__voice_id__post
13
+ */
8
14
  class ElevenLabsSpeechSynthesisModel extends AbstractModel_js_1.AbstractModel {
9
15
  constructor(settings) {
10
16
  super({ settings });
@@ -14,19 +20,9 @@ class ElevenLabsSpeechSynthesisModel extends AbstractModel_js_1.AbstractModel {
14
20
  writable: true,
15
21
  value: "elevenlabs"
16
22
  });
17
- Object.defineProperty(this, "modelName", {
18
- enumerable: true,
19
- configurable: true,
20
- writable: true,
21
- value: null
22
- });
23
23
  }
24
- get apiKey() {
25
- const apiKey = this.settings.apiKey ?? process.env.ELEVENLABS_API_KEY;
26
- if (apiKey == null) {
27
- throw new Error("No ElevenLabs API key provided. Pass it in the constructor or set the ELEVENLABS_API_KEY environment variable.");
28
- }
29
- return apiKey;
24
+ get modelName() {
25
+ return this.settings.voice;
30
26
  }
31
27
  async callAPI(text, options) {
32
28
  const run = options?.run;
@@ -35,23 +31,22 @@ class ElevenLabsSpeechSynthesisModel extends AbstractModel_js_1.AbstractModel {
35
31
  ...this.settings,
36
32
  ...settings,
37
33
  };
34
+ const callSettings = {
35
+ api: combinedSettings.api,
36
+ abortSignal: run?.abortSignal,
37
+ text,
38
+ voiceId: combinedSettings.voice,
39
+ modelId: combinedSettings.model,
40
+ voiceSettings: combinedSettings.voiceSettings,
41
+ };
38
42
  return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
39
- retry: this.settings.retry,
40
- throttle: this.settings.throttle,
41
- call: async () => callElevenLabsTextToSpeechAPI({
42
- baseUrl: combinedSettings.baseUrl,
43
- abortSignal: run?.abortSignal,
44
- apiKey: this.apiKey,
45
- text,
46
- voiceId: combinedSettings.voice,
47
- modelId: combinedSettings.model,
48
- voiceSettings: combinedSettings.voiceSettings,
49
- }),
43
+ retry: combinedSettings.api?.retry,
44
+ throttle: combinedSettings.api?.throttle,
45
+ call: async () => callElevenLabsTextToSpeechAPI(callSettings),
50
46
  });
51
47
  }
52
48
  get settingsForEvent() {
53
49
  return {
54
- baseUrl: this.settings.baseUrl,
55
50
  model: this.settings.model,
56
51
  voice: this.settings.voice,
57
52
  voiceSettings: this.settings.voiceSettings,
@@ -68,15 +63,10 @@ class ElevenLabsSpeechSynthesisModel extends AbstractModel_js_1.AbstractModel {
68
63
  }
69
64
  }
70
65
  exports.ElevenLabsSpeechSynthesisModel = ElevenLabsSpeechSynthesisModel;
71
- /**
72
- * @see https://api.elevenlabs.io/docs#/text-to-speech/Text_to_speech_v1_text_to_speech__voice_id__post
73
- */
74
- async function callElevenLabsTextToSpeechAPI({ baseUrl = "https://api.elevenlabs.io/v1", abortSignal, apiKey, text, voiceId, modelId, voiceSettings, }) {
66
+ async function callElevenLabsTextToSpeechAPI({ api = new ElevenLabsApiConfiguration_js_1.ElevenLabsApiConfiguration(), abortSignal, text, voiceId, modelId, voiceSettings, }) {
75
67
  return (0, postToApi_js_1.postJsonToApi)({
76
- url: `${baseUrl}/text-to-speech/${voiceId}`,
77
- headers: {
78
- "xi-api-key": apiKey,
79
- },
68
+ url: api.assembleUrl(`/text-to-speech/${voiceId}`),
69
+ headers: api.headers,
80
70
  body: {
81
71
  text,
82
72
  model_id: modelId,
@@ -1,13 +1,11 @@
1
1
  /// <reference types="node" resolution-mode="require"/>
2
2
  import { AbstractModel } from "../../model-function/AbstractModel.js";
3
+ import { ApiConfiguration } from "../../model-function/ApiConfiguration.js";
3
4
  import { ModelFunctionOptions } from "../../model-function/ModelFunctionOptions.js";
4
5
  import { SpeechSynthesisModel, SpeechSynthesisModelSettings } from "../../model-function/synthesize-speech/SpeechSynthesisModel.js";
5
- import { RetryFunction } from "../../util/api/RetryFunction.js";
6
- import { ThrottleFunction } from "../../util/api/ThrottleFunction.js";
7
6
  export interface ElevenLabsSpeechSynthesisModelSettings extends SpeechSynthesisModelSettings {
7
+ api?: ApiConfiguration;
8
8
  voice: string;
9
- baseUrl?: string;
10
- apiKey?: string;
11
9
  model?: string;
12
10
  voiceSettings?: {
13
11
  stability: number;
@@ -15,14 +13,16 @@ export interface ElevenLabsSpeechSynthesisModelSettings extends SpeechSynthesisM
15
13
  style?: number;
16
14
  useSpeakerBoost?: boolean;
17
15
  };
18
- retry?: RetryFunction;
19
- throttle?: ThrottleFunction;
20
16
  }
17
+ /**
18
+ * Synthesize speech using the ElevenLabs Text to Speech API.
19
+ *
20
+ * @see https://api.elevenlabs.io/docs#/text-to-speech/Text_to_speech_v1_text_to_speech__voice_id__post
21
+ */
21
22
  export declare class ElevenLabsSpeechSynthesisModel extends AbstractModel<ElevenLabsSpeechSynthesisModelSettings> implements SpeechSynthesisModel<ElevenLabsSpeechSynthesisModelSettings> {
22
23
  constructor(settings: ElevenLabsSpeechSynthesisModelSettings);
23
24
  readonly provider = "elevenlabs";
24
- readonly modelName: null;
25
- private get apiKey();
25
+ get modelName(): string;
26
26
  private callAPI;
27
27
  get settingsForEvent(): Partial<ElevenLabsSpeechSynthesisModelSettings>;
28
28
  generateSpeechResponse(text: string, options?: ModelFunctionOptions<ElevenLabsSpeechSynthesisModelSettings> | undefined): Promise<Buffer>;