modelfusion 0.18.0 → 0.20.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 +4 -5
- package/model-function/embed-text/embedText.cjs +64 -18
- package/model-function/embed-text/embedText.d.ts +3 -19
- package/model-function/embed-text/embedText.js +64 -18
- package/model-function/executeCall.cjs +49 -3
- package/model-function/executeCall.d.ts +18 -5
- package/model-function/executeCall.js +46 -1
- package/model-function/generate-image/generateImage.cjs +17 -9
- package/model-function/generate-image/generateImage.d.ts +2 -11
- package/model-function/generate-image/generateImage.js +17 -9
- package/model-function/generate-json/generateJson.cjs +2 -9
- package/model-function/generate-json/generateJson.d.ts +2 -11
- package/model-function/generate-json/generateJson.js +2 -9
- package/model-function/generate-json/generateJsonOrText.cjs +2 -9
- package/model-function/generate-json/generateJsonOrText.d.ts +3 -15
- package/model-function/generate-json/generateJsonOrText.js +2 -9
- package/model-function/generate-text/generateText.cjs +14 -9
- package/model-function/generate-text/generateText.d.ts +2 -11
- package/model-function/generate-text/generateText.js +14 -9
- package/model-function/generate-text/streamText.cjs +44 -9
- package/model-function/generate-text/streamText.d.ts +16 -12
- package/model-function/generate-text/streamText.js +41 -7
- package/model-function/synthesize-speech/synthesizeSpeech.cjs +5 -8
- package/model-function/synthesize-speech/synthesizeSpeech.d.ts +2 -10
- package/model-function/synthesize-speech/synthesizeSpeech.js +5 -8
- package/model-function/transcribe-speech/transcribe.cjs +16 -9
- package/model-function/transcribe-speech/transcribe.d.ts +2 -11
- package/model-function/transcribe-speech/transcribe.js +16 -9
- package/model-provider/cohere/CohereTextEmbeddingModel.d.ts +3 -3
- package/model-provider/openai/OpenAITextGenerationModel.cjs +8 -0
- package/model-provider/openai/OpenAITextGenerationModel.d.ts +10 -2
- package/model-provider/openai/OpenAITextGenerationModel.js +8 -0
- package/model-provider/openai/TikTokenTokenizer.cjs +5 -1
- package/model-provider/openai/TikTokenTokenizer.js +5 -1
- package/package.json +1 -1
- package/tool/executeTool.cjs +48 -9
- package/tool/executeTool.d.ts +16 -9
- package/tool/executeTool.js +45 -7
- package/tool/useTool.cjs +2 -3
- package/tool/useTool.js +2 -3
@@ -2,10 +2,22 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.generateText = void 0;
|
4
4
|
const executeCall_js_1 = require("../executeCall.cjs");
|
5
|
-
|
5
|
+
/**
|
6
|
+
* Generates a text using a prompt.
|
7
|
+
* The prompt format depends on the model.
|
8
|
+
* For example, OpenAI text models expect a string prompt, and OpenAI chat models expect an array of chat messages.
|
9
|
+
*
|
10
|
+
* @example
|
11
|
+
* const model = new OpenAITextGenerationModel(...);
|
12
|
+
*
|
13
|
+
* const text = await model.generateText(
|
14
|
+
* "Write a short story about a robot learning to love:\n\n"
|
15
|
+
* );
|
16
|
+
*/
|
17
|
+
function generateText(
|
6
18
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
7
19
|
model, prompt, options) {
|
8
|
-
|
20
|
+
return (0, executeCall_js_1.executeCall)({
|
9
21
|
model,
|
10
22
|
options,
|
11
23
|
generateResponse: (options) => model.generateTextResponse(prompt, options),
|
@@ -46,12 +58,5 @@ model, prompt, options) {
|
|
46
58
|
generatedText: output,
|
47
59
|
}),
|
48
60
|
});
|
49
|
-
return options?.fullResponse === true
|
50
|
-
? {
|
51
|
-
text: result.output,
|
52
|
-
response: result.response,
|
53
|
-
metadata: result.metadata,
|
54
|
-
}
|
55
|
-
: result.output;
|
56
61
|
}
|
57
62
|
exports.generateText = generateText;
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import { FunctionOptions } from "../FunctionOptions.js";
|
2
|
-
import {
|
2
|
+
import { ModelFunctionPromise } from "../executeCall.js";
|
3
3
|
import { TextGenerationModel, TextGenerationModelSettings } from "./TextGenerationModel.js";
|
4
4
|
/**
|
5
5
|
* Generates a text using a prompt.
|
@@ -13,13 +13,4 @@ import { TextGenerationModel, TextGenerationModelSettings } from "./TextGenerati
|
|
13
13
|
* "Write a short story about a robot learning to love:\n\n"
|
14
14
|
* );
|
15
15
|
*/
|
16
|
-
export declare function generateText<PROMPT, RESPONSE, SETTINGS extends TextGenerationModelSettings>(model: TextGenerationModel<PROMPT, RESPONSE, any, SETTINGS>, prompt: PROMPT, options
|
17
|
-
fullResponse: true;
|
18
|
-
}): Promise<{
|
19
|
-
text: string;
|
20
|
-
response: RESPONSE;
|
21
|
-
metadata: CallMetadata<TextGenerationModel<PROMPT, RESPONSE, unknown, SETTINGS>>;
|
22
|
-
}>;
|
23
|
-
export declare function generateText<PROMPT, RESPONSE, SETTINGS extends TextGenerationModelSettings>(model: TextGenerationModel<PROMPT, RESPONSE, any, SETTINGS>, prompt: PROMPT, options?: FunctionOptions<SETTINGS> & {
|
24
|
-
fullResponse?: false;
|
25
|
-
}): Promise<string>;
|
16
|
+
export declare function generateText<PROMPT, RESPONSE, SETTINGS extends TextGenerationModelSettings>(model: TextGenerationModel<PROMPT, RESPONSE, any, SETTINGS>, prompt: PROMPT, options?: FunctionOptions<SETTINGS>): ModelFunctionPromise<TextGenerationModel<PROMPT, RESPONSE, any, SETTINGS>, string, RESPONSE>;
|
@@ -1,8 +1,20 @@
|
|
1
1
|
import { executeCall } from "../executeCall.js";
|
2
|
-
|
2
|
+
/**
|
3
|
+
* Generates a text using a prompt.
|
4
|
+
* The prompt format depends on the model.
|
5
|
+
* For example, OpenAI text models expect a string prompt, and OpenAI chat models expect an array of chat messages.
|
6
|
+
*
|
7
|
+
* @example
|
8
|
+
* const model = new OpenAITextGenerationModel(...);
|
9
|
+
*
|
10
|
+
* const text = await model.generateText(
|
11
|
+
* "Write a short story about a robot learning to love:\n\n"
|
12
|
+
* );
|
13
|
+
*/
|
14
|
+
export function generateText(
|
3
15
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
4
16
|
model, prompt, options) {
|
5
|
-
|
17
|
+
return executeCall({
|
6
18
|
model,
|
7
19
|
options,
|
8
20
|
generateResponse: (options) => model.generateTextResponse(prompt, options),
|
@@ -43,11 +55,4 @@ model, prompt, options) {
|
|
43
55
|
generatedText: output,
|
44
56
|
}),
|
45
57
|
});
|
46
|
-
return options?.fullResponse === true
|
47
|
-
? {
|
48
|
-
text: result.output,
|
49
|
-
response: result.response,
|
50
|
-
metadata: result.metadata,
|
51
|
-
}
|
52
|
-
: result.output;
|
53
58
|
}
|
@@ -1,13 +1,51 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.streamText = void 0;
|
3
|
+
exports.streamText = exports.StreamTextPromise = void 0;
|
4
4
|
const nanoid_1 = require("nanoid");
|
5
5
|
const RunFunctionEventSource_js_1 = require("../../run/RunFunctionEventSource.cjs");
|
6
6
|
const DurationMeasurement_js_1 = require("../../util/DurationMeasurement.cjs");
|
7
7
|
const AbortError_js_1 = require("../../util/api/AbortError.cjs");
|
8
8
|
const runSafe_js_1 = require("../../util/runSafe.cjs");
|
9
9
|
const extractTextDeltas_js_1 = require("./extractTextDeltas.cjs");
|
10
|
-
|
10
|
+
class StreamTextPromise extends Promise {
|
11
|
+
constructor(fullPromise) {
|
12
|
+
super((resolve) => {
|
13
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
14
|
+
resolve(null); // we override the resolve function
|
15
|
+
});
|
16
|
+
Object.defineProperty(this, "fullPromise", {
|
17
|
+
enumerable: true,
|
18
|
+
configurable: true,
|
19
|
+
writable: true,
|
20
|
+
value: fullPromise
|
21
|
+
});
|
22
|
+
Object.defineProperty(this, "outputPromise", {
|
23
|
+
enumerable: true,
|
24
|
+
configurable: true,
|
25
|
+
writable: true,
|
26
|
+
value: void 0
|
27
|
+
});
|
28
|
+
this.outputPromise = fullPromise.then((result) => result.output);
|
29
|
+
}
|
30
|
+
asFullResponse() {
|
31
|
+
return this.fullPromise;
|
32
|
+
}
|
33
|
+
then(onfulfilled, onrejected) {
|
34
|
+
return this.outputPromise.then(onfulfilled, onrejected);
|
35
|
+
}
|
36
|
+
catch(onrejected) {
|
37
|
+
return this.outputPromise.catch(onrejected);
|
38
|
+
}
|
39
|
+
finally(onfinally) {
|
40
|
+
return this.outputPromise.finally(onfinally);
|
41
|
+
}
|
42
|
+
}
|
43
|
+
exports.StreamTextPromise = StreamTextPromise;
|
44
|
+
function streamText(model, prompt, options) {
|
45
|
+
return new StreamTextPromise(doStreamText(model, prompt, options));
|
46
|
+
}
|
47
|
+
exports.streamText = streamText;
|
48
|
+
async function doStreamText(model, prompt, options) {
|
11
49
|
if (options?.settings != null) {
|
12
50
|
model = model.withSettings(options.settings);
|
13
51
|
options = {
|
@@ -107,11 +145,8 @@ async function streamText(model, prompt, options) {
|
|
107
145
|
});
|
108
146
|
throw result.error;
|
109
147
|
}
|
110
|
-
return
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
}
|
115
|
-
: result.output;
|
148
|
+
return {
|
149
|
+
output: result.output,
|
150
|
+
metadata: startMetadata,
|
151
|
+
};
|
116
152
|
}
|
117
|
-
exports.streamText = streamText;
|
@@ -2,18 +2,22 @@ import { FunctionOptions } from "../FunctionOptions.js";
|
|
2
2
|
import { CallMetadata } from "../executeCall.js";
|
3
3
|
import { DeltaEvent } from "./DeltaEvent.js";
|
4
4
|
import { TextGenerationModel, TextGenerationModelSettings } from "./TextGenerationModel.js";
|
5
|
+
export declare class StreamTextPromise<PROMPT, FULL_DELTA, SETTINGS extends TextGenerationModelSettings> extends Promise<AsyncIterable<string>> {
|
6
|
+
private fullPromise;
|
7
|
+
private outputPromise;
|
8
|
+
constructor(fullPromise: Promise<{
|
9
|
+
output: AsyncIterable<string>;
|
10
|
+
metadata: Omit<CallMetadata<TextGenerationModel<PROMPT, unknown, FULL_DELTA, SETTINGS>>, "durationInMs">;
|
11
|
+
}>);
|
12
|
+
asFullResponse(): Promise<{
|
13
|
+
output: AsyncIterable<string>;
|
14
|
+
metadata: Omit<CallMetadata<TextGenerationModel<PROMPT, unknown, FULL_DELTA, SETTINGS>>, "durationInMs">;
|
15
|
+
}>;
|
16
|
+
then<TResult1 = AsyncIterable<string>, TResult2 = never>(onfulfilled?: ((value: AsyncIterable<string>) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | undefined | null): Promise<TResult1 | TResult2>;
|
17
|
+
catch<TResult = never>(onrejected?: ((reason: unknown) => TResult | PromiseLike<TResult>) | undefined | null): Promise<AsyncIterable<string> | TResult>;
|
18
|
+
finally(onfinally?: (() => void) | undefined | null): Promise<AsyncIterable<string>>;
|
19
|
+
}
|
5
20
|
export declare function streamText<PROMPT, FULL_DELTA, SETTINGS extends TextGenerationModelSettings>(model: TextGenerationModel<PROMPT, unknown, FULL_DELTA, SETTINGS> & {
|
6
21
|
generateDeltaStreamResponse: (prompt: PROMPT, options: FunctionOptions<SETTINGS>) => PromiseLike<AsyncIterable<DeltaEvent<FULL_DELTA>>>;
|
7
22
|
extractTextDelta: (fullDelta: FULL_DELTA) => string | undefined;
|
8
|
-
}, prompt: PROMPT, options?: FunctionOptions<SETTINGS>
|
9
|
-
fullResponse?: false;
|
10
|
-
}): Promise<AsyncIterable<string>>;
|
11
|
-
export declare function streamText<PROMPT, FULL_DELTA, SETTINGS extends TextGenerationModelSettings>(model: TextGenerationModel<PROMPT, unknown, FULL_DELTA, SETTINGS> & {
|
12
|
-
generateDeltaStreamResponse: (prompt: PROMPT, options: FunctionOptions<SETTINGS>) => PromiseLike<AsyncIterable<DeltaEvent<FULL_DELTA>>>;
|
13
|
-
extractTextDelta: (fullDelta: FULL_DELTA) => string | undefined;
|
14
|
-
}, prompt: PROMPT, options: FunctionOptions<SETTINGS> & {
|
15
|
-
fullResponse: true;
|
16
|
-
}): Promise<{
|
17
|
-
textStream: AsyncIterable<string>;
|
18
|
-
metadata: Omit<CallMetadata<TextGenerationModel<PROMPT, unknown, FULL_DELTA, SETTINGS>>, "durationInMs">;
|
19
|
-
}>;
|
23
|
+
}, prompt: PROMPT, options?: FunctionOptions<SETTINGS>): StreamTextPromise<PROMPT, FULL_DELTA, SETTINGS>;
|
@@ -4,7 +4,43 @@ import { startDurationMeasurement } from "../../util/DurationMeasurement.js";
|
|
4
4
|
import { AbortError } from "../../util/api/AbortError.js";
|
5
5
|
import { runSafe } from "../../util/runSafe.js";
|
6
6
|
import { extractTextDeltas } from "./extractTextDeltas.js";
|
7
|
-
export
|
7
|
+
export class StreamTextPromise extends Promise {
|
8
|
+
constructor(fullPromise) {
|
9
|
+
super((resolve) => {
|
10
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
11
|
+
resolve(null); // we override the resolve function
|
12
|
+
});
|
13
|
+
Object.defineProperty(this, "fullPromise", {
|
14
|
+
enumerable: true,
|
15
|
+
configurable: true,
|
16
|
+
writable: true,
|
17
|
+
value: fullPromise
|
18
|
+
});
|
19
|
+
Object.defineProperty(this, "outputPromise", {
|
20
|
+
enumerable: true,
|
21
|
+
configurable: true,
|
22
|
+
writable: true,
|
23
|
+
value: void 0
|
24
|
+
});
|
25
|
+
this.outputPromise = fullPromise.then((result) => result.output);
|
26
|
+
}
|
27
|
+
asFullResponse() {
|
28
|
+
return this.fullPromise;
|
29
|
+
}
|
30
|
+
then(onfulfilled, onrejected) {
|
31
|
+
return this.outputPromise.then(onfulfilled, onrejected);
|
32
|
+
}
|
33
|
+
catch(onrejected) {
|
34
|
+
return this.outputPromise.catch(onrejected);
|
35
|
+
}
|
36
|
+
finally(onfinally) {
|
37
|
+
return this.outputPromise.finally(onfinally);
|
38
|
+
}
|
39
|
+
}
|
40
|
+
export function streamText(model, prompt, options) {
|
41
|
+
return new StreamTextPromise(doStreamText(model, prompt, options));
|
42
|
+
}
|
43
|
+
async function doStreamText(model, prompt, options) {
|
8
44
|
if (options?.settings != null) {
|
9
45
|
model = model.withSettings(options.settings);
|
10
46
|
options = {
|
@@ -104,10 +140,8 @@ export async function streamText(model, prompt, options) {
|
|
104
140
|
});
|
105
141
|
throw result.error;
|
106
142
|
}
|
107
|
-
return
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
}
|
112
|
-
: result.output;
|
143
|
+
return {
|
144
|
+
output: result.output,
|
145
|
+
metadata: startMetadata,
|
146
|
+
};
|
113
147
|
}
|
@@ -2,8 +2,11 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.synthesizeSpeech = void 0;
|
4
4
|
const executeCall_js_1 = require("../executeCall.cjs");
|
5
|
-
|
6
|
-
|
5
|
+
/**
|
6
|
+
* Synthesizes speech from text.
|
7
|
+
*/
|
8
|
+
function synthesizeSpeech(model, text, options) {
|
9
|
+
return (0, executeCall_js_1.executeCall)({
|
7
10
|
model,
|
8
11
|
options,
|
9
12
|
generateResponse: (options) => model.generateSpeechResponse(text, options),
|
@@ -39,11 +42,5 @@ async function synthesizeSpeech(model, text, options) {
|
|
39
42
|
speech: output,
|
40
43
|
}),
|
41
44
|
});
|
42
|
-
return options?.fullResponse === true
|
43
|
-
? {
|
44
|
-
speech: result.output,
|
45
|
-
metadata: result.metadata,
|
46
|
-
}
|
47
|
-
: result.output;
|
48
45
|
}
|
49
46
|
exports.synthesizeSpeech = synthesizeSpeech;
|
@@ -1,16 +1,8 @@
|
|
1
1
|
/// <reference types="node" resolution-mode="require"/>
|
2
2
|
import { FunctionOptions } from "../FunctionOptions.js";
|
3
|
-
import {
|
3
|
+
import { ModelFunctionPromise } from "../executeCall.js";
|
4
4
|
import { SpeechSynthesisModel, SpeechSynthesisModelSettings } from "./SpeechSynthesisModel.js";
|
5
5
|
/**
|
6
6
|
* Synthesizes speech from text.
|
7
7
|
*/
|
8
|
-
export declare function synthesizeSpeech<SETTINGS extends SpeechSynthesisModelSettings>(model: SpeechSynthesisModel<SETTINGS>, text: string, options
|
9
|
-
fullResponse: true;
|
10
|
-
}): Promise<{
|
11
|
-
speech: Buffer;
|
12
|
-
metadata: CallMetadata<SpeechSynthesisModel<SETTINGS>>;
|
13
|
-
}>;
|
14
|
-
export declare function synthesizeSpeech<SETTINGS extends SpeechSynthesisModelSettings>(model: SpeechSynthesisModel<SETTINGS>, text: string, options?: FunctionOptions<SETTINGS> & {
|
15
|
-
fullResponse?: false;
|
16
|
-
}): Promise<Buffer>;
|
8
|
+
export declare function synthesizeSpeech<SETTINGS extends SpeechSynthesisModelSettings>(model: SpeechSynthesisModel<SETTINGS>, text: string, options?: FunctionOptions<SETTINGS>): ModelFunctionPromise<SpeechSynthesisModel<SETTINGS>, Buffer, Buffer>;
|
@@ -1,6 +1,9 @@
|
|
1
1
|
import { executeCall } from "../executeCall.js";
|
2
|
-
|
3
|
-
|
2
|
+
/**
|
3
|
+
* Synthesizes speech from text.
|
4
|
+
*/
|
5
|
+
export function synthesizeSpeech(model, text, options) {
|
6
|
+
return executeCall({
|
4
7
|
model,
|
5
8
|
options,
|
6
9
|
generateResponse: (options) => model.generateSpeechResponse(text, options),
|
@@ -36,10 +39,4 @@ export async function synthesizeSpeech(model, text, options) {
|
|
36
39
|
speech: output,
|
37
40
|
}),
|
38
41
|
});
|
39
|
-
return options?.fullResponse === true
|
40
|
-
? {
|
41
|
-
speech: result.output,
|
42
|
-
metadata: result.metadata,
|
43
|
-
}
|
44
|
-
: result.output;
|
45
42
|
}
|
@@ -2,8 +2,22 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.transcribe = void 0;
|
4
4
|
const executeCall_js_1 = require("../executeCall.cjs");
|
5
|
-
|
6
|
-
|
5
|
+
/**
|
6
|
+
* Transcribe audio data into text.
|
7
|
+
*
|
8
|
+
* @example
|
9
|
+
* const data = await fs.promises.readFile("data/test.mp3");
|
10
|
+
*
|
11
|
+
* const transcription = await transcribe(
|
12
|
+
* new OpenAITranscriptionModel({ model: "whisper-1" }),
|
13
|
+
* {
|
14
|
+
* type: "mp3",
|
15
|
+
* data,
|
16
|
+
* }
|
17
|
+
* );
|
18
|
+
*/
|
19
|
+
function transcribe(model, data, options) {
|
20
|
+
return (0, executeCall_js_1.executeCall)({
|
7
21
|
model,
|
8
22
|
options,
|
9
23
|
generateResponse: (options) => model.generateTranscriptionResponse(data, options),
|
@@ -39,12 +53,5 @@ async function transcribe(model, data, options) {
|
|
39
53
|
transcription: output,
|
40
54
|
}),
|
41
55
|
});
|
42
|
-
return options?.fullResponse === true
|
43
|
-
? {
|
44
|
-
transcription: result.output,
|
45
|
-
response: result.response,
|
46
|
-
metadata: result.metadata,
|
47
|
-
}
|
48
|
-
: result.output;
|
49
56
|
}
|
50
57
|
exports.transcribe = transcribe;
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import { FunctionOptions } from "../FunctionOptions.js";
|
2
|
-
import {
|
2
|
+
import { ModelFunctionPromise } from "../executeCall.js";
|
3
3
|
import { TranscriptionModel, TranscriptionModelSettings } from "./TranscriptionModel.js";
|
4
4
|
/**
|
5
5
|
* Transcribe audio data into text.
|
@@ -15,13 +15,4 @@ import { TranscriptionModel, TranscriptionModelSettings } from "./TranscriptionM
|
|
15
15
|
* }
|
16
16
|
* );
|
17
17
|
*/
|
18
|
-
export declare function transcribe<DATA, RESPONSE, SETTINGS extends TranscriptionModelSettings>(model: TranscriptionModel<DATA, RESPONSE, SETTINGS>, data: DATA, options
|
19
|
-
fullResponse: true;
|
20
|
-
}): Promise<{
|
21
|
-
transcription: string;
|
22
|
-
response: RESPONSE;
|
23
|
-
metadata: CallMetadata<TranscriptionModel<DATA, RESPONSE, SETTINGS>>;
|
24
|
-
}>;
|
25
|
-
export declare function transcribe<DATA, RESPONSE, SETTINGS extends TranscriptionModelSettings>(model: TranscriptionModel<DATA, RESPONSE, SETTINGS>, data: DATA, options?: FunctionOptions<SETTINGS> & {
|
26
|
-
fullResponse?: false;
|
27
|
-
}): Promise<string>;
|
18
|
+
export declare function transcribe<DATA, RESPONSE, SETTINGS extends TranscriptionModelSettings>(model: TranscriptionModel<DATA, RESPONSE, SETTINGS>, data: DATA, options?: FunctionOptions<SETTINGS>): ModelFunctionPromise<TranscriptionModel<DATA, RESPONSE, SETTINGS>, string, RESPONSE>;
|
@@ -1,6 +1,20 @@
|
|
1
1
|
import { executeCall } from "../executeCall.js";
|
2
|
-
|
3
|
-
|
2
|
+
/**
|
3
|
+
* Transcribe audio data into text.
|
4
|
+
*
|
5
|
+
* @example
|
6
|
+
* const data = await fs.promises.readFile("data/test.mp3");
|
7
|
+
*
|
8
|
+
* const transcription = await transcribe(
|
9
|
+
* new OpenAITranscriptionModel({ model: "whisper-1" }),
|
10
|
+
* {
|
11
|
+
* type: "mp3",
|
12
|
+
* data,
|
13
|
+
* }
|
14
|
+
* );
|
15
|
+
*/
|
16
|
+
export function transcribe(model, data, options) {
|
17
|
+
return executeCall({
|
4
18
|
model,
|
5
19
|
options,
|
6
20
|
generateResponse: (options) => model.generateTranscriptionResponse(data, options),
|
@@ -36,11 +50,4 @@ export async function transcribe(model, data, options) {
|
|
36
50
|
transcription: output,
|
37
51
|
}),
|
38
52
|
});
|
39
|
-
return options?.fullResponse === true
|
40
|
-
? {
|
41
|
-
transcription: result.output,
|
42
|
-
response: result.response,
|
43
|
-
metadata: result.metadata,
|
44
|
-
}
|
45
|
-
: result.output;
|
46
53
|
}
|
@@ -64,13 +64,13 @@ export declare class CohereTextEmbeddingModel extends AbstractModel<CohereTextEm
|
|
64
64
|
callAPI(texts: Array<string>, options?: FunctionOptions<CohereTextEmbeddingModelSettings>): Promise<CohereTextEmbeddingResponse>;
|
65
65
|
generateEmbeddingResponse(texts: string[], options?: FunctionOptions<CohereTextEmbeddingModelSettings>): Promise<{
|
66
66
|
texts: string[];
|
67
|
-
embeddings: number[][];
|
68
67
|
id: string;
|
69
68
|
meta: {
|
70
69
|
api_version: {
|
71
70
|
version: string;
|
72
71
|
};
|
73
72
|
};
|
73
|
+
embeddings: number[][];
|
74
74
|
}>;
|
75
75
|
extractEmbeddings(response: CohereTextEmbeddingResponse): number[][];
|
76
76
|
withSettings(additionalSettings: Partial<CohereTextEmbeddingModelSettings>): this;
|
@@ -98,22 +98,22 @@ declare const cohereTextEmbeddingResponseSchema: z.ZodObject<{
|
|
98
98
|
}>;
|
99
99
|
}, "strip", z.ZodTypeAny, {
|
100
100
|
texts: string[];
|
101
|
-
embeddings: number[][];
|
102
101
|
id: string;
|
103
102
|
meta: {
|
104
103
|
api_version: {
|
105
104
|
version: string;
|
106
105
|
};
|
107
106
|
};
|
107
|
+
embeddings: number[][];
|
108
108
|
}, {
|
109
109
|
texts: string[];
|
110
|
-
embeddings: number[][];
|
111
110
|
id: string;
|
112
111
|
meta: {
|
113
112
|
api_version: {
|
114
113
|
version: string;
|
115
114
|
};
|
116
115
|
};
|
116
|
+
embeddings: number[][];
|
117
117
|
}>;
|
118
118
|
export type CohereTextEmbeddingResponse = z.infer<typeof cohereTextEmbeddingResponseSchema>;
|
119
119
|
export {};
|
@@ -20,6 +20,14 @@ const TikTokenTokenizer_js_1 = require("./TikTokenTokenizer.cjs");
|
|
20
20
|
* @see https://openai.com/pricing
|
21
21
|
*/
|
22
22
|
exports.OPENAI_TEXT_GENERATION_MODELS = {
|
23
|
+
"davinci-002": {
|
24
|
+
contextWindowSize: 16384,
|
25
|
+
tokenCostInMillicents: 0.2,
|
26
|
+
},
|
27
|
+
"babbage-002": {
|
28
|
+
contextWindowSize: 16384,
|
29
|
+
tokenCostInMillicents: 0.04,
|
30
|
+
},
|
23
31
|
"text-davinci-003": {
|
24
32
|
contextWindowSize: 4096,
|
25
33
|
tokenCostInMillicents: 2,
|
@@ -16,6 +16,14 @@ import { TikTokenTokenizer } from "./TikTokenTokenizer.js";
|
|
16
16
|
* @see https://openai.com/pricing
|
17
17
|
*/
|
18
18
|
export declare const OPENAI_TEXT_GENERATION_MODELS: {
|
19
|
+
"davinci-002": {
|
20
|
+
contextWindowSize: number;
|
21
|
+
tokenCostInMillicents: number;
|
22
|
+
};
|
23
|
+
"babbage-002": {
|
24
|
+
contextWindowSize: number;
|
25
|
+
tokenCostInMillicents: number;
|
26
|
+
};
|
19
27
|
"text-davinci-003": {
|
20
28
|
contextWindowSize: number;
|
21
29
|
tokenCostInMillicents: number;
|
@@ -58,7 +66,7 @@ export declare const OPENAI_TEXT_GENERATION_MODELS: {
|
|
58
66
|
};
|
59
67
|
};
|
60
68
|
export type OpenAITextGenerationModelType = keyof typeof OPENAI_TEXT_GENERATION_MODELS;
|
61
|
-
export declare const isOpenAITextGenerationModel: (model: string) => model is "text-davinci-003" | "text-davinci-002" | "code-davinci-002" | "davinci" | "text-curie-001" | "curie" | "text-babbage-001" | "babbage" | "text-ada-001" | "ada";
|
69
|
+
export declare const isOpenAITextGenerationModel: (model: string) => model is "davinci-002" | "babbage-002" | "text-davinci-003" | "text-davinci-002" | "code-davinci-002" | "davinci" | "text-curie-001" | "curie" | "text-babbage-001" | "babbage" | "text-ada-001" | "ada";
|
62
70
|
export declare const calculateOpenAITextGenerationCostInMillicents: ({ model, response, }: {
|
63
71
|
model: OpenAITextGenerationModelType;
|
64
72
|
response: OpenAITextGenerationResponse;
|
@@ -102,7 +110,7 @@ export interface OpenAITextGenerationModelSettings extends TextGenerationModelSe
|
|
102
110
|
export declare class OpenAITextGenerationModel extends AbstractModel<OpenAITextGenerationModelSettings> implements TextGenerationModel<string, OpenAITextGenerationResponse, OpenAITextGenerationDelta, OpenAITextGenerationModelSettings> {
|
103
111
|
constructor(settings: OpenAITextGenerationModelSettings);
|
104
112
|
readonly provider: "openai";
|
105
|
-
get modelName(): "text-davinci-003" | "text-davinci-002" | "code-davinci-002" | "davinci" | "text-curie-001" | "curie" | "text-babbage-001" | "babbage" | "text-ada-001" | "ada";
|
113
|
+
get modelName(): "davinci-002" | "babbage-002" | "text-davinci-003" | "text-davinci-002" | "code-davinci-002" | "davinci" | "text-curie-001" | "curie" | "text-babbage-001" | "babbage" | "text-ada-001" | "ada";
|
106
114
|
readonly contextWindowSize: number;
|
107
115
|
readonly tokenizer: TikTokenTokenizer;
|
108
116
|
private get apiKey();
|
@@ -14,6 +14,14 @@ import { TikTokenTokenizer } from "./TikTokenTokenizer.js";
|
|
14
14
|
* @see https://openai.com/pricing
|
15
15
|
*/
|
16
16
|
export const OPENAI_TEXT_GENERATION_MODELS = {
|
17
|
+
"davinci-002": {
|
18
|
+
contextWindowSize: 16384,
|
19
|
+
tokenCostInMillicents: 0.2,
|
20
|
+
},
|
21
|
+
"babbage-002": {
|
22
|
+
contextWindowSize: 16384,
|
23
|
+
tokenCostInMillicents: 0.04,
|
24
|
+
},
|
17
25
|
"text-davinci-003": {
|
18
26
|
contextWindowSize: 4096,
|
19
27
|
tokenCostInMillicents: 2,
|
@@ -57,13 +57,17 @@ function getEncodingNameForModel(model) {
|
|
57
57
|
case "text-davinci-003": {
|
58
58
|
return "p50k_base";
|
59
59
|
}
|
60
|
+
case "babbage-002":
|
61
|
+
case "davinci-002":
|
60
62
|
case "ada":
|
61
63
|
case "babbage":
|
62
64
|
case "curie":
|
63
65
|
case "davinci":
|
64
66
|
case "text-ada-001":
|
65
67
|
case "text-babbage-001":
|
66
|
-
case "text-curie-001":
|
68
|
+
case "text-curie-001": {
|
69
|
+
return "r50k_base";
|
70
|
+
}
|
67
71
|
case "gpt-3.5-turbo":
|
68
72
|
case "gpt-3.5-turbo-0301":
|
69
73
|
case "gpt-3.5-turbo-0613":
|
@@ -53,13 +53,17 @@ function getEncodingNameForModel(model) {
|
|
53
53
|
case "text-davinci-003": {
|
54
54
|
return "p50k_base";
|
55
55
|
}
|
56
|
+
case "babbage-002":
|
57
|
+
case "davinci-002":
|
56
58
|
case "ada":
|
57
59
|
case "babbage":
|
58
60
|
case "curie":
|
59
61
|
case "davinci":
|
60
62
|
case "text-ada-001":
|
61
63
|
case "text-babbage-001":
|
62
|
-
case "text-curie-001":
|
64
|
+
case "text-curie-001": {
|
65
|
+
return "r50k_base";
|
66
|
+
}
|
63
67
|
case "gpt-3.5-turbo":
|
64
68
|
case "gpt-3.5-turbo-0301":
|
65
69
|
case "gpt-3.5-turbo-0613":
|
package/package.json
CHANGED
package/tool/executeTool.cjs
CHANGED
@@ -1,14 +1,56 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.executeTool = void 0;
|
3
|
+
exports.executeTool = exports.ExecuteToolPromise = void 0;
|
4
4
|
const nanoid_1 = require("nanoid");
|
5
5
|
const RunFunctionEventSource_js_1 = require("../run/RunFunctionEventSource.cjs");
|
6
6
|
const DurationMeasurement_js_1 = require("../util/DurationMeasurement.cjs");
|
7
7
|
const AbortError_js_1 = require("../util/api/AbortError.cjs");
|
8
8
|
const runSafe_js_1 = require("../util/runSafe.cjs");
|
9
9
|
const ToolExecutionError_js_1 = require("./ToolExecutionError.cjs");
|
10
|
+
class ExecuteToolPromise extends Promise {
|
11
|
+
constructor(fullPromise) {
|
12
|
+
super((resolve) => {
|
13
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
14
|
+
resolve(null); // we override the resolve function
|
15
|
+
});
|
16
|
+
Object.defineProperty(this, "fullPromise", {
|
17
|
+
enumerable: true,
|
18
|
+
configurable: true,
|
19
|
+
writable: true,
|
20
|
+
value: fullPromise
|
21
|
+
});
|
22
|
+
Object.defineProperty(this, "outputPromise", {
|
23
|
+
enumerable: true,
|
24
|
+
configurable: true,
|
25
|
+
writable: true,
|
26
|
+
value: void 0
|
27
|
+
});
|
28
|
+
this.outputPromise = fullPromise.then((result) => result.output);
|
29
|
+
}
|
30
|
+
asFullResponse() {
|
31
|
+
return this.fullPromise;
|
32
|
+
}
|
33
|
+
then(onfulfilled, onrejected) {
|
34
|
+
return this.outputPromise.then(onfulfilled, onrejected);
|
35
|
+
}
|
36
|
+
catch(onrejected) {
|
37
|
+
return this.outputPromise.catch(onrejected);
|
38
|
+
}
|
39
|
+
finally(onfinally) {
|
40
|
+
return this.outputPromise.finally(onfinally);
|
41
|
+
}
|
42
|
+
}
|
43
|
+
exports.ExecuteToolPromise = ExecuteToolPromise;
|
44
|
+
/**
|
45
|
+
* `executeTool` directly executes a tool with the given parameters.
|
46
|
+
*/
|
10
47
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
11
|
-
|
48
|
+
function executeTool(tool, input, options) {
|
49
|
+
return new ExecuteToolPromise(doExecuteTool(tool, input, options));
|
50
|
+
}
|
51
|
+
exports.executeTool = executeTool;
|
52
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
53
|
+
async function doExecuteTool(tool, input, options) {
|
12
54
|
const run = options?.run;
|
13
55
|
const eventSource = new RunFunctionEventSource_js_1.RunFunctionEventSource({
|
14
56
|
observers: run?.observers ?? [],
|
@@ -70,11 +112,8 @@ async function executeTool(tool, input, options) {
|
|
70
112
|
input,
|
71
113
|
output,
|
72
114
|
});
|
73
|
-
return
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
}
|
78
|
-
: output;
|
115
|
+
return {
|
116
|
+
output,
|
117
|
+
metadata: finishMetadata,
|
118
|
+
};
|
79
119
|
}
|
80
|
-
exports.executeTool = executeTool;
|