modelfusion 0.60.0 → 0.62.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 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.
@@ -78,7 +79,10 @@ Generate an image from a prompt.
78
79
 
79
80
  ```ts
80
81
  const image = await generateImage(
81
- new OpenAIImageGenerationModel({ size: "512x512" }),
82
+ new OpenAIImageGenerationModel({
83
+ model: "dall-e-3",
84
+ size: "1024x1024",
85
+ }),
82
86
  "the wicked witch of the west in the style of early 19th century painting"
83
87
  );
84
88
  ```
@@ -106,7 +110,7 @@ const speech = await generateSpeech(
106
110
  );
107
111
  ```
108
112
 
109
- 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)
110
114
 
111
115
  #### streamSpeech
112
116
 
@@ -460,7 +464,9 @@ const text = await generateText(
460
464
  new LlamaCppTextGenerationModel({
461
465
  contextWindowSize: 4096, // Llama 2 context window size
462
466
  maxCompletionTokens: 1000,
463
- }).withPromptFormat(mapInstructionPromptToLlama2Format()),
467
+ })
468
+ .withTextPrompt()
469
+ .withPromptFormat(mapInstructionPromptToLlama2Format()),
464
470
  {
465
471
  system: "You are a story writer.",
466
472
  instruction: "Write a short story about a robot learning to love.",
@@ -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
  /**
@@ -6,6 +6,7 @@ const OpenAITextEmbeddingModel_js_1 = require("./OpenAITextEmbeddingModel.cjs");
6
6
  const OpenAICompletionModel_js_1 = require("./OpenAICompletionModel.cjs");
7
7
  const OpenAITranscriptionModel_js_1 = require("./OpenAITranscriptionModel.cjs");
8
8
  const OpenAIChatModel_js_1 = require("./chat/OpenAIChatModel.cjs");
9
+ const OpenAISpeechModel_js_1 = require("./OpenAISpeechModel.cjs");
9
10
  class OpenAICostCalculator {
10
11
  constructor() {
11
12
  Object.defineProperty(this, "provider", {
@@ -68,6 +69,15 @@ class OpenAICostCalculator {
68
69
  .response,
69
70
  });
70
71
  }
72
+ case "generate-speech": {
73
+ if (model == null) {
74
+ return null;
75
+ }
76
+ return (0, OpenAISpeechModel_js_1.calculateOpenAISpeechCostInMillicents)({
77
+ model: model,
78
+ input: call.input,
79
+ });
80
+ }
71
81
  }
72
82
  return null;
73
83
  }
@@ -3,6 +3,7 @@ import { calculateOpenAIEmbeddingCostInMillicents, isOpenAIEmbeddingModel, } fro
3
3
  import { calculateOpenAICompletionCostInMillicents, isOpenAICompletionModel, } from "./OpenAICompletionModel.js";
4
4
  import { calculateOpenAITranscriptionCostInMillicents, } from "./OpenAITranscriptionModel.js";
5
5
  import { calculateOpenAIChatCostInMillicents, isOpenAIChatModel, } from "./chat/OpenAIChatModel.js";
6
+ import { calculateOpenAISpeechCostInMillicents, } from "./OpenAISpeechModel.js";
6
7
  export class OpenAICostCalculator {
7
8
  constructor() {
8
9
  Object.defineProperty(this, "provider", {
@@ -65,6 +66,15 @@ export class OpenAICostCalculator {
65
66
  .response,
66
67
  });
67
68
  }
69
+ case "generate-speech": {
70
+ if (model == null) {
71
+ return null;
72
+ }
73
+ return calculateOpenAISpeechCostInMillicents({
74
+ model: model,
75
+ input: call.input,
76
+ });
77
+ }
68
78
  }
69
79
  return null;
70
80
  }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.OpenAIImageGenerationResponseFormat = exports.OpenAIImageGenerationModel = exports.calculateOpenAIImageGenerationCostInMillicents = void 0;
3
+ exports.OpenAIImageGenerationResponseFormat = exports.OpenAIImageGenerationModel = exports.calculateOpenAIImageGenerationCostInMillicents = exports.OPENAI_IMAGE_MODELS = void 0;
4
4
  const zod_1 = require("zod");
5
5
  const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndThrottle.cjs");
6
6
  const postToApi_js_1 = require("../../core/api/postToApi.cjs");
@@ -8,15 +8,61 @@ const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
8
8
  const PromptFormatImageGenerationModel_js_1 = require("../../model-function/generate-image/PromptFormatImageGenerationModel.cjs");
9
9
  const OpenAIApiConfiguration_js_1 = require("./OpenAIApiConfiguration.cjs");
10
10
  const OpenAIError_js_1 = require("./OpenAIError.cjs");
11
+ exports.OPENAI_IMAGE_MODELS = {
12
+ "dall-e-2": {
13
+ getCost(settings) {
14
+ switch (settings.size ?? "1024x1024") {
15
+ case "1024x1024":
16
+ return 2000;
17
+ case "512x512":
18
+ return 1800;
19
+ case "256x256":
20
+ return 1600;
21
+ default:
22
+ return null;
23
+ }
24
+ },
25
+ },
26
+ "dall-e-3": {
27
+ getCost(settings) {
28
+ switch (settings.quality ?? "standard") {
29
+ case "standard": {
30
+ switch (settings.size ?? "1024x1024") {
31
+ case "1024x1024":
32
+ return 4000;
33
+ case "1024x1792":
34
+ case "1792x1024":
35
+ return 8000;
36
+ default:
37
+ return null;
38
+ }
39
+ }
40
+ case "hd": {
41
+ switch (settings.size ?? "1024x1024") {
42
+ case "1024x1024":
43
+ return 8000;
44
+ case "1024x1792":
45
+ case "1792x1024":
46
+ return 12000;
47
+ default:
48
+ return null;
49
+ }
50
+ }
51
+ }
52
+ },
53
+ },
54
+ };
11
55
  /**
12
56
  * @see https://openai.com/pricing
13
57
  */
14
- const sizeToCostInMillicents = {
15
- "1024x1024": 2000,
16
- "512x512": 1800,
17
- "256x256": 1600,
58
+ const calculateOpenAIImageGenerationCostInMillicents = ({ settings, }) => {
59
+ console.log(settings);
60
+ const cost = exports.OPENAI_IMAGE_MODELS[settings.model]?.getCost(settings);
61
+ if (cost == null) {
62
+ return null;
63
+ }
64
+ return (settings.n ?? 1) * cost;
18
65
  };
19
- const calculateOpenAIImageGenerationCostInMillicents = ({ settings, }) => (settings.n ?? 1) * sizeToCostInMillicents[settings.size ?? "1024x1024"];
20
66
  exports.calculateOpenAIImageGenerationCostInMillicents = calculateOpenAIImageGenerationCostInMillicents;
21
67
  /**
22
68
  * Create an image generation model that calls the OpenAI AI image creation API.
@@ -38,12 +84,9 @@ class OpenAIImageGenerationModel extends AbstractModel_js_1.AbstractModel {
38
84
  writable: true,
39
85
  value: "openai"
40
86
  });
41
- Object.defineProperty(this, "modelName", {
42
- enumerable: true,
43
- configurable: true,
44
- writable: true,
45
- value: null
46
- });
87
+ }
88
+ get modelName() {
89
+ return this.settings.model;
47
90
  }
48
91
  async callAPI(prompt, options) {
49
92
  const run = options?.run;
@@ -63,8 +106,11 @@ class OpenAIImageGenerationModel extends AbstractModel_js_1.AbstractModel {
63
106
  }
64
107
  get settingsForEvent() {
65
108
  const eventSettingProperties = [
109
+ "model",
66
110
  "n",
67
111
  "size",
112
+ "quality",
113
+ "style",
68
114
  ];
69
115
  return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
70
116
  }
@@ -6,13 +6,28 @@ import { AbstractModel } from "../../model-function/AbstractModel.js";
6
6
  import { PromptFormat } from "../../model-function/PromptFormat.js";
7
7
  import { ImageGenerationModel, ImageGenerationModelSettings } from "../../model-function/generate-image/ImageGenerationModel.js";
8
8
  import { PromptFormatImageGenerationModel } from "../../model-function/generate-image/PromptFormatImageGenerationModel.js";
9
+ export declare const OPENAI_IMAGE_MODELS: {
10
+ "dall-e-2": {
11
+ getCost(settings: OpenAIImageGenerationSettings): 2000 | 1800 | 1600 | null;
12
+ };
13
+ "dall-e-3": {
14
+ getCost(settings: OpenAIImageGenerationSettings): 8000 | 4000 | 12000 | null;
15
+ };
16
+ };
17
+ /**
18
+ * @see https://openai.com/pricing
19
+ */
20
+ export declare const calculateOpenAIImageGenerationCostInMillicents: ({ settings, }: {
21
+ settings: OpenAIImageGenerationSettings;
22
+ }) => number | null;
23
+ export type OpenAIImageModelType = keyof typeof OPENAI_IMAGE_MODELS;
9
24
  export interface OpenAIImageGenerationCallSettings {
25
+ model: OpenAIImageModelType;
10
26
  n?: number;
11
- size?: "256x256" | "512x512" | "1024x1024";
27
+ size?: "256x256" | "512x512" | "1024x1024" | "1792x1024" | "1024x1792";
28
+ quality?: "standard" | "hd";
29
+ style?: "vivid" | "natural";
12
30
  }
13
- export declare const calculateOpenAIImageGenerationCostInMillicents: ({ settings, }: {
14
- settings: OpenAIImageGenerationSettings;
15
- }) => number;
16
31
  export interface OpenAIImageGenerationSettings extends ImageGenerationModelSettings, OpenAIImageGenerationCallSettings {
17
32
  api?: ApiConfiguration;
18
33
  isUserIdForwardingEnabled?: boolean;
@@ -31,7 +46,7 @@ export interface OpenAIImageGenerationSettings extends ImageGenerationModelSetti
31
46
  export declare class OpenAIImageGenerationModel extends AbstractModel<OpenAIImageGenerationSettings> implements ImageGenerationModel<string, OpenAIImageGenerationSettings> {
32
47
  constructor(settings: OpenAIImageGenerationSettings);
33
48
  readonly provider: "openai";
34
- readonly modelName: null;
49
+ get modelName(): "dall-e-2" | "dall-e-3";
35
50
  callAPI<RESULT>(prompt: string, options: {
36
51
  responseFormat: OpenAIImageGenerationResponseFormatType<RESULT>;
37
52
  } & FunctionOptions): Promise<RESULT>;
@@ -5,15 +5,61 @@ import { AbstractModel } from "../../model-function/AbstractModel.js";
5
5
  import { PromptFormatImageGenerationModel } from "../../model-function/generate-image/PromptFormatImageGenerationModel.js";
6
6
  import { OpenAIApiConfiguration } from "./OpenAIApiConfiguration.js";
7
7
  import { failedOpenAICallResponseHandler } from "./OpenAIError.js";
8
+ export const OPENAI_IMAGE_MODELS = {
9
+ "dall-e-2": {
10
+ getCost(settings) {
11
+ switch (settings.size ?? "1024x1024") {
12
+ case "1024x1024":
13
+ return 2000;
14
+ case "512x512":
15
+ return 1800;
16
+ case "256x256":
17
+ return 1600;
18
+ default:
19
+ return null;
20
+ }
21
+ },
22
+ },
23
+ "dall-e-3": {
24
+ getCost(settings) {
25
+ switch (settings.quality ?? "standard") {
26
+ case "standard": {
27
+ switch (settings.size ?? "1024x1024") {
28
+ case "1024x1024":
29
+ return 4000;
30
+ case "1024x1792":
31
+ case "1792x1024":
32
+ return 8000;
33
+ default:
34
+ return null;
35
+ }
36
+ }
37
+ case "hd": {
38
+ switch (settings.size ?? "1024x1024") {
39
+ case "1024x1024":
40
+ return 8000;
41
+ case "1024x1792":
42
+ case "1792x1024":
43
+ return 12000;
44
+ default:
45
+ return null;
46
+ }
47
+ }
48
+ }
49
+ },
50
+ },
51
+ };
8
52
  /**
9
53
  * @see https://openai.com/pricing
10
54
  */
11
- const sizeToCostInMillicents = {
12
- "1024x1024": 2000,
13
- "512x512": 1800,
14
- "256x256": 1600,
55
+ export const calculateOpenAIImageGenerationCostInMillicents = ({ settings, }) => {
56
+ console.log(settings);
57
+ const cost = OPENAI_IMAGE_MODELS[settings.model]?.getCost(settings);
58
+ if (cost == null) {
59
+ return null;
60
+ }
61
+ return (settings.n ?? 1) * cost;
15
62
  };
16
- export const calculateOpenAIImageGenerationCostInMillicents = ({ settings, }) => (settings.n ?? 1) * sizeToCostInMillicents[settings.size ?? "1024x1024"];
17
63
  /**
18
64
  * Create an image generation model that calls the OpenAI AI image creation API.
19
65
  *
@@ -34,12 +80,9 @@ export class OpenAIImageGenerationModel extends AbstractModel {
34
80
  writable: true,
35
81
  value: "openai"
36
82
  });
37
- Object.defineProperty(this, "modelName", {
38
- enumerable: true,
39
- configurable: true,
40
- writable: true,
41
- value: null
42
- });
83
+ }
84
+ get modelName() {
85
+ return this.settings.model;
43
86
  }
44
87
  async callAPI(prompt, options) {
45
88
  const run = options?.run;
@@ -59,8 +102,11 @@ export class OpenAIImageGenerationModel extends AbstractModel {
59
102
  }
60
103
  get settingsForEvent() {
61
104
  const eventSettingProperties = [
105
+ "model",
62
106
  "n",
63
107
  "size",
108
+ "quality",
109
+ "style",
64
110
  ];
65
111
  return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
66
112
  }
@@ -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
+ }
@@ -75,12 +75,15 @@ function getTiktokenBPE(model) {
75
75
  case "gpt-3.5-turbo":
76
76
  case "gpt-3.5-turbo-0301":
77
77
  case "gpt-3.5-turbo-0613":
78
+ case "gpt-3.5-turbo-1106":
78
79
  case "gpt-3.5-turbo-16k":
79
80
  case "gpt-3.5-turbo-16k-0613":
80
81
  case "gpt-3.5-turbo-instruct":
81
82
  case "gpt-4":
82
83
  case "gpt-4-0314":
83
84
  case "gpt-4-0613":
85
+ case "gpt-4-1106-preview":
86
+ case "gpt-4-vision-preview":
84
87
  case "gpt-4-32k":
85
88
  case "gpt-4-32k-0314":
86
89
  case "gpt-4-32k-0613":
@@ -68,12 +68,15 @@ function getTiktokenBPE(model) {
68
68
  case "gpt-3.5-turbo":
69
69
  case "gpt-3.5-turbo-0301":
70
70
  case "gpt-3.5-turbo-0613":
71
+ case "gpt-3.5-turbo-1106":
71
72
  case "gpt-3.5-turbo-16k":
72
73
  case "gpt-3.5-turbo-16k-0613":
73
74
  case "gpt-3.5-turbo-instruct":
74
75
  case "gpt-4":
75
76
  case "gpt-4-0314":
76
77
  case "gpt-4-0613":
78
+ case "gpt-4-1106-preview":
79
+ case "gpt-4-vision-preview":
77
80
  case "gpt-4-32k":
78
81
  case "gpt-4-32k-0314":
79
82
  case "gpt-4-32k-0613":
@@ -40,6 +40,16 @@ exports.OPENAI_CHAT_MODELS = {
40
40
  promptTokenCostInMillicents: 3,
41
41
  completionTokenCostInMillicents: 6,
42
42
  },
43
+ "gpt-4-1106-preview": {
44
+ contextWindowSize: 128000,
45
+ promptTokenCostInMillicents: 1,
46
+ completionTokenCostInMillicents: 3,
47
+ },
48
+ "gpt-4-vision-preview": {
49
+ contextWindowSize: 128000,
50
+ promptTokenCostInMillicents: 1,
51
+ completionTokenCostInMillicents: 3,
52
+ },
43
53
  "gpt-4-32k": {
44
54
  contextWindowSize: 32768,
45
55
  promptTokenCostInMillicents: 6,
@@ -59,8 +69,13 @@ exports.OPENAI_CHAT_MODELS = {
59
69
  contextWindowSize: 4096,
60
70
  promptTokenCostInMillicents: 0.15,
61
71
  completionTokenCostInMillicents: 0.2,
62
- fineTunedPromptTokenCostInMillicents: 1.2,
63
- fineTunedCompletionTokenCostInMillicents: 1.6,
72
+ fineTunedPromptTokenCostInMillicents: 0.3,
73
+ fineTunedCompletionTokenCostInMillicents: 0.6,
74
+ },
75
+ "gpt-3.5-turbo-1106": {
76
+ contextWindowSize: 16385,
77
+ promptTokenCostInMillicents: 0.1,
78
+ completionTokenCostInMillicents: 0.2,
64
79
  },
65
80
  "gpt-3.5-turbo-0301": {
66
81
  contextWindowSize: 4096,
@@ -28,6 +28,16 @@ export declare const OPENAI_CHAT_MODELS: {
28
28
  promptTokenCostInMillicents: number;
29
29
  completionTokenCostInMillicents: number;
30
30
  };
31
+ "gpt-4-1106-preview": {
32
+ contextWindowSize: number;
33
+ promptTokenCostInMillicents: number;
34
+ completionTokenCostInMillicents: number;
35
+ };
36
+ "gpt-4-vision-preview": {
37
+ contextWindowSize: number;
38
+ promptTokenCostInMillicents: number;
39
+ completionTokenCostInMillicents: number;
40
+ };
31
41
  "gpt-4-32k": {
32
42
  contextWindowSize: number;
33
43
  promptTokenCostInMillicents: number;
@@ -50,6 +60,11 @@ export declare const OPENAI_CHAT_MODELS: {
50
60
  fineTunedPromptTokenCostInMillicents: number;
51
61
  fineTunedCompletionTokenCostInMillicents: number;
52
62
  };
63
+ "gpt-3.5-turbo-1106": {
64
+ contextWindowSize: number;
65
+ promptTokenCostInMillicents: number;
66
+ completionTokenCostInMillicents: number;
67
+ };
53
68
  "gpt-3.5-turbo-0301": {
54
69
  contextWindowSize: number;
55
70
  promptTokenCostInMillicents: number;
@@ -160,12 +175,11 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
160
175
  object: "chat.completion";
161
176
  usage: {
162
177
  prompt_tokens: number;
163
- completion_tokens: number;
164
178
  total_tokens: number;
179
+ completion_tokens: number;
165
180
  };
166
181
  model: string;
167
182
  id: string;
168
- created: number;
169
183
  choices: {
170
184
  message: {
171
185
  content: string | null;
@@ -179,6 +193,7 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
179
193
  index: number;
180
194
  logprobs?: any;
181
195
  }[];
196
+ created: number;
182
197
  };
183
198
  text: string;
184
199
  usage: {
@@ -200,12 +215,11 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
200
215
  object: "chat.completion";
201
216
  usage: {
202
217
  prompt_tokens: number;
203
- completion_tokens: number;
204
218
  total_tokens: number;
219
+ completion_tokens: number;
205
220
  };
206
221
  model: string;
207
222
  id: string;
208
- created: number;
209
223
  choices: {
210
224
  message: {
211
225
  content: string | null;
@@ -219,6 +233,7 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
219
233
  index: number;
220
234
  logprobs?: any;
221
235
  }[];
236
+ created: number;
222
237
  };
223
238
  valueText: string;
224
239
  value: any;
@@ -234,12 +249,11 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
234
249
  object: "chat.completion";
235
250
  usage: {
236
251
  prompt_tokens: number;
237
- completion_tokens: number;
238
252
  total_tokens: number;
253
+ completion_tokens: number;
239
254
  };
240
255
  model: string;
241
256
  id: string;
242
- created: number;
243
257
  choices: {
244
258
  message: {
245
259
  content: string | null;
@@ -253,6 +267,7 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
253
267
  index: number;
254
268
  logprobs?: any;
255
269
  }[];
270
+ created: number;
256
271
  };
257
272
  structureAndText: {
258
273
  structure: null;
@@ -270,12 +285,11 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
270
285
  object: "chat.completion";
271
286
  usage: {
272
287
  prompt_tokens: number;
273
- completion_tokens: number;
274
288
  total_tokens: number;
289
+ completion_tokens: number;
275
290
  };
276
291
  model: string;
277
292
  id: string;
278
- created: number;
279
293
  choices: {
280
294
  message: {
281
295
  content: string | null;
@@ -289,6 +303,7 @@ export declare class OpenAIChatModel extends AbstractModel<OpenAIChatSettings> i
289
303
  index: number;
290
304
  logprobs?: any;
291
305
  }[];
306
+ created: number;
292
307
  };
293
308
  structureAndText: {
294
309
  structure: string;
@@ -386,23 +401,22 @@ declare const openAIChatResponseSchema: z.ZodObject<{
386
401
  total_tokens: z.ZodNumber;
387
402
  }, "strip", z.ZodTypeAny, {
388
403
  prompt_tokens: number;
389
- completion_tokens: number;
390
404
  total_tokens: number;
405
+ completion_tokens: number;
391
406
  }, {
392
407
  prompt_tokens: number;
393
- completion_tokens: number;
394
408
  total_tokens: number;
409
+ completion_tokens: number;
395
410
  }>;
396
411
  }, "strip", z.ZodTypeAny, {
397
412
  object: "chat.completion";
398
413
  usage: {
399
414
  prompt_tokens: number;
400
- completion_tokens: number;
401
415
  total_tokens: number;
416
+ completion_tokens: number;
402
417
  };
403
418
  model: string;
404
419
  id: string;
405
- created: number;
406
420
  choices: {
407
421
  message: {
408
422
  content: string | null;
@@ -416,16 +430,16 @@ declare const openAIChatResponseSchema: z.ZodObject<{
416
430
  index: number;
417
431
  logprobs?: any;
418
432
  }[];
433
+ created: number;
419
434
  }, {
420
435
  object: "chat.completion";
421
436
  usage: {
422
437
  prompt_tokens: number;
423
- completion_tokens: number;
424
438
  total_tokens: number;
439
+ completion_tokens: number;
425
440
  };
426
441
  model: string;
427
442
  id: string;
428
- created: number;
429
443
  choices: {
430
444
  message: {
431
445
  content: string | null;
@@ -439,6 +453,7 @@ declare const openAIChatResponseSchema: z.ZodObject<{
439
453
  index: number;
440
454
  logprobs?: any;
441
455
  }[];
456
+ created: number;
442
457
  }>;
443
458
  export type OpenAIChatResponse = z.infer<typeof openAIChatResponseSchema>;
444
459
  export type OpenAIChatResponseFormatType<T> = {
@@ -455,12 +470,11 @@ export declare const OpenAIChatResponseFormat: {
455
470
  object: "chat.completion";
456
471
  usage: {
457
472
  prompt_tokens: number;
458
- completion_tokens: number;
459
473
  total_tokens: number;
474
+ completion_tokens: number;
460
475
  };
461
476
  model: string;
462
477
  id: string;
463
- created: number;
464
478
  choices: {
465
479
  message: {
466
480
  content: string | null;
@@ -474,6 +488,7 @@ export declare const OpenAIChatResponseFormat: {
474
488
  index: number;
475
489
  logprobs?: any;
476
490
  }[];
491
+ created: number;
477
492
  }>;
478
493
  };
479
494
  /**
@@ -34,6 +34,16 @@ export const OPENAI_CHAT_MODELS = {
34
34
  promptTokenCostInMillicents: 3,
35
35
  completionTokenCostInMillicents: 6,
36
36
  },
37
+ "gpt-4-1106-preview": {
38
+ contextWindowSize: 128000,
39
+ promptTokenCostInMillicents: 1,
40
+ completionTokenCostInMillicents: 3,
41
+ },
42
+ "gpt-4-vision-preview": {
43
+ contextWindowSize: 128000,
44
+ promptTokenCostInMillicents: 1,
45
+ completionTokenCostInMillicents: 3,
46
+ },
37
47
  "gpt-4-32k": {
38
48
  contextWindowSize: 32768,
39
49
  promptTokenCostInMillicents: 6,
@@ -53,8 +63,13 @@ export const OPENAI_CHAT_MODELS = {
53
63
  contextWindowSize: 4096,
54
64
  promptTokenCostInMillicents: 0.15,
55
65
  completionTokenCostInMillicents: 0.2,
56
- fineTunedPromptTokenCostInMillicents: 1.2,
57
- fineTunedCompletionTokenCostInMillicents: 1.6,
66
+ fineTunedPromptTokenCostInMillicents: 0.3,
67
+ fineTunedCompletionTokenCostInMillicents: 0.6,
68
+ },
69
+ "gpt-3.5-turbo-1106": {
70
+ contextWindowSize: 16385,
71
+ promptTokenCostInMillicents: 0.1,
72
+ completionTokenCostInMillicents: 0.2,
58
73
  },
59
74
  "gpt-3.5-turbo-0301": {
60
75
  contextWindowSize: 4096,
@@ -17,7 +17,7 @@ const chatResponseStreamEventSchema = zod_1.z.object({
17
17
  })
18
18
  .optional(),
19
19
  }),
20
- finish_reason: zod_1.z.enum(["stop", "length"]).nullable(),
20
+ finish_reason: zod_1.z.enum(["stop", "length"]).nullable().optional(),
21
21
  index: zod_1.z.number(),
22
22
  })),
23
23
  created: zod_1.z.number(),
@@ -14,7 +14,7 @@ const chatResponseStreamEventSchema = z.object({
14
14
  })
15
15
  .optional(),
16
16
  }),
17
- finish_reason: z.enum(["stop", "length"]).nullable(),
17
+ finish_reason: z.enum(["stop", "length"]).nullable().optional(),
18
18
  index: z.number(),
19
19
  })),
20
20
  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
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "modelfusion",
3
3
  "description": "Build multimodal applications, chatbots, and agents with JavaScript and TypeScript.",
4
- "version": "0.60.0",
4
+ "version": "0.62.0",
5
5
  "author": "Lars Grammel",
6
6
  "license": "MIT",
7
7
  "keywords": [