modelfusion 0.40.1 → 0.41.1
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 +14 -7
- package/composed-function/summarize/summarizeRecursivelyWithTextGenerationAndTokenSplitting.d.ts +3 -3
- package/core/FunctionEvent.d.ts +1 -1
- package/model-function/AsyncIterableResultPromise.d.ts +1 -1
- package/model-function/Delta.d.ts +8 -0
- package/model-function/ModelCallEvent.d.ts +1 -1
- package/model-function/ModelCallMetadata.d.ts +13 -0
- package/model-function/describe-image/ImageDescriptionEvent.d.ts +1 -1
- package/model-function/describe-image/ImageDescriptionModel.d.ts +6 -4
- package/model-function/describe-image/describeImage.cjs +7 -2
- package/model-function/describe-image/describeImage.d.ts +2 -2
- package/model-function/describe-image/describeImage.js +7 -2
- package/model-function/embed/EmbeddingEvent.d.ts +1 -1
- package/model-function/embed/EmbeddingModel.d.ts +6 -4
- package/model-function/embed/embed.cjs +16 -11
- package/model-function/embed/embed.d.ts +3 -3
- package/model-function/embed/embed.js +16 -11
- package/model-function/executeCall.cjs +26 -30
- package/model-function/executeCall.d.ts +19 -28
- package/model-function/executeCall.js +26 -30
- package/model-function/generate-image/ImageGenerationEvent.d.ts +1 -1
- package/model-function/generate-image/ImageGenerationModel.d.ts +6 -4
- package/model-function/generate-image/generateImage.cjs +7 -2
- package/model-function/generate-image/generateImage.d.ts +2 -2
- package/model-function/generate-image/generateImage.js +7 -2
- package/model-function/generate-structure/StructureFromTextGenerationModel.cjs +6 -5
- package/model-function/generate-structure/StructureFromTextGenerationModel.d.ts +7 -5
- package/model-function/generate-structure/StructureFromTextGenerationModel.js +6 -5
- package/model-function/generate-structure/StructureGenerationEvent.d.ts +1 -1
- package/model-function/generate-structure/StructureGenerationModel.d.ts +15 -18
- package/model-function/generate-structure/StructureOrTextGenerationModel.d.ts +19 -17
- package/model-function/generate-structure/generateStructure.cjs +10 -8
- package/model-function/generate-structure/generateStructure.d.ts +2 -2
- package/model-function/generate-structure/generateStructure.js +10 -8
- package/model-function/generate-structure/generateStructureOrText.cjs +15 -8
- package/model-function/generate-structure/generateStructureOrText.d.ts +4 -4
- package/model-function/generate-structure/generateStructureOrText.js +15 -8
- package/model-function/generate-structure/streamStructure.cjs +4 -16
- package/model-function/generate-structure/streamStructure.d.ts +3 -7
- package/model-function/generate-structure/streamStructure.js +4 -16
- package/model-function/generate-text/TextGenerationEvent.d.ts +1 -1
- package/model-function/generate-text/TextGenerationModel.d.ts +18 -19
- package/model-function/generate-text/generateText.cjs +8 -9
- package/model-function/generate-text/generateText.d.ts +2 -2
- package/model-function/generate-text/generateText.js +8 -9
- package/model-function/generate-text/streamText.cjs +8 -21
- package/model-function/generate-text/streamText.d.ts +3 -7
- package/model-function/generate-text/streamText.js +8 -21
- package/model-function/index.cjs +2 -2
- package/model-function/index.d.ts +2 -2
- package/model-function/index.js +2 -2
- package/model-function/synthesize-speech/SpeechSynthesisEvent.d.ts +1 -1
- package/model-function/synthesize-speech/SpeechSynthesisModel.d.ts +3 -3
- package/model-function/synthesize-speech/synthesizeSpeech.cjs +7 -2
- package/model-function/synthesize-speech/synthesizeSpeech.d.ts +2 -2
- package/model-function/synthesize-speech/synthesizeSpeech.js +7 -2
- package/model-function/transcribe-speech/TranscriptionEvent.d.ts +1 -1
- package/model-function/transcribe-speech/TranscriptionModel.d.ts +6 -4
- package/model-function/transcribe-speech/transcribe.cjs +7 -2
- package/model-function/transcribe-speech/transcribe.d.ts +2 -2
- package/model-function/transcribe-speech/transcribe.js +7 -2
- package/model-provider/automatic1111/Automatic1111ImageGenerationModel.cjs +14 -18
- package/model-provider/automatic1111/Automatic1111ImageGenerationModel.d.ts +11 -9
- package/model-provider/automatic1111/Automatic1111ImageGenerationModel.js +14 -18
- package/model-provider/cohere/CohereTextEmbeddingModel.cjs +13 -16
- package/model-provider/cohere/CohereTextEmbeddingModel.d.ts +12 -10
- package/model-provider/cohere/CohereTextEmbeddingModel.js +13 -16
- package/model-provider/cohere/CohereTextGenerationModel.cjs +25 -28
- package/model-provider/cohere/CohereTextGenerationModel.d.ts +24 -22
- package/model-provider/cohere/CohereTextGenerationModel.js +25 -28
- package/model-provider/elevenlabs/ElevenLabsSpeechSynthesisModel.cjs +10 -17
- package/model-provider/elevenlabs/ElevenLabsSpeechSynthesisModel.d.ts +2 -2
- package/model-provider/elevenlabs/ElevenLabsSpeechSynthesisModel.js +10 -17
- package/model-provider/huggingface/HuggingFaceImageDescriptionModel.cjs +13 -16
- package/model-provider/huggingface/HuggingFaceImageDescriptionModel.d.ts +9 -7
- package/model-provider/huggingface/HuggingFaceImageDescriptionModel.js +13 -16
- package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.cjs +19 -25
- package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.d.ts +8 -6
- package/model-provider/huggingface/HuggingFaceTextEmbeddingModel.js +19 -25
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.cjs +18 -24
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.d.ts +10 -8
- package/model-provider/huggingface/HuggingFaceTextGenerationModel.js +18 -24
- package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.cjs +13 -16
- package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.d.ts +8 -6
- package/model-provider/llamacpp/LlamaCppTextEmbeddingModel.js +13 -16
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.cjs +27 -33
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.d.ts +62 -60
- package/model-provider/llamacpp/LlamaCppTextGenerationModel.js +27 -33
- package/model-provider/lmnt/LmntSpeechSynthesisModel.cjs +7 -12
- package/model-provider/lmnt/LmntSpeechSynthesisModel.d.ts +2 -2
- package/model-provider/lmnt/LmntSpeechSynthesisModel.js +7 -12
- package/model-provider/openai/OpenAIImageGenerationModel.cjs +8 -16
- package/model-provider/openai/OpenAIImageGenerationModel.d.ts +11 -11
- package/model-provider/openai/OpenAIImageGenerationModel.js +8 -16
- package/model-provider/openai/OpenAITextEmbeddingModel.cjs +18 -24
- package/model-provider/openai/OpenAITextEmbeddingModel.d.ts +18 -16
- package/model-provider/openai/OpenAITextEmbeddingModel.js +18 -24
- package/model-provider/openai/OpenAITextGenerationModel.cjs +19 -26
- package/model-provider/openai/OpenAITextGenerationModel.d.ts +31 -33
- package/model-provider/openai/OpenAITextGenerationModel.js +19 -26
- package/model-provider/openai/OpenAITranscriptionModel.cjs +19 -28
- package/model-provider/openai/OpenAITranscriptionModel.d.ts +27 -7
- package/model-provider/openai/OpenAITranscriptionModel.js +19 -28
- package/model-provider/openai/chat/OpenAIChatModel.cjs +76 -85
- package/model-provider/openai/chat/OpenAIChatModel.d.ts +127 -50
- package/model-provider/openai/chat/OpenAIChatModel.js +77 -86
- package/model-provider/openai/chat/OpenAIChatStreamIterable.cjs +4 -3
- package/model-provider/openai/chat/OpenAIChatStreamIterable.d.ts +2 -2
- package/model-provider/openai/chat/OpenAIChatStreamIterable.js +2 -1
- package/model-provider/stability/StabilityImageGenerationModel.cjs +16 -21
- package/model-provider/stability/StabilityImageGenerationModel.d.ts +13 -11
- package/model-provider/stability/StabilityImageGenerationModel.js +16 -21
- package/package.json +1 -1
- package/prompt/PromptFormatTextGenerationModel.cjs +2 -18
- package/prompt/PromptFormatTextGenerationModel.d.ts +14 -10
- package/prompt/PromptFormatTextGenerationModel.js +2 -18
- package/prompt/PromptFormatTextStreamingModel.cjs +31 -0
- package/prompt/PromptFormatTextStreamingModel.d.ts +13 -0
- package/prompt/PromptFormatTextStreamingModel.js +27 -0
- package/prompt/chat/trimChatPrompt.d.ts +2 -2
- package/prompt/index.cjs +1 -0
- package/prompt/index.d.ts +1 -0
- package/prompt/index.js +1 -0
- package/retriever/Retriever.d.ts +3 -6
- package/retriever/retrieve.cjs +2 -2
- package/retriever/retrieve.d.ts +3 -3
- package/retriever/retrieve.js +2 -2
- package/tool/executeTool.cjs +2 -2
- package/tool/executeTool.js +2 -2
- package/tool/useTool.cjs +2 -4
- package/tool/useTool.d.ts +2 -2
- package/tool/useTool.js +2 -4
- package/tool/useToolOrGenerateText.d.ts +2 -2
- package/util/SafeResult.d.ts +1 -1
- package/util/runSafe.cjs +1 -1
- package/util/runSafe.js +1 -1
- package/vector-index/VectorIndexRetriever.cjs +0 -7
- package/vector-index/VectorIndexRetriever.d.ts +5 -5
- package/vector-index/VectorIndexRetriever.js +0 -7
- package/vector-index/upsertIntoVectorIndex.d.ts +4 -4
- package/model-function/DeltaEvent.d.ts +0 -7
- package/model-function/ModelFunctionOptions.d.ts +0 -4
- /package/model-function/{DeltaEvent.cjs → Delta.cjs} +0 -0
- /package/model-function/{DeltaEvent.js → Delta.js} +0 -0
- /package/model-function/{ModelFunctionOptions.cjs → ModelCallMetadata.cjs} +0 -0
- /package/model-function/{ModelFunctionOptions.js → ModelCallMetadata.js} +0 -0
@@ -1,13 +1,12 @@
|
|
1
1
|
import z from "zod";
|
2
|
+
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
2
3
|
import { ApiConfiguration } from "../../core/api/ApiConfiguration.js";
|
3
4
|
import { ResponseHandler } from "../../core/api/postToApi.js";
|
4
5
|
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
5
|
-
import {
|
6
|
-
import {
|
7
|
-
import { TextGenerationModel, TextGenerationModelSettings } from "../../model-function/generate-text/TextGenerationModel.js";
|
6
|
+
import { Delta } from "../../model-function/Delta.js";
|
7
|
+
import { TextGenerationModelSettings, TextStreamingModel } from "../../model-function/generate-text/TextGenerationModel.js";
|
8
8
|
import { PromptFormat } from "../../prompt/PromptFormat.js";
|
9
|
-
import {
|
10
|
-
import { OpenAIImageGenerationCallSettings } from "./OpenAIImageGenerationModel.js";
|
9
|
+
import { PromptFormatTextStreamingModel } from "../../prompt/PromptFormatTextStreamingModel.js";
|
11
10
|
import { TikTokenTokenizer } from "./TikTokenTokenizer.js";
|
12
11
|
/**
|
13
12
|
* @see https://platform.openai.com/docs/models/
|
@@ -135,7 +134,7 @@ export interface OpenAITextGenerationModelSettings extends TextGenerationModelSe
|
|
135
134
|
* "Write a short story about a robot learning to love:\n\n"
|
136
135
|
* );
|
137
136
|
*/
|
138
|
-
export declare class OpenAITextGenerationModel extends AbstractModel<OpenAITextGenerationModelSettings> implements
|
137
|
+
export declare class OpenAITextGenerationModel extends AbstractModel<OpenAITextGenerationModelSettings> implements TextStreamingModel<string, OpenAITextGenerationModelSettings> {
|
139
138
|
constructor(settings: OpenAITextGenerationModelSettings);
|
140
139
|
readonly provider: "openai";
|
141
140
|
get modelName(): OpenAITextGenerationModelType;
|
@@ -144,36 +143,35 @@ export declare class OpenAITextGenerationModel extends AbstractModel<OpenAITextG
|
|
144
143
|
countPromptTokens(input: string): Promise<number>;
|
145
144
|
callAPI<RESULT>(prompt: string, options: {
|
146
145
|
responseFormat: OpenAITextResponseFormatType<RESULT>;
|
147
|
-
} &
|
148
|
-
user?: string;
|
149
|
-
}>>): Promise<RESULT>;
|
146
|
+
} & FunctionOptions): Promise<RESULT>;
|
150
147
|
get settingsForEvent(): Partial<OpenAITextGenerationModelSettings>;
|
151
|
-
|
152
|
-
|
153
|
-
|
148
|
+
doGenerateText(prompt: string, options?: FunctionOptions): Promise<{
|
149
|
+
response: {
|
150
|
+
object: "text_completion";
|
151
|
+
model: string;
|
152
|
+
usage: {
|
153
|
+
prompt_tokens: number;
|
154
|
+
completion_tokens: number;
|
155
|
+
total_tokens: number;
|
156
|
+
};
|
157
|
+
id: string;
|
158
|
+
created: number;
|
159
|
+
choices: {
|
160
|
+
text: string;
|
161
|
+
finish_reason: string;
|
162
|
+
index: number;
|
163
|
+
logprobs?: any;
|
164
|
+
}[];
|
165
|
+
};
|
166
|
+
text: string;
|
154
167
|
usage: {
|
155
|
-
|
156
|
-
|
157
|
-
|
168
|
+
promptTokens: number;
|
169
|
+
completionTokens: number;
|
170
|
+
totalTokens: number;
|
158
171
|
};
|
159
|
-
id: string;
|
160
|
-
created: number;
|
161
|
-
choices: {
|
162
|
-
text: string;
|
163
|
-
finish_reason: string;
|
164
|
-
index: number;
|
165
|
-
logprobs?: any;
|
166
|
-
}[];
|
167
172
|
}>;
|
168
|
-
|
169
|
-
|
170
|
-
extractTextDelta(fullDelta: OpenAITextGenerationDelta): string | undefined;
|
171
|
-
withPromptFormat<INPUT_PROMPT>(promptFormat: PromptFormat<INPUT_PROMPT, string>): PromptFormatTextGenerationModel<INPUT_PROMPT, string, OpenAITextGenerationResponse, OpenAITextGenerationDelta, OpenAITextGenerationModelSettings, this>;
|
172
|
-
extractUsage(response: OpenAITextGenerationResponse): {
|
173
|
-
promptTokens: number;
|
174
|
-
completionTokens: number;
|
175
|
-
totalTokens: number;
|
176
|
-
};
|
173
|
+
doStreamText(prompt: string, options?: FunctionOptions): Promise<AsyncIterable<Delta<string>>>;
|
174
|
+
withPromptFormat<INPUT_PROMPT>(promptFormat: PromptFormat<INPUT_PROMPT, string>): PromptFormatTextStreamingModel<INPUT_PROMPT, string, OpenAITextGenerationModelSettings, this>;
|
177
175
|
withSettings(additionalSettings: Partial<OpenAITextGenerationModelSettings>): this;
|
178
176
|
}
|
179
177
|
declare const openAITextGenerationResponseSchema: z.ZodObject<{
|
@@ -280,7 +278,7 @@ export declare const OpenAITextResponseFormat: {
|
|
280
278
|
stream: true;
|
281
279
|
handler: ({ response }: {
|
282
280
|
response: Response;
|
283
|
-
}) => Promise<AsyncIterable<
|
281
|
+
}) => Promise<AsyncIterable<Delta<string>>>;
|
284
282
|
};
|
285
283
|
};
|
286
284
|
export type OpenAITextGenerationDelta = Array<{
|
@@ -6,7 +6,7 @@ import { AsyncQueue } from "../../event-source/AsyncQueue.js";
|
|
6
6
|
import { parseEventSourceStream } from "../../event-source/parseEventSourceStream.js";
|
7
7
|
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
8
8
|
import { countTokens } from "../../model-function/tokenize-text/countTokens.js";
|
9
|
-
import {
|
9
|
+
import { PromptFormatTextStreamingModel } from "../../prompt/PromptFormatTextStreamingModel.js";
|
10
10
|
import { OpenAIApiConfiguration } from "./OpenAIApiConfiguration.js";
|
11
11
|
import { failedOpenAICallResponseHandler } from "./OpenAIError.js";
|
12
12
|
import { TikTokenTokenizer } from "./TikTokenTokenizer.js";
|
@@ -172,18 +172,14 @@ export class OpenAITextGenerationModel extends AbstractModel {
|
|
172
172
|
return countTokens(this.tokenizer, input);
|
173
173
|
}
|
174
174
|
async callAPI(prompt, options) {
|
175
|
-
const { run,
|
176
|
-
const combinedSettings = {
|
177
|
-
...this.settings,
|
178
|
-
...settings,
|
179
|
-
};
|
175
|
+
const { run, responseFormat } = options;
|
180
176
|
const callSettings = {
|
181
177
|
user: this.settings.isUserIdForwardingEnabled ? run?.userId : undefined,
|
182
178
|
// Copied settings:
|
183
|
-
...
|
179
|
+
...this.settings,
|
184
180
|
// map to OpenAI API names:
|
185
|
-
stop:
|
186
|
-
maxTokens:
|
181
|
+
stop: this.settings.stopSequences,
|
182
|
+
maxTokens: this.settings.maxCompletionTokens,
|
187
183
|
// other settings:
|
188
184
|
abortSignal: run?.abortSignal,
|
189
185
|
prompt,
|
@@ -212,26 +208,29 @@ export class OpenAITextGenerationModel extends AbstractModel {
|
|
212
208
|
];
|
213
209
|
return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
|
214
210
|
}
|
215
|
-
|
216
|
-
|
211
|
+
async doGenerateText(prompt, options) {
|
212
|
+
const response = await this.callAPI(prompt, {
|
217
213
|
...options,
|
218
214
|
responseFormat: OpenAITextResponseFormat.json,
|
219
215
|
});
|
216
|
+
return {
|
217
|
+
response,
|
218
|
+
text: response.choices[0].text,
|
219
|
+
usage: {
|
220
|
+
promptTokens: response.usage.prompt_tokens,
|
221
|
+
completionTokens: response.usage.completion_tokens,
|
222
|
+
totalTokens: response.usage.total_tokens,
|
223
|
+
},
|
224
|
+
};
|
220
225
|
}
|
221
|
-
|
222
|
-
return response.choices[0].text;
|
223
|
-
}
|
224
|
-
generateDeltaStreamResponse(prompt, options) {
|
226
|
+
doStreamText(prompt, options) {
|
225
227
|
return this.callAPI(prompt, {
|
226
228
|
...options,
|
227
229
|
responseFormat: OpenAITextResponseFormat.deltaIterable,
|
228
230
|
});
|
229
231
|
}
|
230
|
-
extractTextDelta(fullDelta) {
|
231
|
-
return fullDelta[0].delta;
|
232
|
-
}
|
233
232
|
withPromptFormat(promptFormat) {
|
234
|
-
return new
|
233
|
+
return new PromptFormatTextStreamingModel({
|
235
234
|
model: this.withSettings({
|
236
235
|
stopSequences: [
|
237
236
|
...(this.settings.stopSequences ?? []),
|
@@ -241,13 +240,6 @@ export class OpenAITextGenerationModel extends AbstractModel {
|
|
241
240
|
promptFormat,
|
242
241
|
});
|
243
242
|
}
|
244
|
-
extractUsage(response) {
|
245
|
-
return {
|
246
|
-
promptTokens: response.usage.prompt_tokens,
|
247
|
-
completionTokens: response.usage.completion_tokens,
|
248
|
-
totalTokens: response.usage.total_tokens,
|
249
|
-
};
|
250
|
-
}
|
251
243
|
withSettings(additionalSettings) {
|
252
244
|
return new OpenAITextGenerationModel(Object.assign({}, this.settings, additionalSettings));
|
253
245
|
}
|
@@ -375,6 +367,7 @@ async function createOpenAITextFullDeltaIterableQueue(stream) {
|
|
375
367
|
queue.push({
|
376
368
|
type: "delta",
|
377
369
|
fullDelta: streamDeltaDeepCopy,
|
370
|
+
valueDelta: streamDeltaDeepCopy[0].delta,
|
378
371
|
});
|
379
372
|
}
|
380
373
|
}
|
@@ -5,9 +5,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
6
|
exports.OpenAITranscriptionResponseFormat = exports.OpenAITranscriptionModel = exports.calculateOpenAITranscriptionCostInMillicents = exports.OPENAI_TRANSCRIPTION_MODELS = void 0;
|
7
7
|
const zod_1 = __importDefault(require("zod"));
|
8
|
-
const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
|
9
8
|
const callWithRetryAndThrottle_js_1 = require("../../core/api/callWithRetryAndThrottle.cjs");
|
10
9
|
const postToApi_js_1 = require("../../core/api/postToApi.cjs");
|
10
|
+
const AbstractModel_js_1 = require("../../model-function/AbstractModel.cjs");
|
11
11
|
const OpenAIApiConfiguration_js_1 = require("./OpenAIApiConfiguration.cjs");
|
12
12
|
const OpenAIError_js_1 = require("./OpenAIError.cjs");
|
13
13
|
/**
|
@@ -56,40 +56,31 @@ class OpenAITranscriptionModel extends AbstractModel_js_1.AbstractModel {
|
|
56
56
|
get modelName() {
|
57
57
|
return this.settings.model;
|
58
58
|
}
|
59
|
-
|
60
|
-
|
59
|
+
async doTranscribe(data, options) {
|
60
|
+
const response = await this.callAPI(data, {
|
61
61
|
responseFormat: exports.OpenAITranscriptionResponseFormat.verboseJson,
|
62
62
|
functionId: options?.functionId,
|
63
|
-
settings: options?.settings,
|
64
63
|
run: options?.run,
|
65
64
|
});
|
66
|
-
|
67
|
-
|
68
|
-
|
65
|
+
return {
|
66
|
+
response,
|
67
|
+
transcription: response.text,
|
68
|
+
};
|
69
69
|
}
|
70
70
|
async callAPI(data, options) {
|
71
|
-
const run = options?.run;
|
72
|
-
const settings = options?.settings;
|
73
|
-
const responseFormat = options?.responseFormat;
|
74
|
-
const combinedSettings = {
|
75
|
-
...this.settings,
|
76
|
-
...settings,
|
77
|
-
};
|
78
|
-
const callSettings = {
|
79
|
-
// Copied settings:
|
80
|
-
...combinedSettings,
|
81
|
-
// other settings:
|
82
|
-
abortSignal: run?.abortSignal,
|
83
|
-
file: {
|
84
|
-
name: `audio.${data.type}`,
|
85
|
-
data: data.data,
|
86
|
-
},
|
87
|
-
responseFormat,
|
88
|
-
};
|
89
71
|
return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
|
90
|
-
retry:
|
91
|
-
throttle:
|
92
|
-
call: async () => callOpenAITranscriptionAPI(
|
72
|
+
retry: this.settings.api?.retry,
|
73
|
+
throttle: this.settings.api?.throttle,
|
74
|
+
call: async () => callOpenAITranscriptionAPI({
|
75
|
+
...this.settings,
|
76
|
+
// other settings:
|
77
|
+
abortSignal: options?.run?.abortSignal,
|
78
|
+
file: {
|
79
|
+
name: `audio.${data.type}`,
|
80
|
+
data: data.data,
|
81
|
+
},
|
82
|
+
responseFormat: options?.responseFormat,
|
83
|
+
}),
|
93
84
|
});
|
94
85
|
}
|
95
86
|
get settingsForEvent() {
|
@@ -1,10 +1,10 @@
|
|
1
1
|
/// <reference types="node" />
|
2
2
|
import z from "zod";
|
3
|
-
import {
|
4
|
-
import { ModelFunctionOptions } from "../../model-function/ModelFunctionOptions.js";
|
3
|
+
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
5
4
|
import { ApiConfiguration } from "../../core/api/ApiConfiguration.js";
|
6
|
-
import { TranscriptionModel, TranscriptionModelSettings } from "../../model-function/transcribe-speech/TranscriptionModel.js";
|
7
5
|
import { ResponseHandler } from "../../core/api/postToApi.js";
|
6
|
+
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
7
|
+
import { TranscriptionModel, TranscriptionModelSettings } from "../../model-function/transcribe-speech/TranscriptionModel.js";
|
8
8
|
/**
|
9
9
|
* @see https://openai.com/pricing
|
10
10
|
*/
|
@@ -42,15 +42,35 @@ export type OpenAITranscriptionInput = {
|
|
42
42
|
* }
|
43
43
|
* );
|
44
44
|
*/
|
45
|
-
export declare class OpenAITranscriptionModel extends AbstractModel<OpenAITranscriptionModelSettings> implements TranscriptionModel<OpenAITranscriptionInput,
|
45
|
+
export declare class OpenAITranscriptionModel extends AbstractModel<OpenAITranscriptionModelSettings> implements TranscriptionModel<OpenAITranscriptionInput, OpenAITranscriptionModelSettings> {
|
46
46
|
constructor(settings: OpenAITranscriptionModelSettings);
|
47
47
|
readonly provider: "openai";
|
48
48
|
get modelName(): "whisper-1";
|
49
|
-
|
50
|
-
|
49
|
+
doTranscribe(data: OpenAITranscriptionInput, options?: FunctionOptions): Promise<{
|
50
|
+
response: {
|
51
|
+
text: string;
|
52
|
+
segments: {
|
53
|
+
text: string;
|
54
|
+
temperature: number;
|
55
|
+
id: number;
|
56
|
+
tokens: number[];
|
57
|
+
seek: number;
|
58
|
+
start: number;
|
59
|
+
end: number;
|
60
|
+
avg_logprob: number;
|
61
|
+
compression_ratio: number;
|
62
|
+
no_speech_prob: number;
|
63
|
+
transient?: boolean | undefined;
|
64
|
+
}[];
|
65
|
+
task: "transcribe";
|
66
|
+
language: string;
|
67
|
+
duration: number;
|
68
|
+
};
|
69
|
+
transcription: string;
|
70
|
+
}>;
|
51
71
|
callAPI<RESULT>(data: OpenAITranscriptionInput, options: {
|
52
72
|
responseFormat: OpenAITranscriptionResponseFormatType<RESULT>;
|
53
|
-
} &
|
73
|
+
} & FunctionOptions): Promise<RESULT>;
|
54
74
|
get settingsForEvent(): Partial<OpenAITranscriptionModelSettings>;
|
55
75
|
withSettings(additionalSettings: OpenAITranscriptionModelSettings): this;
|
56
76
|
}
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import z from "zod";
|
2
|
-
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
3
2
|
import { callWithRetryAndThrottle } from "../../core/api/callWithRetryAndThrottle.js";
|
4
3
|
import { createJsonResponseHandler, createTextResponseHandler, postToApi, } from "../../core/api/postToApi.js";
|
4
|
+
import { AbstractModel } from "../../model-function/AbstractModel.js";
|
5
5
|
import { OpenAIApiConfiguration } from "./OpenAIApiConfiguration.js";
|
6
6
|
import { failedOpenAICallResponseHandler } from "./OpenAIError.js";
|
7
7
|
/**
|
@@ -49,40 +49,31 @@ export class OpenAITranscriptionModel extends AbstractModel {
|
|
49
49
|
get modelName() {
|
50
50
|
return this.settings.model;
|
51
51
|
}
|
52
|
-
|
53
|
-
|
52
|
+
async doTranscribe(data, options) {
|
53
|
+
const response = await this.callAPI(data, {
|
54
54
|
responseFormat: OpenAITranscriptionResponseFormat.verboseJson,
|
55
55
|
functionId: options?.functionId,
|
56
|
-
settings: options?.settings,
|
57
56
|
run: options?.run,
|
58
57
|
});
|
59
|
-
|
60
|
-
|
61
|
-
|
58
|
+
return {
|
59
|
+
response,
|
60
|
+
transcription: response.text,
|
61
|
+
};
|
62
62
|
}
|
63
63
|
async callAPI(data, options) {
|
64
|
-
const run = options?.run;
|
65
|
-
const settings = options?.settings;
|
66
|
-
const responseFormat = options?.responseFormat;
|
67
|
-
const combinedSettings = {
|
68
|
-
...this.settings,
|
69
|
-
...settings,
|
70
|
-
};
|
71
|
-
const callSettings = {
|
72
|
-
// Copied settings:
|
73
|
-
...combinedSettings,
|
74
|
-
// other settings:
|
75
|
-
abortSignal: run?.abortSignal,
|
76
|
-
file: {
|
77
|
-
name: `audio.${data.type}`,
|
78
|
-
data: data.data,
|
79
|
-
},
|
80
|
-
responseFormat,
|
81
|
-
};
|
82
64
|
return callWithRetryAndThrottle({
|
83
|
-
retry:
|
84
|
-
throttle:
|
85
|
-
call: async () => callOpenAITranscriptionAPI(
|
65
|
+
retry: this.settings.api?.retry,
|
66
|
+
throttle: this.settings.api?.throttle,
|
67
|
+
call: async () => callOpenAITranscriptionAPI({
|
68
|
+
...this.settings,
|
69
|
+
// other settings:
|
70
|
+
abortSignal: options?.run?.abortSignal,
|
71
|
+
file: {
|
72
|
+
name: `audio.${data.type}`,
|
73
|
+
data: data.data,
|
74
|
+
},
|
75
|
+
responseFormat: options?.responseFormat,
|
76
|
+
}),
|
86
77
|
});
|
87
78
|
}
|
88
79
|
get settingsForEvent() {
|
@@ -10,7 +10,7 @@ const callWithRetryAndThrottle_js_1 = require("../../../core/api/callWithRetryAn
|
|
10
10
|
const postToApi_js_1 = require("../../../core/api/postToApi.cjs");
|
11
11
|
const AbstractModel_js_1 = require("../../../model-function/AbstractModel.cjs");
|
12
12
|
const parsePartialJson_js_1 = require("../../../model-function/generate-structure/parsePartialJson.cjs");
|
13
|
-
const
|
13
|
+
const PromptFormatTextStreamingModel_js_1 = require("../../../prompt/PromptFormatTextStreamingModel.cjs");
|
14
14
|
const OpenAIApiConfiguration_js_1 = require("../OpenAIApiConfiguration.cjs");
|
15
15
|
const OpenAIError_js_1 = require("../OpenAIError.cjs");
|
16
16
|
const TikTokenTokenizer_js_1 = require("../TikTokenTokenizer.cjs");
|
@@ -183,27 +183,25 @@ class OpenAIChatModel extends AbstractModel_js_1.AbstractModel {
|
|
183
183
|
});
|
184
184
|
}
|
185
185
|
async callAPI(messages, options) {
|
186
|
-
const { run, settings, responseFormat } = options;
|
187
|
-
const combinedSettings = {
|
188
|
-
...this.settings,
|
189
|
-
...settings,
|
190
|
-
};
|
191
|
-
const callSettings = {
|
192
|
-
user: this.settings.isUserIdForwardingEnabled ? run?.userId : undefined,
|
193
|
-
// Copied settings:
|
194
|
-
...combinedSettings,
|
195
|
-
// map to OpenAI API names:
|
196
|
-
stop: combinedSettings.stopSequences,
|
197
|
-
maxTokens: combinedSettings.maxCompletionTokens,
|
198
|
-
// other settings:
|
199
|
-
abortSignal: run?.abortSignal,
|
200
|
-
messages,
|
201
|
-
responseFormat,
|
202
|
-
};
|
203
186
|
return (0, callWithRetryAndThrottle_js_1.callWithRetryAndThrottle)({
|
204
|
-
retry:
|
205
|
-
throttle:
|
206
|
-
call: async () => callOpenAIChatCompletionAPI(
|
187
|
+
retry: this.settings.api?.retry,
|
188
|
+
throttle: this.settings.api?.throttle,
|
189
|
+
call: async () => callOpenAIChatCompletionAPI({
|
190
|
+
...this.settings,
|
191
|
+
// function calling:
|
192
|
+
functions: options.functions ?? this.settings.functions,
|
193
|
+
functionCall: options.functionCall ?? this.settings.functionCall,
|
194
|
+
// map to OpenAI API names:
|
195
|
+
stop: this.settings.stopSequences,
|
196
|
+
maxTokens: this.settings.maxCompletionTokens,
|
197
|
+
// other settings:
|
198
|
+
user: this.settings.isUserIdForwardingEnabled
|
199
|
+
? options.run?.userId
|
200
|
+
: undefined,
|
201
|
+
abortSignal: options.run?.abortSignal,
|
202
|
+
responseFormat: options.responseFormat,
|
203
|
+
messages,
|
204
|
+
}),
|
207
205
|
});
|
208
206
|
}
|
209
207
|
get settingsForEvent() {
|
@@ -221,24 +219,23 @@ class OpenAIChatModel extends AbstractModel_js_1.AbstractModel {
|
|
221
219
|
];
|
222
220
|
return Object.fromEntries(Object.entries(this.settings).filter(([key]) => eventSettingProperties.includes(key)));
|
223
221
|
}
|
224
|
-
|
225
|
-
|
222
|
+
async doGenerateText(prompt, options) {
|
223
|
+
const response = await this.callAPI(prompt, {
|
226
224
|
...options,
|
227
225
|
responseFormat: exports.OpenAIChatResponseFormat.json,
|
228
226
|
});
|
227
|
+
return {
|
228
|
+
response,
|
229
|
+
text: response.choices[0].message.content,
|
230
|
+
usage: this.extractUsage(response),
|
231
|
+
};
|
229
232
|
}
|
230
|
-
|
231
|
-
return response.choices[0].message.content;
|
232
|
-
}
|
233
|
-
generateDeltaStreamResponse(prompt, options) {
|
233
|
+
doStreamText(prompt, options) {
|
234
234
|
return this.callAPI(prompt, {
|
235
235
|
...options,
|
236
|
-
responseFormat: exports.OpenAIChatResponseFormat.
|
236
|
+
responseFormat: exports.OpenAIChatResponseFormat.textDeltaIterable,
|
237
237
|
});
|
238
238
|
}
|
239
|
-
extractTextDelta(fullDelta) {
|
240
|
-
return fullDelta[0]?.delta.content ?? undefined;
|
241
|
-
}
|
242
239
|
/**
|
243
240
|
* JSON generation uses the OpenAI GPT function calling API.
|
244
241
|
* It provides a single function specification and instructs the model to provide parameters for calling the function.
|
@@ -246,69 +243,54 @@ class OpenAIChatModel extends AbstractModel_js_1.AbstractModel {
|
|
246
243
|
*
|
247
244
|
* @see https://platform.openai.com/docs/guides/gpt/function-calling
|
248
245
|
*/
|
249
|
-
|
250
|
-
|
246
|
+
async doGenerateStructure(structureDefinition, prompt, options) {
|
247
|
+
const response = await this.callAPI(prompt, {
|
248
|
+
...options,
|
251
249
|
responseFormat: exports.OpenAIChatResponseFormat.json,
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
parameters: structureDefinition.schema.getJsonSchema(),
|
261
|
-
},
|
262
|
-
],
|
263
|
-
},
|
264
|
-
run: options?.run,
|
265
|
-
});
|
266
|
-
}
|
267
|
-
extractStructure(response) {
|
268
|
-
return secure_json_parse_1.default.parse(response.choices[0].message.function_call.arguments);
|
269
|
-
}
|
270
|
-
generateStructureStreamResponse(structureDefinition, prompt, options) {
|
271
|
-
return this.callAPI(prompt, {
|
272
|
-
responseFormat: exports.OpenAIChatResponseFormat.deltaIterable,
|
273
|
-
functionId: options?.functionId,
|
274
|
-
settings: {
|
275
|
-
...options,
|
276
|
-
functionCall: { name: structureDefinition.name },
|
277
|
-
functions: [
|
278
|
-
{
|
279
|
-
name: structureDefinition.name,
|
280
|
-
description: structureDefinition.description,
|
281
|
-
parameters: structureDefinition.schema.getJsonSchema(),
|
282
|
-
},
|
283
|
-
],
|
284
|
-
},
|
285
|
-
run: options?.run,
|
250
|
+
functionCall: { name: structureDefinition.name },
|
251
|
+
functions: [
|
252
|
+
{
|
253
|
+
name: structureDefinition.name,
|
254
|
+
description: structureDefinition.description,
|
255
|
+
parameters: structureDefinition.schema.getJsonSchema(),
|
256
|
+
},
|
257
|
+
],
|
286
258
|
});
|
259
|
+
return {
|
260
|
+
response,
|
261
|
+
structure: secure_json_parse_1.default.parse(response.choices[0].message.function_call.arguments),
|
262
|
+
usage: this.extractUsage(response),
|
263
|
+
};
|
287
264
|
}
|
288
|
-
|
289
|
-
return (0, parsePartialJson_js_1.parsePartialJson)(fullDelta[0]?.function_call?.arguments);
|
290
|
-
}
|
291
|
-
generateStructureOrTextResponse(structureDefinitions, prompt, options) {
|
265
|
+
async doStreamStructure(structureDefinition, prompt, options) {
|
292
266
|
return this.callAPI(prompt, {
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
functions: structureDefinitions.map((structureDefinition) => ({
|
267
|
+
...options,
|
268
|
+
responseFormat: exports.OpenAIChatResponseFormat.structureDeltaIterable,
|
269
|
+
functionCall: { name: structureDefinition.name },
|
270
|
+
functions: [
|
271
|
+
{
|
299
272
|
name: structureDefinition.name,
|
300
273
|
description: structureDefinition.description,
|
301
274
|
parameters: structureDefinition.schema.getJsonSchema(),
|
302
|
-
}
|
303
|
-
|
304
|
-
run: options?.run,
|
275
|
+
},
|
276
|
+
],
|
305
277
|
});
|
306
278
|
}
|
307
|
-
|
279
|
+
async doGenerateStructureOrText(structureDefinitions, prompt, options) {
|
280
|
+
const response = await this.callAPI(prompt, {
|
281
|
+
...options,
|
282
|
+
responseFormat: exports.OpenAIChatResponseFormat.json,
|
283
|
+
functionCall: "auto",
|
284
|
+
functions: structureDefinitions.map((structureDefinition) => ({
|
285
|
+
name: structureDefinition.name,
|
286
|
+
description: structureDefinition.description,
|
287
|
+
parameters: structureDefinition.schema.getJsonSchema(),
|
288
|
+
})),
|
289
|
+
});
|
308
290
|
const message = response.choices[0].message;
|
309
291
|
const content = message.content;
|
310
292
|
const functionCall = message.function_call;
|
311
|
-
|
293
|
+
const structureAndText = functionCall == null
|
312
294
|
? {
|
313
295
|
structure: null,
|
314
296
|
value: null,
|
@@ -319,6 +301,11 @@ class OpenAIChatModel extends AbstractModel_js_1.AbstractModel {
|
|
319
301
|
value: secure_json_parse_1.default.parse(functionCall.arguments),
|
320
302
|
text: content,
|
321
303
|
};
|
304
|
+
return {
|
305
|
+
response,
|
306
|
+
structureAndText,
|
307
|
+
usage: this.extractUsage(response),
|
308
|
+
};
|
322
309
|
}
|
323
310
|
extractUsage(response) {
|
324
311
|
return {
|
@@ -328,7 +315,7 @@ class OpenAIChatModel extends AbstractModel_js_1.AbstractModel {
|
|
328
315
|
};
|
329
316
|
}
|
330
317
|
withPromptFormat(promptFormat) {
|
331
|
-
return new
|
318
|
+
return new PromptFormatTextStreamingModel_js_1.PromptFormatTextStreamingModel({
|
332
319
|
model: this.withSettings({
|
333
320
|
stopSequences: [
|
334
321
|
...(this.settings.stopSequences ?? []),
|
@@ -409,8 +396,12 @@ exports.OpenAIChatResponseFormat = {
|
|
409
396
|
/**
|
410
397
|
* Returns an async iterable over the text deltas (only the tex different of the first choice).
|
411
398
|
*/
|
412
|
-
|
399
|
+
textDeltaIterable: {
|
400
|
+
stream: true,
|
401
|
+
handler: async ({ response }) => (0, OpenAIChatStreamIterable_js_1.createOpenAIChatDeltaIterableQueue)(response.body, (delta) => delta[0]?.delta.content ?? ""),
|
402
|
+
},
|
403
|
+
structureDeltaIterable: {
|
413
404
|
stream: true,
|
414
|
-
handler: async ({ response }) => (0, OpenAIChatStreamIterable_js_1.
|
405
|
+
handler: async ({ response }) => (0, OpenAIChatStreamIterable_js_1.createOpenAIChatDeltaIterableQueue)(response.body, (delta) => (0, parsePartialJson_js_1.parsePartialJson)(delta[0]?.function_call?.arguments)),
|
415
406
|
},
|
416
407
|
};
|