modelfusion 0.57.2 → 0.58.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 -3
- package/core/api/retryWithExponentialBackoff.cjs +5 -4
- package/core/api/retryWithExponentialBackoff.js +5 -4
- package/model-function/embed/embed.cjs +8 -44
- package/model-function/embed/embed.d.ts +23 -5
- package/model-function/embed/embed.js +8 -44
- package/model-function/generate-image/generateImage.cjs +12 -28
- package/model-function/generate-image/generateImage.d.ts +16 -3
- package/model-function/generate-image/generateImage.js +12 -28
- package/model-function/generate-speech/generateSpeech.cjs +4 -22
- package/model-function/generate-speech/generateSpeech.d.ts +12 -3
- package/model-function/generate-speech/generateSpeech.js +4 -22
- package/model-function/generate-speech/streamSpeech.cjs +5 -31
- package/model-function/generate-speech/streamSpeech.d.ts +11 -3
- package/model-function/generate-speech/streamSpeech.js +5 -31
- package/model-function/generate-structure/StructureFromTextGenerationModel.cjs +4 -1
- package/model-function/generate-structure/StructureFromTextGenerationModel.js +4 -1
- package/model-function/generate-structure/generateStructure.cjs +4 -45
- package/model-function/generate-structure/generateStructure.d.ts +12 -3
- package/model-function/generate-structure/generateStructure.js +4 -45
- package/model-function/generate-structure/generateStructureOrText.cjs +4 -66
- package/model-function/generate-structure/generateStructureOrText.d.ts +16 -3
- package/model-function/generate-structure/generateStructureOrText.js +4 -66
- package/model-function/generate-structure/streamStructure.cjs +4 -71
- package/model-function/generate-structure/streamStructure.d.ts +10 -2
- package/model-function/generate-structure/streamStructure.js +4 -71
- package/model-function/generate-text/generateText.cjs +5 -28
- package/model-function/generate-text/generateText.d.ts +12 -3
- package/model-function/generate-text/generateText.js +5 -28
- package/model-function/generate-text/streamText.cjs +4 -29
- package/model-function/generate-text/streamText.d.ts +10 -2
- package/model-function/generate-text/streamText.js +4 -29
- package/model-function/generate-transcription/generateTranscription.cjs +4 -23
- package/model-function/generate-transcription/generateTranscription.d.ts +12 -3
- package/model-function/generate-transcription/generateTranscription.js +4 -23
- package/model-provider/ollama/OllamaTextGenerationModel.cjs +18 -2
- package/model-provider/ollama/OllamaTextGenerationModel.js +18 -2
- package/package.json +1 -1
- package/tool/executeTool.cjs +5 -41
- package/tool/executeTool.d.ts +11 -16
- package/tool/executeTool.js +4 -39
- package/tool/useTool.cjs +4 -1
- package/tool/useTool.js +4 -1
- package/tool/useToolOrGenerateText.cjs +4 -1
- package/tool/useToolOrGenerateText.js +4 -1
- package/util/runSafe.test.cjs +10 -1
- package/util/runSafe.test.js +10 -1
- package/model-function/AsyncIterableResultPromise.cjs +0 -37
- package/model-function/AsyncIterableResultPromise.d.ts +0 -16
- package/model-function/AsyncIterableResultPromise.js +0 -33
- package/model-function/ModelFunctionPromise.cjs +0 -37
- package/model-function/ModelFunctionPromise.d.ts +0 -18
- package/model-function/ModelFunctionPromise.js +0 -33
- package/model-function/generate-image/ImageGenerationPromise.cjs +0 -50
- package/model-function/generate-image/ImageGenerationPromise.d.ts +0 -22
- package/model-function/generate-image/ImageGenerationPromise.js +0 -46
package/README.md
CHANGED
@@ -513,7 +513,7 @@ const image = await generateImage(
|
|
513
513
|
|
514
514
|
### Metadata and original responses
|
515
515
|
|
516
|
-
ModelFusion model functions return rich results that include the original response and metadata when you
|
516
|
+
ModelFusion model functions return rich results that include the original response and metadata when you set the `returnType` option to `full`.
|
517
517
|
|
518
518
|
```ts
|
519
519
|
// access the full response (needs to be typed) and the metadata:
|
@@ -523,8 +523,9 @@ const { value, response, metadata } = await generateText(
|
|
523
523
|
maxCompletionTokens: 1000,
|
524
524
|
n: 2, // generate 2 completions
|
525
525
|
}),
|
526
|
-
"Write a short story about a robot learning to love:\n\n"
|
527
|
-
|
526
|
+
"Write a short story about a robot learning to love:\n\n",
|
527
|
+
{ returnType: "full" }
|
528
|
+
);
|
528
529
|
|
529
530
|
console.log(metadata);
|
530
531
|
|
@@ -1,6 +1,7 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.retryWithExponentialBackoff = void 0;
|
4
|
+
const delay_js_1 = require("../../util/delay.cjs");
|
4
5
|
const ApiCallError_js_1 = require("./ApiCallError.cjs");
|
5
6
|
const RetryError_js_1 = require("./RetryError.cjs");
|
6
7
|
/**
|
@@ -9,11 +10,11 @@ const RetryError_js_1 = require("./RetryError.cjs");
|
|
9
10
|
*/
|
10
11
|
const retryWithExponentialBackoff = ({ maxTries = 3, initialDelayInMs = 2000, backoffFactor = 2, } = {}) => async (f) => _retryWithExponentialBackoff(f, {
|
11
12
|
maxTries,
|
12
|
-
|
13
|
+
delayInMs: initialDelayInMs,
|
13
14
|
backoffFactor,
|
14
15
|
});
|
15
16
|
exports.retryWithExponentialBackoff = retryWithExponentialBackoff;
|
16
|
-
async function _retryWithExponentialBackoff(f, { maxTries,
|
17
|
+
async function _retryWithExponentialBackoff(f, { maxTries, delayInMs, backoffFactor, }, errors = []) {
|
17
18
|
try {
|
18
19
|
return await f();
|
19
20
|
}
|
@@ -34,8 +35,8 @@ async function _retryWithExponentialBackoff(f, { maxTries, delay, backoffFactor,
|
|
34
35
|
if (error instanceof ApiCallError_js_1.ApiCallError &&
|
35
36
|
error.isRetryable &&
|
36
37
|
tryNumber < maxTries) {
|
37
|
-
await
|
38
|
-
return _retryWithExponentialBackoff(f, { maxTries,
|
38
|
+
await (0, delay_js_1.delay)(delayInMs);
|
39
|
+
return _retryWithExponentialBackoff(f, { maxTries, delayInMs: backoffFactor * delayInMs, backoffFactor }, newErrors);
|
39
40
|
}
|
40
41
|
}
|
41
42
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
@@ -1,3 +1,4 @@
|
|
1
|
+
import { delay } from "../../util/delay.js";
|
1
2
|
import { ApiCallError } from "./ApiCallError.js";
|
2
3
|
import { RetryError } from "./RetryError.js";
|
3
4
|
/**
|
@@ -6,10 +7,10 @@ import { RetryError } from "./RetryError.js";
|
|
6
7
|
*/
|
7
8
|
export const retryWithExponentialBackoff = ({ maxTries = 3, initialDelayInMs = 2000, backoffFactor = 2, } = {}) => async (f) => _retryWithExponentialBackoff(f, {
|
8
9
|
maxTries,
|
9
|
-
|
10
|
+
delayInMs: initialDelayInMs,
|
10
11
|
backoffFactor,
|
11
12
|
});
|
12
|
-
async function _retryWithExponentialBackoff(f, { maxTries,
|
13
|
+
async function _retryWithExponentialBackoff(f, { maxTries, delayInMs, backoffFactor, }, errors = []) {
|
13
14
|
try {
|
14
15
|
return await f();
|
15
16
|
}
|
@@ -30,8 +31,8 @@ async function _retryWithExponentialBackoff(f, { maxTries, delay, backoffFactor,
|
|
30
31
|
if (error instanceof ApiCallError &&
|
31
32
|
error.isRetryable &&
|
32
33
|
tryNumber < maxTries) {
|
33
|
-
await
|
34
|
-
return _retryWithExponentialBackoff(f, { maxTries,
|
34
|
+
await delay(delayInMs);
|
35
|
+
return _retryWithExponentialBackoff(f, { maxTries, delayInMs: backoffFactor * delayInMs, backoffFactor }, newErrors);
|
35
36
|
}
|
36
37
|
}
|
37
38
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
@@ -2,29 +2,8 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.embed = exports.embedMany = void 0;
|
4
4
|
const executeStandardCall_js_1 = require("../executeStandardCall.cjs");
|
5
|
-
|
6
|
-
|
7
|
-
* Generate embeddings for multiple values.
|
8
|
-
*
|
9
|
-
* @see https://modelfusion.dev/guide/function/embed
|
10
|
-
*
|
11
|
-
* @example
|
12
|
-
* const embeddings = await embedMany(
|
13
|
-
* new OpenAITextEmbeddingModel(...),
|
14
|
-
* [
|
15
|
-
* "At first, Nox didn't know what to do with the pup.",
|
16
|
-
* "He keenly observed and absorbed everything around him, from the birds in the sky to the trees in the forest.",
|
17
|
-
* ]
|
18
|
-
* );
|
19
|
-
*
|
20
|
-
* @param {EmbeddingModel<VALUE, EmbeddingModelSettings>} model - The model to use for generating embeddings.
|
21
|
-
* @param {VALUE[]} values - The values to generate embeddings for.
|
22
|
-
* @param {FunctionOptions} [options] - Optional settings for the function.
|
23
|
-
*
|
24
|
-
* @returns {ModelFunctionPromise<Vector[]>} - A promise that resolves to an array of vectors representing the embeddings.
|
25
|
-
*/
|
26
|
-
function embedMany(model, values, options) {
|
27
|
-
return new ModelFunctionPromise_js_1.ModelFunctionPromise((0, executeStandardCall_js_1.executeStandardCall)({
|
5
|
+
async function embedMany(model, values, options) {
|
6
|
+
const fullResponse = await (0, executeStandardCall_js_1.executeStandardCall)({
|
28
7
|
functionType: "embed",
|
29
8
|
input: values,
|
30
9
|
model,
|
@@ -63,28 +42,12 @@ function embedMany(model, values, options) {
|
|
63
42
|
extractedValue: embeddings,
|
64
43
|
};
|
65
44
|
},
|
66
|
-
})
|
45
|
+
});
|
46
|
+
return options?.returnType === "full" ? fullResponse : fullResponse.value;
|
67
47
|
}
|
68
48
|
exports.embedMany = embedMany;
|
69
|
-
|
70
|
-
|
71
|
-
*
|
72
|
-
* @see https://modelfusion.dev/guide/function/embed
|
73
|
-
*
|
74
|
-
* @example
|
75
|
-
* const embedding = await embed(
|
76
|
-
* new OpenAITextEmbeddingModel(...),
|
77
|
-
* "At first, Nox didn't know what to do with the pup."
|
78
|
-
* );
|
79
|
-
*
|
80
|
-
* @param {EmbeddingModel<VALUE, EmbeddingModelSettings>} model - The model to use for generating the embedding.
|
81
|
-
* @param {VALUE} value - The value to generate an embedding for.
|
82
|
-
* @param {FunctionOptions} [options] - Optional settings for the function.
|
83
|
-
*
|
84
|
-
* @returns {ModelFunctionPromise<Vector>} - A promise that resolves to a vector representing the embedding.
|
85
|
-
*/
|
86
|
-
function embed(model, value, options) {
|
87
|
-
return new ModelFunctionPromise_js_1.ModelFunctionPromise((0, executeStandardCall_js_1.executeStandardCall)({
|
49
|
+
async function embed(model, value, options) {
|
50
|
+
const fullResponse = await (0, executeStandardCall_js_1.executeStandardCall)({
|
88
51
|
functionType: "embed",
|
89
52
|
input: value,
|
90
53
|
model,
|
@@ -96,6 +59,7 @@ function embed(model, value, options) {
|
|
96
59
|
extractedValue: result.embeddings[0],
|
97
60
|
};
|
98
61
|
},
|
99
|
-
})
|
62
|
+
});
|
63
|
+
return options?.returnType === "full" ? fullResponse : fullResponse.value;
|
100
64
|
}
|
101
65
|
exports.embed = embed;
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
2
2
|
import { Vector } from "../../core/Vector.js";
|
3
|
-
import {
|
3
|
+
import { ModelCallMetadata } from "../ModelCallMetadata.js";
|
4
4
|
import { EmbeddingModel, EmbeddingModelSettings } from "./EmbeddingModel.js";
|
5
5
|
/**
|
6
6
|
* Generate embeddings for multiple values.
|
@@ -20,9 +20,18 @@ import { EmbeddingModel, EmbeddingModelSettings } from "./EmbeddingModel.js";
|
|
20
20
|
* @param {VALUE[]} values - The values to generate embeddings for.
|
21
21
|
* @param {FunctionOptions} [options] - Optional settings for the function.
|
22
22
|
*
|
23
|
-
* @returns {
|
23
|
+
* @returns {Promise<Vector[]>} - A promise that resolves to an array of vectors representing the embeddings.
|
24
24
|
*/
|
25
|
-
export declare function embedMany<VALUE>(model: EmbeddingModel<VALUE, EmbeddingModelSettings>, values: VALUE[], options?: FunctionOptions
|
25
|
+
export declare function embedMany<VALUE>(model: EmbeddingModel<VALUE, EmbeddingModelSettings>, values: VALUE[], options?: FunctionOptions & {
|
26
|
+
returnType?: "vectors";
|
27
|
+
}): Promise<Vector[]>;
|
28
|
+
export declare function embedMany<VALUE>(model: EmbeddingModel<VALUE, EmbeddingModelSettings>, values: VALUE[], options: FunctionOptions & {
|
29
|
+
returnType: "full";
|
30
|
+
}): Promise<{
|
31
|
+
value: Vector[];
|
32
|
+
response: unknown;
|
33
|
+
metadata: ModelCallMetadata;
|
34
|
+
}>;
|
26
35
|
/**
|
27
36
|
* Generate an embedding for a single value.
|
28
37
|
*
|
@@ -38,6 +47,15 @@ export declare function embedMany<VALUE>(model: EmbeddingModel<VALUE, EmbeddingM
|
|
38
47
|
* @param {VALUE} value - The value to generate an embedding for.
|
39
48
|
* @param {FunctionOptions} [options] - Optional settings for the function.
|
40
49
|
*
|
41
|
-
* @returns {
|
50
|
+
* @returns {Promise<Vector>} - A promise that resolves to a vector representing the embedding.
|
42
51
|
*/
|
43
|
-
export declare function embed<VALUE>(model: EmbeddingModel<VALUE, EmbeddingModelSettings>, value: VALUE, options?: FunctionOptions
|
52
|
+
export declare function embed<VALUE>(model: EmbeddingModel<VALUE, EmbeddingModelSettings>, value: VALUE, options?: FunctionOptions & {
|
53
|
+
returnType?: "vector";
|
54
|
+
}): Promise<Vector>;
|
55
|
+
export declare function embed<VALUE>(model: EmbeddingModel<VALUE, EmbeddingModelSettings>, value: VALUE, options: FunctionOptions & {
|
56
|
+
returnType: "full";
|
57
|
+
}): Promise<{
|
58
|
+
value: Vector;
|
59
|
+
response: unknown;
|
60
|
+
metadata: ModelCallMetadata;
|
61
|
+
}>;
|
@@ -1,27 +1,6 @@
|
|
1
1
|
import { executeStandardCall } from "../executeStandardCall.js";
|
2
|
-
|
3
|
-
|
4
|
-
* Generate embeddings for multiple values.
|
5
|
-
*
|
6
|
-
* @see https://modelfusion.dev/guide/function/embed
|
7
|
-
*
|
8
|
-
* @example
|
9
|
-
* const embeddings = await embedMany(
|
10
|
-
* new OpenAITextEmbeddingModel(...),
|
11
|
-
* [
|
12
|
-
* "At first, Nox didn't know what to do with the pup.",
|
13
|
-
* "He keenly observed and absorbed everything around him, from the birds in the sky to the trees in the forest.",
|
14
|
-
* ]
|
15
|
-
* );
|
16
|
-
*
|
17
|
-
* @param {EmbeddingModel<VALUE, EmbeddingModelSettings>} model - The model to use for generating embeddings.
|
18
|
-
* @param {VALUE[]} values - The values to generate embeddings for.
|
19
|
-
* @param {FunctionOptions} [options] - Optional settings for the function.
|
20
|
-
*
|
21
|
-
* @returns {ModelFunctionPromise<Vector[]>} - A promise that resolves to an array of vectors representing the embeddings.
|
22
|
-
*/
|
23
|
-
export function embedMany(model, values, options) {
|
24
|
-
return new ModelFunctionPromise(executeStandardCall({
|
2
|
+
export async function embedMany(model, values, options) {
|
3
|
+
const fullResponse = await executeStandardCall({
|
25
4
|
functionType: "embed",
|
26
5
|
input: values,
|
27
6
|
model,
|
@@ -60,27 +39,11 @@ export function embedMany(model, values, options) {
|
|
60
39
|
extractedValue: embeddings,
|
61
40
|
};
|
62
41
|
},
|
63
|
-
})
|
42
|
+
});
|
43
|
+
return options?.returnType === "full" ? fullResponse : fullResponse.value;
|
64
44
|
}
|
65
|
-
|
66
|
-
|
67
|
-
*
|
68
|
-
* @see https://modelfusion.dev/guide/function/embed
|
69
|
-
*
|
70
|
-
* @example
|
71
|
-
* const embedding = await embed(
|
72
|
-
* new OpenAITextEmbeddingModel(...),
|
73
|
-
* "At first, Nox didn't know what to do with the pup."
|
74
|
-
* );
|
75
|
-
*
|
76
|
-
* @param {EmbeddingModel<VALUE, EmbeddingModelSettings>} model - The model to use for generating the embedding.
|
77
|
-
* @param {VALUE} value - The value to generate an embedding for.
|
78
|
-
* @param {FunctionOptions} [options] - Optional settings for the function.
|
79
|
-
*
|
80
|
-
* @returns {ModelFunctionPromise<Vector>} - A promise that resolves to a vector representing the embedding.
|
81
|
-
*/
|
82
|
-
export function embed(model, value, options) {
|
83
|
-
return new ModelFunctionPromise(executeStandardCall({
|
45
|
+
export async function embed(model, value, options) {
|
46
|
+
const fullResponse = await executeStandardCall({
|
84
47
|
functionType: "embed",
|
85
48
|
input: value,
|
86
49
|
model,
|
@@ -92,5 +55,6 @@ export function embed(model, value, options) {
|
|
92
55
|
extractedValue: result.embeddings[0],
|
93
56
|
};
|
94
57
|
},
|
95
|
-
})
|
58
|
+
});
|
59
|
+
return options?.returnType === "full" ? fullResponse : fullResponse.value;
|
96
60
|
}
|
@@ -2,33 +2,8 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.generateImage = void 0;
|
4
4
|
const executeStandardCall_js_1 = require("../executeStandardCall.cjs");
|
5
|
-
|
6
|
-
|
7
|
-
* Generates an image using a prompt.
|
8
|
-
*
|
9
|
-
* The prompt depends on the model. For example, OpenAI image models expect a string prompt,
|
10
|
-
* and Stability AI models expect an array of text prompts with optional weights.
|
11
|
-
*
|
12
|
-
* @see https://modelfusion.dev/guide/function/generate-image
|
13
|
-
*
|
14
|
-
* @example
|
15
|
-
* const image = await generateImage(
|
16
|
-
* new StabilityImageGenerationModel(...),
|
17
|
-
* [
|
18
|
-
* { text: "the wicked witch of the west" },
|
19
|
-
* { text: "style of early 19th century painting", weight: 0.5 },
|
20
|
-
* ]
|
21
|
-
* );
|
22
|
-
*
|
23
|
-
* @param {ImageGenerationModel<PROMPT, ImageGenerationModelSettings>} model - The image generation model to be used.
|
24
|
-
* @param {PROMPT} prompt - The prompt to be used for image generation.
|
25
|
-
* @param {FunctionOptions} [options] - Optional settings for the function.
|
26
|
-
*
|
27
|
-
* @returns {ImageGenerationPromise} - Returns a promise that resolves to the generated image.
|
28
|
-
* The image is a Buffer containing the image data in PNG format.
|
29
|
-
*/
|
30
|
-
function generateImage(model, prompt, options) {
|
31
|
-
return new ImageGenerationPromise_js_1.ImageGenerationPromise((0, executeStandardCall_js_1.executeStandardCall)({
|
5
|
+
async function generateImage(model, prompt, options) {
|
6
|
+
const fullResponse = await (0, executeStandardCall_js_1.executeStandardCall)({
|
32
7
|
functionType: "generate-image",
|
33
8
|
input: prompt,
|
34
9
|
model,
|
@@ -40,6 +15,15 @@ function generateImage(model, prompt, options) {
|
|
40
15
|
extractedValue: result.base64Image,
|
41
16
|
};
|
42
17
|
},
|
43
|
-
})
|
18
|
+
});
|
19
|
+
switch (options?.returnType) {
|
20
|
+
case "full":
|
21
|
+
return fullResponse;
|
22
|
+
case "base64":
|
23
|
+
return fullResponse.value;
|
24
|
+
case "buffer":
|
25
|
+
default:
|
26
|
+
return Buffer.from(fullResponse.value, "base64");
|
27
|
+
}
|
44
28
|
}
|
45
29
|
exports.generateImage = generateImage;
|
@@ -1,6 +1,7 @@
|
|
1
|
+
/// <reference types="node" />
|
1
2
|
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
3
|
+
import { ModelCallMetadata } from "../ModelCallMetadata.js";
|
2
4
|
import { ImageGenerationModel, ImageGenerationModelSettings } from "./ImageGenerationModel.js";
|
3
|
-
import { ImageGenerationPromise } from "./ImageGenerationPromise.js";
|
4
5
|
/**
|
5
6
|
* Generates an image using a prompt.
|
6
7
|
*
|
@@ -22,7 +23,19 @@ import { ImageGenerationPromise } from "./ImageGenerationPromise.js";
|
|
22
23
|
* @param {PROMPT} prompt - The prompt to be used for image generation.
|
23
24
|
* @param {FunctionOptions} [options] - Optional settings for the function.
|
24
25
|
*
|
25
|
-
* @returns {
|
26
|
+
* @returns {Promise} - Returns a promise that resolves to the generated image.
|
26
27
|
* The image is a Buffer containing the image data in PNG format.
|
27
28
|
*/
|
28
|
-
export declare function generateImage<PROMPT>(model: ImageGenerationModel<PROMPT, ImageGenerationModelSettings>, prompt: PROMPT, options?: FunctionOptions
|
29
|
+
export declare function generateImage<PROMPT>(model: ImageGenerationModel<PROMPT, ImageGenerationModelSettings>, prompt: PROMPT, options?: FunctionOptions & {
|
30
|
+
returnType?: "buffer";
|
31
|
+
}): Promise<Buffer>;
|
32
|
+
export declare function generateImage<PROMPT>(model: ImageGenerationModel<PROMPT, ImageGenerationModelSettings>, prompt: PROMPT, options: FunctionOptions & {
|
33
|
+
returnType: "base64";
|
34
|
+
}): Promise<string>;
|
35
|
+
export declare function generateImage<PROMPT>(model: ImageGenerationModel<PROMPT, ImageGenerationModelSettings>, prompt: PROMPT, options: FunctionOptions & {
|
36
|
+
returnType: "full";
|
37
|
+
}): Promise<{
|
38
|
+
value: string;
|
39
|
+
response: unknown;
|
40
|
+
metadata: ModelCallMetadata;
|
41
|
+
}>;
|
@@ -1,31 +1,6 @@
|
|
1
1
|
import { executeStandardCall } from "../executeStandardCall.js";
|
2
|
-
|
3
|
-
|
4
|
-
* Generates an image using a prompt.
|
5
|
-
*
|
6
|
-
* The prompt depends on the model. For example, OpenAI image models expect a string prompt,
|
7
|
-
* and Stability AI models expect an array of text prompts with optional weights.
|
8
|
-
*
|
9
|
-
* @see https://modelfusion.dev/guide/function/generate-image
|
10
|
-
*
|
11
|
-
* @example
|
12
|
-
* const image = await generateImage(
|
13
|
-
* new StabilityImageGenerationModel(...),
|
14
|
-
* [
|
15
|
-
* { text: "the wicked witch of the west" },
|
16
|
-
* { text: "style of early 19th century painting", weight: 0.5 },
|
17
|
-
* ]
|
18
|
-
* );
|
19
|
-
*
|
20
|
-
* @param {ImageGenerationModel<PROMPT, ImageGenerationModelSettings>} model - The image generation model to be used.
|
21
|
-
* @param {PROMPT} prompt - The prompt to be used for image generation.
|
22
|
-
* @param {FunctionOptions} [options] - Optional settings for the function.
|
23
|
-
*
|
24
|
-
* @returns {ImageGenerationPromise} - Returns a promise that resolves to the generated image.
|
25
|
-
* The image is a Buffer containing the image data in PNG format.
|
26
|
-
*/
|
27
|
-
export function generateImage(model, prompt, options) {
|
28
|
-
return new ImageGenerationPromise(executeStandardCall({
|
2
|
+
export async function generateImage(model, prompt, options) {
|
3
|
+
const fullResponse = await executeStandardCall({
|
29
4
|
functionType: "generate-image",
|
30
5
|
input: prompt,
|
31
6
|
model,
|
@@ -37,5 +12,14 @@ export function generateImage(model, prompt, options) {
|
|
37
12
|
extractedValue: result.base64Image,
|
38
13
|
};
|
39
14
|
},
|
40
|
-
})
|
15
|
+
});
|
16
|
+
switch (options?.returnType) {
|
17
|
+
case "full":
|
18
|
+
return fullResponse;
|
19
|
+
case "base64":
|
20
|
+
return fullResponse.value;
|
21
|
+
case "buffer":
|
22
|
+
default:
|
23
|
+
return Buffer.from(fullResponse.value, "base64");
|
24
|
+
}
|
41
25
|
}
|
@@ -1,28 +1,9 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.generateSpeech = void 0;
|
4
|
-
const ModelFunctionPromise_js_1 = require("../ModelFunctionPromise.cjs");
|
5
4
|
const executeStandardCall_js_1 = require("../executeStandardCall.cjs");
|
6
|
-
|
7
|
-
|
8
|
-
*
|
9
|
-
* @see https://modelfusion.dev/guide/function/generate-speech
|
10
|
-
*
|
11
|
-
* @example
|
12
|
-
* const speech = await generateSpeech(
|
13
|
-
* new LmntSpeechModel(...),
|
14
|
-
* "Good evening, ladies and gentlemen! Exciting news on the airwaves tonight " +
|
15
|
-
* "as The Rolling Stones unveil 'Hackney Diamonds.'
|
16
|
-
* );
|
17
|
-
*
|
18
|
-
* @param {SpeechGenerationModel<SpeechGenerationModelSettings>} model - The speech generation model.
|
19
|
-
* @param {string} text - The text to be converted to speech.
|
20
|
-
* @param {FunctionOptions} [options] - Optional function options.
|
21
|
-
*
|
22
|
-
* @returns {ModelFunctionPromise<Buffer>} - A promise that resolves to a buffer containing the synthesized speech.
|
23
|
-
*/
|
24
|
-
function generateSpeech(model, text, options) {
|
25
|
-
return new ModelFunctionPromise_js_1.ModelFunctionPromise((0, executeStandardCall_js_1.executeStandardCall)({
|
5
|
+
async function generateSpeech(model, text, options) {
|
6
|
+
const fullResponse = await (0, executeStandardCall_js_1.executeStandardCall)({
|
26
7
|
functionType: "generate-speech",
|
27
8
|
input: text,
|
28
9
|
model,
|
@@ -34,6 +15,7 @@ function generateSpeech(model, text, options) {
|
|
34
15
|
extractedValue: response,
|
35
16
|
};
|
36
17
|
},
|
37
|
-
})
|
18
|
+
});
|
19
|
+
return options?.returnType === "full" ? fullResponse : fullResponse.value;
|
38
20
|
}
|
39
21
|
exports.generateSpeech = generateSpeech;
|
@@ -1,6 +1,6 @@
|
|
1
1
|
/// <reference types="node" />
|
2
2
|
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
3
|
-
import {
|
3
|
+
import { ModelCallMetadata } from "../ModelCallMetadata.js";
|
4
4
|
import { SpeechGenerationModel, SpeechGenerationModelSettings } from "./SpeechGenerationModel.js";
|
5
5
|
/**
|
6
6
|
* Synthesizes speech from text. Also called text-to-speech (TTS).
|
@@ -18,6 +18,15 @@ import { SpeechGenerationModel, SpeechGenerationModelSettings } from "./SpeechGe
|
|
18
18
|
* @param {string} text - The text to be converted to speech.
|
19
19
|
* @param {FunctionOptions} [options] - Optional function options.
|
20
20
|
*
|
21
|
-
* @returns {
|
21
|
+
* @returns {Promise<Buffer>} - A promise that resolves to a buffer containing the synthesized speech.
|
22
22
|
*/
|
23
|
-
export declare function generateSpeech(model: SpeechGenerationModel<SpeechGenerationModelSettings>, text: string, options?: FunctionOptions
|
23
|
+
export declare function generateSpeech(model: SpeechGenerationModel<SpeechGenerationModelSettings>, text: string, options?: FunctionOptions & {
|
24
|
+
returnType?: "buffer";
|
25
|
+
}): Promise<Buffer>;
|
26
|
+
export declare function generateSpeech(model: SpeechGenerationModel<SpeechGenerationModelSettings>, text: string, options: FunctionOptions & {
|
27
|
+
returnType: "full";
|
28
|
+
}): Promise<{
|
29
|
+
value: Buffer;
|
30
|
+
response: unknown;
|
31
|
+
metadata: ModelCallMetadata;
|
32
|
+
}>;
|
@@ -1,25 +1,6 @@
|
|
1
|
-
import { ModelFunctionPromise } from "../ModelFunctionPromise.js";
|
2
1
|
import { executeStandardCall } from "../executeStandardCall.js";
|
3
|
-
|
4
|
-
|
5
|
-
*
|
6
|
-
* @see https://modelfusion.dev/guide/function/generate-speech
|
7
|
-
*
|
8
|
-
* @example
|
9
|
-
* const speech = await generateSpeech(
|
10
|
-
* new LmntSpeechModel(...),
|
11
|
-
* "Good evening, ladies and gentlemen! Exciting news on the airwaves tonight " +
|
12
|
-
* "as The Rolling Stones unveil 'Hackney Diamonds.'
|
13
|
-
* );
|
14
|
-
*
|
15
|
-
* @param {SpeechGenerationModel<SpeechGenerationModelSettings>} model - The speech generation model.
|
16
|
-
* @param {string} text - The text to be converted to speech.
|
17
|
-
* @param {FunctionOptions} [options] - Optional function options.
|
18
|
-
*
|
19
|
-
* @returns {ModelFunctionPromise<Buffer>} - A promise that resolves to a buffer containing the synthesized speech.
|
20
|
-
*/
|
21
|
-
export function generateSpeech(model, text, options) {
|
22
|
-
return new ModelFunctionPromise(executeStandardCall({
|
2
|
+
export async function generateSpeech(model, text, options) {
|
3
|
+
const fullResponse = await executeStandardCall({
|
23
4
|
functionType: "generate-speech",
|
24
5
|
input: text,
|
25
6
|
model,
|
@@ -31,5 +12,6 @@ export function generateSpeech(model, text, options) {
|
|
31
12
|
extractedValue: response,
|
32
13
|
};
|
33
14
|
},
|
34
|
-
})
|
15
|
+
});
|
16
|
+
return options?.returnType === "full" ? fullResponse : fullResponse.value;
|
35
17
|
}
|
@@ -2,33 +2,8 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.streamSpeech = void 0;
|
4
4
|
const AsyncQueue_js_1 = require("../../util/AsyncQueue.cjs");
|
5
|
-
const AsyncIterableResultPromise_js_1 = require("../AsyncIterableResultPromise.cjs");
|
6
5
|
const executeStreamCall_js_1 = require("../executeStreamCall.cjs");
|
7
|
-
|
8
|
-
* Stream synthesized speech from text. Also called text-to-speech (TTS).
|
9
|
-
* Duplex streaming where both the input and output are streamed is supported.
|
10
|
-
*
|
11
|
-
* @see https://modelfusion.dev/guide/function/generate-speech
|
12
|
-
*
|
13
|
-
* @example
|
14
|
-
* const textStream = await streamText(...);
|
15
|
-
*
|
16
|
-
* const speechStream = await streamSpeech(
|
17
|
-
* new ElevenLabsSpeechModel(...),
|
18
|
-
* textStream
|
19
|
-
* );
|
20
|
-
*
|
21
|
-
* for await (const speechPart of speechStream) {
|
22
|
-
* // ...
|
23
|
-
* }
|
24
|
-
*
|
25
|
-
* @param {StreamingSpeechGenerationModel<SpeechGenerationModelSettings>} model - The speech generation model.
|
26
|
-
* @param {AsyncIterable<string> | string} text - The text to be converted to speech. Can be a string or an async iterable of strings.
|
27
|
-
* @param {FunctionOptions} [options] - Optional function options.
|
28
|
-
*
|
29
|
-
* @returns {AsyncIterableResultPromise<Buffer>} An async iterable promise that contains the synthesized speech chunks.
|
30
|
-
*/
|
31
|
-
function streamSpeech(model, text, options) {
|
6
|
+
async function streamSpeech(model, text, options) {
|
32
7
|
let textStream;
|
33
8
|
// simulate a stream with a single value for a string input:
|
34
9
|
if (typeof text === "string") {
|
@@ -40,16 +15,15 @@ function streamSpeech(model, text, options) {
|
|
40
15
|
else {
|
41
16
|
textStream = text;
|
42
17
|
}
|
43
|
-
|
18
|
+
const fullResponse = await (0, executeStreamCall_js_1.executeStreamCall)({
|
44
19
|
functionType: "stream-speech",
|
45
20
|
input: text,
|
46
21
|
model,
|
47
22
|
options,
|
48
23
|
startStream: async (options) => model.doGenerateSpeechStreamDuplex(textStream, options),
|
49
|
-
processDelta: (delta) =>
|
50
|
-
return delta.valueDelta;
|
51
|
-
},
|
24
|
+
processDelta: (delta) => delta.valueDelta,
|
52
25
|
getResult: () => ({}),
|
53
|
-
})
|
26
|
+
});
|
27
|
+
return options?.returnType === "full" ? fullResponse : fullResponse.value;
|
54
28
|
}
|
55
29
|
exports.streamSpeech = streamSpeech;
|
@@ -1,7 +1,7 @@
|
|
1
1
|
/// <reference types="node" />
|
2
2
|
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
3
|
-
import {
|
4
|
-
import {
|
3
|
+
import { ModelCallMetadata } from "../ModelCallMetadata.js";
|
4
|
+
import { SpeechGenerationModelSettings, StreamingSpeechGenerationModel } from "./SpeechGenerationModel.js";
|
5
5
|
/**
|
6
6
|
* Stream synthesized speech from text. Also called text-to-speech (TTS).
|
7
7
|
* Duplex streaming where both the input and output are streamed is supported.
|
@@ -26,4 +26,12 @@ import { StreamingSpeechGenerationModel, SpeechGenerationModelSettings } from ".
|
|
26
26
|
*
|
27
27
|
* @returns {AsyncIterableResultPromise<Buffer>} An async iterable promise that contains the synthesized speech chunks.
|
28
28
|
*/
|
29
|
-
export declare function streamSpeech(model: StreamingSpeechGenerationModel<SpeechGenerationModelSettings>, text: AsyncIterable<string> | string, options?: FunctionOptions
|
29
|
+
export declare function streamSpeech(model: StreamingSpeechGenerationModel<SpeechGenerationModelSettings>, text: AsyncIterable<string> | string, options?: FunctionOptions & {
|
30
|
+
returnType?: "buffer-stream";
|
31
|
+
}): Promise<AsyncIterable<Buffer>>;
|
32
|
+
export declare function streamSpeech(model: StreamingSpeechGenerationModel<SpeechGenerationModelSettings>, text: AsyncIterable<string> | string, options: FunctionOptions & {
|
33
|
+
returnType: "full";
|
34
|
+
}): Promise<{
|
35
|
+
value: AsyncIterable<Buffer>;
|
36
|
+
metadata: Omit<ModelCallMetadata, "durationInMs" | "finishTimestamp">;
|
37
|
+
}>;
|
@@ -1,31 +1,6 @@
|
|
1
1
|
import { AsyncQueue } from "../../util/AsyncQueue.js";
|
2
|
-
import { AsyncIterableResultPromise } from "../AsyncIterableResultPromise.js";
|
3
2
|
import { executeStreamCall } from "../executeStreamCall.js";
|
4
|
-
|
5
|
-
* Stream synthesized speech from text. Also called text-to-speech (TTS).
|
6
|
-
* Duplex streaming where both the input and output are streamed is supported.
|
7
|
-
*
|
8
|
-
* @see https://modelfusion.dev/guide/function/generate-speech
|
9
|
-
*
|
10
|
-
* @example
|
11
|
-
* const textStream = await streamText(...);
|
12
|
-
*
|
13
|
-
* const speechStream = await streamSpeech(
|
14
|
-
* new ElevenLabsSpeechModel(...),
|
15
|
-
* textStream
|
16
|
-
* );
|
17
|
-
*
|
18
|
-
* for await (const speechPart of speechStream) {
|
19
|
-
* // ...
|
20
|
-
* }
|
21
|
-
*
|
22
|
-
* @param {StreamingSpeechGenerationModel<SpeechGenerationModelSettings>} model - The speech generation model.
|
23
|
-
* @param {AsyncIterable<string> | string} text - The text to be converted to speech. Can be a string or an async iterable of strings.
|
24
|
-
* @param {FunctionOptions} [options] - Optional function options.
|
25
|
-
*
|
26
|
-
* @returns {AsyncIterableResultPromise<Buffer>} An async iterable promise that contains the synthesized speech chunks.
|
27
|
-
*/
|
28
|
-
export function streamSpeech(model, text, options) {
|
3
|
+
export async function streamSpeech(model, text, options) {
|
29
4
|
let textStream;
|
30
5
|
// simulate a stream with a single value for a string input:
|
31
6
|
if (typeof text === "string") {
|
@@ -37,15 +12,14 @@ export function streamSpeech(model, text, options) {
|
|
37
12
|
else {
|
38
13
|
textStream = text;
|
39
14
|
}
|
40
|
-
|
15
|
+
const fullResponse = await executeStreamCall({
|
41
16
|
functionType: "stream-speech",
|
42
17
|
input: text,
|
43
18
|
model,
|
44
19
|
options,
|
45
20
|
startStream: async (options) => model.doGenerateSpeechStreamDuplex(textStream, options),
|
46
|
-
processDelta: (delta) =>
|
47
|
-
return delta.valueDelta;
|
48
|
-
},
|
21
|
+
processDelta: (delta) => delta.valueDelta,
|
49
22
|
getResult: () => ({}),
|
50
|
-
})
|
23
|
+
});
|
24
|
+
return options?.returnType === "full" ? fullResponse : fullResponse.value;
|
51
25
|
}
|
@@ -30,7 +30,10 @@ class StructureFromTextGenerationModel {
|
|
30
30
|
return this.model.settingsForEvent;
|
31
31
|
}
|
32
32
|
async doGenerateStructure(structure, prompt, options) {
|
33
|
-
const { response, value } = await (0, generateText_js_1.generateText)(this.model, this.format.createPrompt(prompt, structure),
|
33
|
+
const { response, value } = await (0, generateText_js_1.generateText)(this.model, this.format.createPrompt(prompt, structure), {
|
34
|
+
...options,
|
35
|
+
returnType: "full",
|
36
|
+
});
|
34
37
|
try {
|
35
38
|
return {
|
36
39
|
response,
|