modelfusion 0.53.1 → 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.
Files changed (33) hide show
  1. package/core/FunctionOptions.d.ts +4 -3
  2. package/model-function/embed/embed.cjs +16 -0
  3. package/model-function/embed/embed.d.ts +16 -0
  4. package/model-function/embed/embed.js +16 -0
  5. package/model-function/generate-image/generateImage.cjs +12 -3
  6. package/model-function/generate-image/generateImage.d.ts +12 -3
  7. package/model-function/generate-image/generateImage.js +12 -3
  8. package/model-function/generate-speech/generateSpeech.cjs +16 -1
  9. package/model-function/generate-speech/generateSpeech.d.ts +16 -1
  10. package/model-function/generate-speech/generateSpeech.js +16 -1
  11. package/model-function/generate-speech/streamSpeech.cjs +22 -1
  12. package/model-function/generate-speech/streamSpeech.d.ts +22 -1
  13. package/model-function/generate-speech/streamSpeech.js +22 -1
  14. package/model-function/generate-structure/generateStructure.cjs +41 -0
  15. package/model-function/generate-structure/generateStructure.d.ts +41 -0
  16. package/model-function/generate-structure/generateStructure.js +41 -0
  17. package/model-function/generate-structure/generateStructureOrText.cjs +62 -0
  18. package/model-function/generate-structure/generateStructureOrText.d.ts +62 -0
  19. package/model-function/generate-structure/generateStructureOrText.js +62 -0
  20. package/model-function/generate-structure/streamStructure.cjs +72 -1
  21. package/model-function/generate-structure/streamStructure.d.ts +68 -1
  22. package/model-function/generate-structure/streamStructure.js +72 -1
  23. package/model-function/generate-text/generateText.cjs +14 -6
  24. package/model-function/generate-text/generateText.d.ts +14 -6
  25. package/model-function/generate-text/generateText.js +14 -6
  26. package/model-function/generate-text/streamText.cjs +25 -0
  27. package/model-function/generate-text/streamText.d.ts +25 -0
  28. package/model-function/generate-text/streamText.js +25 -0
  29. package/model-function/generate-transcription/generateTranscription.cjs +10 -5
  30. package/model-function/generate-transcription/generateTranscription.d.ts +10 -5
  31. package/model-function/generate-transcription/generateTranscription.js +10 -5
  32. package/model-function/tokenize-text/Tokenizer.d.ts +27 -3
  33. package/package.json +1 -1
@@ -5,7 +5,7 @@ import { FunctionObserver } from "./FunctionObserver.js";
5
5
  */
6
6
  export type FunctionOptions = {
7
7
  /**
8
- * Optional function identifier that is used in events to identify the function.
8
+ * Optional function identifier. Used in events and logging.
9
9
  */
10
10
  functionId?: string;
11
11
  /**
@@ -18,11 +18,12 @@ export type FunctionOptions = {
18
18
  */
19
19
  observers?: Array<FunctionObserver>;
20
20
  /**
21
- * Optional run as part of which this function is called.
21
+ * Optional run as part of which this function is called. Used in events and logging.
22
+ * Run callbacks are invoked when it is provided.
22
23
  */
23
24
  run?: Run;
24
25
  /**
25
- * Unique identifier of the call id of the parent function.
26
+ * Unique identifier of the call id of the parent function. Used in events and logging.
26
27
  */
27
28
  parentCallId?: string | undefined;
28
29
  };
@@ -6,6 +6,8 @@ const ModelFunctionPromise_js_1 = require("../ModelFunctionPromise.cjs");
6
6
  /**
7
7
  * Generate embeddings for multiple values.
8
8
  *
9
+ * @see https://modelfusion.dev/guide/function/embed
10
+ *
9
11
  * @example
10
12
  * const embeddings = await embedMany(
11
13
  * new OpenAITextEmbeddingModel(...),
@@ -14,6 +16,12 @@ const ModelFunctionPromise_js_1 = require("../ModelFunctionPromise.cjs");
14
16
  * "He keenly observed and absorbed everything around him, from the birds in the sky to the trees in the forest.",
15
17
  * ]
16
18
  * );
19
+ *
20
+ * @param {EmbeddingModel<VALUE, EmbeddingModelSettings>} model - The model to use for generating embeddings.
21
+ * @param {VALUE[]} values - The values to generate embeddings for.
22
+ * @param {FunctionOptions} [options] - Optional settings for the function.
23
+ *
24
+ * @returns {ModelFunctionPromise<Vector[]>} - A promise that resolves to an array of vectors representing the embeddings.
17
25
  */
18
26
  function embedMany(model, values, options) {
19
27
  return new ModelFunctionPromise_js_1.ModelFunctionPromise((0, executeStandardCall_js_1.executeStandardCall)({
@@ -50,11 +58,19 @@ exports.embedMany = embedMany;
50
58
  /**
51
59
  * Generate an embedding for a single value.
52
60
  *
61
+ * @see https://modelfusion.dev/guide/function/embed
62
+ *
53
63
  * @example
54
64
  * const embedding = await embed(
55
65
  * new OpenAITextEmbeddingModel(...),
56
66
  * "At first, Nox didn't know what to do with the pup."
57
67
  * );
68
+ *
69
+ * @param {EmbeddingModel<VALUE, EmbeddingModelSettings>} model - The model to use for generating the embedding.
70
+ * @param {VALUE} value - The value to generate an embedding for.
71
+ * @param {FunctionOptions} [options] - Optional settings for the function.
72
+ *
73
+ * @returns {ModelFunctionPromise<Vector>} - A promise that resolves to a vector representing the embedding.
58
74
  */
59
75
  function embed(model, value, options) {
60
76
  return new ModelFunctionPromise_js_1.ModelFunctionPromise((0, executeStandardCall_js_1.executeStandardCall)({
@@ -5,6 +5,8 @@ import { EmbeddingModel, EmbeddingModelSettings } from "./EmbeddingModel.js";
5
5
  /**
6
6
  * Generate embeddings for multiple values.
7
7
  *
8
+ * @see https://modelfusion.dev/guide/function/embed
9
+ *
8
10
  * @example
9
11
  * const embeddings = await embedMany(
10
12
  * new OpenAITextEmbeddingModel(...),
@@ -13,15 +15,29 @@ import { EmbeddingModel, EmbeddingModelSettings } from "./EmbeddingModel.js";
13
15
  * "He keenly observed and absorbed everything around him, from the birds in the sky to the trees in the forest.",
14
16
  * ]
15
17
  * );
18
+ *
19
+ * @param {EmbeddingModel<VALUE, EmbeddingModelSettings>} model - The model to use for generating embeddings.
20
+ * @param {VALUE[]} values - The values to generate embeddings for.
21
+ * @param {FunctionOptions} [options] - Optional settings for the function.
22
+ *
23
+ * @returns {ModelFunctionPromise<Vector[]>} - A promise that resolves to an array of vectors representing the embeddings.
16
24
  */
17
25
  export declare function embedMany<VALUE>(model: EmbeddingModel<VALUE, EmbeddingModelSettings>, values: VALUE[], options?: FunctionOptions): ModelFunctionPromise<Vector[]>;
18
26
  /**
19
27
  * Generate an embedding for a single value.
20
28
  *
29
+ * @see https://modelfusion.dev/guide/function/embed
30
+ *
21
31
  * @example
22
32
  * const embedding = await embed(
23
33
  * new OpenAITextEmbeddingModel(...),
24
34
  * "At first, Nox didn't know what to do with the pup."
25
35
  * );
36
+ *
37
+ * @param {EmbeddingModel<VALUE, EmbeddingModelSettings>} model - The model to use for generating the embedding.
38
+ * @param {VALUE} value - The value to generate an embedding for.
39
+ * @param {FunctionOptions} [options] - Optional settings for the function.
40
+ *
41
+ * @returns {ModelFunctionPromise<Vector>} - A promise that resolves to a vector representing the embedding.
26
42
  */
27
43
  export declare function embed<VALUE>(model: EmbeddingModel<VALUE, EmbeddingModelSettings>, value: VALUE, options?: FunctionOptions): ModelFunctionPromise<Vector>;
@@ -3,6 +3,8 @@ import { ModelFunctionPromise } from "../ModelFunctionPromise.js";
3
3
  /**
4
4
  * Generate embeddings for multiple values.
5
5
  *
6
+ * @see https://modelfusion.dev/guide/function/embed
7
+ *
6
8
  * @example
7
9
  * const embeddings = await embedMany(
8
10
  * new OpenAITextEmbeddingModel(...),
@@ -11,6 +13,12 @@ import { ModelFunctionPromise } from "../ModelFunctionPromise.js";
11
13
  * "He keenly observed and absorbed everything around him, from the birds in the sky to the trees in the forest.",
12
14
  * ]
13
15
  * );
16
+ *
17
+ * @param {EmbeddingModel<VALUE, EmbeddingModelSettings>} model - The model to use for generating embeddings.
18
+ * @param {VALUE[]} values - The values to generate embeddings for.
19
+ * @param {FunctionOptions} [options] - Optional settings for the function.
20
+ *
21
+ * @returns {ModelFunctionPromise<Vector[]>} - A promise that resolves to an array of vectors representing the embeddings.
14
22
  */
15
23
  export function embedMany(model, values, options) {
16
24
  return new ModelFunctionPromise(executeStandardCall({
@@ -46,11 +54,19 @@ export function embedMany(model, values, options) {
46
54
  /**
47
55
  * Generate an embedding for a single value.
48
56
  *
57
+ * @see https://modelfusion.dev/guide/function/embed
58
+ *
49
59
  * @example
50
60
  * const embedding = await embed(
51
61
  * new OpenAITextEmbeddingModel(...),
52
62
  * "At first, Nox didn't know what to do with the pup."
53
63
  * );
64
+ *
65
+ * @param {EmbeddingModel<VALUE, EmbeddingModelSettings>} model - The model to use for generating the embedding.
66
+ * @param {VALUE} value - The value to generate an embedding for.
67
+ * @param {FunctionOptions} [options] - Optional settings for the function.
68
+ *
69
+ * @returns {ModelFunctionPromise<Vector>} - A promise that resolves to a vector representing the embedding.
54
70
  */
55
71
  export function embed(model, value, options) {
56
72
  return new ModelFunctionPromise(executeStandardCall({
@@ -4,11 +4,13 @@ exports.generateImage = void 0;
4
4
  const executeStandardCall_js_1 = require("../executeStandardCall.cjs");
5
5
  const ImageGenerationPromise_js_1 = require("./ImageGenerationPromise.cjs");
6
6
  /**
7
- * Generates a base64-encoded image using a prompt.
8
- * The prompt format depends on the model.
9
- * For example, OpenAI image models expect a string prompt,
7
+ * Generates an image using a prompt.
8
+ *
9
+ * The prompt depends on the model. For example, OpenAI image models expect a string prompt,
10
10
  * and Stability AI models expect an array of text prompts with optional weights.
11
11
  *
12
+ * @see https://modelfusion.dev/guide/function/generate-image
13
+ *
12
14
  * @example
13
15
  * const image = await generateImage(
14
16
  * new StabilityImageGenerationModel(...),
@@ -17,6 +19,13 @@ const ImageGenerationPromise_js_1 = require("./ImageGenerationPromise.cjs");
17
19
  * { text: "style of early 19th century painting", weight: 0.5 },
18
20
  * ]
19
21
  * );
22
+ *
23
+ * @param {ImageGenerationModel<PROMPT, ImageGenerationModelSettings>} model - The image generation model to be used.
24
+ * @param {PROMPT} prompt - The prompt to be used for image generation.
25
+ * @param {FunctionOptions} [options] - Optional settings for the function.
26
+ *
27
+ * @returns {ImageGenerationPromise} - Returns a promise that resolves to the generated image.
28
+ * The image is a Buffer containing the image data in PNG format.
20
29
  */
21
30
  function generateImage(model, prompt, options) {
22
31
  return new ImageGenerationPromise_js_1.ImageGenerationPromise((0, executeStandardCall_js_1.executeStandardCall)({
@@ -2,11 +2,13 @@ import { FunctionOptions } from "../../core/FunctionOptions.js";
2
2
  import { ImageGenerationModel, ImageGenerationModelSettings } from "./ImageGenerationModel.js";
3
3
  import { ImageGenerationPromise } from "./ImageGenerationPromise.js";
4
4
  /**
5
- * Generates a base64-encoded image using a prompt.
6
- * The prompt format depends on the model.
7
- * For example, OpenAI image models expect a string prompt,
5
+ * Generates an image using a prompt.
6
+ *
7
+ * The prompt depends on the model. For example, OpenAI image models expect a string prompt,
8
8
  * and Stability AI models expect an array of text prompts with optional weights.
9
9
  *
10
+ * @see https://modelfusion.dev/guide/function/generate-image
11
+ *
10
12
  * @example
11
13
  * const image = await generateImage(
12
14
  * new StabilityImageGenerationModel(...),
@@ -15,5 +17,12 @@ import { ImageGenerationPromise } from "./ImageGenerationPromise.js";
15
17
  * { text: "style of early 19th century painting", weight: 0.5 },
16
18
  * ]
17
19
  * );
20
+ *
21
+ * @param {ImageGenerationModel<PROMPT, ImageGenerationModelSettings>} model - The image generation model to be used.
22
+ * @param {PROMPT} prompt - The prompt to be used for image generation.
23
+ * @param {FunctionOptions} [options] - Optional settings for the function.
24
+ *
25
+ * @returns {ImageGenerationPromise} - Returns a promise that resolves to the generated image.
26
+ * The image is a Buffer containing the image data in PNG format.
18
27
  */
19
28
  export declare function generateImage<PROMPT>(model: ImageGenerationModel<PROMPT, ImageGenerationModelSettings>, prompt: PROMPT, options?: FunctionOptions): ImageGenerationPromise;
@@ -1,11 +1,13 @@
1
1
  import { executeStandardCall } from "../executeStandardCall.js";
2
2
  import { ImageGenerationPromise } from "./ImageGenerationPromise.js";
3
3
  /**
4
- * Generates a base64-encoded image using a prompt.
5
- * The prompt format depends on the model.
6
- * For example, OpenAI image models expect a string prompt,
4
+ * Generates an image using a prompt.
5
+ *
6
+ * The prompt depends on the model. For example, OpenAI image models expect a string prompt,
7
7
  * and Stability AI models expect an array of text prompts with optional weights.
8
8
  *
9
+ * @see https://modelfusion.dev/guide/function/generate-image
10
+ *
9
11
  * @example
10
12
  * const image = await generateImage(
11
13
  * new StabilityImageGenerationModel(...),
@@ -14,6 +16,13 @@ import { ImageGenerationPromise } from "./ImageGenerationPromise.js";
14
16
  * { text: "style of early 19th century painting", weight: 0.5 },
15
17
  * ]
16
18
  * );
19
+ *
20
+ * @param {ImageGenerationModel<PROMPT, ImageGenerationModelSettings>} model - The image generation model to be used.
21
+ * @param {PROMPT} prompt - The prompt to be used for image generation.
22
+ * @param {FunctionOptions} [options] - Optional settings for the function.
23
+ *
24
+ * @returns {ImageGenerationPromise} - Returns a promise that resolves to the generated image.
25
+ * The image is a Buffer containing the image data in PNG format.
17
26
  */
18
27
  export function generateImage(model, prompt, options) {
19
28
  return new ImageGenerationPromise(executeStandardCall({
@@ -4,7 +4,22 @@ exports.generateSpeech = void 0;
4
4
  const ModelFunctionPromise_js_1 = require("../ModelFunctionPromise.cjs");
5
5
  const executeStandardCall_js_1 = require("../executeStandardCall.cjs");
6
6
  /**
7
- * Synthesizes speech from text.
7
+ * Synthesizes speech from text. Also called text-to-speech (TTS).
8
+ *
9
+ * @see https://modelfusion.dev/guide/function/generate-speech
10
+ *
11
+ * @example
12
+ * const speech = await generateSpeech(
13
+ * new LmntSpeechModel(...),
14
+ * "Good evening, ladies and gentlemen! Exciting news on the airwaves tonight " +
15
+ * "as The Rolling Stones unveil 'Hackney Diamonds.'
16
+ * );
17
+ *
18
+ * @param {SpeechGenerationModel<SpeechGenerationModelSettings>} model - The speech generation model.
19
+ * @param {string} text - The text to be converted to speech.
20
+ * @param {FunctionOptions} [options] - Optional function options.
21
+ *
22
+ * @returns {ModelFunctionPromise<Buffer>} - A promise that resolves to a buffer containing the synthesized speech.
8
23
  */
9
24
  function generateSpeech(model, text, options) {
10
25
  return new ModelFunctionPromise_js_1.ModelFunctionPromise((0, executeStandardCall_js_1.executeStandardCall)({
@@ -3,6 +3,21 @@ import { FunctionOptions } from "../../core/FunctionOptions.js";
3
3
  import { ModelFunctionPromise } from "../ModelFunctionPromise.js";
4
4
  import { SpeechGenerationModel, SpeechGenerationModelSettings } from "./SpeechGenerationModel.js";
5
5
  /**
6
- * Synthesizes speech from text.
6
+ * Synthesizes speech from text. Also called text-to-speech (TTS).
7
+ *
8
+ * @see https://modelfusion.dev/guide/function/generate-speech
9
+ *
10
+ * @example
11
+ * const speech = await generateSpeech(
12
+ * new LmntSpeechModel(...),
13
+ * "Good evening, ladies and gentlemen! Exciting news on the airwaves tonight " +
14
+ * "as The Rolling Stones unveil 'Hackney Diamonds.'
15
+ * );
16
+ *
17
+ * @param {SpeechGenerationModel<SpeechGenerationModelSettings>} model - The speech generation model.
18
+ * @param {string} text - The text to be converted to speech.
19
+ * @param {FunctionOptions} [options] - Optional function options.
20
+ *
21
+ * @returns {ModelFunctionPromise<Buffer>} - A promise that resolves to a buffer containing the synthesized speech.
7
22
  */
8
23
  export declare function generateSpeech(model: SpeechGenerationModel<SpeechGenerationModelSettings>, text: string, options?: FunctionOptions): ModelFunctionPromise<Buffer>;
@@ -1,7 +1,22 @@
1
1
  import { ModelFunctionPromise } from "../ModelFunctionPromise.js";
2
2
  import { executeStandardCall } from "../executeStandardCall.js";
3
3
  /**
4
- * Synthesizes speech from text.
4
+ * Synthesizes speech from text. Also called text-to-speech (TTS).
5
+ *
6
+ * @see https://modelfusion.dev/guide/function/generate-speech
7
+ *
8
+ * @example
9
+ * const speech = await generateSpeech(
10
+ * new LmntSpeechModel(...),
11
+ * "Good evening, ladies and gentlemen! Exciting news on the airwaves tonight " +
12
+ * "as The Rolling Stones unveil 'Hackney Diamonds.'
13
+ * );
14
+ *
15
+ * @param {SpeechGenerationModel<SpeechGenerationModelSettings>} model - The speech generation model.
16
+ * @param {string} text - The text to be converted to speech.
17
+ * @param {FunctionOptions} [options] - Optional function options.
18
+ *
19
+ * @returns {ModelFunctionPromise<Buffer>} - A promise that resolves to a buffer containing the synthesized speech.
5
20
  */
6
21
  export function generateSpeech(model, text, options) {
7
22
  return new ModelFunctionPromise(executeStandardCall({
@@ -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"