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