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
@@ -27,7 +27,10 @@ export class StructureFromTextGenerationModel {
|
|
27
27
|
return this.model.settingsForEvent;
|
28
28
|
}
|
29
29
|
async doGenerateStructure(structure, prompt, options) {
|
30
|
-
const { response, value } = await generateText(this.model, this.format.createPrompt(prompt, structure),
|
30
|
+
const { response, value } = await generateText(this.model, this.format.createPrompt(prompt, structure), {
|
31
|
+
...options,
|
32
|
+
returnType: "full",
|
33
|
+
});
|
31
34
|
try {
|
32
35
|
return {
|
33
36
|
response,
|
@@ -2,55 +2,13 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.generateStructure = void 0;
|
4
4
|
const executeStandardCall_js_1 = require("../executeStandardCall.cjs");
|
5
|
-
const ModelFunctionPromise_js_1 = require("../ModelFunctionPromise.cjs");
|
6
5
|
const StructureValidationError_js_1 = require("./StructureValidationError.cjs");
|
7
|
-
|
8
|
-
* Generate a typed object for a prompt and a structure definition.
|
9
|
-
* The structure definition is used as part of the final prompt.
|
10
|
-
*
|
11
|
-
* For the OpenAI chat model, this generates and parses a function call with a single function.
|
12
|
-
*
|
13
|
-
* @see https://modelfusion.dev/guide/function/generate-structure
|
14
|
-
*
|
15
|
-
* @example
|
16
|
-
* const sentiment = await generateStructure(
|
17
|
-
* new OpenAIChatModel(...),
|
18
|
-
* new ZodStructureDefinition({
|
19
|
-
* name: "sentiment",
|
20
|
-
* description: "Write the sentiment analysis",
|
21
|
-
* schema: z.object({
|
22
|
-
* sentiment: z
|
23
|
-
* .enum(["positive", "neutral", "negative"])
|
24
|
-
* .describe("Sentiment."),
|
25
|
-
* }),
|
26
|
-
* }),
|
27
|
-
* [
|
28
|
-
* OpenAIChatMessage.system(
|
29
|
-
* "You are a sentiment evaluator. " +
|
30
|
-
* "Analyze the sentiment of the following product review:"
|
31
|
-
* ),
|
32
|
-
* OpenAIChatMessage.user(
|
33
|
-
* "After I opened the package, I was met by a very unpleasant smell " +
|
34
|
-
* "that did not disappear even after washing. Never again!"
|
35
|
-
* ),
|
36
|
-
* ]
|
37
|
-
* );
|
38
|
-
*
|
39
|
-
* @param {StructureGenerationModel<PROMPT, SETTINGS>} model - The model to generate the structure.
|
40
|
-
* @param {StructureDefinition<NAME, STRUCTURE>} structureDefinition - The structure definition to be used.
|
41
|
-
* @param {PROMPT | ((structureDefinition: StructureDefinition<NAME, STRUCTURE>) => PROMPT)} prompt
|
42
|
-
* The prompt to be used.
|
43
|
-
* You can also pass a function that takes the structure definition as an argument and returns the prompt.
|
44
|
-
* @param {FunctionOptions} [options] - Optional function options.
|
45
|
-
*
|
46
|
-
* @returns {ModelFunctionPromise<STRUCTURE>} - Returns a promise that resolves to the generated structure.
|
47
|
-
*/
|
48
|
-
function generateStructure(model, structureDefinition, prompt, options) {
|
6
|
+
async function generateStructure(model, structureDefinition, prompt, options) {
|
49
7
|
// Note: PROMPT must not be a function.
|
50
8
|
const expandedPrompt = typeof prompt === "function"
|
51
9
|
? prompt(structureDefinition)
|
52
10
|
: prompt;
|
53
|
-
|
11
|
+
const fullResponse = await (0, executeStandardCall_js_1.executeStandardCall)({
|
54
12
|
functionType: "generate-structure",
|
55
13
|
input: expandedPrompt,
|
56
14
|
model,
|
@@ -74,6 +32,7 @@ function generateStructure(model, structureDefinition, prompt, options) {
|
|
74
32
|
usage: result.usage,
|
75
33
|
};
|
76
34
|
},
|
77
|
-
})
|
35
|
+
});
|
36
|
+
return options?.returnType === "full" ? fullResponse : fullResponse.value;
|
78
37
|
}
|
79
38
|
exports.generateStructure = generateStructure;
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
2
2
|
import { StructureDefinition } from "../../core/structure/StructureDefinition.js";
|
3
|
-
import {
|
3
|
+
import { ModelCallMetadata } from "../ModelCallMetadata.js";
|
4
4
|
import { StructureGenerationModel, StructureGenerationModelSettings } from "./StructureGenerationModel.js";
|
5
5
|
/**
|
6
6
|
* Generate a typed object for a prompt and a structure definition.
|
@@ -41,6 +41,15 @@ import { StructureGenerationModel, StructureGenerationModelSettings } from "./St
|
|
41
41
|
* You can also pass a function that takes the structure definition as an argument and returns the prompt.
|
42
42
|
* @param {FunctionOptions} [options] - Optional function options.
|
43
43
|
*
|
44
|
-
* @returns {
|
44
|
+
* @returns {Promise<STRUCTURE>} - Returns a promise that resolves to the generated structure.
|
45
45
|
*/
|
46
|
-
export declare function generateStructure<STRUCTURE, PROMPT, NAME extends string, SETTINGS extends StructureGenerationModelSettings>(model: StructureGenerationModel<PROMPT, SETTINGS>, structureDefinition: StructureDefinition<NAME, STRUCTURE>, prompt: PROMPT | ((structureDefinition: StructureDefinition<NAME, STRUCTURE>) => PROMPT), options?: FunctionOptions
|
46
|
+
export declare function generateStructure<STRUCTURE, PROMPT, NAME extends string, SETTINGS extends StructureGenerationModelSettings>(model: StructureGenerationModel<PROMPT, SETTINGS>, structureDefinition: StructureDefinition<NAME, STRUCTURE>, prompt: PROMPT | ((structureDefinition: StructureDefinition<NAME, STRUCTURE>) => PROMPT), options?: FunctionOptions & {
|
47
|
+
returnType?: "structure";
|
48
|
+
}): Promise<STRUCTURE>;
|
49
|
+
export declare function generateStructure<STRUCTURE, PROMPT, NAME extends string, SETTINGS extends StructureGenerationModelSettings>(model: StructureGenerationModel<PROMPT, SETTINGS>, structureDefinition: StructureDefinition<NAME, STRUCTURE>, prompt: PROMPT | ((structureDefinition: StructureDefinition<NAME, STRUCTURE>) => PROMPT), options: FunctionOptions & {
|
50
|
+
returnType: "full";
|
51
|
+
}): Promise<{
|
52
|
+
value: STRUCTURE;
|
53
|
+
response: unknown;
|
54
|
+
metadata: ModelCallMetadata;
|
55
|
+
}>;
|
@@ -1,53 +1,11 @@
|
|
1
1
|
import { executeStandardCall } from "../executeStandardCall.js";
|
2
|
-
import { ModelFunctionPromise } from "../ModelFunctionPromise.js";
|
3
2
|
import { StructureValidationError } from "./StructureValidationError.js";
|
4
|
-
|
5
|
-
* Generate a typed object for a prompt and a structure definition.
|
6
|
-
* The structure definition is used as part of the final prompt.
|
7
|
-
*
|
8
|
-
* For the OpenAI chat model, this generates and parses a function call with a single function.
|
9
|
-
*
|
10
|
-
* @see https://modelfusion.dev/guide/function/generate-structure
|
11
|
-
*
|
12
|
-
* @example
|
13
|
-
* const sentiment = await generateStructure(
|
14
|
-
* new OpenAIChatModel(...),
|
15
|
-
* new ZodStructureDefinition({
|
16
|
-
* name: "sentiment",
|
17
|
-
* description: "Write the sentiment analysis",
|
18
|
-
* schema: z.object({
|
19
|
-
* sentiment: z
|
20
|
-
* .enum(["positive", "neutral", "negative"])
|
21
|
-
* .describe("Sentiment."),
|
22
|
-
* }),
|
23
|
-
* }),
|
24
|
-
* [
|
25
|
-
* OpenAIChatMessage.system(
|
26
|
-
* "You are a sentiment evaluator. " +
|
27
|
-
* "Analyze the sentiment of the following product review:"
|
28
|
-
* ),
|
29
|
-
* OpenAIChatMessage.user(
|
30
|
-
* "After I opened the package, I was met by a very unpleasant smell " +
|
31
|
-
* "that did not disappear even after washing. Never again!"
|
32
|
-
* ),
|
33
|
-
* ]
|
34
|
-
* );
|
35
|
-
*
|
36
|
-
* @param {StructureGenerationModel<PROMPT, SETTINGS>} model - The model to generate the structure.
|
37
|
-
* @param {StructureDefinition<NAME, STRUCTURE>} structureDefinition - The structure definition to be used.
|
38
|
-
* @param {PROMPT | ((structureDefinition: StructureDefinition<NAME, STRUCTURE>) => PROMPT)} prompt
|
39
|
-
* The prompt to be used.
|
40
|
-
* You can also pass a function that takes the structure definition as an argument and returns the prompt.
|
41
|
-
* @param {FunctionOptions} [options] - Optional function options.
|
42
|
-
*
|
43
|
-
* @returns {ModelFunctionPromise<STRUCTURE>} - Returns a promise that resolves to the generated structure.
|
44
|
-
*/
|
45
|
-
export function generateStructure(model, structureDefinition, prompt, options) {
|
3
|
+
export async function generateStructure(model, structureDefinition, prompt, options) {
|
46
4
|
// Note: PROMPT must not be a function.
|
47
5
|
const expandedPrompt = typeof prompt === "function"
|
48
6
|
? prompt(structureDefinition)
|
49
7
|
: prompt;
|
50
|
-
|
8
|
+
const fullResponse = await executeStandardCall({
|
51
9
|
functionType: "generate-structure",
|
52
10
|
input: expandedPrompt,
|
53
11
|
model,
|
@@ -71,5 +29,6 @@ export function generateStructure(model, structureDefinition, prompt, options) {
|
|
71
29
|
usage: result.usage,
|
72
30
|
};
|
73
31
|
},
|
74
|
-
})
|
32
|
+
});
|
33
|
+
return options?.returnType === "full" ? fullResponse : fullResponse.value;
|
75
34
|
}
|
@@ -2,77 +2,14 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.generateStructureOrText = void 0;
|
4
4
|
const executeStandardCall_js_1 = require("../executeStandardCall.cjs");
|
5
|
-
const ModelFunctionPromise_js_1 = require("../ModelFunctionPromise.cjs");
|
6
5
|
const NoSuchStructureError_js_1 = require("./NoSuchStructureError.cjs");
|
7
6
|
const StructureValidationError_js_1 = require("./StructureValidationError.cjs");
|
8
|
-
|
9
|
-
* Generates a typed object or plain text based on the given prompt and structure definitions.
|
10
|
-
* The structure definition is used as part of the final prompt.
|
11
|
-
*
|
12
|
-
* This function interacts with a specified model to either return a structured object conforming to one of the provided
|
13
|
-
* structure definitions or a plain text response if the model decides not to return a structured object.
|
14
|
-
*
|
15
|
-
* For the OpenAI chat model, this generates and parses a function call with automatic function selection.
|
16
|
-
*
|
17
|
-
* @see https://modelfusion.dev/guide/function/generate-structure-or-text
|
18
|
-
*
|
19
|
-
* @example
|
20
|
-
* const { structure, value, text } = await generateStructureOrText(
|
21
|
-
* new OpenAIChatModel(...),
|
22
|
-
* [
|
23
|
-
* new ZodStructureDefinition({
|
24
|
-
* name: "getCurrentWeather" as const, // mark 'as const' for type inference
|
25
|
-
* description: "Get the current weather in a given location",
|
26
|
-
* schema: z.object({
|
27
|
-
* location: z
|
28
|
-
* .string()
|
29
|
-
* .describe("The city and state, e.g. San Francisco, CA"),
|
30
|
-
* unit: z.enum(["celsius", "fahrenheit"]).optional(),
|
31
|
-
* }),
|
32
|
-
* }),
|
33
|
-
* new ZodStructureDefinition({
|
34
|
-
* name: "getContactInformation" as const,
|
35
|
-
* description: "Get the contact information for a given person",
|
36
|
-
* schema: z.object({
|
37
|
-
* name: z.string().describe("The name of the person"),
|
38
|
-
* }),
|
39
|
-
* }),
|
40
|
-
* ],
|
41
|
-
* [OpenAIChatMessage.user(query)]
|
42
|
-
* );
|
43
|
-
*
|
44
|
-
* switch (structure) {
|
45
|
-
* case "getCurrentWeather": {
|
46
|
-
* const { location, unit } = value;
|
47
|
-
* console.log("getCurrentWeather", location, unit);
|
48
|
-
* break;
|
49
|
-
* }
|
50
|
-
*
|
51
|
-
* case "getContactInformation": {
|
52
|
-
* const { name } = value;
|
53
|
-
* console.log("getContactInformation", name);
|
54
|
-
* break;
|
55
|
-
* }
|
56
|
-
*
|
57
|
-
* case null: {
|
58
|
-
* console.log("No function call. Generated text: ", text);
|
59
|
-
* }
|
60
|
-
* }
|
61
|
-
*
|
62
|
-
* @param {StructureOrTextGenerationModel<PROMPT, SETTINGS>} model - The model responsible for generating structured data or text.
|
63
|
-
* @param {STRUCTURES} structureDefinitions - An array of StructureDefinition instances defining the possible structures of the expected response.
|
64
|
-
* @param {PROMPT | ((structureDefinitions: STRUCTURES) => PROMPT)} prompt - The prompt used to generate the structure or text.
|
65
|
-
* You can also pass a function that takes the array of structure definitions as an argument and returns the prompt.
|
66
|
-
* @param {FunctionOptions} [options] - Additional options to control the function's execution behavior.
|
67
|
-
*
|
68
|
-
* @returns {ModelFunctionPromise<{ structure: null; value: null; text: string } | ToOutputValue<STRUCTURES>>} - Returns a promise that resolves to an object containing either a structured response conforming to one of the provided definitions or a plain text response.
|
69
|
-
*/
|
70
|
-
function generateStructureOrText(model, structureDefinitions, prompt, options) {
|
7
|
+
async function generateStructureOrText(model, structureDefinitions, prompt, options) {
|
71
8
|
// Note: PROMPT must not be a function.
|
72
9
|
const expandedPrompt = typeof prompt === "function"
|
73
10
|
? prompt(structureDefinitions)
|
74
11
|
: prompt;
|
75
|
-
|
12
|
+
const fullResponse = await (0, executeStandardCall_js_1.executeStandardCall)({
|
76
13
|
functionType: "generate-structure-or-text",
|
77
14
|
input: expandedPrompt,
|
78
15
|
model,
|
@@ -111,6 +48,7 @@ function generateStructureOrText(model, structureDefinitions, prompt, options) {
|
|
111
48
|
usage: result.usage,
|
112
49
|
};
|
113
50
|
},
|
114
|
-
})
|
51
|
+
});
|
52
|
+
return options?.returnType === "full" ? fullResponse : fullResponse.value;
|
115
53
|
}
|
116
54
|
exports.generateStructureOrText = generateStructureOrText;
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
2
2
|
import { StructureDefinition } from "../../core/structure/StructureDefinition.js";
|
3
|
-
import {
|
3
|
+
import { ModelCallMetadata } from "../ModelCallMetadata.js";
|
4
4
|
import { StructureOrTextGenerationModel, StructureOrTextGenerationModelSettings } from "./StructureOrTextGenerationModel.js";
|
5
5
|
type StructureDefinitionArray<T extends StructureDefinition<any, any>[]> = T;
|
6
6
|
type ToStructureDefinitionMap<T extends StructureDefinitionArray<StructureDefinition<any, any>[]>> = {
|
@@ -74,11 +74,24 @@ type ToOutputValue<STRUCTURES extends StructureDefinitionArray<StructureDefiniti
|
|
74
74
|
* You can also pass a function that takes the array of structure definitions as an argument and returns the prompt.
|
75
75
|
* @param {FunctionOptions} [options] - Additional options to control the function's execution behavior.
|
76
76
|
*
|
77
|
-
* @returns {
|
77
|
+
* @returns {Promise<{ structure: null; value: null; text: string } | ToOutputValue<STRUCTURES>>} - Returns a promise that resolves to an object containing either a structured response conforming to one of the provided definitions or a plain text response.
|
78
78
|
*/
|
79
|
-
export declare function generateStructureOrText<STRUCTURES extends StructureDefinition<any, any>[], PROMPT>(model: StructureOrTextGenerationModel<PROMPT, StructureOrTextGenerationModelSettings>, structureDefinitions: STRUCTURES, prompt: PROMPT | ((structureDefinitions: STRUCTURES) => PROMPT), options?: FunctionOptions
|
79
|
+
export declare function generateStructureOrText<STRUCTURES extends StructureDefinition<any, any>[], PROMPT>(model: StructureOrTextGenerationModel<PROMPT, StructureOrTextGenerationModelSettings>, structureDefinitions: STRUCTURES, prompt: PROMPT | ((structureDefinitions: STRUCTURES) => PROMPT), options?: FunctionOptions & {
|
80
|
+
returnType?: "structure";
|
81
|
+
}): Promise<{
|
80
82
|
structure: null;
|
81
83
|
value: null;
|
82
84
|
text: string;
|
83
85
|
} | ToOutputValue<STRUCTURES>>;
|
86
|
+
export declare function generateStructureOrText<STRUCTURES extends StructureDefinition<any, any>[], PROMPT>(model: StructureOrTextGenerationModel<PROMPT, StructureOrTextGenerationModelSettings>, structureDefinitions: STRUCTURES, prompt: PROMPT | ((structureDefinitions: STRUCTURES) => PROMPT), options: FunctionOptions & {
|
87
|
+
returnType?: "full";
|
88
|
+
}): Promise<{
|
89
|
+
value: {
|
90
|
+
structure: null;
|
91
|
+
value: null;
|
92
|
+
text: string;
|
93
|
+
} | ToOutputValue<STRUCTURES>;
|
94
|
+
response: unknown;
|
95
|
+
metadata: ModelCallMetadata;
|
96
|
+
}>;
|
84
97
|
export {};
|
@@ -1,75 +1,12 @@
|
|
1
1
|
import { executeStandardCall } from "../executeStandardCall.js";
|
2
|
-
import { ModelFunctionPromise } from "../ModelFunctionPromise.js";
|
3
2
|
import { NoSuchStructureError } from "./NoSuchStructureError.js";
|
4
3
|
import { StructureValidationError } from "./StructureValidationError.js";
|
5
|
-
|
6
|
-
* Generates a typed object or plain text based on the given prompt and structure definitions.
|
7
|
-
* The structure definition is used as part of the final prompt.
|
8
|
-
*
|
9
|
-
* This function interacts with a specified model to either return a structured object conforming to one of the provided
|
10
|
-
* structure definitions or a plain text response if the model decides not to return a structured object.
|
11
|
-
*
|
12
|
-
* For the OpenAI chat model, this generates and parses a function call with automatic function selection.
|
13
|
-
*
|
14
|
-
* @see https://modelfusion.dev/guide/function/generate-structure-or-text
|
15
|
-
*
|
16
|
-
* @example
|
17
|
-
* const { structure, value, text } = await generateStructureOrText(
|
18
|
-
* new OpenAIChatModel(...),
|
19
|
-
* [
|
20
|
-
* new ZodStructureDefinition({
|
21
|
-
* name: "getCurrentWeather" as const, // mark 'as const' for type inference
|
22
|
-
* description: "Get the current weather in a given location",
|
23
|
-
* schema: z.object({
|
24
|
-
* location: z
|
25
|
-
* .string()
|
26
|
-
* .describe("The city and state, e.g. San Francisco, CA"),
|
27
|
-
* unit: z.enum(["celsius", "fahrenheit"]).optional(),
|
28
|
-
* }),
|
29
|
-
* }),
|
30
|
-
* new ZodStructureDefinition({
|
31
|
-
* name: "getContactInformation" as const,
|
32
|
-
* description: "Get the contact information for a given person",
|
33
|
-
* schema: z.object({
|
34
|
-
* name: z.string().describe("The name of the person"),
|
35
|
-
* }),
|
36
|
-
* }),
|
37
|
-
* ],
|
38
|
-
* [OpenAIChatMessage.user(query)]
|
39
|
-
* );
|
40
|
-
*
|
41
|
-
* switch (structure) {
|
42
|
-
* case "getCurrentWeather": {
|
43
|
-
* const { location, unit } = value;
|
44
|
-
* console.log("getCurrentWeather", location, unit);
|
45
|
-
* break;
|
46
|
-
* }
|
47
|
-
*
|
48
|
-
* case "getContactInformation": {
|
49
|
-
* const { name } = value;
|
50
|
-
* console.log("getContactInformation", name);
|
51
|
-
* break;
|
52
|
-
* }
|
53
|
-
*
|
54
|
-
* case null: {
|
55
|
-
* console.log("No function call. Generated text: ", text);
|
56
|
-
* }
|
57
|
-
* }
|
58
|
-
*
|
59
|
-
* @param {StructureOrTextGenerationModel<PROMPT, SETTINGS>} model - The model responsible for generating structured data or text.
|
60
|
-
* @param {STRUCTURES} structureDefinitions - An array of StructureDefinition instances defining the possible structures of the expected response.
|
61
|
-
* @param {PROMPT | ((structureDefinitions: STRUCTURES) => PROMPT)} prompt - The prompt used to generate the structure or text.
|
62
|
-
* You can also pass a function that takes the array of structure definitions as an argument and returns the prompt.
|
63
|
-
* @param {FunctionOptions} [options] - Additional options to control the function's execution behavior.
|
64
|
-
*
|
65
|
-
* @returns {ModelFunctionPromise<{ structure: null; value: null; text: string } | ToOutputValue<STRUCTURES>>} - Returns a promise that resolves to an object containing either a structured response conforming to one of the provided definitions or a plain text response.
|
66
|
-
*/
|
67
|
-
export function generateStructureOrText(model, structureDefinitions, prompt, options) {
|
4
|
+
export async function generateStructureOrText(model, structureDefinitions, prompt, options) {
|
68
5
|
// Note: PROMPT must not be a function.
|
69
6
|
const expandedPrompt = typeof prompt === "function"
|
70
7
|
? prompt(structureDefinitions)
|
71
8
|
: prompt;
|
72
|
-
|
9
|
+
const fullResponse = await executeStandardCall({
|
73
10
|
functionType: "generate-structure-or-text",
|
74
11
|
input: expandedPrompt,
|
75
12
|
model,
|
@@ -108,5 +45,6 @@ export function generateStructureOrText(model, structureDefinitions, prompt, opt
|
|
108
45
|
usage: result.usage,
|
109
46
|
};
|
110
47
|
},
|
111
|
-
})
|
48
|
+
});
|
49
|
+
return options?.returnType === "full" ? fullResponse : fullResponse.value;
|
112
50
|
}
|
@@ -2,83 +2,15 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.streamStructure = void 0;
|
4
4
|
const isDeepEqualData_js_1 = require("../../util/isDeepEqualData.cjs");
|
5
|
-
const AsyncIterableResultPromise_js_1 = require("../AsyncIterableResultPromise.cjs");
|
6
5
|
const executeStreamCall_js_1 = require("../executeStreamCall.cjs");
|
7
|
-
|
8
|
-
* Generate and stream an object for a prompt and a structure definition.
|
9
|
-
*
|
10
|
-
* The final object is typed according to the structure definition.
|
11
|
-
* The partial objects are of unknown type,
|
12
|
-
* but are supposed to be partial version of the final object
|
13
|
-
* (unless the underlying model returns invalid data).
|
14
|
-
*
|
15
|
-
* The structure definition is used as part of the final prompt.
|
16
|
-
*
|
17
|
-
* For the OpenAI chat model, this generates and parses a function call with a single function.
|
18
|
-
*
|
19
|
-
* @see https://modelfusion.dev/guide/function/generate-structure
|
20
|
-
*
|
21
|
-
* @example
|
22
|
-
* const structureStream = await streamStructure(
|
23
|
-
* new OpenAIChatModel({
|
24
|
-
* model: "gpt-3.5-turbo",
|
25
|
-
* temperature: 0,
|
26
|
-
* maxCompletionTokens: 2000,
|
27
|
-
* }),
|
28
|
-
* new ZodStructureDefinition({
|
29
|
-
* name: "generateCharacter",
|
30
|
-
* description: "Generate character descriptions.",
|
31
|
-
* schema: z.object({
|
32
|
-
* characters: z.array(
|
33
|
-
* z.object({
|
34
|
-
* name: z.string(),
|
35
|
-
* class: z
|
36
|
-
* .string()
|
37
|
-
* .describe("Character class, e.g. warrior, mage, or thief."),
|
38
|
-
* description: z.string(),
|
39
|
-
* })
|
40
|
-
* ),
|
41
|
-
* }),
|
42
|
-
* }),
|
43
|
-
* [
|
44
|
-
* OpenAIChatMessage.user(
|
45
|
-
* "Generate 3 character descriptions for a fantasy role playing game."
|
46
|
-
* ),
|
47
|
-
* ]
|
48
|
-
* );
|
49
|
-
*
|
50
|
-
* for await (const part of structureStream) {
|
51
|
-
* if (!part.isComplete) {
|
52
|
-
* const unknownPartialStructure = part.value;
|
53
|
-
* // use your own logic to handle partial structures, e.g. with Zod .deepPartial()
|
54
|
-
* // it depends on your application at which points you want to act on the partial structures
|
55
|
-
* } else {
|
56
|
-
* const fullyTypedStructure = part.value;
|
57
|
-
* // ...
|
58
|
-
* }
|
59
|
-
* }
|
60
|
-
*
|
61
|
-
* @param {StructureStreamingModel<PROMPT>} model - The model to use for streaming
|
62
|
-
* @param {StructureDefinition<NAME, STRUCTURE>} structureDefinition - The structure definition to use
|
63
|
-
* @param {PROMPT | ((structureDefinition: StructureDefinition<NAME, STRUCTURE>) => PROMPT)} prompt
|
64
|
-
* The prompt to be used.
|
65
|
-
* You can also pass a function that takes the structure definition as an argument and returns the prompt.
|
66
|
-
* @param {FunctionOptions} [options] - Optional function options
|
67
|
-
*
|
68
|
-
* @returns {AsyncIterableResultPromise<StructureStreamPart<STRUCTURE>>}
|
69
|
-
* The async iterable result promise.
|
70
|
-
* Each part of the stream is either a partial structure or the final structure.
|
71
|
-
* It contains a isComplete flag to indicate whether the structure is complete,
|
72
|
-
* and a value that is either the partial structure or the final structure.
|
73
|
-
*/
|
74
|
-
function streamStructure(model, structureDefinition, prompt, options) {
|
6
|
+
async function streamStructure(model, structureDefinition, prompt, options) {
|
75
7
|
// Note: PROMPT must not be a function.
|
76
8
|
const expandedPrompt = typeof prompt === "function"
|
77
9
|
? prompt(structureDefinition)
|
78
10
|
: prompt;
|
79
11
|
let lastStructure;
|
80
12
|
let lastFullDelta;
|
81
|
-
|
13
|
+
const fullResponse = await (0, executeStreamCall_js_1.executeStreamCall)({
|
82
14
|
functionType: "stream-structure",
|
83
15
|
input: prompt,
|
84
16
|
model,
|
@@ -114,6 +46,7 @@ function streamStructure(model, structureDefinition, prompt, options) {
|
|
114
46
|
response: lastFullDelta,
|
115
47
|
value: lastStructure,
|
116
48
|
}),
|
117
|
-
})
|
49
|
+
});
|
50
|
+
return options?.returnType === "full" ? fullResponse : fullResponse.value;
|
118
51
|
}
|
119
52
|
exports.streamStructure = streamStructure;
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { FunctionOptions } from "../../core/FunctionOptions.js";
|
2
2
|
import { StructureDefinition } from "../../core/structure/StructureDefinition.js";
|
3
|
-
import {
|
3
|
+
import { ModelCallMetadata } from "../ModelCallMetadata.js";
|
4
4
|
import { StructureStreamingModel } from "./StructureGenerationModel.js";
|
5
5
|
export type StructureStreamPart<STRUCTURE> = {
|
6
6
|
isComplete: false;
|
@@ -76,4 +76,12 @@ export type StructureStreamPart<STRUCTURE> = {
|
|
76
76
|
* It contains a isComplete flag to indicate whether the structure is complete,
|
77
77
|
* and a value that is either the partial structure or the final structure.
|
78
78
|
*/
|
79
|
-
export declare function streamStructure<STRUCTURE, PROMPT, NAME extends string>(model: StructureStreamingModel<PROMPT>, structureDefinition: StructureDefinition<NAME, STRUCTURE>, prompt: PROMPT | ((structureDefinition: StructureDefinition<NAME, STRUCTURE>) => PROMPT), options?: FunctionOptions
|
79
|
+
export declare function streamStructure<STRUCTURE, PROMPT, NAME extends string>(model: StructureStreamingModel<PROMPT>, structureDefinition: StructureDefinition<NAME, STRUCTURE>, prompt: PROMPT | ((structureDefinition: StructureDefinition<NAME, STRUCTURE>) => PROMPT), options?: FunctionOptions & {
|
80
|
+
returnType?: "structure-stream";
|
81
|
+
}): Promise<AsyncIterable<StructureStreamPart<STRUCTURE>>>;
|
82
|
+
export declare function streamStructure<STRUCTURE, PROMPT, NAME extends string>(model: StructureStreamingModel<PROMPT>, structureDefinition: StructureDefinition<NAME, STRUCTURE>, prompt: PROMPT | ((structureDefinition: StructureDefinition<NAME, STRUCTURE>) => PROMPT), options: FunctionOptions & {
|
83
|
+
returnType: "full";
|
84
|
+
}): Promise<{
|
85
|
+
value: AsyncIterable<StructureStreamPart<STRUCTURE>>;
|
86
|
+
metadata: Omit<ModelCallMetadata, "durationInMs" | "finishTimestamp">;
|
87
|
+
}>;
|
@@ -1,81 +1,13 @@
|
|
1
1
|
import { isDeepEqualData } from "../../util/isDeepEqualData.js";
|
2
|
-
import { AsyncIterableResultPromise } from "../AsyncIterableResultPromise.js";
|
3
2
|
import { executeStreamCall } from "../executeStreamCall.js";
|
4
|
-
|
5
|
-
* Generate and stream an object for a prompt and a structure definition.
|
6
|
-
*
|
7
|
-
* The final object is typed according to the structure definition.
|
8
|
-
* The partial objects are of unknown type,
|
9
|
-
* but are supposed to be partial version of the final object
|
10
|
-
* (unless the underlying model returns invalid data).
|
11
|
-
*
|
12
|
-
* The structure definition is used as part of the final prompt.
|
13
|
-
*
|
14
|
-
* For the OpenAI chat model, this generates and parses a function call with a single function.
|
15
|
-
*
|
16
|
-
* @see https://modelfusion.dev/guide/function/generate-structure
|
17
|
-
*
|
18
|
-
* @example
|
19
|
-
* const structureStream = await streamStructure(
|
20
|
-
* new OpenAIChatModel({
|
21
|
-
* model: "gpt-3.5-turbo",
|
22
|
-
* temperature: 0,
|
23
|
-
* maxCompletionTokens: 2000,
|
24
|
-
* }),
|
25
|
-
* new ZodStructureDefinition({
|
26
|
-
* name: "generateCharacter",
|
27
|
-
* description: "Generate character descriptions.",
|
28
|
-
* schema: z.object({
|
29
|
-
* characters: z.array(
|
30
|
-
* z.object({
|
31
|
-
* name: z.string(),
|
32
|
-
* class: z
|
33
|
-
* .string()
|
34
|
-
* .describe("Character class, e.g. warrior, mage, or thief."),
|
35
|
-
* description: z.string(),
|
36
|
-
* })
|
37
|
-
* ),
|
38
|
-
* }),
|
39
|
-
* }),
|
40
|
-
* [
|
41
|
-
* OpenAIChatMessage.user(
|
42
|
-
* "Generate 3 character descriptions for a fantasy role playing game."
|
43
|
-
* ),
|
44
|
-
* ]
|
45
|
-
* );
|
46
|
-
*
|
47
|
-
* for await (const part of structureStream) {
|
48
|
-
* if (!part.isComplete) {
|
49
|
-
* const unknownPartialStructure = part.value;
|
50
|
-
* // use your own logic to handle partial structures, e.g. with Zod .deepPartial()
|
51
|
-
* // it depends on your application at which points you want to act on the partial structures
|
52
|
-
* } else {
|
53
|
-
* const fullyTypedStructure = part.value;
|
54
|
-
* // ...
|
55
|
-
* }
|
56
|
-
* }
|
57
|
-
*
|
58
|
-
* @param {StructureStreamingModel<PROMPT>} model - The model to use for streaming
|
59
|
-
* @param {StructureDefinition<NAME, STRUCTURE>} structureDefinition - The structure definition to use
|
60
|
-
* @param {PROMPT | ((structureDefinition: StructureDefinition<NAME, STRUCTURE>) => PROMPT)} prompt
|
61
|
-
* The prompt to be used.
|
62
|
-
* You can also pass a function that takes the structure definition as an argument and returns the prompt.
|
63
|
-
* @param {FunctionOptions} [options] - Optional function options
|
64
|
-
*
|
65
|
-
* @returns {AsyncIterableResultPromise<StructureStreamPart<STRUCTURE>>}
|
66
|
-
* The async iterable result promise.
|
67
|
-
* Each part of the stream is either a partial structure or the final structure.
|
68
|
-
* It contains a isComplete flag to indicate whether the structure is complete,
|
69
|
-
* and a value that is either the partial structure or the final structure.
|
70
|
-
*/
|
71
|
-
export function streamStructure(model, structureDefinition, prompt, options) {
|
3
|
+
export async function streamStructure(model, structureDefinition, prompt, options) {
|
72
4
|
// Note: PROMPT must not be a function.
|
73
5
|
const expandedPrompt = typeof prompt === "function"
|
74
6
|
? prompt(structureDefinition)
|
75
7
|
: prompt;
|
76
8
|
let lastStructure;
|
77
9
|
let lastFullDelta;
|
78
|
-
|
10
|
+
const fullResponse = await executeStreamCall({
|
79
11
|
functionType: "stream-structure",
|
80
12
|
input: prompt,
|
81
13
|
model,
|
@@ -111,5 +43,6 @@ export function streamStructure(model, structureDefinition, prompt, options) {
|
|
111
43
|
response: lastFullDelta,
|
112
44
|
value: lastStructure,
|
113
45
|
}),
|
114
|
-
})
|
46
|
+
});
|
47
|
+
return options?.returnType === "full" ? fullResponse : fullResponse.value;
|
115
48
|
}
|
@@ -2,30 +2,8 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.generateText = void 0;
|
4
4
|
const executeStandardCall_js_1 = require("../executeStandardCall.cjs");
|
5
|
-
|
6
|
-
|
7
|
-
* Generate text for a prompt and return it as a string.
|
8
|
-
*
|
9
|
-
* The prompt depends on the model used.
|
10
|
-
* For instance, OpenAI completion models expect a string prompt,
|
11
|
-
* whereas OpenAI chat models expect an array of chat messages.
|
12
|
-
*
|
13
|
-
* @see https://modelfusion.dev/guide/function/generate-text
|
14
|
-
*
|
15
|
-
* @example
|
16
|
-
* const text = await generateText(
|
17
|
-
* new OpenAICompletionModel(...),
|
18
|
-
* "Write a short story about a robot learning to love:\n\n"
|
19
|
-
* );
|
20
|
-
*
|
21
|
-
* @param {TextGenerationModel<PROMPT, TextGenerationModelSettings>} model - The text generation model to use.
|
22
|
-
* @param {PROMPT} prompt - The prompt to use for text generation.
|
23
|
-
* @param {FunctionOptions} [options] - Optional parameters for the function.
|
24
|
-
*
|
25
|
-
* @returns {ModelFunctionPromise<string>} - A promise that resolves to the generated text.
|
26
|
-
*/
|
27
|
-
function generateText(model, prompt, options) {
|
28
|
-
return new ModelFunctionPromise_js_1.ModelFunctionPromise((0, executeStandardCall_js_1.executeStandardCall)({
|
5
|
+
async function generateText(model, prompt, options) {
|
6
|
+
const fullResponse = await (0, executeStandardCall_js_1.executeStandardCall)({
|
29
7
|
functionType: "generate-text",
|
30
8
|
input: prompt,
|
31
9
|
model,
|
@@ -35,12 +13,11 @@ function generateText(model, prompt, options) {
|
|
35
13
|
const shouldTrimWhitespace = model.settings.trimWhitespace ?? true;
|
36
14
|
return {
|
37
15
|
response: result.response,
|
38
|
-
extractedValue: shouldTrimWhitespace
|
39
|
-
? result.text.trim()
|
40
|
-
: result.text,
|
16
|
+
extractedValue: shouldTrimWhitespace ? result.text.trim() : result.text,
|
41
17
|
usage: result.usage,
|
42
18
|
};
|
43
19
|
},
|
44
|
-
})
|
20
|
+
});
|
21
|
+
return options?.returnType === "full" ? fullResponse : fullResponse.value;
|
45
22
|
}
|
46
23
|
exports.generateText = generateText;
|