modelfusion 0.27.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
@@ -1,24 +1,24 @@
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 LmntSpeechSynthesisModelSettings extends SpeechSynthesisModelSettings {
8
- baseUrl?: string;
9
- apiKey?: string;
7
+ api?: ApiConfiguration;
10
8
  voice: string;
11
9
  speed?: number;
12
10
  seed?: number;
13
11
  length?: number;
14
- retry?: RetryFunction;
15
- throttle?: ThrottleFunction;
16
12
  }
13
+ /**
14
+ * Synthesize speech using the LMNT API.
15
+ *
16
+ * @see https://www.lmnt.com/docs/rest/#synthesize-speech
17
+ */
17
18
  export declare class LmntSpeechSynthesisModel extends AbstractModel<LmntSpeechSynthesisModelSettings> implements SpeechSynthesisModel<LmntSpeechSynthesisModelSettings> {
18
19
  constructor(settings: LmntSpeechSynthesisModelSettings);
19
20
  readonly provider = "lmnt";
20
- readonly modelName: null;
21
- private get apiKey();
21
+ get modelName(): string;
22
22
  private callAPI;
23
23
  get settingsForEvent(): Partial<LmntSpeechSynthesisModelSettings>;
24
24
  generateSpeechResponse(text: string, options?: ModelFunctionOptions<LmntSpeechSynthesisModelSettings> | undefined): Promise<Buffer>;
@@ -1,7 +1,13 @@
1
1
  import { AbstractModel } from "../../model-function/AbstractModel.js";
2
2
  import { callWithRetryAndThrottle } from "../../util/api/callWithRetryAndThrottle.js";
3
3
  import { createAudioMpegResponseHandler, postToApi, } from "../../util/api/postToApi.js";
4
+ import { LmntApiConfiguration } from "./LmntApiConfiguration.js";
4
5
  import { failedLmntCallResponseHandler } from "./LmntError.js";
6
+ /**
7
+ * Synthesize speech using the LMNT API.
8
+ *
9
+ * @see https://www.lmnt.com/docs/rest/#synthesize-speech
10
+ */
5
11
  export class LmntSpeechSynthesisModel extends AbstractModel {
6
12
  constructor(settings) {
7
13
  super({ settings });
@@ -11,45 +17,28 @@ export class LmntSpeechSynthesisModel extends AbstractModel {
11
17
  writable: true,
12
18
  value: "lmnt"
13
19
  });
14
- Object.defineProperty(this, "modelName", {
15
- enumerable: true,
16
- configurable: true,
17
- writable: true,
18
- value: null
19
- });
20
20
  }
21
- get apiKey() {
22
- const apiKey = this.settings.apiKey ?? process.env.LMNT_API_KEY;
23
- if (apiKey == null) {
24
- throw new Error("No LMNT API key provided. Pass it in the constructor or set the LMNT_API_KEY environment variable.");
25
- }
26
- return apiKey;
21
+ get modelName() {
22
+ return this.settings.voice;
27
23
  }
28
24
  async callAPI(text, options) {
29
25
  const run = options?.run;
30
26
  const settings = options?.settings;
31
- const combinedSettings = {
27
+ const callSettings = {
28
+ // copied settings:
32
29
  ...this.settings,
33
30
  ...settings,
31
+ abortSignal: run?.abortSignal,
32
+ text,
34
33
  };
35
34
  return callWithRetryAndThrottle({
36
- retry: this.settings.retry,
37
- throttle: this.settings.throttle,
38
- call: async () => callLmntTextToSpeechAPI({
39
- baseUrl: combinedSettings.baseUrl,
40
- abortSignal: run?.abortSignal,
41
- apiKey: this.apiKey,
42
- text,
43
- voice: combinedSettings.voice,
44
- speed: combinedSettings.speed,
45
- seed: combinedSettings.seed,
46
- length: combinedSettings.length,
47
- }),
35
+ retry: callSettings.api?.retry,
36
+ throttle: callSettings.api?.throttle,
37
+ call: async () => callLmntTextToSpeechAPI(callSettings),
48
38
  });
49
39
  }
50
40
  get settingsForEvent() {
51
41
  return {
52
- baseUrl: this.settings.baseUrl,
53
42
  voice: this.settings.voice,
54
43
  speed: this.settings.speed,
55
44
  seed: this.settings.seed,
@@ -66,10 +55,7 @@ export class LmntSpeechSynthesisModel extends AbstractModel {
66
55
  });
67
56
  }
68
57
  }
69
- /**
70
- * @see https://www.lmnt.com/docs/rest/#synthesize-speech
71
- */
72
- async function callLmntTextToSpeechAPI({ baseUrl = "https://api.lmnt.com/speech/beta", abortSignal, apiKey, text, voice, speed, seed, length, }) {
58
+ async function callLmntTextToSpeechAPI({ api = new LmntApiConfiguration(), abortSignal, text, voice, speed, seed, length, }) {
73
59
  const formData = new FormData();
74
60
  formData.append("text", text);
75
61
  formData.append("voice", voice);
@@ -81,10 +67,8 @@ async function callLmntTextToSpeechAPI({ baseUrl = "https://api.lmnt.com/speech/
81
67
  if (length != null)
82
68
  formData.append("length", length.toString());
83
69
  return postToApi({
84
- url: `${baseUrl}/synthesize`,
85
- headers: {
86
- "X-API-Key": apiKey,
87
- },
70
+ url: api.assembleUrl(`/synthesize`),
71
+ headers: api.headers,
88
72
  body: {
89
73
  content: formData,
90
74
  values: {
@@ -14,4 +14,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./LmntApiConfiguration.cjs"), exports);
17
18
  __exportStar(require("./LmntSpeechSynthesisModel.cjs"), exports);
@@ -1 +1,2 @@
1
+ export * from "./LmntApiConfiguration.js";
1
2
  export * from "./LmntSpeechSynthesisModel.js";
@@ -1 +1,2 @@
1
+ export * from "./LmntApiConfiguration.js";
1
2
  export * from "./LmntSpeechSynthesisModel.js";
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AzureOpenAIApiConfiguration = void 0;
4
+ const BasicApiConfiguration_js_1 = require("../../model-function/BasicApiConfiguration.cjs");
5
+ const loadApiKey_js_1 = require("../../util/api/loadApiKey.cjs");
6
+ /**
7
+ * Cconfiguration for the Azure OpenAI API. This class is responsible for constructing URLs specific to the Azure OpenAI deployment.
8
+ * It creates URLs of the form
9
+ * `https://[resourceName].openai.azure.com/openai/deployments/[deploymentId]/[path]?api-version=[apiVersion]`
10
+ *
11
+ * @see https://learn.microsoft.com/en-us/azure/ai-services/openai/reference
12
+ */
13
+ class AzureOpenAIApiConfiguration extends BasicApiConfiguration_js_1.BasicApiConfiguration {
14
+ constructor({ resourceName, deploymentId, apiVersion, apiKey, retry, throttle, }) {
15
+ super({
16
+ baseUrl: `https://${resourceName}.openai.azure.com/openai/deployments/${deploymentId}`,
17
+ retry,
18
+ throttle,
19
+ });
20
+ Object.defineProperty(this, "resourceName", {
21
+ enumerable: true,
22
+ configurable: true,
23
+ writable: true,
24
+ value: void 0
25
+ });
26
+ Object.defineProperty(this, "deploymentId", {
27
+ enumerable: true,
28
+ configurable: true,
29
+ writable: true,
30
+ value: void 0
31
+ });
32
+ Object.defineProperty(this, "apiVersion", {
33
+ enumerable: true,
34
+ configurable: true,
35
+ writable: true,
36
+ value: void 0
37
+ });
38
+ Object.defineProperty(this, "apiKey", {
39
+ enumerable: true,
40
+ configurable: true,
41
+ writable: true,
42
+ value: void 0
43
+ });
44
+ this.resourceName = resourceName;
45
+ this.deploymentId = deploymentId;
46
+ this.apiVersion = apiVersion;
47
+ this.apiKey = (0, loadApiKey_js_1.loadApiKey)({
48
+ apiKey,
49
+ environmentVariableName: "AZURE_OPENAI_API_KEY",
50
+ description: "Azure OpenAI",
51
+ });
52
+ }
53
+ assembleUrl(path) {
54
+ return `${this.baseUrl}${path}?api-version=${this.apiVersion}`;
55
+ }
56
+ get headers() {
57
+ return {
58
+ "api-key": this.apiKey,
59
+ };
60
+ }
61
+ }
62
+ exports.AzureOpenAIApiConfiguration = AzureOpenAIApiConfiguration;
@@ -0,0 +1,26 @@
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
+ /**
5
+ * Cconfiguration for the Azure OpenAI API. This class is responsible for constructing URLs specific to the Azure OpenAI deployment.
6
+ * It creates URLs of the form
7
+ * `https://[resourceName].openai.azure.com/openai/deployments/[deploymentId]/[path]?api-version=[apiVersion]`
8
+ *
9
+ * @see https://learn.microsoft.com/en-us/azure/ai-services/openai/reference
10
+ */
11
+ export declare class AzureOpenAIApiConfiguration extends BasicApiConfiguration {
12
+ readonly resourceName: string;
13
+ readonly deploymentId: string;
14
+ readonly apiVersion: string;
15
+ readonly apiKey: string;
16
+ constructor({ resourceName, deploymentId, apiVersion, apiKey, retry, throttle, }: {
17
+ resourceName: string;
18
+ deploymentId: string;
19
+ apiVersion: string;
20
+ apiKey?: string;
21
+ retry?: RetryFunction;
22
+ throttle?: ThrottleFunction;
23
+ });
24
+ assembleUrl(path: string): string;
25
+ get headers(): Record<string, string>;
26
+ }
@@ -0,0 +1,58 @@
1
+ import { BasicApiConfiguration } from "../../model-function/BasicApiConfiguration.js";
2
+ import { loadApiKey } from "../../util/api/loadApiKey.js";
3
+ /**
4
+ * Cconfiguration for the Azure OpenAI API. This class is responsible for constructing URLs specific to the Azure OpenAI deployment.
5
+ * It creates URLs of the form
6
+ * `https://[resourceName].openai.azure.com/openai/deployments/[deploymentId]/[path]?api-version=[apiVersion]`
7
+ *
8
+ * @see https://learn.microsoft.com/en-us/azure/ai-services/openai/reference
9
+ */
10
+ export class AzureOpenAIApiConfiguration extends BasicApiConfiguration {
11
+ constructor({ resourceName, deploymentId, apiVersion, apiKey, retry, throttle, }) {
12
+ super({
13
+ baseUrl: `https://${resourceName}.openai.azure.com/openai/deployments/${deploymentId}`,
14
+ retry,
15
+ throttle,
16
+ });
17
+ Object.defineProperty(this, "resourceName", {
18
+ enumerable: true,
19
+ configurable: true,
20
+ writable: true,
21
+ value: void 0
22
+ });
23
+ Object.defineProperty(this, "deploymentId", {
24
+ enumerable: true,
25
+ configurable: true,
26
+ writable: true,
27
+ value: void 0
28
+ });
29
+ Object.defineProperty(this, "apiVersion", {
30
+ enumerable: true,
31
+ configurable: true,
32
+ writable: true,
33
+ value: void 0
34
+ });
35
+ Object.defineProperty(this, "apiKey", {
36
+ enumerable: true,
37
+ configurable: true,
38
+ writable: true,
39
+ value: void 0
40
+ });
41
+ this.resourceName = resourceName;
42
+ this.deploymentId = deploymentId;
43
+ this.apiVersion = apiVersion;
44
+ this.apiKey = loadApiKey({
45
+ apiKey,
46
+ environmentVariableName: "AZURE_OPENAI_API_KEY",
47
+ description: "Azure OpenAI",
48
+ });
49
+ }
50
+ assembleUrl(path) {
51
+ return `${this.baseUrl}${path}?api-version=${this.apiVersion}`;
52
+ }
53
+ get headers() {
54
+ return {
55
+ "api-key": this.apiKey,
56
+ };
57
+ }
58
+ }
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OpenAIApiConfiguration = void 0;
4
+ const BasicApiConfiguration_js_1 = require("../../model-function/BasicApiConfiguration.cjs");
5
+ const loadApiKey_js_1 = require("../../util/api/loadApiKey.cjs");
6
+ class OpenAIApiConfiguration extends BasicApiConfiguration_js_1.BasicApiConfiguration {
7
+ constructor({ baseUrl = "https://api.openai.com/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: "OPENAI_API_KEY",
22
+ description: "OpenAI",
23
+ });
24
+ }
25
+ get headers() {
26
+ return {
27
+ Authorization: `Bearer ${this.apiKey}`,
28
+ };
29
+ }
30
+ }
31
+ exports.OpenAIApiConfiguration = OpenAIApiConfiguration;
@@ -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 OpenAIApiConfiguration 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 OpenAIApiConfiguration extends BasicApiConfiguration {
4
+ constructor({ baseUrl = "https://api.openai.com/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: "OPENAI_API_KEY",
19
+ description: "OpenAI",
20
+ });
21
+ }
22
+ get headers() {
23
+ return {
24
+ Authorization: `Bearer ${this.apiKey}`,
25
+ };
26
+ }
27
+ }
@@ -5,6 +5,7 @@ const zod_1 = require("zod");
5
5
  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
+ const OpenAIApiConfiguration_js_1 = require("./OpenAIApiConfiguration.cjs");
8
9
  const OpenAIError_js_1 = require("./OpenAIError.cjs");
9
10
  /**
10
11
  * @see https://openai.com/pricing
@@ -43,34 +44,31 @@ class OpenAIImageGenerationModel extends AbstractModel_js_1.AbstractModel {
43
44
  value: null
44
45
  });
45
46
  }
46
- get apiKey() {
47
- const apiKey = this.settings.apiKey ?? process.env.OPENAI_API_KEY;
48
- if (apiKey == null) {
49
- 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.`);
50
- }
51
- return apiKey;
52
- }
53
47
  async callAPI(prompt, options) {
54
48
  const run = options?.run;
55
49
  const settings = options?.settings;
56
50
  const responseFormat = options?.responseFormat;
57
- const callSettings = Object.assign({
58
- apiKey: this.apiKey,
51
+ const combinedSettings = {
52
+ ...this.settings,
53
+ ...settings,
54
+ };
55
+ const callSettings = {
59
56
  user: this.settings.isUserIdForwardingEnabled ? run?.userId : undefined,
60
- }, this.settings, settings, {
57
+ // Copied settings:
58
+ ...combinedSettings,
59
+ // other settings:
61
60
  abortSignal: run?.abortSignal,
62
- prompt,
63
61
  responseFormat,
64
- });
62
+ prompt,
63
+ };
65
64
  return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
66
- retry: callSettings.retry,
67
- throttle: callSettings.throttle,
65
+ retry: callSettings.api?.retry,
66
+ throttle: callSettings.api?.throttle,
68
67
  call: async () => callOpenAIImageGenerationAPI(callSettings),
69
68
  });
70
69
  }
71
70
  get settingsForEvent() {
72
71
  const eventSettingProperties = [
73
- "baseUrl",
74
72
  "n",
75
73
  "size",
76
74
  ];
@@ -114,12 +112,10 @@ exports.OpenAIImageGenerationResponseFormat = {
114
112
  handler: (0, postToApi_js_1.createJsonResponseHandler)(openAIImageGenerationBase64JsonSchema),
115
113
  },
116
114
  };
117
- async function callOpenAIImageGenerationAPI({ baseUrl = "https://api.openai.com/v1", abortSignal, apiKey, prompt, n, size, responseFormat, user, }) {
115
+ async function callOpenAIImageGenerationAPI({ api = new OpenAIApiConfiguration_js_1.OpenAIApiConfiguration(), abortSignal, prompt, n, size, responseFormat, user, }) {
118
116
  return (0, postToApi_js_1.postJsonToApi)({
119
- url: `${baseUrl}/images/generations`,
120
- headers: {
121
- Authorization: `Bearer ${apiKey}`,
122
- },
117
+ url: api.assembleUrl("/images/generations"),
118
+ headers: api.headers,
123
119
  body: {
124
120
  prompt,
125
121
  n,
@@ -1,9 +1,9 @@
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
6
  import { ResponseHandler } from "../../util/api/postToApi.js";
6
- import { OpenAIModelSettings } from "./OpenAIModelSettings.js";
7
7
  export interface OpenAIImageGenerationCallSettings {
8
8
  n?: number;
9
9
  size?: "256x256" | "512x512" | "1024x1024";
@@ -11,7 +11,8 @@ export interface OpenAIImageGenerationCallSettings {
11
11
  export declare const calculateOpenAIImageGenerationCostInMillicents: ({ settings, }: {
12
12
  settings: OpenAIImageGenerationSettings;
13
13
  }) => number;
14
- export interface OpenAIImageGenerationSettings extends ImageGenerationModelSettings, OpenAIImageGenerationCallSettings, OpenAIModelSettings {
14
+ export interface OpenAIImageGenerationSettings extends ImageGenerationModelSettings, OpenAIImageGenerationCallSettings {
15
+ api?: ApiConfiguration;
15
16
  isUserIdForwardingEnabled?: boolean;
16
17
  }
17
18
  /**
@@ -29,10 +30,9 @@ export declare class OpenAIImageGenerationModel extends AbstractModel<OpenAIImag
29
30
  constructor(settings: OpenAIImageGenerationSettings);
30
31
  readonly provider: "openai";
31
32
  readonly modelName: null;
32
- private get apiKey();
33
33
  callAPI<RESULT>(prompt: string, options: {
34
34
  responseFormat: OpenAIImageGenerationResponseFormatType<RESULT>;
35
- } & ModelFunctionOptions<Partial<OpenAIImageGenerationCallSettings & OpenAIModelSettings & {
35
+ } & ModelFunctionOptions<Partial<OpenAIImageGenerationCallSettings & {
36
36
  user?: string;
37
37
  }>>): Promise<RESULT>;
38
38
  get settingsForEvent(): Partial<OpenAIImageGenerationSettings>;
@@ -2,6 +2,7 @@ import { z } from "zod";
2
2
  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
+ import { OpenAIApiConfiguration } from "./OpenAIApiConfiguration.js";
5
6
  import { failedOpenAICallResponseHandler } from "./OpenAIError.js";
6
7
  /**
7
8
  * @see https://openai.com/pricing
@@ -39,34 +40,31 @@ export class OpenAIImageGenerationModel extends AbstractModel {
39
40
  value: null
40
41
  });
41
42
  }
42
- get apiKey() {
43
- const apiKey = this.settings.apiKey ?? process.env.OPENAI_API_KEY;
44
- if (apiKey == null) {
45
- 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.`);
46
- }
47
- return apiKey;
48
- }
49
43
  async callAPI(prompt, options) {
50
44
  const run = options?.run;
51
45
  const settings = options?.settings;
52
46
  const responseFormat = options?.responseFormat;
53
- const callSettings = Object.assign({
54
- apiKey: this.apiKey,
47
+ const combinedSettings = {
48
+ ...this.settings,
49
+ ...settings,
50
+ };
51
+ const callSettings = {
55
52
  user: this.settings.isUserIdForwardingEnabled ? run?.userId : undefined,
56
- }, this.settings, settings, {
53
+ // Copied settings:
54
+ ...combinedSettings,
55
+ // other settings:
57
56
  abortSignal: run?.abortSignal,
58
- prompt,
59
57
  responseFormat,
60
- });
58
+ prompt,
59
+ };
61
60
  return callWithRetryAndThrottle({
62
- retry: callSettings.retry,
63
- throttle: callSettings.throttle,
61
+ retry: callSettings.api?.retry,
62
+ throttle: callSettings.api?.throttle,
64
63
  call: async () => callOpenAIImageGenerationAPI(callSettings),
65
64
  });
66
65
  }
67
66
  get settingsForEvent() {
68
67
  const eventSettingProperties = [
69
- "baseUrl",
70
68
  "n",
71
69
  "size",
72
70
  ];
@@ -109,12 +107,10 @@ export const OpenAIImageGenerationResponseFormat = {
109
107
  handler: createJsonResponseHandler(openAIImageGenerationBase64JsonSchema),
110
108
  },
111
109
  };
112
- async function callOpenAIImageGenerationAPI({ baseUrl = "https://api.openai.com/v1", abortSignal, apiKey, prompt, n, size, responseFormat, user, }) {
110
+ async function callOpenAIImageGenerationAPI({ api = new OpenAIApiConfiguration(), abortSignal, prompt, n, size, responseFormat, user, }) {
113
111
  return postJsonToApi({
114
- url: `${baseUrl}/images/generations`,
115
- headers: {
116
- Authorization: `Bearer ${apiKey}`,
117
- },
112
+ url: api.assembleUrl("/images/generations"),
113
+ headers: api.headers,
118
114
  body: {
119
115
  prompt,
120
116
  n,
@@ -9,6 +9,7 @@ const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
9
9
  const countTokens_js_1 = require("../../model-function/tokenize-text/countTokens.cjs");
10
10
  const callWithRetryAndThrottle_js_1 = require("../../util/api/callWithRetryAndThrottle.cjs");
11
11
  const postToApi_js_1 = require("../../util/api/postToApi.cjs");
12
+ const OpenAIApiConfiguration_js_1 = require("./OpenAIApiConfiguration.cjs");
12
13
  const OpenAIError_js_1 = require("./OpenAIError.cjs");
13
14
  const TikTokenTokenizer_js_1 = require("./TikTokenTokenizer.cjs");
14
15
  exports.OPENAI_TEXT_EMBEDDING_MODELS = {
@@ -86,36 +87,32 @@ class OpenAITextEmbeddingModel extends AbstractModel_js_1.AbstractModel {
86
87
  get modelName() {
87
88
  return this.settings.model;
88
89
  }
89
- get apiKey() {
90
- const apiKey = this.settings.apiKey ?? process.env.OPENAI_API_KEY;
91
- if (apiKey == null) {
92
- 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.`);
93
- }
94
- return apiKey;
95
- }
96
90
  async countTokens(input) {
97
91
  return (0, countTokens_js_1.countTokens)(this.tokenizer, input);
98
92
  }
99
93
  async callAPI(text, options) {
100
94
  const run = options?.run;
101
95
  const settings = options?.settings;
102
- const callSettings = Object.assign({
103
- apiKey: this.apiKey,
96
+ const combinedSettings = {
97
+ ...this.settings,
98
+ ...settings,
99
+ };
100
+ const callSettings = {
104
101
  user: this.settings.isUserIdForwardingEnabled ? run?.userId : undefined,
105
- }, this.settings, settings, {
102
+ // Copied settings:
103
+ ...combinedSettings,
104
+ // other settings:
106
105
  abortSignal: run?.abortSignal,
107
106
  input: text,
108
- });
107
+ };
109
108
  return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
110
- retry: this.settings.retry,
111
- throttle: this.settings.throttle,
109
+ retry: callSettings.api?.retry,
110
+ throttle: callSettings.api?.throttle,
112
111
  call: async () => callOpenAITextEmbeddingAPI(callSettings),
113
112
  });
114
113
  }
115
114
  get settingsForEvent() {
116
- return {
117
- baseUrl: this.settings.baseUrl,
118
- };
115
+ return {};
119
116
  }
120
117
  generateEmbeddingResponse(texts, options) {
121
118
  if (texts.length > this.maxTextsPerCall) {
@@ -146,26 +143,10 @@ const openAITextEmbeddingResponseSchema = zod_1.default.object({
146
143
  total_tokens: zod_1.default.number(),
147
144
  }),
148
145
  });
149
- /**
150
- * Call the OpenAI Embedding API to generate an embedding for the given input.
151
- *
152
- * @see https://platform.openai.com/docs/api-reference/embeddings
153
- *
154
- * @example
155
- * const response = await callOpenAITextEmbeddingAPI({
156
- * apiKey: OPENAI_API_KEY,
157
- * model: "text-embedding-ada-002",
158
- * input: "At first, Nox didn't know what to do with the pup.",
159
- * });
160
- *
161
- * console.log(response.data[0].embedding);
162
- */
163
- async function callOpenAITextEmbeddingAPI({ baseUrl = "https://api.openai.com/v1", abortSignal, apiKey, model, input, user, }) {
146
+ async function callOpenAITextEmbeddingAPI({ api = new OpenAIApiConfiguration_js_1.OpenAIApiConfiguration(), abortSignal, model, input, user, }) {
164
147
  return (0, postToApi_js_1.postJsonToApi)({
165
- url: `${baseUrl}/embeddings`,
166
- headers: {
167
- Authorization: `Bearer ${apiKey}`,
168
- },
148
+ url: api.assembleUrl("/embeddings"),
149
+ headers: api.headers,
169
150
  body: {
170
151
  model,
171
152
  input,
@@ -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 { TextEmbeddingModel, TextEmbeddingModelSettings } from "../../model-function/embed-text/TextEmbeddingModel.js";
5
- import { RetryFunction } from "../../util/api/RetryFunction.js";
6
- import { ThrottleFunction } from "../../util/api/ThrottleFunction.js";
7
6
  import { TikTokenTokenizer } from "./TikTokenTokenizer.js";
8
7
  export declare const OPENAI_TEXT_EMBEDDING_MODELS: {
9
8
  "text-embedding-ada-002": {
@@ -19,11 +18,8 @@ export declare const calculateOpenAIEmbeddingCostInMillicents: ({ model, respons
19
18
  responses: OpenAITextEmbeddingResponse[];
20
19
  }) => number;
21
20
  export interface OpenAITextEmbeddingModelSettings extends TextEmbeddingModelSettings {
21
+ api?: ApiConfiguration;
22
22
  model: OpenAITextEmbeddingModelType;
23
- baseUrl?: string;
24
- apiKey?: string;
25
- retry?: RetryFunction;
26
- throttle?: ThrottleFunction;
27
23
  isUserIdForwardingEnabled?: boolean;
28
24
  }
29
25
  /**
@@ -48,7 +44,6 @@ export declare class OpenAITextEmbeddingModel extends AbstractModel<OpenAITextEm
48
44
  readonly embeddingDimensions: number;
49
45
  readonly tokenizer: TikTokenTokenizer;
50
46
  readonly contextWindowSize: number;
51
- private get apiKey();
52
47
  countTokens(input: string): Promise<number>;
53
48
  callAPI(text: string, options?: ModelFunctionOptions<OpenAITextEmbeddingModelSettings>): Promise<OpenAITextEmbeddingResponse>;
54
49
  get settingsForEvent(): Partial<OpenAITextEmbeddingModelSettings>;