modelfusion 0.27.0 → 0.27.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.cjs +1 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/model-function/ApiConfiguration.d.ts +14 -0
- package/model-function/BasicApiConfiguration.cjs +35 -0
- package/model-function/BasicApiConfiguration.d.ts +15 -0
- package/model-function/BasicApiConfiguration.js +31 -0
- package/model-function/index.cjs +2 -0
- package/model-function/index.d.ts +2 -0
- package/model-function/index.js +2 -0
- package/model-provider/automatic1111/Automatic1111ApiConfiguration.cjs +14 -0
- package/model-provider/automatic1111/Automatic1111ApiConfiguration.d.ts +10 -0
- package/model-provider/automatic1111/Automatic1111ApiConfiguration.js +10 -0
- package/model-provider/automatic1111/Automatic1111ImageGenerationModel.cjs +10 -7
- package/model-provider/automatic1111/Automatic1111ImageGenerationModel.d.ts +2 -5
- package/model-provider/automatic1111/Automatic1111ImageGenerationModel.js +10 -7
- package/model-provider/automatic1111/index.cjs +1 -0
- package/model-provider/automatic1111/index.d.ts +1 -0
- package/model-provider/automatic1111/index.js +1 -0
- package/model-provider/cohere/CohereApiConfiguration.cjs +29 -0
- package/model-provider/cohere/CohereApiConfiguration.d.ts +13 -0
- package/model-provider/cohere/CohereApiConfiguration.js +25 -0
- package/model-provider/cohere/CohereTextEmbeddingModel.cjs +11 -38
- package/model-provider/cohere/CohereTextEmbeddingModel.d.ts +2 -11
- package/model-provider/cohere/CohereTextEmbeddingModel.js +11 -38
- package/model-provider/cohere/CohereTextGenerationModel.cjs +7 -36
- package/model-provider/cohere/CohereTextGenerationModel.d.ts +2 -11
- package/model-provider/cohere/CohereTextGenerationModel.js +7 -36
- package/model-provider/cohere/CohereTokenizer.cjs +11 -33
- package/model-provider/cohere/CohereTokenizer.d.ts +4 -9
- package/model-provider/cohere/CohereTokenizer.js +11 -33
- package/model-provider/cohere/index.cjs +1 -0
- package/model-provider/cohere/index.d.ts +1 -0
- package/model-provider/cohere/index.js +1 -0
- package/model-provider/elevenlabs/ElevenLabsApiConfiguration.cjs +29 -0
- package/model-provider/elevenlabs/ElevenLabsApiConfiguration.d.ts +13 -0
- package/model-provider/elevenlabs/ElevenLabsApiConfiguration.js +25 -0
- package/model-provider/elevenlabs/ElevenLabsSpeechSynthesisModel.cjs +22 -32
- package/model-provider/elevenlabs/ElevenLabsSpeechSynthesisModel.d.ts +8 -8
- package/model-provider/elevenlabs/ElevenLabsSpeechSynthesisModel.js +22 -32
- package/model-provider/elevenlabs/index.cjs +1 -0
- package/model-provider/elevenlabs/index.d.ts +1 -0
- package/model-provider/elevenlabs/index.js +1 -0
- package/model-provider/huggingface/HuggingFaceApiConfiguration.cjs +29 -0
- package/model-provider/huggingface/HuggingFaceApiConfiguration.d.ts +13 -0
- package/model-provider/huggingface/HuggingFaceApiConfiguration.js +25 -0
- package/model-provider/huggingface/HuggingFaceError.cjs +3 -2
- package/model-provider/huggingface/HuggingFaceError.d.ts +3 -3
- package/model-provider/huggingface/HuggingFaceError.js +3 -2
- package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.cjs +15 -21
- package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.d.ts +3 -8
- package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.js +15 -21
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.cjs +7 -36
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.d.ts +2 -7
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.js +7 -36
- package/model-provider/huggingface/index.cjs +1 -0
- package/model-provider/huggingface/index.d.ts +1 -0
- package/model-provider/huggingface/index.js +1 -0
- package/model-provider/llamacpp/LlamaCppApiConfiguration.cjs +14 -0
- package/model-provider/llamacpp/LlamaCppApiConfiguration.d.ts +10 -0
- package/model-provider/llamacpp/LlamaCppApiConfiguration.js +10 -0
- package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.cjs +11 -12
- package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.d.ts +2 -9
- package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.js +11 -12
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.cjs +9 -10
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.d.ts +2 -9
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.js +9 -10
- package/model-provider/llamacpp/LlamaCppTokenizer.cjs +10 -8
- package/model-provider/llamacpp/LlamaCppTokenizer.d.ts +4 -10
- package/model-provider/llamacpp/LlamaCppTokenizer.js +10 -8
- package/model-provider/llamacpp/index.cjs +1 -0
- package/model-provider/llamacpp/index.d.ts +1 -0
- package/model-provider/llamacpp/index.js +1 -0
- package/model-provider/lmnt/LmntApiConfiguration.cjs +29 -0
- package/model-provider/lmnt/LmntApiConfiguration.d.ts +13 -0
- package/model-provider/lmnt/LmntApiConfiguration.js +25 -0
- package/model-provider/lmnt/LmntSpeechSynthesisModel.cjs +18 -34
- package/model-provider/lmnt/LmntSpeechSynthesisModel.d.ts +8 -8
- package/model-provider/lmnt/LmntSpeechSynthesisModel.js +18 -34
- package/model-provider/lmnt/index.cjs +1 -0
- package/model-provider/lmnt/index.d.ts +1 -0
- package/model-provider/lmnt/index.js +1 -0
- package/model-provider/openai/AzureOpenAIApiConfiguration.cjs +51 -0
- package/model-provider/openai/AzureOpenAIApiConfiguration.d.ts +26 -0
- package/model-provider/openai/AzureOpenAIApiConfiguration.js +47 -0
- package/model-provider/openai/OpenAIApiConfiguration.cjs +31 -0
- package/model-provider/openai/OpenAIApiConfiguration.d.ts +13 -0
- package/model-provider/openai/OpenAIApiConfiguration.js +27 -0
- package/model-provider/openai/OpenAIImageGenerationModel.cjs +16 -20
- package/model-provider/openai/OpenAIImageGenerationModel.d.ts +4 -4
- package/model-provider/openai/OpenAIImageGenerationModel.js +16 -20
- package/model-provider/openai/OpenAITextEmbeddingModel.cjs +16 -35
- package/model-provider/openai/OpenAITextEmbeddingModel.d.ts +2 -7
- package/model-provider/openai/OpenAITextEmbeddingModel.js +16 -35
- package/model-provider/openai/OpenAITextGenerationModel.cjs +6 -33
- package/model-provider/openai/OpenAITextGenerationModel.d.ts +4 -5
- package/model-provider/openai/OpenAITextGenerationModel.js +6 -33
- package/model-provider/openai/OpenAITranscriptionModel.cjs +16 -39
- package/model-provider/openai/OpenAITranscriptionModel.d.ts +4 -11
- package/model-provider/openai/OpenAITranscriptionModel.js +16 -39
- package/model-provider/openai/chat/OpenAIChatModel.cjs +9 -17
- package/model-provider/openai/chat/OpenAIChatModel.d.ts +4 -5
- package/model-provider/openai/chat/OpenAIChatModel.js +9 -17
- package/model-provider/openai/index.cjs +2 -1
- package/model-provider/openai/index.d.ts +2 -1
- package/model-provider/openai/index.js +2 -1
- package/model-provider/stability/StabilityApiConfiguration.cjs +31 -0
- package/model-provider/stability/StabilityApiConfiguration.d.ts +13 -0
- package/model-provider/stability/StabilityApiConfiguration.js +27 -0
- package/model-provider/stability/StabilityImageGenerationModel.cjs +12 -46
- package/model-provider/stability/StabilityImageGenerationModel.d.ts +2 -7
- package/model-provider/stability/StabilityImageGenerationModel.js +12 -46
- package/model-provider/stability/index.cjs +1 -0
- package/model-provider/stability/index.d.ts +1 -0
- package/model-provider/stability/index.js +1 -0
- package/observability/helicone/HeliconeOpenAIApiConfiguration.cjs +45 -0
- package/observability/helicone/HeliconeOpenAIApiConfiguration.d.ts +15 -0
- package/observability/helicone/HeliconeOpenAIApiConfiguration.js +41 -0
- package/observability/index.cjs +17 -0
- package/observability/index.d.ts +1 -0
- package/observability/index.js +1 -0
- package/package.json +1 -1
- package/util/api/loadApiKey.cjs +11 -0
- package/util/api/loadApiKey.d.ts +6 -0
- package/util/api/loadApiKey.js +7 -0
- package/model-provider/openai/OpenAIModelSettings.d.ts +0 -8
- /package/{model-provider/openai/OpenAIModelSettings.cjs → model-function/ApiConfiguration.cjs} +0 -0
- /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
|
-
|
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
|
-
|
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
|
22
|
-
|
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
|
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:
|
37
|
-
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:
|
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);
|
@@ -0,0 +1,51 @@
|
|
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
|
+
* `[baseUrl]/[path]?api-version=[apiVersion]`
|
10
|
+
*
|
11
|
+
* The `baseUrl` should have a format similar to: `https://${resourceName}.openai.azure.com/openai/deployments/${deploymentId}`
|
12
|
+
* (no trailing slash).
|
13
|
+
*
|
14
|
+
* @see https://learn.microsoft.com/en-us/azure/ai-services/openai/reference
|
15
|
+
*/
|
16
|
+
class AzureOpenAIApiConfiguration extends BasicApiConfiguration_js_1.BasicApiConfiguration {
|
17
|
+
constructor({ baseUrl, apiVersion, apiKey, retry, throttle, }) {
|
18
|
+
super({
|
19
|
+
baseUrl,
|
20
|
+
retry,
|
21
|
+
throttle,
|
22
|
+
});
|
23
|
+
Object.defineProperty(this, "apiVersion", {
|
24
|
+
enumerable: true,
|
25
|
+
configurable: true,
|
26
|
+
writable: true,
|
27
|
+
value: void 0
|
28
|
+
});
|
29
|
+
Object.defineProperty(this, "apiKey", {
|
30
|
+
enumerable: true,
|
31
|
+
configurable: true,
|
32
|
+
writable: true,
|
33
|
+
value: void 0
|
34
|
+
});
|
35
|
+
this.apiVersion = apiVersion;
|
36
|
+
this.apiKey = (0, loadApiKey_js_1.loadApiKey)({
|
37
|
+
apiKey,
|
38
|
+
environmentVariableName: "AZURE_OPENAI_API_KEY",
|
39
|
+
description: "Azure OpenAI",
|
40
|
+
});
|
41
|
+
}
|
42
|
+
assembleUrl(path) {
|
43
|
+
return `${this.baseUrl}${path}?api-version=${this.apiVersion}`;
|
44
|
+
}
|
45
|
+
get headers() {
|
46
|
+
return {
|
47
|
+
"api-key": this.apiKey,
|
48
|
+
};
|
49
|
+
}
|
50
|
+
}
|
51
|
+
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
|
+
* `[baseUrl]/[path]?api-version=[apiVersion]`
|
8
|
+
*
|
9
|
+
* The `baseUrl` should have a format similar to: `https://${resourceName}.openai.azure.com/openai/deployments/${deploymentId}`
|
10
|
+
* (no trailing slash).
|
11
|
+
*
|
12
|
+
* @see https://learn.microsoft.com/en-us/azure/ai-services/openai/reference
|
13
|
+
*/
|
14
|
+
export declare class AzureOpenAIApiConfiguration extends BasicApiConfiguration {
|
15
|
+
readonly apiVersion: string;
|
16
|
+
readonly apiKey: string;
|
17
|
+
constructor({ baseUrl, apiVersion, apiKey, retry, throttle, }: {
|
18
|
+
baseUrl: 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,47 @@
|
|
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
|
+
* `[baseUrl]/[path]?api-version=[apiVersion]`
|
7
|
+
*
|
8
|
+
* The `baseUrl` should have a format similar to: `https://${resourceName}.openai.azure.com/openai/deployments/${deploymentId}`
|
9
|
+
* (no trailing slash).
|
10
|
+
*
|
11
|
+
* @see https://learn.microsoft.com/en-us/azure/ai-services/openai/reference
|
12
|
+
*/
|
13
|
+
export class AzureOpenAIApiConfiguration extends BasicApiConfiguration {
|
14
|
+
constructor({ baseUrl, apiVersion, apiKey, retry, throttle, }) {
|
15
|
+
super({
|
16
|
+
baseUrl,
|
17
|
+
retry,
|
18
|
+
throttle,
|
19
|
+
});
|
20
|
+
Object.defineProperty(this, "apiVersion", {
|
21
|
+
enumerable: true,
|
22
|
+
configurable: true,
|
23
|
+
writable: true,
|
24
|
+
value: void 0
|
25
|
+
});
|
26
|
+
Object.defineProperty(this, "apiKey", {
|
27
|
+
enumerable: true,
|
28
|
+
configurable: true,
|
29
|
+
writable: true,
|
30
|
+
value: void 0
|
31
|
+
});
|
32
|
+
this.apiVersion = apiVersion;
|
33
|
+
this.apiKey = loadApiKey({
|
34
|
+
apiKey,
|
35
|
+
environmentVariableName: "AZURE_OPENAI_API_KEY",
|
36
|
+
description: "Azure OpenAI",
|
37
|
+
});
|
38
|
+
}
|
39
|
+
assembleUrl(path) {
|
40
|
+
return `${this.baseUrl}${path}?api-version=${this.apiVersion}`;
|
41
|
+
}
|
42
|
+
get headers() {
|
43
|
+
return {
|
44
|
+
"api-key": this.apiKey,
|
45
|
+
};
|
46
|
+
}
|
47
|
+
}
|
@@ -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
|
58
|
-
|
51
|
+
const combinedSettings = {
|
52
|
+
...this.settings,
|
53
|
+
...settings,
|
54
|
+
};
|
55
|
+
const callSettings = {
|
59
56
|
user: this.settings.isUserIdForwardingEnabled ? run?.userId : undefined,
|
60
|
-
|
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({
|
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:
|
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
|
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 &
|
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
|
54
|
-
|
47
|
+
const combinedSettings = {
|
48
|
+
...this.settings,
|
49
|
+
...settings,
|
50
|
+
};
|
51
|
+
const callSettings = {
|
55
52
|
user: this.settings.isUserIdForwardingEnabled ? run?.userId : undefined,
|
56
|
-
|
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({
|
110
|
+
async function callOpenAIImageGenerationAPI({ api = new OpenAIApiConfiguration(), abortSignal, prompt, n, size, responseFormat, user, }) {
|
113
111
|
return postJsonToApi({
|
114
|
-
url:
|
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
|
103
|
-
|
96
|
+
const combinedSettings = {
|
97
|
+
...this.settings,
|
98
|
+
...settings,
|
99
|
+
};
|
100
|
+
const callSettings = {
|
104
101
|
user: this.settings.isUserIdForwardingEnabled ? run?.userId : undefined,
|
105
|
-
|
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:
|
111
|
-
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:
|
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>;
|