modelfusion 0.61.0 → 0.63.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -1
- package/model-provider/openai/OpenAICompletionModel.d.ts +10 -10
- package/model-provider/openai/OpenAICostCalculator.cjs +15 -1
- package/model-provider/openai/OpenAICostCalculator.js +15 -1
- package/model-provider/openai/OpenAIImageGenerationModel.cjs +2 -4
- package/model-provider/openai/OpenAIImageGenerationModel.d.ts +3 -2
- package/model-provider/openai/OpenAIImageGenerationModel.js +2 -4
- package/model-provider/openai/OpenAISpeechModel.cjs +93 -0
- package/model-provider/openai/OpenAISpeechModel.d.ts +52 -0
- package/model-provider/openai/OpenAISpeechModel.js +88 -0
- package/model-provider/openai/chat/OpenAIChatModel.cjs +12 -2
- package/model-provider/openai/chat/OpenAIChatModel.d.ts +38 -26
- package/model-provider/openai/chat/OpenAIChatModel.js +12 -2
- package/model-provider/openai/chat/OpenAIChatStreamIterable.cjs +10 -1
- package/model-provider/openai/chat/OpenAIChatStreamIterable.js +10 -1
- 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/package.json +1 -1
package/README.md
CHANGED
@@ -14,6 +14,7 @@
|
|
14
14
|
|
15
15
|
**ModelFusion** is a TypeScript library for building AI applications, chatbots, and agents.
|
16
16
|
|
17
|
+
- **Vendor-neutral**: ModelFusion is a non-commercial open source project that is community-driven. You can use it with any supported vendor.
|
17
18
|
- **Multimodal**: ModelFusion supports a wide range of models including text generation, image generation, text-to-speech, speech-to-text, and embedding models.
|
18
19
|
- **Streaming**: ModelFusion supports streaming for many generation models, e.g. text streaming, structure streaming, and full duplex speech streaming.
|
19
20
|
- **Utility functions**: ModelFusion provides functionality for tools and tool usage, vector indices, and guards functions.
|
@@ -109,7 +110,7 @@ const speech = await generateSpeech(
|
|
109
110
|
);
|
110
111
|
```
|
111
112
|
|
112
|
-
Providers: [Eleven Labs](https://modelfusion.dev/integration/model-provider/elevenlabs), [LMNT](https://modelfusion.dev/integration/model-provider/lmnt)
|
113
|
+
Providers: [Eleven Labs](https://modelfusion.dev/integration/model-provider/elevenlabs), [LMNT](https://modelfusion.dev/integration/model-provider/lmnt), [OpenAI](https://modelfusion.dev/integration/model-provider/openai)
|
113
114
|
|
114
115
|
#### streamSpeech
|
115
116
|
|
@@ -150,18 +150,18 @@ export declare class OpenAICompletionModel extends AbstractModel<OpenAICompletio
|
|
150
150
|
object: "text_completion";
|
151
151
|
usage: {
|
152
152
|
prompt_tokens: number;
|
153
|
-
completion_tokens: number;
|
154
153
|
total_tokens: number;
|
154
|
+
completion_tokens: number;
|
155
155
|
};
|
156
156
|
model: string;
|
157
157
|
id: string;
|
158
|
-
created: number;
|
159
158
|
choices: {
|
160
159
|
text: string;
|
161
160
|
finish_reason: string;
|
162
161
|
index: number;
|
163
162
|
logprobs?: any;
|
164
163
|
}[];
|
164
|
+
created: number;
|
165
165
|
};
|
166
166
|
text: string;
|
167
167
|
usage: {
|
@@ -212,45 +212,45 @@ declare const OpenAICompletionResponseSchema: z.ZodObject<{
|
|
212
212
|
total_tokens: z.ZodNumber;
|
213
213
|
}, "strip", z.ZodTypeAny, {
|
214
214
|
prompt_tokens: number;
|
215
|
-
completion_tokens: number;
|
216
215
|
total_tokens: number;
|
216
|
+
completion_tokens: number;
|
217
217
|
}, {
|
218
218
|
prompt_tokens: number;
|
219
|
-
completion_tokens: number;
|
220
219
|
total_tokens: number;
|
220
|
+
completion_tokens: number;
|
221
221
|
}>;
|
222
222
|
}, "strip", z.ZodTypeAny, {
|
223
223
|
object: "text_completion";
|
224
224
|
usage: {
|
225
225
|
prompt_tokens: number;
|
226
|
-
completion_tokens: number;
|
227
226
|
total_tokens: number;
|
227
|
+
completion_tokens: number;
|
228
228
|
};
|
229
229
|
model: string;
|
230
230
|
id: string;
|
231
|
-
created: number;
|
232
231
|
choices: {
|
233
232
|
text: string;
|
234
233
|
finish_reason: string;
|
235
234
|
index: number;
|
236
235
|
logprobs?: any;
|
237
236
|
}[];
|
237
|
+
created: number;
|
238
238
|
}, {
|
239
239
|
object: "text_completion";
|
240
240
|
usage: {
|
241
241
|
prompt_tokens: number;
|
242
|
-
completion_tokens: number;
|
243
242
|
total_tokens: number;
|
243
|
+
completion_tokens: number;
|
244
244
|
};
|
245
245
|
model: string;
|
246
246
|
id: string;
|
247
|
-
created: number;
|
248
247
|
choices: {
|
249
248
|
text: string;
|
250
249
|
finish_reason: string;
|
251
250
|
index: number;
|
252
251
|
logprobs?: any;
|
253
252
|
}[];
|
253
|
+
created: number;
|
254
254
|
}>;
|
255
255
|
export type OpenAICompletionResponse = z.infer<typeof OpenAICompletionResponseSchema>;
|
256
256
|
export type OpenAITextResponseFormatType<T> = {
|
@@ -267,18 +267,18 @@ export declare const OpenAITextResponseFormat: {
|
|
267
267
|
object: "text_completion";
|
268
268
|
usage: {
|
269
269
|
prompt_tokens: number;
|
270
|
-
completion_tokens: number;
|
271
270
|
total_tokens: number;
|
271
|
+
completion_tokens: number;
|
272
272
|
};
|
273
273
|
model: string;
|
274
274
|
id: string;
|
275
|
-
created: number;
|
276
275
|
choices: {
|
277
276
|
text: string;
|
278
277
|
finish_reason: string;
|
279
278
|
index: number;
|
280
279
|
logprobs?: any;
|
281
280
|
}[];
|
281
|
+
created: number;
|
282
282
|
}>;
|
283
283
|
};
|
284
284
|
/**
|
@@ -1,9 +1,10 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.OpenAICostCalculator = void 0;
|
4
|
+
const OpenAICompletionModel_js_1 = require("./OpenAICompletionModel.cjs");
|
4
5
|
const OpenAIImageGenerationModel_js_1 = require("./OpenAIImageGenerationModel.cjs");
|
6
|
+
const OpenAISpeechModel_js_1 = require("./OpenAISpeechModel.cjs");
|
5
7
|
const OpenAITextEmbeddingModel_js_1 = require("./OpenAITextEmbeddingModel.cjs");
|
6
|
-
const OpenAICompletionModel_js_1 = require("./OpenAICompletionModel.cjs");
|
7
8
|
const OpenAITranscriptionModel_js_1 = require("./OpenAITranscriptionModel.cjs");
|
8
9
|
const OpenAIChatModel_js_1 = require("./chat/OpenAIChatModel.cjs");
|
9
10
|
class OpenAICostCalculator {
|
@@ -20,7 +21,11 @@ class OpenAICostCalculator {
|
|
20
21
|
const model = call.model.modelName;
|
21
22
|
switch (type) {
|
22
23
|
case "generate-image": {
|
24
|
+
if (model == null) {
|
25
|
+
return null;
|
26
|
+
}
|
23
27
|
return (0, OpenAIImageGenerationModel_js_1.calculateOpenAIImageGenerationCostInMillicents)({
|
28
|
+
model: model,
|
24
29
|
settings: call.settings,
|
25
30
|
});
|
26
31
|
}
|
@@ -68,6 +73,15 @@ class OpenAICostCalculator {
|
|
68
73
|
.response,
|
69
74
|
});
|
70
75
|
}
|
76
|
+
case "generate-speech": {
|
77
|
+
if (model == null) {
|
78
|
+
return null;
|
79
|
+
}
|
80
|
+
return (0, OpenAISpeechModel_js_1.calculateOpenAISpeechCostInMillicents)({
|
81
|
+
model: model,
|
82
|
+
input: call.input,
|
83
|
+
});
|
84
|
+
}
|
71
85
|
}
|
72
86
|
return null;
|
73
87
|
}
|
@@ -1,6 +1,7 @@
|
|
1
|
+
import { calculateOpenAICompletionCostInMillicents, isOpenAICompletionModel, } from "./OpenAICompletionModel.js";
|
1
2
|
import { calculateOpenAIImageGenerationCostInMillicents, } from "./OpenAIImageGenerationModel.js";
|
3
|
+
import { calculateOpenAISpeechCostInMillicents, } from "./OpenAISpeechModel.js";
|
2
4
|
import { calculateOpenAIEmbeddingCostInMillicents, isOpenAIEmbeddingModel, } from "./OpenAITextEmbeddingModel.js";
|
3
|
-
import { calculateOpenAICompletionCostInMillicents, isOpenAICompletionModel, } from "./OpenAICompletionModel.js";
|
4
5
|
import { calculateOpenAITranscriptionCostInMillicents, } from "./OpenAITranscriptionModel.js";
|
5
6
|
import { calculateOpenAIChatCostInMillicents, isOpenAIChatModel, } from "./chat/OpenAIChatModel.js";
|
6
7
|
export class OpenAICostCalculator {
|
@@ -17,7 +18,11 @@ export class OpenAICostCalculator {
|
|
17
18
|
const model = call.model.modelName;
|
18
19
|
switch (type) {
|
19
20
|
case "generate-image": {
|
21
|
+
if (model == null) {
|
22
|
+
return null;
|
23
|
+
}
|
20
24
|
return calculateOpenAIImageGenerationCostInMillicents({
|
25
|
+
model: model,
|
21
26
|
settings: call.settings,
|
22
27
|
});
|
23
28
|
}
|
@@ -65,6 +70,15 @@ export class OpenAICostCalculator {
|
|
65
70
|
.response,
|
66
71
|
});
|
67
72
|
}
|
73
|
+
case "generate-speech": {
|
74
|
+
if (model == null) {
|
75
|
+
return null;
|
76
|
+
}
|
77
|
+
return calculateOpenAISpeechCostInMillicents({
|
78
|
+
model: model,
|
79
|
+
input: call.input,
|
80
|
+
});
|
81
|
+
}
|
68
82
|
}
|
69
83
|
return null;
|
70
84
|
}
|
@@ -55,9 +55,8 @@ exports.OPENAI_IMAGE_MODELS = {
|
|
55
55
|
/**
|
56
56
|
* @see https://openai.com/pricing
|
57
57
|
*/
|
58
|
-
const calculateOpenAIImageGenerationCostInMillicents = ({ settings, }) => {
|
59
|
-
|
60
|
-
const cost = exports.OPENAI_IMAGE_MODELS[settings.model]?.getCost(settings);
|
58
|
+
const calculateOpenAIImageGenerationCostInMillicents = ({ model, settings, }) => {
|
59
|
+
const cost = exports.OPENAI_IMAGE_MODELS[model]?.getCost(settings);
|
61
60
|
if (cost == null) {
|
62
61
|
return null;
|
63
62
|
}
|
@@ -106,7 +105,6 @@ class OpenAIImageGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
106
105
|
}
|
107
106
|
get settingsForEvent() {
|
108
107
|
const eventSettingProperties = [
|
109
|
-
"model",
|
110
108
|
"n",
|
111
109
|
"size",
|
112
110
|
"quality",
|
@@ -11,13 +11,14 @@ export declare const OPENAI_IMAGE_MODELS: {
|
|
11
11
|
getCost(settings: OpenAIImageGenerationSettings): 2000 | 1800 | 1600 | null;
|
12
12
|
};
|
13
13
|
"dall-e-3": {
|
14
|
-
getCost(settings: OpenAIImageGenerationSettings):
|
14
|
+
getCost(settings: OpenAIImageGenerationSettings): 8000 | 4000 | 12000 | null;
|
15
15
|
};
|
16
16
|
};
|
17
17
|
/**
|
18
18
|
* @see https://openai.com/pricing
|
19
19
|
*/
|
20
|
-
export declare const calculateOpenAIImageGenerationCostInMillicents: ({ settings, }: {
|
20
|
+
export declare const calculateOpenAIImageGenerationCostInMillicents: ({ model, settings, }: {
|
21
|
+
model: OpenAIImageModelType;
|
21
22
|
settings: OpenAIImageGenerationSettings;
|
22
23
|
}) => number | null;
|
23
24
|
export type OpenAIImageModelType = keyof typeof OPENAI_IMAGE_MODELS;
|
@@ -52,9 +52,8 @@ export const OPENAI_IMAGE_MODELS = {
|
|
52
52
|
/**
|
53
53
|
* @see https://openai.com/pricing
|
54
54
|
*/
|
55
|
-
export const calculateOpenAIImageGenerationCostInMillicents = ({ settings, }) => {
|
56
|
-
|
57
|
-
const cost = OPENAI_IMAGE_MODELS[settings.model]?.getCost(settings);
|
55
|
+
export const calculateOpenAIImageGenerationCostInMillicents = ({ model, settings, }) => {
|
56
|
+
const cost = OPENAI_IMAGE_MODELS[model]?.getCost(settings);
|
58
57
|
if (cost == null) {
|
59
58
|
return null;
|
60
59
|
}
|
@@ -102,7 +101,6 @@ export class OpenAIImageGenerationModel extends AbstractModel {
|
|
102
101
|
}
|
103
102
|
get settingsForEvent() {
|
104
103
|
const eventSettingProperties = [
|
105
|
-
"model",
|
106
104
|
"n",
|
107
105
|
"size",
|
108
106
|
"quality",
|
@@ -0,0 +1,93 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.OpenAISpeechModel = exports.calculateOpenAISpeechCostInMillicents = exports.OPENAI_SPEECH_MODELS = void 0;
|
4
|
+
const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
|
5
|
+
const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndThrottle.cjs");
|
6
|
+
const postToApi_js_1 = require("../../core/api/postToApi.cjs");
|
7
|
+
const OpenAIApiConfiguration_js_1 = require("./OpenAIApiConfiguration.cjs");
|
8
|
+
const OpenAIError_js_1 = require("./OpenAIError.cjs");
|
9
|
+
/**
|
10
|
+
* @see https://openai.com/pricing
|
11
|
+
*/
|
12
|
+
exports.OPENAI_SPEECH_MODELS = {
|
13
|
+
"tts-1": {
|
14
|
+
costInMillicentsPerCharacter: 1.5, // = 1500 / 1000,
|
15
|
+
},
|
16
|
+
"tts-1-hd": {
|
17
|
+
costInMillicentsPerCharacter: 3, // = 3000 / 1000
|
18
|
+
},
|
19
|
+
};
|
20
|
+
const calculateOpenAISpeechCostInMillicents = ({ model, input, }) => {
|
21
|
+
if (!exports.OPENAI_SPEECH_MODELS[model]) {
|
22
|
+
return null;
|
23
|
+
}
|
24
|
+
return (input.length * exports.OPENAI_SPEECH_MODELS[model].costInMillicentsPerCharacter);
|
25
|
+
};
|
26
|
+
exports.calculateOpenAISpeechCostInMillicents = calculateOpenAISpeechCostInMillicents;
|
27
|
+
/**
|
28
|
+
* Synthesize speech using the OpenAI API.
|
29
|
+
*
|
30
|
+
* @see https://platform.openai.com/docs/api-reference/audio/createSpeech
|
31
|
+
*/
|
32
|
+
class OpenAISpeechModel extends AbstractModel_js_1.AbstractModel {
|
33
|
+
constructor(settings) {
|
34
|
+
super({ settings });
|
35
|
+
Object.defineProperty(this, "provider", {
|
36
|
+
enumerable: true,
|
37
|
+
configurable: true,
|
38
|
+
writable: true,
|
39
|
+
value: "openai"
|
40
|
+
});
|
41
|
+
}
|
42
|
+
get voice() {
|
43
|
+
return this.settings.voice;
|
44
|
+
}
|
45
|
+
get modelName() {
|
46
|
+
return this.settings.model;
|
47
|
+
}
|
48
|
+
async callAPI(text, options) {
|
49
|
+
return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
|
50
|
+
retry: this.settings.api?.retry,
|
51
|
+
throttle: this.settings.api?.throttle,
|
52
|
+
call: async () => callOpenAITextToSpeechAPI({
|
53
|
+
...this.settings,
|
54
|
+
abortSignal: options?.run?.abortSignal,
|
55
|
+
text,
|
56
|
+
}),
|
57
|
+
});
|
58
|
+
}
|
59
|
+
get settingsForEvent() {
|
60
|
+
return {
|
61
|
+
voice: this.settings.voice,
|
62
|
+
speed: this.settings.speed,
|
63
|
+
model: this.settings.model,
|
64
|
+
responseFormat: this.settings.responseFormat,
|
65
|
+
};
|
66
|
+
}
|
67
|
+
doGenerateSpeechStandard(text, options) {
|
68
|
+
return this.callAPI(text, options);
|
69
|
+
}
|
70
|
+
withSettings(additionalSettings) {
|
71
|
+
return new OpenAISpeechModel({
|
72
|
+
...this.settings,
|
73
|
+
...additionalSettings,
|
74
|
+
});
|
75
|
+
}
|
76
|
+
}
|
77
|
+
exports.OpenAISpeechModel = OpenAISpeechModel;
|
78
|
+
async function callOpenAITextToSpeechAPI({ api = new OpenAIApiConfiguration_js_1.OpenAIApiConfiguration(), abortSignal, text, voice, model, speed, responseFormat, }) {
|
79
|
+
return (0, postToApi_js_1.postJsonToApi)({
|
80
|
+
url: api.assembleUrl(`/audio/speech`),
|
81
|
+
headers: api.headers,
|
82
|
+
body: {
|
83
|
+
input: text,
|
84
|
+
voice,
|
85
|
+
speed,
|
86
|
+
model,
|
87
|
+
response_format: responseFormat,
|
88
|
+
},
|
89
|
+
failedResponseHandler: OpenAIError_js_1.failedOpenAICallResponseHandler,
|
90
|
+
successfulResponseHandler: (0, postToApi_js_1.createAudioMpegResponseHandler)(),
|
91
|
+
abortSignal,
|
92
|
+
});
|
93
|
+
}
|
@@ -0,0 +1,52 @@
|
|
1
|
+
/// <reference types="node" />
|
2
|
+
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
3
|
+
import { ApiConfiguration } from "../../core/api/ApiConfiguration.js";
|
4
|
+
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
5
|
+
import { SpeechGenerationModel, SpeechGenerationModelSettings } from "../../model-function/generate-speech/SpeechGenerationModel.js";
|
6
|
+
/**
|
7
|
+
* @see https://openai.com/pricing
|
8
|
+
*/
|
9
|
+
export declare const OPENAI_SPEECH_MODELS: {
|
10
|
+
"tts-1": {
|
11
|
+
costInMillicentsPerCharacter: number;
|
12
|
+
};
|
13
|
+
"tts-1-hd": {
|
14
|
+
costInMillicentsPerCharacter: number;
|
15
|
+
};
|
16
|
+
};
|
17
|
+
export type OpenAISpeechModelType = keyof typeof OPENAI_SPEECH_MODELS;
|
18
|
+
export declare const calculateOpenAISpeechCostInMillicents: ({ model, input, }: {
|
19
|
+
model: OpenAISpeechModelType;
|
20
|
+
input: string;
|
21
|
+
}) => number | null;
|
22
|
+
export type OpenAISpeechVoice = "alloy" | "echo" | "fable" | "onyx" | "nova" | "shimmer";
|
23
|
+
type OpenAISpeechModelResponseFormat = "mp3" | "opus" | "aac" | "flac";
|
24
|
+
export interface OpenAISpeechModelSettings extends SpeechGenerationModelSettings {
|
25
|
+
api?: ApiConfiguration;
|
26
|
+
voice: OpenAISpeechVoice;
|
27
|
+
model: OpenAISpeechModelType;
|
28
|
+
/**
|
29
|
+
* The speed of the generated audio. Select a value from 0.25 to 4.0. 1.0 is the default.
|
30
|
+
*/
|
31
|
+
speed?: number;
|
32
|
+
/**
|
33
|
+
* Defaults to mp3.
|
34
|
+
*/
|
35
|
+
responseFormat?: OpenAISpeechModelResponseFormat;
|
36
|
+
}
|
37
|
+
/**
|
38
|
+
* Synthesize speech using the OpenAI API.
|
39
|
+
*
|
40
|
+
* @see https://platform.openai.com/docs/api-reference/audio/createSpeech
|
41
|
+
*/
|
42
|
+
export declare class OpenAISpeechModel extends AbstractModel<OpenAISpeechModelSettings> implements SpeechGenerationModel<OpenAISpeechModelSettings> {
|
43
|
+
constructor(settings: OpenAISpeechModelSettings);
|
44
|
+
readonly provider: "openai";
|
45
|
+
get voice(): OpenAISpeechVoice;
|
46
|
+
get modelName(): "tts-1" | "tts-1-hd";
|
47
|
+
private callAPI;
|
48
|
+
get settingsForEvent(): Partial<OpenAISpeechModelSettings>;
|
49
|
+
doGenerateSpeechStandard(text: string, options?: FunctionOptions): Promise<Buffer>;
|
50
|
+
withSettings(additionalSettings: Partial<OpenAISpeechModelSettings>): this;
|
51
|
+
}
|
52
|
+
export {};
|
@@ -0,0 +1,88 @@
|
|
1
|
+
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
2
|
+
import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottle.js";
|
3
|
+
import { createAudioMpegResponseHandler, postJsonToApi, } from "../../core/api/postToApi.js";
|
4
|
+
import { OpenAIApiConfiguration } from "./OpenAIApiConfiguration.js";
|
5
|
+
import { failedOpenAICallResponseHandler } from "./OpenAIError.js";
|
6
|
+
/**
|
7
|
+
* @see https://openai.com/pricing
|
8
|
+
*/
|
9
|
+
export const OPENAI_SPEECH_MODELS = {
|
10
|
+
"tts-1": {
|
11
|
+
costInMillicentsPerCharacter: 1.5, // = 1500 / 1000,
|
12
|
+
},
|
13
|
+
"tts-1-hd": {
|
14
|
+
costInMillicentsPerCharacter: 3, // = 3000 / 1000
|
15
|
+
},
|
16
|
+
};
|
17
|
+
export const calculateOpenAISpeechCostInMillicents = ({ model, input, }) => {
|
18
|
+
if (!OPENAI_SPEECH_MODELS[model]) {
|
19
|
+
return null;
|
20
|
+
}
|
21
|
+
return (input.length * OPENAI_SPEECH_MODELS[model].costInMillicentsPerCharacter);
|
22
|
+
};
|
23
|
+
/**
|
24
|
+
* Synthesize speech using the OpenAI API.
|
25
|
+
*
|
26
|
+
* @see https://platform.openai.com/docs/api-reference/audio/createSpeech
|
27
|
+
*/
|
28
|
+
export class OpenAISpeechModel extends AbstractModel {
|
29
|
+
constructor(settings) {
|
30
|
+
super({ settings });
|
31
|
+
Object.defineProperty(this, "provider", {
|
32
|
+
enumerable: true,
|
33
|
+
configurable: true,
|
34
|
+
writable: true,
|
35
|
+
value: "openai"
|
36
|
+
});
|
37
|
+
}
|
38
|
+
get voice() {
|
39
|
+
return this.settings.voice;
|
40
|
+
}
|
41
|
+
get modelName() {
|
42
|
+
return this.settings.model;
|
43
|
+
}
|
44
|
+
async callAPI(text, options) {
|
45
|
+
return callWithRetryAndThrottle({
|
46
|
+
retry: this.settings.api?.retry,
|
47
|
+
throttle: this.settings.api?.throttle,
|
48
|
+
call: async () => callOpenAITextToSpeechAPI({
|
49
|
+
...this.settings,
|
50
|
+
abortSignal: options?.run?.abortSignal,
|
51
|
+
text,
|
52
|
+
}),
|
53
|
+
});
|
54
|
+
}
|
55
|
+
get settingsForEvent() {
|
56
|
+
return {
|
57
|
+
voice: this.settings.voice,
|
58
|
+
speed: this.settings.speed,
|
59
|
+
model: this.settings.model,
|
60
|
+
responseFormat: this.settings.responseFormat,
|
61
|
+
};
|
62
|
+
}
|
63
|
+
doGenerateSpeechStandard(text, options) {
|
64
|
+
return this.callAPI(text, options);
|
65
|
+
}
|
66
|
+
withSettings(additionalSettings) {
|
67
|
+
return new OpenAISpeechModel({
|
68
|
+
...this.settings,
|
69
|
+
...additionalSettings,
|
70
|
+
});
|
71
|
+
}
|
72
|
+
}
|
73
|
+
async function callOpenAITextToSpeechAPI({ api = new OpenAIApiConfiguration(), abortSignal, text, voice, model, speed, responseFormat, }) {
|
74
|
+
return postJsonToApi({
|
75
|
+
url: api.assembleUrl(`/audio/speech`),
|
76
|
+
headers: api.headers,
|
77
|
+
body: {
|
78
|
+
input: text,
|
79
|
+
voice,
|
80
|
+
speed,
|
81
|
+
model,
|
82
|
+
response_format: responseFormat,
|
83
|
+
},
|
84
|
+
failedResponseHandler: failedOpenAICallResponseHandler,
|
85
|
+
successfulResponseHandler: createAudioMpegResponseHandler(),
|
86
|
+
abortSignal,
|
87
|
+
});
|
88
|
+
}
|
@@ -211,6 +211,7 @@ class OpenAIChatModel extends AbstractModel_js_1.AbstractModel {
|
|
211
211
|
// map to OpenAI API names:
|
212
212
|
stop: this.settings.stopSequences,
|
213
213
|
maxTokens: this.settings.maxCompletionTokens,
|
214
|
+
openAIResponseFormat: this.settings.responseFormat,
|
214
215
|
// other settings:
|
215
216
|
user: this.settings.isUserIdForwardingEnabled
|
216
217
|
? options.run?.userId
|
@@ -390,6 +391,7 @@ const openAIChatResponseSchema = zod_1.z.object({
|
|
390
391
|
object: zod_1.z.literal("chat.completion"),
|
391
392
|
created: zod_1.z.number(),
|
392
393
|
model: zod_1.z.string(),
|
394
|
+
system_fingerprint: zod_1.z.string(),
|
393
395
|
choices: zod_1.z.array(zod_1.z.object({
|
394
396
|
message: zod_1.z.object({
|
395
397
|
role: zod_1.z.literal("assistant"),
|
@@ -403,7 +405,13 @@ const openAIChatResponseSchema = zod_1.z.object({
|
|
403
405
|
}),
|
404
406
|
index: zod_1.z.number(),
|
405
407
|
logprobs: zod_1.z.nullable(zod_1.z.any()),
|
406
|
-
finish_reason: zod_1.z.
|
408
|
+
finish_reason: zod_1.z.enum([
|
409
|
+
"stop",
|
410
|
+
"length",
|
411
|
+
"tool_calls",
|
412
|
+
"content_filter",
|
413
|
+
"function_call",
|
414
|
+
]),
|
407
415
|
})),
|
408
416
|
usage: zod_1.z.object({
|
409
417
|
prompt_tokens: zod_1.z.number(),
|
@@ -411,7 +419,7 @@ const openAIChatResponseSchema = zod_1.z.object({
|
|
411
419
|
total_tokens: zod_1.z.number(),
|
412
420
|
}),
|
413
421
|
});
|
414
|
-
async function callOpenAIChatCompletionAPI({ api = new OpenAIApiConfiguration_js_1.OpenAIApiConfiguration(), abortSignal, responseFormat, model, messages, functions, functionCall, temperature, topP, n, stop, maxTokens, presencePenalty, frequencyPenalty, logitBias, user, }) {
|
422
|
+
async function callOpenAIChatCompletionAPI({ api = new OpenAIApiConfiguration_js_1.OpenAIApiConfiguration(), abortSignal, responseFormat, model, messages, functions, functionCall, temperature, topP, n, stop, maxTokens, presencePenalty, frequencyPenalty, logitBias, user, openAIResponseFormat, seed, }) {
|
415
423
|
// empty arrays are not allowed for stop:
|
416
424
|
if (stop != null && Array.isArray(stop) && stop.length === 0) {
|
417
425
|
stop = undefined;
|
@@ -433,6 +441,8 @@ async function callOpenAIChatCompletionAPI({ api = new OpenAIApiConfiguration_js
|
|
433
441
|
presence_penalty: presencePenalty,
|
434
442
|
frequency_penalty: frequencyPenalty,
|
435
443
|
logit_bias: logitBias,
|
444
|
+
seed,
|
445
|
+
response_format: openAIResponseFormat,
|
436
446
|
user,
|
437
447
|
},
|
438
448
|
failedResponseHandler: OpenAIError_js_1.failedOpenAICallResponseHandler,
|
@@ -119,6 +119,10 @@ export interface OpenAIChatCallSettings {
|
|
119
119
|
maxTokens?: number;
|
120
120
|
temperature?: number;
|
121
121
|
topP?: number;
|
122
|
+
seed?: number | null;
|
123
|
+
responseFormat?: {
|
124
|
+
type?: "text" | "json_object";
|
125
|
+
};
|
122
126
|
n?: number;
|
123
127
|
presencePenalty?: number;
|
124
128
|
frequencyPenalty?: number;
|
@@ -175,12 +179,11 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
|
|
175
179
|
object: "chat.completion";
|
176
180
|
usage: {
|
177
181
|
prompt_tokens: number;
|
178
|
-
completion_tokens: number;
|
179
182
|
total_tokens: number;
|
183
|
+
completion_tokens: number;
|
180
184
|
};
|
181
185
|
model: string;
|
182
186
|
id: string;
|
183
|
-
created: number;
|
184
187
|
choices: {
|
185
188
|
message: {
|
186
189
|
content: string | null;
|
@@ -190,10 +193,12 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
|
|
190
193
|
arguments: string;
|
191
194
|
} | undefined;
|
192
195
|
};
|
193
|
-
finish_reason:
|
196
|
+
finish_reason: "length" | "stop" | "function_call" | "tool_calls" | "content_filter";
|
194
197
|
index: number;
|
195
198
|
logprobs?: any;
|
196
199
|
}[];
|
200
|
+
created: number;
|
201
|
+
system_fingerprint: string;
|
197
202
|
};
|
198
203
|
text: string;
|
199
204
|
usage: {
|
@@ -215,12 +220,11 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
|
|
215
220
|
object: "chat.completion";
|
216
221
|
usage: {
|
217
222
|
prompt_tokens: number;
|
218
|
-
completion_tokens: number;
|
219
223
|
total_tokens: number;
|
224
|
+
completion_tokens: number;
|
220
225
|
};
|
221
226
|
model: string;
|
222
227
|
id: string;
|
223
|
-
created: number;
|
224
228
|
choices: {
|
225
229
|
message: {
|
226
230
|
content: string | null;
|
@@ -230,10 +234,12 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
|
|
230
234
|
arguments: string;
|
231
235
|
} | undefined;
|
232
236
|
};
|
233
|
-
finish_reason:
|
237
|
+
finish_reason: "length" | "stop" | "function_call" | "tool_calls" | "content_filter";
|
234
238
|
index: number;
|
235
239
|
logprobs?: any;
|
236
240
|
}[];
|
241
|
+
created: number;
|
242
|
+
system_fingerprint: string;
|
237
243
|
};
|
238
244
|
valueText: string;
|
239
245
|
value: any;
|
@@ -249,12 +255,11 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
|
|
249
255
|
object: "chat.completion";
|
250
256
|
usage: {
|
251
257
|
prompt_tokens: number;
|
252
|
-
completion_tokens: number;
|
253
258
|
total_tokens: number;
|
259
|
+
completion_tokens: number;
|
254
260
|
};
|
255
261
|
model: string;
|
256
262
|
id: string;
|
257
|
-
created: number;
|
258
263
|
choices: {
|
259
264
|
message: {
|
260
265
|
content: string | null;
|
@@ -264,10 +269,12 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
|
|
264
269
|
arguments: string;
|
265
270
|
} | undefined;
|
266
271
|
};
|
267
|
-
finish_reason:
|
272
|
+
finish_reason: "length" | "stop" | "function_call" | "tool_calls" | "content_filter";
|
268
273
|
index: number;
|
269
274
|
logprobs?: any;
|
270
275
|
}[];
|
276
|
+
created: number;
|
277
|
+
system_fingerprint: string;
|
271
278
|
};
|
272
279
|
structureAndText: {
|
273
280
|
structure: null;
|
@@ -285,12 +292,11 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
|
|
285
292
|
object: "chat.completion";
|
286
293
|
usage: {
|
287
294
|
prompt_tokens: number;
|
288
|
-
completion_tokens: number;
|
289
295
|
total_tokens: number;
|
296
|
+
completion_tokens: number;
|
290
297
|
};
|
291
298
|
model: string;
|
292
299
|
id: string;
|
293
|
-
created: number;
|
294
300
|
choices: {
|
295
301
|
message: {
|
296
302
|
content: string | null;
|
@@ -300,10 +306,12 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
|
|
300
306
|
arguments: string;
|
301
307
|
} | undefined;
|
302
308
|
};
|
303
|
-
finish_reason:
|
309
|
+
finish_reason: "length" | "stop" | "function_call" | "tool_calls" | "content_filter";
|
304
310
|
index: number;
|
305
311
|
logprobs?: any;
|
306
312
|
}[];
|
313
|
+
created: number;
|
314
|
+
system_fingerprint: string;
|
307
315
|
};
|
308
316
|
structureAndText: {
|
309
317
|
structure: string;
|
@@ -338,6 +346,7 @@ declare const openAIChatResponseSchema: z.ZodObject<{
|
|
338
346
|
object: z.ZodLiteral<"chat.completion">;
|
339
347
|
created: z.ZodNumber;
|
340
348
|
model: z.ZodString;
|
349
|
+
system_fingerprint: z.ZodString;
|
341
350
|
choices: z.ZodArray<z.ZodObject<{
|
342
351
|
message: z.ZodObject<{
|
343
352
|
role: z.ZodLiteral<"assistant">;
|
@@ -369,7 +378,7 @@ declare const openAIChatResponseSchema: z.ZodObject<{
|
|
369
378
|
}>;
|
370
379
|
index: z.ZodNumber;
|
371
380
|
logprobs: z.ZodNullable<z.ZodAny>;
|
372
|
-
finish_reason: z.
|
381
|
+
finish_reason: z.ZodEnum<["stop", "length", "tool_calls", "content_filter", "function_call"]>;
|
373
382
|
}, "strip", z.ZodTypeAny, {
|
374
383
|
message: {
|
375
384
|
content: string | null;
|
@@ -379,7 +388,7 @@ declare const openAIChatResponseSchema: z.ZodObject<{
|
|
379
388
|
arguments: string;
|
380
389
|
} | undefined;
|
381
390
|
};
|
382
|
-
finish_reason:
|
391
|
+
finish_reason: "length" | "stop" | "function_call" | "tool_calls" | "content_filter";
|
383
392
|
index: number;
|
384
393
|
logprobs?: any;
|
385
394
|
}, {
|
@@ -391,7 +400,7 @@ declare const openAIChatResponseSchema: z.ZodObject<{
|
|
391
400
|
arguments: string;
|
392
401
|
} | undefined;
|
393
402
|
};
|
394
|
-
finish_reason:
|
403
|
+
finish_reason: "length" | "stop" | "function_call" | "tool_calls" | "content_filter";
|
395
404
|
index: number;
|
396
405
|
logprobs?: any;
|
397
406
|
}>, "many">;
|
@@ -401,23 +410,22 @@ declare const openAIChatResponseSchema: z.ZodObject<{
|
|
401
410
|
total_tokens: z.ZodNumber;
|
402
411
|
}, "strip", z.ZodTypeAny, {
|
403
412
|
prompt_tokens: number;
|
404
|
-
completion_tokens: number;
|
405
413
|
total_tokens: number;
|
414
|
+
completion_tokens: number;
|
406
415
|
}, {
|
407
416
|
prompt_tokens: number;
|
408
|
-
completion_tokens: number;
|
409
417
|
total_tokens: number;
|
418
|
+
completion_tokens: number;
|
410
419
|
}>;
|
411
420
|
}, "strip", z.ZodTypeAny, {
|
412
421
|
object: "chat.completion";
|
413
422
|
usage: {
|
414
423
|
prompt_tokens: number;
|
415
|
-
completion_tokens: number;
|
416
424
|
total_tokens: number;
|
425
|
+
completion_tokens: number;
|
417
426
|
};
|
418
427
|
model: string;
|
419
428
|
id: string;
|
420
|
-
created: number;
|
421
429
|
choices: {
|
422
430
|
message: {
|
423
431
|
content: string | null;
|
@@ -427,20 +435,21 @@ declare const openAIChatResponseSchema: z.ZodObject<{
|
|
427
435
|
arguments: string;
|
428
436
|
} | undefined;
|
429
437
|
};
|
430
|
-
finish_reason:
|
438
|
+
finish_reason: "length" | "stop" | "function_call" | "tool_calls" | "content_filter";
|
431
439
|
index: number;
|
432
440
|
logprobs?: any;
|
433
441
|
}[];
|
442
|
+
created: number;
|
443
|
+
system_fingerprint: string;
|
434
444
|
}, {
|
435
445
|
object: "chat.completion";
|
436
446
|
usage: {
|
437
447
|
prompt_tokens: number;
|
438
|
-
completion_tokens: number;
|
439
448
|
total_tokens: number;
|
449
|
+
completion_tokens: number;
|
440
450
|
};
|
441
451
|
model: string;
|
442
452
|
id: string;
|
443
|
-
created: number;
|
444
453
|
choices: {
|
445
454
|
message: {
|
446
455
|
content: string | null;
|
@@ -450,10 +459,12 @@ declare const openAIChatResponseSchema: z.ZodObject<{
|
|
450
459
|
arguments: string;
|
451
460
|
} | undefined;
|
452
461
|
};
|
453
|
-
finish_reason:
|
462
|
+
finish_reason: "length" | "stop" | "function_call" | "tool_calls" | "content_filter";
|
454
463
|
index: number;
|
455
464
|
logprobs?: any;
|
456
465
|
}[];
|
466
|
+
created: number;
|
467
|
+
system_fingerprint: string;
|
457
468
|
}>;
|
458
469
|
export type OpenAIChatResponse = z.infer<typeof openAIChatResponseSchema>;
|
459
470
|
export type OpenAIChatResponseFormatType<T> = {
|
@@ -470,12 +481,11 @@ export declare const OpenAIChatResponseFormat: {
|
|
470
481
|
object: "chat.completion";
|
471
482
|
usage: {
|
472
483
|
prompt_tokens: number;
|
473
|
-
completion_tokens: number;
|
474
484
|
total_tokens: number;
|
485
|
+
completion_tokens: number;
|
475
486
|
};
|
476
487
|
model: string;
|
477
488
|
id: string;
|
478
|
-
created: number;
|
479
489
|
choices: {
|
480
490
|
message: {
|
481
491
|
content: string | null;
|
@@ -485,10 +495,12 @@ export declare const OpenAIChatResponseFormat: {
|
|
485
495
|
arguments: string;
|
486
496
|
} | undefined;
|
487
497
|
};
|
488
|
-
finish_reason:
|
498
|
+
finish_reason: "length" | "stop" | "function_call" | "tool_calls" | "content_filter";
|
489
499
|
index: number;
|
490
500
|
logprobs?: any;
|
491
501
|
}[];
|
502
|
+
created: number;
|
503
|
+
system_fingerprint: string;
|
492
504
|
}>;
|
493
505
|
};
|
494
506
|
/**
|
@@ -202,6 +202,7 @@ export class OpenAIChatModel extends AbstractModel {
|
|
202
202
|
// map to OpenAI API names:
|
203
203
|
stop: this.settings.stopSequences,
|
204
204
|
maxTokens: this.settings.maxCompletionTokens,
|
205
|
+
openAIResponseFormat: this.settings.responseFormat,
|
205
206
|
// other settings:
|
206
207
|
user: this.settings.isUserIdForwardingEnabled
|
207
208
|
? options.run?.userId
|
@@ -380,6 +381,7 @@ const openAIChatResponseSchema = z.object({
|
|
380
381
|
object: z.literal("chat.completion"),
|
381
382
|
created: z.number(),
|
382
383
|
model: z.string(),
|
384
|
+
system_fingerprint: z.string(),
|
383
385
|
choices: z.array(z.object({
|
384
386
|
message: z.object({
|
385
387
|
role: z.literal("assistant"),
|
@@ -393,7 +395,13 @@ const openAIChatResponseSchema = z.object({
|
|
393
395
|
}),
|
394
396
|
index: z.number(),
|
395
397
|
logprobs: z.nullable(z.any()),
|
396
|
-
finish_reason: z.
|
398
|
+
finish_reason: z.enum([
|
399
|
+
"stop",
|
400
|
+
"length",
|
401
|
+
"tool_calls",
|
402
|
+
"content_filter",
|
403
|
+
"function_call",
|
404
|
+
]),
|
397
405
|
})),
|
398
406
|
usage: z.object({
|
399
407
|
prompt_tokens: z.number(),
|
@@ -401,7 +409,7 @@ const openAIChatResponseSchema = z.object({
|
|
401
409
|
total_tokens: z.number(),
|
402
410
|
}),
|
403
411
|
});
|
404
|
-
async function callOpenAIChatCompletionAPI({ api = new OpenAIApiConfiguration(), abortSignal, responseFormat, model, messages, functions, functionCall, temperature, topP, n, stop, maxTokens, presencePenalty, frequencyPenalty, logitBias, user, }) {
|
412
|
+
async function callOpenAIChatCompletionAPI({ api = new OpenAIApiConfiguration(), abortSignal, responseFormat, model, messages, functions, functionCall, temperature, topP, n, stop, maxTokens, presencePenalty, frequencyPenalty, logitBias, user, openAIResponseFormat, seed, }) {
|
405
413
|
// empty arrays are not allowed for stop:
|
406
414
|
if (stop != null && Array.isArray(stop) && stop.length === 0) {
|
407
415
|
stop = undefined;
|
@@ -423,6 +431,8 @@ async function callOpenAIChatCompletionAPI({ api = new OpenAIApiConfiguration(),
|
|
423
431
|
presence_penalty: presencePenalty,
|
424
432
|
frequency_penalty: frequencyPenalty,
|
425
433
|
logit_bias: logitBias,
|
434
|
+
seed,
|
435
|
+
response_format: openAIResponseFormat,
|
426
436
|
user,
|
427
437
|
},
|
428
438
|
failedResponseHandler: failedOpenAICallResponseHandler,
|
@@ -17,7 +17,16 @@ const chatResponseStreamEventSchema = zod_1.z.object({
|
|
17
17
|
})
|
18
18
|
.optional(),
|
19
19
|
}),
|
20
|
-
finish_reason: zod_1.z
|
20
|
+
finish_reason: zod_1.z
|
21
|
+
.enum([
|
22
|
+
"stop",
|
23
|
+
"length",
|
24
|
+
"tool_calls",
|
25
|
+
"content_filter",
|
26
|
+
"function_call",
|
27
|
+
])
|
28
|
+
.nullable()
|
29
|
+
.optional(),
|
21
30
|
index: zod_1.z.number(),
|
22
31
|
})),
|
23
32
|
created: zod_1.z.number(),
|
@@ -14,7 +14,16 @@ const chatResponseStreamEventSchema = z.object({
|
|
14
14
|
})
|
15
15
|
.optional(),
|
16
16
|
}),
|
17
|
-
finish_reason: z
|
17
|
+
finish_reason: z
|
18
|
+
.enum([
|
19
|
+
"stop",
|
20
|
+
"length",
|
21
|
+
"tool_calls",
|
22
|
+
"content_filter",
|
23
|
+
"function_call",
|
24
|
+
])
|
25
|
+
.nullable()
|
26
|
+
.optional(),
|
18
27
|
index: z.number(),
|
19
28
|
})),
|
20
29
|
created: z.number(),
|
@@ -17,12 +17,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
exports.OpenAIError = void 0;
|
18
18
|
__exportStar(require("./AzureOpenAIApiConfiguration.cjs"), exports);
|
19
19
|
__exportStar(require("./OpenAIApiConfiguration.cjs"), exports);
|
20
|
+
__exportStar(require("./OpenAICompletionModel.cjs"), exports);
|
20
21
|
__exportStar(require("./OpenAICostCalculator.cjs"), exports);
|
21
22
|
var OpenAIError_js_1 = require("./OpenAIError.cjs");
|
22
23
|
Object.defineProperty(exports, "OpenAIError", { enumerable: true, get: function () { return OpenAIError_js_1.OpenAIError; } });
|
23
24
|
__exportStar(require("./OpenAIImageGenerationModel.cjs"), exports);
|
25
|
+
__exportStar(require("./OpenAISpeechModel.cjs"), exports);
|
24
26
|
__exportStar(require("./OpenAITextEmbeddingModel.cjs"), exports);
|
25
|
-
__exportStar(require("./OpenAICompletionModel.cjs"), exports);
|
26
27
|
__exportStar(require("./OpenAITranscriptionModel.cjs"), exports);
|
27
28
|
__exportStar(require("./TikTokenTokenizer.cjs"), exports);
|
28
29
|
__exportStar(require("./chat/OpenAIChatMessage.cjs"), exports);
|
@@ -1,10 +1,11 @@
|
|
1
1
|
export * from "./AzureOpenAIApiConfiguration.js";
|
2
2
|
export * from "./OpenAIApiConfiguration.js";
|
3
|
+
export * from "./OpenAICompletionModel.js";
|
3
4
|
export * from "./OpenAICostCalculator.js";
|
4
5
|
export { OpenAIError, OpenAIErrorData } from "./OpenAIError.js";
|
5
6
|
export * from "./OpenAIImageGenerationModel.js";
|
7
|
+
export * from "./OpenAISpeechModel.js";
|
6
8
|
export * from "./OpenAITextEmbeddingModel.js";
|
7
|
-
export * from "./OpenAICompletionModel.js";
|
8
9
|
export * from "./OpenAITranscriptionModel.js";
|
9
10
|
export * from "./TikTokenTokenizer.js";
|
10
11
|
export * from "./chat/OpenAIChatMessage.js";
|
@@ -1,10 +1,11 @@
|
|
1
1
|
export * from "./AzureOpenAIApiConfiguration.js";
|
2
2
|
export * from "./OpenAIApiConfiguration.js";
|
3
|
+
export * from "./OpenAICompletionModel.js";
|
3
4
|
export * from "./OpenAICostCalculator.js";
|
4
5
|
export { OpenAIError } from "./OpenAIError.js";
|
5
6
|
export * from "./OpenAIImageGenerationModel.js";
|
7
|
+
export * from "./OpenAISpeechModel.js";
|
6
8
|
export * from "./OpenAITextEmbeddingModel.js";
|
7
|
-
export * from "./OpenAICompletionModel.js";
|
8
9
|
export * from "./OpenAITranscriptionModel.js";
|
9
10
|
export * from "./TikTokenTokenizer.js";
|
10
11
|
export * from "./chat/OpenAIChatMessage.js";
|
package/package.json
CHANGED