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
@@ -4,6 +4,7 @@ import { AbstractModel } from "../../../model-function/AbstractModel.js";
4
4
  import { PromptFormatTextGenerationModel } from "../../../prompt/PromptFormatTextGenerationModel.js";
5
5
  import { callWithRetryAndThrottle } from "../../../util/api/callWithRetryAndThrottle.js";
6
6
  import { createJsonResponseHandler, postJsonToApi, } from "../../../util/api/postToApi.js";
7
+ import { OpenAIApiConfiguration } from "../OpenAIApiConfiguration.js";
7
8
  import { failedOpenAICallResponseHandler } from "../OpenAIError.js";
8
9
  import { TikTokenTokenizer } from "../TikTokenTokenizer.js";
9
10
  import { createOpenAIChatFullDeltaIterableQueue, } from "./OpenAIChatStreamIterable.js";
@@ -161,13 +162,6 @@ export class OpenAIChatModel extends AbstractModel {
161
162
  get modelName() {
162
163
  return this.settings.model;
163
164
  }
164
- get apiKey() {
165
- const apiKey = this.settings.apiKey ?? process.env.OPENAI_API_KEY;
166
- if (apiKey == null) {
167
- throw new Error(`OpenAI API key is missing. Pass it as an argument to the constructor or set it as an environment variable named OPENAI_API_KEY.`);
168
- }
169
- return apiKey;
170
- }
171
165
  /**
172
166
  * Counts the prompt tokens required for the messages. This includes the message base tokens
173
167
  * and the prompt base tokens.
@@ -185,18 +179,20 @@ export class OpenAIChatModel extends AbstractModel {
185
179
  ...settings,
186
180
  };
187
181
  const callSettings = {
188
- apiKey: this.apiKey,
189
182
  user: this.settings.isUserIdForwardingEnabled ? run?.userId : undefined,
183
+ // Copied settings:
190
184
  ...combinedSettings,
185
+ // map to OpenAI API names:
191
186
  stop: combinedSettings.stopSequences,
192
187
  maxTokens: combinedSettings.maxCompletionTokens,
188
+ // other settings:
193
189
  abortSignal: run?.abortSignal,
194
190
  messages,
195
191
  responseFormat,
196
192
  };
197
193
  return callWithRetryAndThrottle({
198
- retry: callSettings.retry,
199
- throttle: callSettings.throttle,
194
+ retry: callSettings.api?.retry,
195
+ throttle: callSettings.api?.throttle,
200
196
  call: async () => callOpenAIChatCompletionAPI(callSettings),
201
197
  });
202
198
  }
@@ -204,7 +200,6 @@ export class OpenAIChatModel extends AbstractModel {
204
200
  const eventSettingProperties = [
205
201
  "stopSequences",
206
202
  "maxCompletionTokens",
207
- "baseUrl",
208
203
  "functions",
209
204
  "functionCall",
210
205
  "temperature",
@@ -300,13 +295,10 @@ const openAIChatResponseSchema = z.object({
300
295
  total_tokens: z.number(),
301
296
  }),
302
297
  });
303
- async function callOpenAIChatCompletionAPI({ baseUrl = "https://api.openai.com/v1", headers, abortSignal, responseFormat, apiKey, model, messages, functions, functionCall, temperature, topP, n, stop, maxTokens, presencePenalty, frequencyPenalty, logitBias, user, }) {
298
+ async function callOpenAIChatCompletionAPI({ api = new OpenAIApiConfiguration(), abortSignal, responseFormat, model, messages, functions, functionCall, temperature, topP, n, stop, maxTokens, presencePenalty, frequencyPenalty, logitBias, user, }) {
304
299
  return postJsonToApi({
305
- url: `${baseUrl}/chat/completions`,
306
- headers: {
307
- ...headers,
308
- Authorization: `Bearer ${apiKey}`,
309
- },
300
+ url: api.assembleUrl("/chat/completions"),
301
+ headers: api.headers,
310
302
  body: {
311
303
  stream: responseFormat.stream,
312
304
  model,
@@ -15,11 +15,12 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.OpenAIChatFunctionPrompt = exports.OpenAIError = void 0;
18
+ __exportStar(require("./AzureOpenAIApiConfiguration.cjs"), exports);
19
+ __exportStar(require("./OpenAIApiConfiguration.cjs"), exports);
18
20
  __exportStar(require("./OpenAICostCalculator.cjs"), exports);
19
21
  var OpenAIError_js_1 = require("./OpenAIError.cjs");
20
22
  Object.defineProperty(exports, "OpenAIError", { enumerable: true, get: function () { return OpenAIError_js_1.OpenAIError; } });
21
23
  __exportStar(require("./OpenAIImageGenerationModel.cjs"), exports);
22
- __exportStar(require("./OpenAIModelSettings.cjs"), exports);
23
24
  __exportStar(require("./OpenAITextEmbeddingModel.cjs"), exports);
24
25
  __exportStar(require("./OpenAITextGenerationModel.cjs"), exports);
25
26
  __exportStar(require("./OpenAITranscriptionModel.cjs"), exports);
@@ -1,7 +1,8 @@
1
+ export * from "./AzureOpenAIApiConfiguration.js";
2
+ export * from "./OpenAIApiConfiguration.js";
1
3
  export * from "./OpenAICostCalculator.js";
2
4
  export { OpenAIError, OpenAIErrorData } from "./OpenAIError.js";
3
5
  export * from "./OpenAIImageGenerationModel.js";
4
- export * from "./OpenAIModelSettings.js";
5
6
  export * from "./OpenAITextEmbeddingModel.js";
6
7
  export * from "./OpenAITextGenerationModel.js";
7
8
  export * from "./OpenAITranscriptionModel.js";
@@ -1,7 +1,8 @@
1
+ export * from "./AzureOpenAIApiConfiguration.js";
2
+ export * from "./OpenAIApiConfiguration.js";
1
3
  export * from "./OpenAICostCalculator.js";
2
4
  export { OpenAIError } from "./OpenAIError.js";
3
5
  export * from "./OpenAIImageGenerationModel.js";
4
- export * from "./OpenAIModelSettings.js";
5
6
  export * from "./OpenAITextEmbeddingModel.js";
6
7
  export * from "./OpenAITextGenerationModel.js";
7
8
  export * from "./OpenAITranscriptionModel.js";
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StabilityApiConfiguration = void 0;
4
+ const BasicApiConfiguration_js_1 = require("../../model-function/BasicApiConfiguration.cjs");
5
+ const loadApiKey_js_1 = require("../../util/api/loadApiKey.cjs");
6
+ class StabilityApiConfiguration extends BasicApiConfiguration_js_1.BasicApiConfiguration {
7
+ constructor({ baseUrl = "https://api.stability.ai/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: "STABILITY_API_KEY",
22
+ description: "Stability",
23
+ });
24
+ }
25
+ get headers() {
26
+ return {
27
+ Authorization: `Bearer ${this.apiKey}`,
28
+ };
29
+ }
30
+ }
31
+ exports.StabilityApiConfiguration = StabilityApiConfiguration;
@@ -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 StabilityApiConfiguration 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,27 @@
1
+ import { BasicApiConfiguration } from "../../model-function/BasicApiConfiguration.js";
2
+ import { loadApiKey } from "../../util/api/loadApiKey.js";
3
+ export class StabilityApiConfiguration extends BasicApiConfiguration {
4
+ constructor({ baseUrl = "https://api.stability.ai/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: "STABILITY_API_KEY",
19
+ description: "Stability",
20
+ });
21
+ }
22
+ get headers() {
23
+ return {
24
+ Authorization: `Bearer ${this.apiKey}`,
25
+ };
26
+ }
27
+ }
@@ -6,6 +6,7 @@ const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
6
6
  const callWithRetryAndThrottle_js_1 = require("../../util/api/callWithRetryAndThrottle.cjs");
7
7
  const postToApi_js_1 = require("../../util/api/postToApi.cjs");
8
8
  const StabilityError_js_1 = require("./StabilityError.cjs");
9
+ const StabilityApiConfiguration_js_1 = require("./StabilityApiConfiguration.cjs");
9
10
  /**
10
11
  * Create an image generation model that calls the Stability AI image generation API.
11
12
  *
@@ -41,26 +42,21 @@ class StabilityImageGenerationModel extends AbstractModel_js_1.AbstractModel {
41
42
  get modelName() {
42
43
  return this.settings.model;
43
44
  }
44
- get apiKey() {
45
- const apiKey = this.settings.apiKey ?? process.env.STABILITY_API_KEY;
46
- if (apiKey == null) {
47
- throw new Error("No API key provided. Either pass an API key to the constructor or set the STABILITY_API_KEY environment variable.");
48
- }
49
- return apiKey;
50
- }
51
45
  async callAPI(input, options) {
52
46
  const run = options?.run;
53
47
  const settings = options?.settings;
54
- const callSettings = Object.assign({
55
- apiKey: this.apiKey,
56
- }, this.settings, settings, {
48
+ const callSettings = {
49
+ // copied settings:
50
+ ...this.settings,
51
+ ...settings,
52
+ // other settings:
57
53
  abortSignal: run?.abortSignal,
58
54
  engineId: this.settings.model,
59
55
  textPrompts: input,
60
- });
56
+ };
61
57
  return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
62
- retry: this.settings.retry,
63
- throttle: this.settings.throttle,
58
+ retry: callSettings.api?.retry,
59
+ throttle: callSettings.api?.throttle,
64
60
  call: async () => callStabilityImageGenerationAPI(callSettings),
65
61
  });
66
62
  }
@@ -97,40 +93,10 @@ const stabilityImageGenerationResponseSchema = zod_1.z.object({
97
93
  finishReason: zod_1.z.enum(["SUCCESS", "ERROR", "CONTENT_FILTERED"]),
98
94
  })),
99
95
  });
100
- /**
101
- * Call the Stability AI API for image generation.
102
- *
103
- * @see https://api.stability.ai/docs#tag/v1generation/operation/textToImage
104
- *
105
- * @example
106
- * const imageResponse = await callStabilityImageGenerationAPI({
107
- * apiKey: STABILITY_API_KEY,
108
- * engineId: "stable-diffusion-512-v2-1",
109
- * textPrompts: [
110
- * { text: "the wicked witch of the west" },
111
- * { text: "style of early 19th century painting", weight: 0.5 },
112
- * ],
113
- * cfgScale: 7,
114
- * clipGuidancePreset: "FAST_BLUE",
115
- * height: 512,
116
- * width: 512,
117
- * samples: 1,
118
- * steps: 30,
119
- * });
120
- *
121
- * imageResponse.artifacts.forEach((image, index) => {
122
- * fs.writeFileSync(
123
- * `./stability-image-example-${index}.png`,
124
- * Buffer.from(image.base64, "base64")
125
- * );
126
- * });
127
- */
128
- async function callStabilityImageGenerationAPI({ baseUrl = "https://api.stability.ai/v1", abortSignal, apiKey, engineId, height, width, textPrompts, cfgScale, clipGuidancePreset, sampler, samples, seed, steps, stylePreset, }) {
96
+ async function callStabilityImageGenerationAPI({ api = new StabilityApiConfiguration_js_1.StabilityApiConfiguration(), abortSignal, engineId, height, width, textPrompts, cfgScale, clipGuidancePreset, sampler, samples, seed, steps, stylePreset, }) {
129
97
  return (0, postToApi_js_1.postJsonToApi)({
130
- url: `${baseUrl}/generation/${engineId}/text-to-image`,
131
- headers: {
132
- Authorization: `Bearer ${apiKey}`,
133
- },
98
+ url: api.assembleUrl(`/generation/${engineId}/text-to-image`),
99
+ headers: api.headers,
134
100
  body: {
135
101
  height,
136
102
  width,
@@ -1,9 +1,8 @@
1
1
  import { z } from "zod";
2
2
  import { AbstractModel } from "../../model-function/AbstractModel.js";
3
3
  import { ModelFunctionOptions } from "../../model-function/ModelFunctionOptions.js";
4
+ import { ApiConfiguration } from "../../model-function/ApiConfiguration.js";
4
5
  import { ImageGenerationModel, ImageGenerationModelSettings } from "../../model-function/generate-image/ImageGenerationModel.js";
5
- import { RetryFunction } from "../../util/api/RetryFunction.js";
6
- import { ThrottleFunction } from "../../util/api/ThrottleFunction.js";
7
6
  /**
8
7
  * Create an image generation model that calls the Stability AI image generation API.
9
8
  *
@@ -30,7 +29,6 @@ export declare class StabilityImageGenerationModel extends AbstractModel<Stabili
30
29
  constructor(settings: StabilityImageGenerationModelSettings);
31
30
  readonly provider: "stability";
32
31
  get modelName(): string;
33
- private get apiKey();
34
32
  callAPI(input: StabilityImageGenerationPrompt, options?: ModelFunctionOptions<StabilityImageGenerationModelSettings>): Promise<StabilityImageGenerationResponse>;
35
33
  get settingsForEvent(): Partial<StabilityImageGenerationModelSettings>;
36
34
  generateImageResponse(prompt: StabilityImageGenerationPrompt, options?: ModelFunctionOptions<StabilityImageGenerationModelSettings>): Promise<{
@@ -44,11 +42,8 @@ export declare class StabilityImageGenerationModel extends AbstractModel<Stabili
44
42
  withSettings(additionalSettings: StabilityImageGenerationModelSettings): this;
45
43
  }
46
44
  export interface StabilityImageGenerationModelSettings extends ImageGenerationModelSettings {
45
+ api?: ApiConfiguration;
47
46
  model: string;
48
- baseUrl?: string;
49
- apiKey?: string;
50
- retry?: RetryFunction;
51
- throttle?: ThrottleFunction;
52
47
  height?: number;
53
48
  width?: number;
54
49
  cfgScale?: number;
@@ -3,6 +3,7 @@ import { AbstractModel } from "../../model-function/AbstractModel.js";
3
3
  import { callWithRetryAndThrottle } from "../../util/api/callWithRetryAndThrottle.js";
4
4
  import { createJsonResponseHandler, postJsonToApi, } from "../../util/api/postToApi.js";
5
5
  import { failedStabilityCallResponseHandler } from "./StabilityError.js";
6
+ import { StabilityApiConfiguration } from "./StabilityApiConfiguration.js";
6
7
  /**
7
8
  * Create an image generation model that calls the Stability AI image generation API.
8
9
  *
@@ -38,26 +39,21 @@ export class StabilityImageGenerationModel extends AbstractModel {
38
39
  get modelName() {
39
40
  return this.settings.model;
40
41
  }
41
- get apiKey() {
42
- const apiKey = this.settings.apiKey ?? process.env.STABILITY_API_KEY;
43
- if (apiKey == null) {
44
- throw new Error("No API key provided. Either pass an API key to the constructor or set the STABILITY_API_KEY environment variable.");
45
- }
46
- return apiKey;
47
- }
48
42
  async callAPI(input, options) {
49
43
  const run = options?.run;
50
44
  const settings = options?.settings;
51
- const callSettings = Object.assign({
52
- apiKey: this.apiKey,
53
- }, this.settings, settings, {
45
+ const callSettings = {
46
+ // copied settings:
47
+ ...this.settings,
48
+ ...settings,
49
+ // other settings:
54
50
  abortSignal: run?.abortSignal,
55
51
  engineId: this.settings.model,
56
52
  textPrompts: input,
57
- });
53
+ };
58
54
  return callWithRetryAndThrottle({
59
- retry: this.settings.retry,
60
- throttle: this.settings.throttle,
55
+ retry: callSettings.api?.retry,
56
+ throttle: callSettings.api?.throttle,
61
57
  call: async () => callStabilityImageGenerationAPI(callSettings),
62
58
  });
63
59
  }
@@ -93,40 +89,10 @@ const stabilityImageGenerationResponseSchema = z.object({
93
89
  finishReason: z.enum(["SUCCESS", "ERROR", "CONTENT_FILTERED"]),
94
90
  })),
95
91
  });
96
- /**
97
- * Call the Stability AI API for image generation.
98
- *
99
- * @see https://api.stability.ai/docs#tag/v1generation/operation/textToImage
100
- *
101
- * @example
102
- * const imageResponse = await callStabilityImageGenerationAPI({
103
- * apiKey: STABILITY_API_KEY,
104
- * engineId: "stable-diffusion-512-v2-1",
105
- * textPrompts: [
106
- * { text: "the wicked witch of the west" },
107
- * { text: "style of early 19th century painting", weight: 0.5 },
108
- * ],
109
- * cfgScale: 7,
110
- * clipGuidancePreset: "FAST_BLUE",
111
- * height: 512,
112
- * width: 512,
113
- * samples: 1,
114
- * steps: 30,
115
- * });
116
- *
117
- * imageResponse.artifacts.forEach((image, index) => {
118
- * fs.writeFileSync(
119
- * `./stability-image-example-${index}.png`,
120
- * Buffer.from(image.base64, "base64")
121
- * );
122
- * });
123
- */
124
- async function callStabilityImageGenerationAPI({ baseUrl = "https://api.stability.ai/v1", abortSignal, apiKey, engineId, height, width, textPrompts, cfgScale, clipGuidancePreset, sampler, samples, seed, steps, stylePreset, }) {
92
+ async function callStabilityImageGenerationAPI({ api = new StabilityApiConfiguration(), abortSignal, engineId, height, width, textPrompts, cfgScale, clipGuidancePreset, sampler, samples, seed, steps, stylePreset, }) {
125
93
  return postJsonToApi({
126
- url: `${baseUrl}/generation/${engineId}/text-to-image`,
127
- headers: {
128
- Authorization: `Bearer ${apiKey}`,
129
- },
94
+ url: api.assembleUrl(`/generation/${engineId}/text-to-image`),
95
+ headers: api.headers,
130
96
  body: {
131
97
  height,
132
98
  width,
@@ -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.StabilityError = void 0;
18
+ __exportStar(require("./StabilityApiConfiguration.cjs"), exports);
18
19
  var StabilityError_js_1 = require("./StabilityError.cjs");
19
20
  Object.defineProperty(exports, "StabilityError", { enumerable: true, get: function () { return StabilityError_js_1.StabilityError; } });
20
21
  __exportStar(require("./StabilityImageGenerationModel.cjs"), exports);
@@ -1,2 +1,3 @@
1
+ export * from "./StabilityApiConfiguration.js";
1
2
  export { StabilityError, StabilityErrorData } from "./StabilityError.js";
2
3
  export * from "./StabilityImageGenerationModel.js";
@@ -1,2 +1,3 @@
1
+ export * from "./StabilityApiConfiguration.js";
1
2
  export { StabilityError } from "./StabilityError.js";
2
3
  export * from "./StabilityImageGenerationModel.js";
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HeliconeOpenAIApiConfiguration = void 0;
4
+ const BasicApiConfiguration_js_1 = require("../../model-function/BasicApiConfiguration.cjs");
5
+ const loadApiKey_js_1 = require("../../util/api/loadApiKey.cjs");
6
+ class HeliconeOpenAIApiConfiguration extends BasicApiConfiguration_js_1.BasicApiConfiguration {
7
+ constructor({ baseUrl = "https://oai.hconeai.com/v1", openAIApiKey, heliconeApiKey, retry, throttle, } = {}) {
8
+ super({
9
+ baseUrl,
10
+ retry,
11
+ throttle,
12
+ });
13
+ Object.defineProperty(this, "openAIApiKey", {
14
+ enumerable: true,
15
+ configurable: true,
16
+ writable: true,
17
+ value: void 0
18
+ });
19
+ Object.defineProperty(this, "heliconeApiKey", {
20
+ enumerable: true,
21
+ configurable: true,
22
+ writable: true,
23
+ value: void 0
24
+ });
25
+ this.openAIApiKey = (0, loadApiKey_js_1.loadApiKey)({
26
+ apiKey: openAIApiKey,
27
+ environmentVariableName: "OPENAI_API_KEY",
28
+ apiKeyParameterName: "openAIApiKey",
29
+ description: "OpenAI",
30
+ });
31
+ this.heliconeApiKey = (0, loadApiKey_js_1.loadApiKey)({
32
+ apiKey: heliconeApiKey,
33
+ environmentVariableName: "HELICONE_API_KEY",
34
+ apiKeyParameterName: "heliconeApiKey",
35
+ description: "Helicone",
36
+ });
37
+ }
38
+ get headers() {
39
+ return {
40
+ Authorization: `Bearer ${this.openAIApiKey}`,
41
+ "Helicone-Auth": `Bearer ${this.heliconeApiKey}`,
42
+ };
43
+ }
44
+ }
45
+ exports.HeliconeOpenAIApiConfiguration = HeliconeOpenAIApiConfiguration;
@@ -0,0 +1,15 @@
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 HeliconeOpenAIApiConfiguration extends BasicApiConfiguration {
5
+ readonly openAIApiKey: string;
6
+ readonly heliconeApiKey: string;
7
+ constructor({ baseUrl, openAIApiKey, heliconeApiKey, retry, throttle, }?: {
8
+ baseUrl?: string;
9
+ openAIApiKey?: string;
10
+ heliconeApiKey?: string;
11
+ retry?: RetryFunction;
12
+ throttle?: ThrottleFunction;
13
+ });
14
+ get headers(): Record<string, string>;
15
+ }
@@ -0,0 +1,41 @@
1
+ import { BasicApiConfiguration } from "../../model-function/BasicApiConfiguration.js";
2
+ import { loadApiKey } from "../../util/api/loadApiKey.js";
3
+ export class HeliconeOpenAIApiConfiguration extends BasicApiConfiguration {
4
+ constructor({ baseUrl = "https://oai.hconeai.com/v1", openAIApiKey, heliconeApiKey, retry, throttle, } = {}) {
5
+ super({
6
+ baseUrl,
7
+ retry,
8
+ throttle,
9
+ });
10
+ Object.defineProperty(this, "openAIApiKey", {
11
+ enumerable: true,
12
+ configurable: true,
13
+ writable: true,
14
+ value: void 0
15
+ });
16
+ Object.defineProperty(this, "heliconeApiKey", {
17
+ enumerable: true,
18
+ configurable: true,
19
+ writable: true,
20
+ value: void 0
21
+ });
22
+ this.openAIApiKey = loadApiKey({
23
+ apiKey: openAIApiKey,
24
+ environmentVariableName: "OPENAI_API_KEY",
25
+ apiKeyParameterName: "openAIApiKey",
26
+ description: "OpenAI",
27
+ });
28
+ this.heliconeApiKey = loadApiKey({
29
+ apiKey: heliconeApiKey,
30
+ environmentVariableName: "HELICONE_API_KEY",
31
+ apiKeyParameterName: "heliconeApiKey",
32
+ description: "Helicone",
33
+ });
34
+ }
35
+ get headers() {
36
+ return {
37
+ Authorization: `Bearer ${this.openAIApiKey}`,
38
+ "Helicone-Auth": `Bearer ${this.heliconeApiKey}`,
39
+ };
40
+ }
41
+ }
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./helicone/HeliconeOpenAIApiConfiguration.cjs"), exports);
@@ -0,0 +1 @@
1
+ export * from "./helicone/HeliconeOpenAIApiConfiguration.js";
@@ -0,0 +1 @@
1
+ export * from "./helicone/HeliconeOpenAIApiConfiguration.js";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "modelfusion",
3
3
  "description": "Build AI applications, chatbots, and agents with JavaScript and TypeScript.",
4
- "version": "0.26.0",
4
+ "version": "0.27.1",
5
5
  "author": "Lars Grammel",
6
6
  "license": "MIT",
7
7
  "keywords": [
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.loadApiKey = void 0;
4
+ function loadApiKey({ apiKey, environmentVariableName, apiKeyParameterName = "apiKey", description, }) {
5
+ apiKey ??= process.env[environmentVariableName];
6
+ if (apiKey == null) {
7
+ throw new Error(`${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter or set it as an environment variable named ${environmentVariableName}.`);
8
+ }
9
+ return apiKey;
10
+ }
11
+ exports.loadApiKey = loadApiKey;
@@ -0,0 +1,6 @@
1
+ export declare function loadApiKey({ apiKey, environmentVariableName, apiKeyParameterName, description, }: {
2
+ apiKey: string | undefined;
3
+ environmentVariableName: string;
4
+ apiKeyParameterName?: string;
5
+ description: string;
6
+ }): string;
@@ -0,0 +1,7 @@
1
+ export function loadApiKey({ apiKey, environmentVariableName, apiKeyParameterName = "apiKey", description, }) {
2
+ apiKey ??= process.env[environmentVariableName];
3
+ if (apiKey == null) {
4
+ throw new Error(`${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter or set it as an environment variable named ${environmentVariableName}.`);
5
+ }
6
+ return apiKey;
7
+ }
@@ -1,8 +0,0 @@
1
- import { RetryFunction } from "../../util/api/RetryFunction.js";
2
- import { ThrottleFunction } from "../../util/api/ThrottleFunction.js";
3
- export interface OpenAIModelSettings {
4
- baseUrl?: string;
5
- apiKey?: string;
6
- retry?: RetryFunction;
7
- throttle?: ThrottleFunction;
8
- }