modelfusion 0.53.1 → 0.54.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.
Files changed (98) hide show
  1. package/README.md +2 -2
  2. package/browser/convertAudioChunksToBase64.cjs +8 -0
  3. package/browser/convertAudioChunksToBase64.d.ts +4 -0
  4. package/browser/convertAudioChunksToBase64.js +4 -0
  5. package/browser/convertBlobToBase64.cjs +23 -0
  6. package/browser/convertBlobToBase64.d.ts +1 -0
  7. package/browser/convertBlobToBase64.js +19 -0
  8. package/{ui → browser}/index.cjs +5 -0
  9. package/browser/index.d.ts +6 -0
  10. package/browser/index.js +6 -0
  11. package/browser/invokeFlow.cjs +23 -0
  12. package/browser/invokeFlow.d.ts +8 -0
  13. package/browser/invokeFlow.js +19 -0
  14. package/{event-source → browser}/readEventSource.cjs +8 -3
  15. package/{event-source → browser}/readEventSource.d.ts +3 -1
  16. package/{event-source → browser}/readEventSource.js +8 -3
  17. package/{event-source → browser}/readEventSourceStream.cjs +1 -1
  18. package/{event-source → browser}/readEventSourceStream.js +1 -1
  19. package/core/FunctionOptions.d.ts +4 -3
  20. package/event-source/createEventSourceStream.cjs +7 -3
  21. package/event-source/createEventSourceStream.js +7 -3
  22. package/event-source/index.cjs +0 -2
  23. package/event-source/index.d.ts +0 -2
  24. package/event-source/index.js +0 -2
  25. package/index.cjs +0 -1
  26. package/index.d.ts +0 -1
  27. package/index.js +0 -1
  28. package/model-function/embed/embed.cjs +16 -0
  29. package/model-function/embed/embed.d.ts +16 -0
  30. package/model-function/embed/embed.js +16 -0
  31. package/model-function/generate-image/generateImage.cjs +12 -3
  32. package/model-function/generate-image/generateImage.d.ts +12 -3
  33. package/model-function/generate-image/generateImage.js +12 -3
  34. package/model-function/generate-speech/generateSpeech.cjs +16 -1
  35. package/model-function/generate-speech/generateSpeech.d.ts +16 -1
  36. package/model-function/generate-speech/generateSpeech.js +16 -1
  37. package/model-function/generate-speech/streamSpeech.cjs +22 -1
  38. package/model-function/generate-speech/streamSpeech.d.ts +22 -1
  39. package/model-function/generate-speech/streamSpeech.js +22 -1
  40. package/model-function/generate-structure/generateStructure.cjs +41 -0
  41. package/model-function/generate-structure/generateStructure.d.ts +41 -0
  42. package/model-function/generate-structure/generateStructure.js +41 -0
  43. package/model-function/generate-structure/generateStructureOrText.cjs +62 -0
  44. package/model-function/generate-structure/generateStructureOrText.d.ts +62 -0
  45. package/model-function/generate-structure/generateStructureOrText.js +62 -0
  46. package/model-function/generate-structure/streamStructure.cjs +72 -1
  47. package/model-function/generate-structure/streamStructure.d.ts +68 -1
  48. package/model-function/generate-structure/streamStructure.js +72 -1
  49. package/model-function/generate-text/generateText.cjs +14 -6
  50. package/model-function/generate-text/generateText.d.ts +14 -6
  51. package/model-function/generate-text/generateText.js +14 -6
  52. package/model-function/generate-text/streamText.cjs +25 -0
  53. package/model-function/generate-text/streamText.d.ts +25 -0
  54. package/model-function/generate-text/streamText.js +25 -0
  55. package/model-function/generate-transcription/generateTranscription.cjs +10 -5
  56. package/model-function/generate-transcription/generateTranscription.d.ts +10 -5
  57. package/model-function/generate-transcription/generateTranscription.js +10 -5
  58. package/model-function/tokenize-text/Tokenizer.d.ts +27 -3
  59. package/package.json +13 -4
  60. package/server/fastify/AssetStorage.cjs +2 -0
  61. package/server/fastify/AssetStorage.d.ts +17 -0
  62. package/server/fastify/AssetStorage.js +1 -0
  63. package/server/fastify/DefaultFlow.cjs +22 -0
  64. package/server/fastify/DefaultFlow.d.ts +16 -0
  65. package/server/fastify/DefaultFlow.js +18 -0
  66. package/server/fastify/FileSystemAssetStorage.cjs +60 -0
  67. package/server/fastify/FileSystemAssetStorage.d.ts +19 -0
  68. package/server/fastify/FileSystemAssetStorage.js +56 -0
  69. package/server/fastify/FileSystemLogger.cjs +48 -0
  70. package/server/fastify/FileSystemLogger.d.ts +18 -0
  71. package/server/fastify/FileSystemLogger.js +44 -0
  72. package/server/fastify/Flow.cjs +2 -0
  73. package/server/fastify/Flow.d.ts +9 -0
  74. package/server/fastify/Flow.js +1 -0
  75. package/server/fastify/FlowRun.cjs +71 -0
  76. package/server/fastify/FlowRun.d.ts +28 -0
  77. package/server/fastify/FlowRun.js +67 -0
  78. package/server/fastify/FlowSchema.cjs +2 -0
  79. package/server/fastify/FlowSchema.d.ts +5 -0
  80. package/server/fastify/FlowSchema.js +1 -0
  81. package/server/fastify/Logger.cjs +2 -0
  82. package/server/fastify/Logger.d.ts +13 -0
  83. package/server/fastify/Logger.js +1 -0
  84. package/server/fastify/PathProvider.cjs +34 -0
  85. package/server/fastify/PathProvider.d.ts +12 -0
  86. package/server/fastify/PathProvider.js +30 -0
  87. package/server/fastify/index.cjs +24 -0
  88. package/server/fastify/index.d.ts +8 -0
  89. package/server/fastify/index.js +8 -0
  90. package/server/fastify/modelFusionFlowPlugin.cjs +102 -0
  91. package/server/fastify/modelFusionFlowPlugin.d.ts +12 -0
  92. package/server/fastify/modelFusionFlowPlugin.js +98 -0
  93. package/ui/index.d.ts +0 -1
  94. package/ui/index.js +0 -1
  95. /package/{ui → browser}/MediaSourceAppender.cjs +0 -0
  96. /package/{ui → browser}/MediaSourceAppender.d.ts +0 -0
  97. /package/{ui → browser}/MediaSourceAppender.js +0 -0
  98. /package/{event-source → browser}/readEventSourceStream.d.ts +0 -0
@@ -5,7 +5,28 @@ const AsyncQueue_js_1 = require("../../util/AsyncQueue.cjs");
5
5
  const AsyncIterableResultPromise_js_1 = require("../AsyncIterableResultPromise.cjs");
6
6
  const executeStreamCall_js_1 = require("../executeStreamCall.cjs");
7
7
  /**
8
- * Synthesizes speech from text.
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.
9
30
  */
10
31
  function streamSpeech(model, text, options) {
11
32
  let textStream;
@@ -3,6 +3,27 @@ import { FunctionOptions } from "../../core/FunctionOptions.js";
3
3
  import { AsyncIterableResultPromise } from "../AsyncIterableResultPromise.js";
4
4
  import { StreamingSpeechGenerationModel, SpeechGenerationModelSettings } from "./SpeechGenerationModel.js";
5
5
  /**
6
- * Synthesizes speech from text.
6
+ * Stream synthesized speech from text. Also called text-to-speech (TTS).
7
+ * Duplex streaming where both the input and output are streamed is supported.
8
+ *
9
+ * @see https://modelfusion.dev/guide/function/generate-speech
10
+ *
11
+ * @example
12
+ * const textStream = await streamText(...);
13
+ *
14
+ * const speechStream = await streamSpeech(
15
+ * new ElevenLabsSpeechModel(...),
16
+ * textStream
17
+ * );
18
+ *
19
+ * for await (const speechPart of speechStream) {
20
+ * // ...
21
+ * }
22
+ *
23
+ * @param {StreamingSpeechGenerationModel<SpeechGenerationModelSettings>} model - The speech generation model.
24
+ * @param {AsyncIterable<string> | string} text - The text to be converted to speech. Can be a string or an async iterable of strings.
25
+ * @param {FunctionOptions} [options] - Optional function options.
26
+ *
27
+ * @returns {AsyncIterableResultPromise<Buffer>} An async iterable promise that contains the synthesized speech chunks.
7
28
  */
8
29
  export declare function streamSpeech(model: StreamingSpeechGenerationModel<SpeechGenerationModelSettings>, text: AsyncIterable<string> | string, options?: FunctionOptions): AsyncIterableResultPromise<Buffer>;
@@ -2,7 +2,28 @@ import { AsyncQueue } from "../../util/AsyncQueue.js";
2
2
  import { AsyncIterableResultPromise } from "../AsyncIterableResultPromise.js";
3
3
  import { executeStreamCall } from "../executeStreamCall.js";
4
4
  /**
5
- * Synthesizes speech from text.
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.
6
27
  */
7
28
  export function streamSpeech(model, text, options) {
8
29
  let textStream;
@@ -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, prompt, options),
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
- export declare function streamStructure<STRUCTURE, PROMPT, NAME extends string>(model: StructureStreamingModel<PROMPT>, structureDefinition: StructureDefinition<NAME, STRUCTURE>, prompt: PROMPT, options?: FunctionOptions): AsyncIterableResultPromise<StructureStreamPart<STRUCTURE>>;
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>>;