modelfusion 0.60.0 → 0.61.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 +7 -2
- package/model-provider/openai/OpenAIImageGenerationModel.cjs +58 -12
- package/model-provider/openai/OpenAIImageGenerationModel.d.ts +20 -5
- package/model-provider/openai/OpenAIImageGenerationModel.js +57 -11
- package/model-provider/openai/TikTokenTokenizer.cjs +3 -0
- package/model-provider/openai/TikTokenTokenizer.js +3 -0
- package/model-provider/openai/chat/OpenAIChatModel.cjs +17 -2
- package/model-provider/openai/chat/OpenAIChatModel.d.ts +15 -0
- package/model-provider/openai/chat/OpenAIChatModel.js +17 -2
- package/model-provider/openai/chat/OpenAIChatStreamIterable.cjs +1 -1
- package/model-provider/openai/chat/OpenAIChatStreamIterable.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
@@ -78,7 +78,10 @@ Generate an image from a prompt.
|
|
78
78
|
|
79
79
|
```ts
|
80
80
|
const image = await generateImage(
|
81
|
-
new OpenAIImageGenerationModel({
|
81
|
+
new OpenAIImageGenerationModel({
|
82
|
+
model: "dall-e-3",
|
83
|
+
size: "1024x1024",
|
84
|
+
}),
|
82
85
|
"the wicked witch of the west in the style of early 19th century painting"
|
83
86
|
);
|
84
87
|
```
|
@@ -460,7 +463,9 @@ const text = await generateText(
|
|
460
463
|
new LlamaCppTextGenerationModel({
|
461
464
|
contextWindowSize: 4096, // Llama 2 context window size
|
462
465
|
maxCompletionTokens: 1000,
|
463
|
-
})
|
466
|
+
})
|
467
|
+
.withTextPrompt()
|
468
|
+
.withPromptFormat(mapInstructionPromptToLlama2Format()),
|
464
469
|
{
|
465
470
|
system: "You are a story writer.",
|
466
471
|
instruction: "Write a short story about a robot learning to love.",
|
@@ -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
|
15
|
-
|
16
|
-
|
17
|
-
|
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
|
-
|
42
|
-
|
43
|
-
|
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): 4000 | 8000 | 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
|
-
|
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
|
12
|
-
|
13
|
-
|
14
|
-
|
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
|
-
|
38
|
-
|
39
|
-
|
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
|
}
|
@@ -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:
|
63
|
-
fineTunedCompletionTokenCostInMillicents:
|
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;
|
@@ -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:
|
57
|
-
fineTunedCompletionTokenCostInMillicents:
|
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(),
|
package/package.json
CHANGED