modelfusion 0.48.0 → 0.49.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 +30 -45
- package/model-function/AsyncIterableResultPromise.cjs +5 -5
- package/model-function/AsyncIterableResultPromise.d.ts +3 -3
- package/model-function/AsyncIterableResultPromise.js +5 -5
- package/model-function/Model.d.ts +1 -1
- package/model-function/ModelCallEvent.d.ts +5 -7
- package/model-function/embed/embed.cjs +3 -3
- package/model-function/embed/embed.js +3 -3
- package/model-function/{executeCall.cjs → executeStandardCall.cjs} +3 -3
- package/model-function/{executeCall.d.ts → executeStandardCall.d.ts} +1 -1
- package/model-function/{executeCall.js → executeStandardCall.js} +1 -1
- package/model-function/executeStreamCall.cjs +132 -0
- package/model-function/executeStreamCall.d.ts +20 -0
- package/model-function/executeStreamCall.js +128 -0
- package/model-function/generate-image/generateImage.cjs +2 -2
- package/model-function/generate-image/generateImage.js +2 -2
- package/model-function/generate-speech/SpeechGenerationEvent.d.ts +27 -0
- package/model-function/generate-speech/SpeechGenerationModel.d.ts +15 -0
- package/model-function/generate-speech/generateSpeech.cjs +24 -0
- package/model-function/generate-speech/generateSpeech.d.ts +8 -0
- package/model-function/generate-speech/generateSpeech.js +20 -0
- package/model-function/generate-speech/index.cjs +20 -0
- package/model-function/generate-speech/index.d.ts +4 -0
- package/model-function/generate-speech/index.js +4 -0
- package/model-function/generate-speech/streamSpeech.cjs +34 -0
- package/model-function/generate-speech/streamSpeech.d.ts +8 -0
- package/model-function/generate-speech/streamSpeech.js +30 -0
- package/model-function/generate-structure/generateStructure.cjs +2 -2
- package/model-function/generate-structure/generateStructure.js +2 -2
- package/model-function/generate-structure/generateStructureOrText.cjs +2 -2
- package/model-function/generate-structure/generateStructureOrText.js +2 -2
- package/model-function/generate-structure/index.cjs +27 -0
- package/model-function/generate-structure/index.d.ts +11 -0
- package/model-function/generate-structure/index.js +11 -0
- package/model-function/generate-structure/streamStructure.cjs +28 -136
- package/model-function/generate-structure/streamStructure.js +27 -135
- package/model-function/generate-text/TextGenerationEvent.d.ts +6 -0
- package/model-function/generate-text/generateText.cjs +3 -3
- package/model-function/generate-text/generateText.d.ts +1 -1
- package/model-function/generate-text/generateText.js +3 -3
- package/model-function/generate-text/index.cjs +0 -1
- package/model-function/generate-text/index.d.ts +0 -1
- package/model-function/generate-text/index.js +0 -1
- package/model-function/generate-text/streamText.cjs +21 -128
- package/model-function/generate-text/streamText.js +20 -127
- package/model-function/generate-text/trimChatPrompt.cjs +1 -1
- package/model-function/generate-text/trimChatPrompt.d.ts +1 -1
- package/model-function/generate-text/trimChatPrompt.js +1 -1
- package/model-function/{transcribe-speech/transcribe.cjs → generate-transcription/generateTranscription.cjs} +6 -6
- package/model-function/{transcribe-speech/transcribe.d.ts → generate-transcription/generateTranscription.d.ts} +2 -2
- package/model-function/{transcribe-speech/transcribe.js → generate-transcription/generateTranscription.js} +4 -4
- package/model-function/index.cjs +5 -20
- package/model-function/index.d.ts +5 -20
- package/model-function/index.js +5 -20
- package/model-provider/elevenlabs/{ElevenLabsSpeechSynthesisModel.cjs → ElevenLabsSpeechModel.cjs} +6 -6
- package/model-provider/elevenlabs/{ElevenLabsSpeechSynthesisModel.d.ts → ElevenLabsSpeechModel.d.ts} +8 -8
- package/model-provider/elevenlabs/{ElevenLabsSpeechSynthesisModel.js → ElevenLabsSpeechModel.js} +4 -4
- package/model-provider/elevenlabs/index.cjs +1 -1
- package/model-provider/elevenlabs/index.d.ts +1 -1
- package/model-provider/elevenlabs/index.js +1 -1
- package/model-provider/huggingface/HuggingFaceImageDescriptionModel.cjs +21 -2
- package/model-provider/huggingface/HuggingFaceImageDescriptionModel.d.ts +11 -6
- package/model-provider/huggingface/HuggingFaceImageDescriptionModel.js +21 -2
- package/model-provider/lmnt/{LmntSpeechSynthesisModel.cjs → LmntSpeechModel.cjs} +5 -5
- package/model-provider/lmnt/LmntSpeechModel.d.ts +26 -0
- package/model-provider/lmnt/{LmntSpeechSynthesisModel.js → LmntSpeechModel.js} +3 -3
- package/model-provider/lmnt/index.cjs +1 -1
- package/model-provider/lmnt/index.d.ts +1 -1
- package/model-provider/lmnt/index.js +1 -1
- package/model-provider/openai/{OpenAITextGenerationModel.cjs → OpenAICompletionModel.cjs} +17 -17
- package/model-provider/openai/{OpenAITextGenerationModel.d.ts → OpenAICompletionModel.d.ts} +25 -25
- package/model-provider/openai/{OpenAITextGenerationModel.js → OpenAICompletionModel.js} +12 -12
- package/model-provider/openai/OpenAICostCalculator.cjs +3 -3
- package/model-provider/openai/OpenAICostCalculator.js +3 -3
- package/model-provider/openai/OpenAITranscriptionModel.d.ts +1 -1
- package/model-provider/openai/TikTokenTokenizer.d.ts +2 -2
- package/model-provider/openai/index.cjs +1 -1
- package/model-provider/openai/index.d.ts +1 -1
- package/model-provider/openai/index.js +1 -1
- package/package.json +1 -1
- package/model-function/describe-image/ImageDescriptionEvent.d.ts +0 -18
- package/model-function/describe-image/ImageDescriptionModel.d.ts +0 -10
- package/model-function/describe-image/describeImage.cjs +0 -26
- package/model-function/describe-image/describeImage.d.ts +0 -9
- package/model-function/describe-image/describeImage.js +0 -22
- package/model-function/generate-text/TextStreamingEvent.cjs +0 -2
- package/model-function/generate-text/TextStreamingEvent.d.ts +0 -7
- package/model-function/generate-text/TextStreamingEvent.js +0 -1
- package/model-function/synthesize-speech/SpeechSynthesisEvent.cjs +0 -2
- package/model-function/synthesize-speech/SpeechSynthesisEvent.d.ts +0 -21
- package/model-function/synthesize-speech/SpeechSynthesisEvent.js +0 -1
- package/model-function/synthesize-speech/SpeechSynthesisModel.cjs +0 -2
- package/model-function/synthesize-speech/SpeechSynthesisModel.d.ts +0 -15
- package/model-function/synthesize-speech/SpeechSynthesisModel.js +0 -1
- package/model-function/synthesize-speech/synthesizeSpeech.cjs +0 -67
- package/model-function/synthesize-speech/synthesizeSpeech.d.ts +0 -14
- package/model-function/synthesize-speech/synthesizeSpeech.js +0 -63
- package/model-provider/lmnt/LmntSpeechSynthesisModel.d.ts +0 -26
- /package/model-function/{describe-image/ImageDescriptionEvent.cjs → generate-speech/SpeechGenerationEvent.cjs} +0 -0
- /package/model-function/{describe-image/ImageDescriptionEvent.js → generate-speech/SpeechGenerationEvent.js} +0 -0
- /package/model-function/{describe-image/ImageDescriptionModel.cjs → generate-speech/SpeechGenerationModel.cjs} +0 -0
- /package/model-function/{describe-image/ImageDescriptionModel.js → generate-speech/SpeechGenerationModel.js} +0 -0
- /package/model-function/{transcribe-speech → generate-transcription}/TranscriptionEvent.cjs +0 -0
- /package/model-function/{transcribe-speech → generate-transcription}/TranscriptionEvent.d.ts +0 -0
- /package/model-function/{transcribe-speech → generate-transcription}/TranscriptionEvent.js +0 -0
- /package/model-function/{transcribe-speech → generate-transcription}/TranscriptionModel.cjs +0 -0
- /package/model-function/{transcribe-speech → generate-transcription}/TranscriptionModel.d.ts +0 -0
- /package/model-function/{transcribe-speech → generate-transcription}/TranscriptionModel.js +0 -0
@@ -2,6 +2,7 @@ import { z } from "zod";
|
|
2
2
|
import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottle.js";
|
3
3
|
import { createJsonResponseHandler, postToApi, } from "../../core/api/postToApi.js";
|
4
4
|
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
5
|
+
import { PromptFormatTextGenerationModel } from "../../model-function/generate-text/PromptFormatTextGenerationModel.js";
|
5
6
|
import { HuggingFaceApiConfiguration } from "./HuggingFaceApiConfiguration.js";
|
6
7
|
import { failedHuggingFaceCallResponseHandler } from "./HuggingFaceError.js";
|
7
8
|
/**
|
@@ -18,6 +19,18 @@ export class HuggingFaceImageDescriptionModel extends AbstractModel {
|
|
18
19
|
writable: true,
|
19
20
|
value: "huggingface"
|
20
21
|
});
|
22
|
+
Object.defineProperty(this, "contextWindowSize", {
|
23
|
+
enumerable: true,
|
24
|
+
configurable: true,
|
25
|
+
writable: true,
|
26
|
+
value: undefined
|
27
|
+
});
|
28
|
+
Object.defineProperty(this, "tokenizer", {
|
29
|
+
enumerable: true,
|
30
|
+
configurable: true,
|
31
|
+
writable: true,
|
32
|
+
value: undefined
|
33
|
+
});
|
21
34
|
Object.defineProperty(this, "countPromptTokens", {
|
22
35
|
enumerable: true,
|
23
36
|
configurable: true,
|
@@ -42,13 +55,19 @@ export class HuggingFaceImageDescriptionModel extends AbstractModel {
|
|
42
55
|
get settingsForEvent() {
|
43
56
|
return {};
|
44
57
|
}
|
45
|
-
async
|
58
|
+
async doGenerateText(data, options) {
|
46
59
|
const response = await this.callAPI(data, options);
|
47
60
|
return {
|
48
61
|
response,
|
49
|
-
|
62
|
+
text: response[0].generated_text,
|
50
63
|
};
|
51
64
|
}
|
65
|
+
withPromptFormat(promptFormat) {
|
66
|
+
return new PromptFormatTextGenerationModel({
|
67
|
+
model: this,
|
68
|
+
promptFormat,
|
69
|
+
});
|
70
|
+
}
|
52
71
|
withSettings(additionalSettings) {
|
53
72
|
return new HuggingFaceImageDescriptionModel(Object.assign({}, this.settings, additionalSettings));
|
54
73
|
}
|
@@ -1,6 +1,6 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.
|
3
|
+
exports.LmntSpeechModel = void 0;
|
4
4
|
const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
|
5
5
|
const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndThrottle.cjs");
|
6
6
|
const postToApi_js_1 = require("../../core/api/postToApi.cjs");
|
@@ -11,7 +11,7 @@ const LmntError_js_1 = require("./LmntError.cjs");
|
|
11
11
|
*
|
12
12
|
* @see https://www.lmnt.com/docs/rest/#synthesize-speech
|
13
13
|
*/
|
14
|
-
class
|
14
|
+
class LmntSpeechModel extends AbstractModel_js_1.AbstractModel {
|
15
15
|
constructor(settings) {
|
16
16
|
super({ settings });
|
17
17
|
Object.defineProperty(this, "provider", {
|
@@ -43,17 +43,17 @@ class LmntSpeechSynthesisModel extends AbstractModel_js_1.AbstractModel {
|
|
43
43
|
length: this.settings.length,
|
44
44
|
};
|
45
45
|
}
|
46
|
-
|
46
|
+
doGenerateSpeechStandard(text, options) {
|
47
47
|
return this.callAPI(text, options);
|
48
48
|
}
|
49
49
|
withSettings(additionalSettings) {
|
50
|
-
return new
|
50
|
+
return new LmntSpeechModel({
|
51
51
|
...this.settings,
|
52
52
|
...additionalSettings,
|
53
53
|
});
|
54
54
|
}
|
55
55
|
}
|
56
|
-
exports.
|
56
|
+
exports.LmntSpeechModel = LmntSpeechModel;
|
57
57
|
async function callLmntTextToSpeechAPI({ api = new LmntApiConfiguration_js_1.LmntApiConfiguration(), abortSignal, text, voice, speed, seed, length, }) {
|
58
58
|
const formData = new FormData();
|
59
59
|
formData.append("text", text);
|
@@ -0,0 +1,26 @@
|
|
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
|
+
export interface LmntSpeechModelSettings extends SpeechGenerationModelSettings {
|
7
|
+
api?: ApiConfiguration;
|
8
|
+
voice: string;
|
9
|
+
speed?: number;
|
10
|
+
seed?: number;
|
11
|
+
length?: number;
|
12
|
+
}
|
13
|
+
/**
|
14
|
+
* Synthesize speech using the LMNT API.
|
15
|
+
*
|
16
|
+
* @see https://www.lmnt.com/docs/rest/#synthesize-speech
|
17
|
+
*/
|
18
|
+
export declare class LmntSpeechModel extends AbstractModel<LmntSpeechModelSettings> implements SpeechGenerationModel<LmntSpeechModelSettings> {
|
19
|
+
constructor(settings: LmntSpeechModelSettings);
|
20
|
+
readonly provider = "lmnt";
|
21
|
+
get modelName(): string;
|
22
|
+
private callAPI;
|
23
|
+
get settingsForEvent(): Partial<LmntSpeechModelSettings>;
|
24
|
+
doGenerateSpeechStandard(text: string, options?: FunctionOptions): Promise<Buffer>;
|
25
|
+
withSettings(additionalSettings: Partial<LmntSpeechModelSettings>): this;
|
26
|
+
}
|
@@ -8,7 +8,7 @@ import { failedLmntCallResponseHandler } from "./LmntError.js";
|
|
8
8
|
*
|
9
9
|
* @see https://www.lmnt.com/docs/rest/#synthesize-speech
|
10
10
|
*/
|
11
|
-
export class
|
11
|
+
export class LmntSpeechModel extends AbstractModel {
|
12
12
|
constructor(settings) {
|
13
13
|
super({ settings });
|
14
14
|
Object.defineProperty(this, "provider", {
|
@@ -40,11 +40,11 @@ export class LmntSpeechSynthesisModel extends AbstractModel {
|
|
40
40
|
length: this.settings.length,
|
41
41
|
};
|
42
42
|
}
|
43
|
-
|
43
|
+
doGenerateSpeechStandard(text, options) {
|
44
44
|
return this.callAPI(text, options);
|
45
45
|
}
|
46
46
|
withSettings(additionalSettings) {
|
47
|
-
return new
|
47
|
+
return new LmntSpeechModel({
|
48
48
|
...this.settings,
|
49
49
|
...additionalSettings,
|
50
50
|
});
|
@@ -15,4 +15,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
15
15
|
};
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
17
17
|
__exportStar(require("./LmntApiConfiguration.cjs"), exports);
|
18
|
-
__exportStar(require("./
|
18
|
+
__exportStar(require("./LmntSpeechModel.cjs"), exports);
|
@@ -1,2 +1,2 @@
|
|
1
1
|
export * from "./LmntApiConfiguration.js";
|
2
|
-
export * from "./
|
2
|
+
export * from "./LmntSpeechModel.js";
|
@@ -1,2 +1,2 @@
|
|
1
1
|
export * from "./LmntApiConfiguration.js";
|
2
|
-
export * from "./
|
2
|
+
export * from "./LmntSpeechModel.js";
|
@@ -1,6 +1,6 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.OpenAITextResponseFormat = exports.
|
3
|
+
exports.OpenAITextResponseFormat = exports.OpenAICompletionModel = exports.calculateOpenAICompletionCostInMillicents = exports.isOpenAICompletionModel = exports.getOpenAICompletionModelInformation = exports.OPENAI_TEXT_GENERATION_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");
|
@@ -87,7 +87,7 @@ exports.OPENAI_TEXT_GENERATION_MODELS = {
|
|
87
87
|
completionTokenCostInMillicents: 0.04,
|
88
88
|
},
|
89
89
|
};
|
90
|
-
function
|
90
|
+
function getOpenAICompletionModelInformation(model) {
|
91
91
|
// Model is already a base model:
|
92
92
|
if (model in exports.OPENAI_TEXT_GENERATION_MODELS) {
|
93
93
|
const baseModelInformation = exports.OPENAI_TEXT_GENERATION_MODELS[model];
|
@@ -114,26 +114,26 @@ function getOpenAITextGenerationModelInformation(model) {
|
|
114
114
|
}
|
115
115
|
throw new Error(`Unknown OpenAI chat base model ${baseModel}.`);
|
116
116
|
}
|
117
|
-
exports.
|
118
|
-
const
|
117
|
+
exports.getOpenAICompletionModelInformation = getOpenAICompletionModelInformation;
|
118
|
+
const isOpenAICompletionModel = (model) => model in exports.OPENAI_TEXT_GENERATION_MODELS ||
|
119
119
|
model.startsWith("ft:davinci-002:") ||
|
120
120
|
model.startsWith("ft:babbage-002:");
|
121
|
-
exports.
|
122
|
-
const
|
123
|
-
const modelInformation =
|
121
|
+
exports.isOpenAICompletionModel = isOpenAICompletionModel;
|
122
|
+
const calculateOpenAICompletionCostInMillicents = ({ model, response, }) => {
|
123
|
+
const modelInformation = getOpenAICompletionModelInformation(model);
|
124
124
|
return (response.usage.prompt_tokens *
|
125
125
|
modelInformation.promptTokenCostInMillicents +
|
126
126
|
response.usage.completion_tokens *
|
127
127
|
modelInformation.completionTokenCostInMillicents);
|
128
128
|
};
|
129
|
-
exports.
|
129
|
+
exports.calculateOpenAICompletionCostInMillicents = calculateOpenAICompletionCostInMillicents;
|
130
130
|
/**
|
131
131
|
* Create a text generation model that calls the OpenAI text completion API.
|
132
132
|
*
|
133
133
|
* @see https://platform.openai.com/docs/api-reference/completions/create
|
134
134
|
*
|
135
135
|
* @example
|
136
|
-
* const model = new
|
136
|
+
* const model = new OpenAICompletionModel({
|
137
137
|
* model: "gpt-3.5-turbo-instruct",
|
138
138
|
* temperature: 0.7,
|
139
139
|
* maxCompletionTokens: 500,
|
@@ -145,7 +145,7 @@ exports.calculateOpenAITextGenerationCostInMillicents = calculateOpenAITextGener
|
|
145
145
|
* "Write a short story about a robot learning to love:\n\n"
|
146
146
|
* );
|
147
147
|
*/
|
148
|
-
class
|
148
|
+
class OpenAICompletionModel extends AbstractModel_js_1.AbstractModel {
|
149
149
|
constructor(settings) {
|
150
150
|
super({ settings });
|
151
151
|
Object.defineProperty(this, "provider", {
|
@@ -166,7 +166,7 @@ class OpenAITextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
166
166
|
writable: true,
|
167
167
|
value: void 0
|
168
168
|
});
|
169
|
-
const modelInformation =
|
169
|
+
const modelInformation = getOpenAICompletionModelInformation(this.settings.model);
|
170
170
|
this.tokenizer = new TikTokenTokenizer_js_1.TikTokenTokenizer({
|
171
171
|
model: modelInformation.baseModel,
|
172
172
|
});
|
@@ -195,7 +195,7 @@ class OpenAITextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
195
195
|
return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
|
196
196
|
retry: callSettings.api?.retry,
|
197
197
|
throttle: callSettings.api?.throttle,
|
198
|
-
call: async () =>
|
198
|
+
call: async () => callOpenAICompletionAPI(callSettings),
|
199
199
|
});
|
200
200
|
}
|
201
201
|
get settingsForEvent() {
|
@@ -260,11 +260,11 @@ class OpenAITextGenerationModel extends AbstractModel_js_1.AbstractModel {
|
|
260
260
|
});
|
261
261
|
}
|
262
262
|
withSettings(additionalSettings) {
|
263
|
-
return new
|
263
|
+
return new OpenAICompletionModel(Object.assign({}, this.settings, additionalSettings));
|
264
264
|
}
|
265
265
|
}
|
266
|
-
exports.
|
267
|
-
const
|
266
|
+
exports.OpenAICompletionModel = OpenAICompletionModel;
|
267
|
+
const OpenAICompletionResponseSchema = zod_1.z.object({
|
268
268
|
id: zod_1.z.string(),
|
269
269
|
object: zod_1.z.literal("text_completion"),
|
270
270
|
created: zod_1.z.number(),
|
@@ -281,7 +281,7 @@ const openAITextGenerationResponseSchema = zod_1.z.object({
|
|
281
281
|
total_tokens: zod_1.z.number(),
|
282
282
|
}),
|
283
283
|
});
|
284
|
-
async function
|
284
|
+
async function callOpenAICompletionAPI({ api = new OpenAIApiConfiguration_js_1.OpenAIApiConfiguration(), abortSignal, responseFormat, model, prompt, suffix, maxTokens, temperature, topP, n, logprobs, echo, stop, presencePenalty, frequencyPenalty, bestOf, logitBias, user, }) {
|
285
285
|
// empty arrays are not allowed for stop:
|
286
286
|
if (stop != null && Array.isArray(stop) && stop.length === 0) {
|
287
287
|
stop = undefined;
|
@@ -318,7 +318,7 @@ exports.OpenAITextResponseFormat = {
|
|
318
318
|
*/
|
319
319
|
json: {
|
320
320
|
stream: false,
|
321
|
-
handler: (0, postToApi_js_1.createJsonResponseHandler)(
|
321
|
+
handler: (0, postToApi_js_1.createJsonResponseHandler)(OpenAICompletionResponseSchema),
|
322
322
|
},
|
323
323
|
/**
|
324
324
|
* Returns an async iterable over the full deltas (all choices, including full current state at time of event)
|
@@ -81,25 +81,25 @@ export declare const OPENAI_TEXT_GENERATION_MODELS: {
|
|
81
81
|
completionTokenCostInMillicents: number;
|
82
82
|
};
|
83
83
|
};
|
84
|
-
export declare function
|
85
|
-
baseModel:
|
84
|
+
export declare function getOpenAICompletionModelInformation(model: OpenAICompletionModelType): {
|
85
|
+
baseModel: OpenAICompletionBaseModelType;
|
86
86
|
isFineTuned: boolean;
|
87
87
|
contextWindowSize: number;
|
88
88
|
promptTokenCostInMillicents: number;
|
89
89
|
completionTokenCostInMillicents: number;
|
90
90
|
};
|
91
|
-
type
|
92
|
-
type
|
93
|
-
export type
|
94
|
-
export type
|
95
|
-
export declare const
|
96
|
-
export declare const
|
97
|
-
model:
|
98
|
-
response:
|
91
|
+
type FineTuneableOpenAICompletionModelType = "davinci-002" | "babbage-002";
|
92
|
+
type FineTunedOpenAICompletionModelType = `ft:${FineTuneableOpenAICompletionModelType}:${string}:${string}:${string}`;
|
93
|
+
export type OpenAICompletionBaseModelType = keyof typeof OPENAI_TEXT_GENERATION_MODELS;
|
94
|
+
export type OpenAICompletionModelType = OpenAICompletionBaseModelType | FineTunedOpenAICompletionModelType;
|
95
|
+
export declare const isOpenAICompletionModel: (model: string) => model is OpenAICompletionModelType;
|
96
|
+
export declare const calculateOpenAICompletionCostInMillicents: ({ model, response, }: {
|
97
|
+
model: OpenAICompletionModelType;
|
98
|
+
response: OpenAICompletionResponse;
|
99
99
|
}) => number;
|
100
|
-
export interface
|
100
|
+
export interface OpenAICompletionCallSettings {
|
101
101
|
api?: ApiConfiguration;
|
102
|
-
model:
|
102
|
+
model: OpenAICompletionModelType;
|
103
103
|
suffix?: string;
|
104
104
|
maxTokens?: number;
|
105
105
|
temperature?: number;
|
@@ -113,7 +113,7 @@ export interface OpenAITextGenerationCallSettings {
|
|
113
113
|
bestOf?: number;
|
114
114
|
logitBias?: Record<number, number>;
|
115
115
|
}
|
116
|
-
export interface
|
116
|
+
export interface OpenAICompletionModelSettings extends TextGenerationModelSettings, Omit<OpenAICompletionCallSettings, "stop" | "maxTokens"> {
|
117
117
|
isUserIdForwardingEnabled?: boolean;
|
118
118
|
}
|
119
119
|
/**
|
@@ -122,7 +122,7 @@ export interface OpenAITextGenerationModelSettings extends TextGenerationModelSe
|
|
122
122
|
* @see https://platform.openai.com/docs/api-reference/completions/create
|
123
123
|
*
|
124
124
|
* @example
|
125
|
-
* const model = new
|
125
|
+
* const model = new OpenAICompletionModel({
|
126
126
|
* model: "gpt-3.5-turbo-instruct",
|
127
127
|
* temperature: 0.7,
|
128
128
|
* maxCompletionTokens: 500,
|
@@ -134,17 +134,17 @@ export interface OpenAITextGenerationModelSettings extends TextGenerationModelSe
|
|
134
134
|
* "Write a short story about a robot learning to love:\n\n"
|
135
135
|
* );
|
136
136
|
*/
|
137
|
-
export declare class
|
138
|
-
constructor(settings:
|
137
|
+
export declare class OpenAICompletionModel extends AbstractModel<OpenAICompletionModelSettings> implements TextStreamingModel<string, OpenAICompletionModelSettings> {
|
138
|
+
constructor(settings: OpenAICompletionModelSettings);
|
139
139
|
readonly provider: "openai";
|
140
|
-
get modelName():
|
140
|
+
get modelName(): OpenAICompletionModelType;
|
141
141
|
readonly contextWindowSize: number;
|
142
142
|
readonly tokenizer: TikTokenTokenizer;
|
143
143
|
countPromptTokens(input: string): Promise<number>;
|
144
144
|
callAPI<RESULT>(prompt: string, options: {
|
145
145
|
responseFormat: OpenAITextResponseFormatType<RESULT>;
|
146
146
|
} & FunctionOptions): Promise<RESULT>;
|
147
|
-
get settingsForEvent(): Partial<
|
147
|
+
get settingsForEvent(): Partial<OpenAICompletionModelSettings>;
|
148
148
|
doGenerateText(prompt: string, options?: FunctionOptions): Promise<{
|
149
149
|
response: {
|
150
150
|
object: "text_completion";
|
@@ -174,18 +174,18 @@ export declare class OpenAITextGenerationModel extends AbstractModel<OpenAITextG
|
|
174
174
|
/**
|
175
175
|
* Returns this model with an instruction prompt format.
|
176
176
|
*/
|
177
|
-
withInstructionPrompt(): PromptFormatTextStreamingModel<import("../../index.js").InstructionPrompt, string,
|
177
|
+
withInstructionPrompt(): PromptFormatTextStreamingModel<import("../../index.js").InstructionPrompt, string, OpenAICompletionModelSettings, this>;
|
178
178
|
/**
|
179
179
|
* Returns this model with a chat prompt format.
|
180
180
|
*/
|
181
181
|
withChatPrompt(options?: {
|
182
182
|
user?: string;
|
183
183
|
ai?: string;
|
184
|
-
}): PromptFormatTextStreamingModel<import("../../index.js").ChatPrompt, string,
|
185
|
-
withPromptFormat<INPUT_PROMPT>(promptFormat: TextGenerationPromptFormat<INPUT_PROMPT, string>): PromptFormatTextStreamingModel<INPUT_PROMPT, string,
|
186
|
-
withSettings(additionalSettings: Partial<
|
184
|
+
}): PromptFormatTextStreamingModel<import("../../index.js").ChatPrompt, string, OpenAICompletionModelSettings, this>;
|
185
|
+
withPromptFormat<INPUT_PROMPT>(promptFormat: TextGenerationPromptFormat<INPUT_PROMPT, string>): PromptFormatTextStreamingModel<INPUT_PROMPT, string, OpenAICompletionModelSettings, this>;
|
186
|
+
withSettings(additionalSettings: Partial<OpenAICompletionModelSettings>): this;
|
187
187
|
}
|
188
|
-
declare const
|
188
|
+
declare const OpenAICompletionResponseSchema: z.ZodObject<{
|
189
189
|
id: z.ZodString;
|
190
190
|
object: z.ZodLiteral<"text_completion">;
|
191
191
|
created: z.ZodNumber;
|
@@ -252,7 +252,7 @@ declare const openAITextGenerationResponseSchema: z.ZodObject<{
|
|
252
252
|
logprobs?: any;
|
253
253
|
}[];
|
254
254
|
}>;
|
255
|
-
export type
|
255
|
+
export type OpenAICompletionResponse = z.infer<typeof OpenAICompletionResponseSchema>;
|
256
256
|
export type OpenAITextResponseFormatType<T> = {
|
257
257
|
stream: boolean;
|
258
258
|
handler: ResponseHandler<T>;
|
@@ -292,7 +292,7 @@ export declare const OpenAITextResponseFormat: {
|
|
292
292
|
}) => Promise<AsyncIterable<Delta<string>>>;
|
293
293
|
};
|
294
294
|
};
|
295
|
-
export type
|
295
|
+
export type OpenAICompletionDelta = Array<{
|
296
296
|
content: string;
|
297
297
|
isComplete: boolean;
|
298
298
|
delta: string;
|
@@ -84,7 +84,7 @@ export const OPENAI_TEXT_GENERATION_MODELS = {
|
|
84
84
|
completionTokenCostInMillicents: 0.04,
|
85
85
|
},
|
86
86
|
};
|
87
|
-
export function
|
87
|
+
export function getOpenAICompletionModelInformation(model) {
|
88
88
|
// Model is already a base model:
|
89
89
|
if (model in OPENAI_TEXT_GENERATION_MODELS) {
|
90
90
|
const baseModelInformation = OPENAI_TEXT_GENERATION_MODELS[model];
|
@@ -111,11 +111,11 @@ export function getOpenAITextGenerationModelInformation(model) {
|
|
111
111
|
}
|
112
112
|
throw new Error(`Unknown OpenAI chat base model ${baseModel}.`);
|
113
113
|
}
|
114
|
-
export const
|
114
|
+
export const isOpenAICompletionModel = (model) => model in OPENAI_TEXT_GENERATION_MODELS ||
|
115
115
|
model.startsWith("ft:davinci-002:") ||
|
116
116
|
model.startsWith("ft:babbage-002:");
|
117
|
-
export const
|
118
|
-
const modelInformation =
|
117
|
+
export const calculateOpenAICompletionCostInMillicents = ({ model, response, }) => {
|
118
|
+
const modelInformation = getOpenAICompletionModelInformation(model);
|
119
119
|
return (response.usage.prompt_tokens *
|
120
120
|
modelInformation.promptTokenCostInMillicents +
|
121
121
|
response.usage.completion_tokens *
|
@@ -127,7 +127,7 @@ export const calculateOpenAITextGenerationCostInMillicents = ({ model, response,
|
|
127
127
|
* @see https://platform.openai.com/docs/api-reference/completions/create
|
128
128
|
*
|
129
129
|
* @example
|
130
|
-
* const model = new
|
130
|
+
* const model = new OpenAICompletionModel({
|
131
131
|
* model: "gpt-3.5-turbo-instruct",
|
132
132
|
* temperature: 0.7,
|
133
133
|
* maxCompletionTokens: 500,
|
@@ -139,7 +139,7 @@ export const calculateOpenAITextGenerationCostInMillicents = ({ model, response,
|
|
139
139
|
* "Write a short story about a robot learning to love:\n\n"
|
140
140
|
* );
|
141
141
|
*/
|
142
|
-
export class
|
142
|
+
export class OpenAICompletionModel extends AbstractModel {
|
143
143
|
constructor(settings) {
|
144
144
|
super({ settings });
|
145
145
|
Object.defineProperty(this, "provider", {
|
@@ -160,7 +160,7 @@ export class OpenAITextGenerationModel extends AbstractModel {
|
|
160
160
|
writable: true,
|
161
161
|
value: void 0
|
162
162
|
});
|
163
|
-
const modelInformation =
|
163
|
+
const modelInformation = getOpenAICompletionModelInformation(this.settings.model);
|
164
164
|
this.tokenizer = new TikTokenTokenizer({
|
165
165
|
model: modelInformation.baseModel,
|
166
166
|
});
|
@@ -189,7 +189,7 @@ export class OpenAITextGenerationModel extends AbstractModel {
|
|
189
189
|
return callWithRetryAndThrottle({
|
190
190
|
retry: callSettings.api?.retry,
|
191
191
|
throttle: callSettings.api?.throttle,
|
192
|
-
call: async () =>
|
192
|
+
call: async () => callOpenAICompletionAPI(callSettings),
|
193
193
|
});
|
194
194
|
}
|
195
195
|
get settingsForEvent() {
|
@@ -254,10 +254,10 @@ export class OpenAITextGenerationModel extends AbstractModel {
|
|
254
254
|
});
|
255
255
|
}
|
256
256
|
withSettings(additionalSettings) {
|
257
|
-
return new
|
257
|
+
return new OpenAICompletionModel(Object.assign({}, this.settings, additionalSettings));
|
258
258
|
}
|
259
259
|
}
|
260
|
-
const
|
260
|
+
const OpenAICompletionResponseSchema = z.object({
|
261
261
|
id: z.string(),
|
262
262
|
object: z.literal("text_completion"),
|
263
263
|
created: z.number(),
|
@@ -274,7 +274,7 @@ const openAITextGenerationResponseSchema = z.object({
|
|
274
274
|
total_tokens: z.number(),
|
275
275
|
}),
|
276
276
|
});
|
277
|
-
async function
|
277
|
+
async function callOpenAICompletionAPI({ api = new OpenAIApiConfiguration(), abortSignal, responseFormat, model, prompt, suffix, maxTokens, temperature, topP, n, logprobs, echo, stop, presencePenalty, frequencyPenalty, bestOf, logitBias, user, }) {
|
278
278
|
// empty arrays are not allowed for stop:
|
279
279
|
if (stop != null && Array.isArray(stop) && stop.length === 0) {
|
280
280
|
stop = undefined;
|
@@ -311,7 +311,7 @@ export const OpenAITextResponseFormat = {
|
|
311
311
|
*/
|
312
312
|
json: {
|
313
313
|
stream: false,
|
314
|
-
handler: createJsonResponseHandler(
|
314
|
+
handler: createJsonResponseHandler(OpenAICompletionResponseSchema),
|
315
315
|
},
|
316
316
|
/**
|
317
317
|
* Returns an async iterable over the full deltas (all choices, including full current state at time of event)
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.OpenAICostCalculator = void 0;
|
4
4
|
const OpenAIImageGenerationModel_js_1 = require("./OpenAIImageGenerationModel.cjs");
|
5
5
|
const OpenAITextEmbeddingModel_js_1 = require("./OpenAITextEmbeddingModel.cjs");
|
6
|
-
const
|
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
9
|
class OpenAICostCalculator {
|
@@ -50,8 +50,8 @@ class OpenAICostCalculator {
|
|
50
50
|
response: call.result.response,
|
51
51
|
});
|
52
52
|
}
|
53
|
-
if ((0,
|
54
|
-
return (0,
|
53
|
+
if ((0, OpenAICompletionModel_js_1.isOpenAICompletionModel)(model)) {
|
54
|
+
return (0, OpenAICompletionModel_js_1.calculateOpenAICompletionCostInMillicents)({
|
55
55
|
model,
|
56
56
|
response: call.result.response,
|
57
57
|
});
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { calculateOpenAIImageGenerationCostInMillicents, } from "./OpenAIImageGenerationModel.js";
|
2
2
|
import { calculateOpenAIEmbeddingCostInMillicents, isOpenAIEmbeddingModel, } from "./OpenAITextEmbeddingModel.js";
|
3
|
-
import {
|
3
|
+
import { calculateOpenAICompletionCostInMillicents, isOpenAICompletionModel, } from "./OpenAICompletionModel.js";
|
4
4
|
import { calculateOpenAITranscriptionCostInMillicents, } from "./OpenAITranscriptionModel.js";
|
5
5
|
import { calculateOpenAIChatCostInMillicents, isOpenAIChatModel, } from "./chat/OpenAIChatModel.js";
|
6
6
|
export class OpenAICostCalculator {
|
@@ -47,8 +47,8 @@ export class OpenAICostCalculator {
|
|
47
47
|
response: call.result.response,
|
48
48
|
});
|
49
49
|
}
|
50
|
-
if (
|
51
|
-
return
|
50
|
+
if (isOpenAICompletionModel(model)) {
|
51
|
+
return calculateOpenAICompletionCostInMillicents({
|
52
52
|
model,
|
53
53
|
response: call.result.response,
|
54
54
|
});
|
@@ -4,7 +4,7 @@ import { FunctionOptions } from "../../core/FunctionOptions.js";
|
|
4
4
|
import { ApiConfiguration } from "../../core/api/ApiConfiguration.js";
|
5
5
|
import { ResponseHandler } from "../../core/api/postToApi.js";
|
6
6
|
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
7
|
-
import { TranscriptionModel, TranscriptionModelSettings } from "../../model-function/
|
7
|
+
import { TranscriptionModel, TranscriptionModelSettings } from "../../model-function/generate-transcription/TranscriptionModel.js";
|
8
8
|
/**
|
9
9
|
* @see https://openai.com/pricing
|
10
10
|
*/
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { FullTokenizer } from "../../model-function/tokenize-text/Tokenizer.js";
|
2
2
|
import { OpenAITextEmbeddingModelType } from "./OpenAITextEmbeddingModel.js";
|
3
|
-
import {
|
3
|
+
import { OpenAICompletionBaseModelType } from "./OpenAICompletionModel.js";
|
4
4
|
import { OpenAIChatBaseModelType } from "./chat/OpenAIChatModel.js";
|
5
5
|
/**
|
6
6
|
* TikToken tokenizer for OpenAI language models.
|
@@ -22,7 +22,7 @@ export declare class TikTokenTokenizer implements FullTokenizer {
|
|
22
22
|
* Get a TikToken tokenizer for a specific model or encoding.
|
23
23
|
*/
|
24
24
|
constructor(options: {
|
25
|
-
model: OpenAIChatBaseModelType |
|
25
|
+
model: OpenAIChatBaseModelType | OpenAICompletionBaseModelType | OpenAITextEmbeddingModelType;
|
26
26
|
});
|
27
27
|
private readonly tiktoken;
|
28
28
|
tokenize(text: string): Promise<number[]>;
|
@@ -22,7 +22,7 @@ var OpenAIError_js_1 = require("./OpenAIError.cjs");
|
|
22
22
|
Object.defineProperty(exports, "OpenAIError", { enumerable: true, get: function () { return OpenAIError_js_1.OpenAIError; } });
|
23
23
|
__exportStar(require("./OpenAIImageGenerationModel.cjs"), exports);
|
24
24
|
__exportStar(require("./OpenAITextEmbeddingModel.cjs"), exports);
|
25
|
-
__exportStar(require("./
|
25
|
+
__exportStar(require("./OpenAICompletionModel.cjs"), exports);
|
26
26
|
__exportStar(require("./OpenAITranscriptionModel.cjs"), exports);
|
27
27
|
__exportStar(require("./TikTokenTokenizer.cjs"), exports);
|
28
28
|
__exportStar(require("./chat/OpenAIChatMessage.cjs"), exports);
|
@@ -4,7 +4,7 @@ export * from "./OpenAICostCalculator.js";
|
|
4
4
|
export { OpenAIError, OpenAIErrorData } from "./OpenAIError.js";
|
5
5
|
export * from "./OpenAIImageGenerationModel.js";
|
6
6
|
export * from "./OpenAITextEmbeddingModel.js";
|
7
|
-
export * from "./
|
7
|
+
export * from "./OpenAICompletionModel.js";
|
8
8
|
export * from "./OpenAITranscriptionModel.js";
|
9
9
|
export * from "./TikTokenTokenizer.js";
|
10
10
|
export * from "./chat/OpenAIChatMessage.js";
|
@@ -4,7 +4,7 @@ export * from "./OpenAICostCalculator.js";
|
|
4
4
|
export { OpenAIError } from "./OpenAIError.js";
|
5
5
|
export * from "./OpenAIImageGenerationModel.js";
|
6
6
|
export * from "./OpenAITextEmbeddingModel.js";
|
7
|
-
export * from "./
|
7
|
+
export * from "./OpenAICompletionModel.js";
|
8
8
|
export * from "./OpenAITranscriptionModel.js";
|
9
9
|
export * from "./TikTokenTokenizer.js";
|
10
10
|
export * from "./chat/OpenAIChatMessage.js";
|
package/package.json
CHANGED
@@ -1,18 +0,0 @@
|
|
1
|
-
import { BaseModelCallFinishedEvent, BaseModelCallStartedEvent } from "../ModelCallEvent.js";
|
2
|
-
export interface ImageDescriptionStartedEvent extends BaseModelCallStartedEvent {
|
3
|
-
functionType: "image-description";
|
4
|
-
}
|
5
|
-
export type ImageDescriptionFinishedEventResult = {
|
6
|
-
status: "success";
|
7
|
-
response: unknown;
|
8
|
-
value: string;
|
9
|
-
} | {
|
10
|
-
status: "error";
|
11
|
-
error: unknown;
|
12
|
-
} | {
|
13
|
-
status: "abort";
|
14
|
-
};
|
15
|
-
export interface ImageDescriptionFinishedEvent extends BaseModelCallFinishedEvent {
|
16
|
-
functionType: "image-description";
|
17
|
-
result: ImageDescriptionFinishedEventResult;
|
18
|
-
}
|
@@ -1,10 +0,0 @@
|
|
1
|
-
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
2
|
-
import { Model, ModelSettings } from "../Model.js";
|
3
|
-
export interface ImageDescriptionModelSettings extends ModelSettings {
|
4
|
-
}
|
5
|
-
export interface ImageDescriptionModel<DATA, SETTINGS extends ImageDescriptionModelSettings = ImageDescriptionModelSettings> extends Model<SETTINGS> {
|
6
|
-
doDescribeImage: (data: DATA, options?: FunctionOptions) => PromiseLike<{
|
7
|
-
response: unknown;
|
8
|
-
description: string;
|
9
|
-
}>;
|
10
|
-
}
|
@@ -1,26 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.describeImage = void 0;
|
4
|
-
const executeCall_js_1 = require("../executeCall.cjs");
|
5
|
-
const ModelFunctionPromise_js_1 = require("../ModelFunctionPromise.cjs");
|
6
|
-
/**
|
7
|
-
* Describe an image as text.
|
8
|
-
*
|
9
|
-
* Depending on the model, this can be used for image captioning, for describing the contents of an image, or for OCR.
|
10
|
-
*/
|
11
|
-
function describeImage(model, data, options) {
|
12
|
-
return new ModelFunctionPromise_js_1.ModelFunctionPromise((0, executeCall_js_1.executeCall)({
|
13
|
-
functionType: "image-description",
|
14
|
-
input: data,
|
15
|
-
model,
|
16
|
-
options,
|
17
|
-
generateResponse: async (options) => {
|
18
|
-
const result = await model.doDescribeImage(data, options);
|
19
|
-
return {
|
20
|
-
response: result.response,
|
21
|
-
extractedValue: result.description,
|
22
|
-
};
|
23
|
-
},
|
24
|
-
}));
|
25
|
-
}
|
26
|
-
exports.describeImage = describeImage;
|