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
@@ -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>>;
@@ -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, prompt, options),
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
- * Generates a text using a prompt.
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
- * @example
12
- * const model = new OpenAICompletionModel(...);
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
- * model,
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
- * Generates a text using a prompt.
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
- * @example
10
- * const model = new OpenAICompletionModel(...);
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
- * model,
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>;
@@ -1,17 +1,25 @@
1
1
  import { executeStandardCall } from "../executeStandardCall.js";
2
2
  import { ModelFunctionPromise } from "../ModelFunctionPromise.js";
3
3
  /**
4
- * Generates a text using a prompt.
5
- * The prompt format depends on the model.
6
- * For example, OpenAI text models expect a string prompt, and OpenAI chat models expect an array of chat messages.
4
+ * Generate text for a prompt and return it as a string.
7
5
  *
8
- * @example
9
- * const model = new OpenAICompletionModel(...);
6
+ * The prompt depends on the model used.
7
+ * For instance, OpenAI completion models expect a string prompt,
8
+ * whereas OpenAI chat models expect an array of chat messages.
9
+ *
10
+ * @see https://modelfusion.dev/guide/function/generate-text
10
11
  *
12
+ * @example
11
13
  * const text = await generateText(
12
- * model,
14
+ * new OpenAICompletionModel(...),
13
15
  * "Write a short story about a robot learning to love:\n\n"
14
16
  * );
17
+ *
18
+ * @param {TextGenerationModel<PROMPT, TextGenerationModelSettings>} model - The text generation model to use.
19
+ * @param {PROMPT} prompt - The prompt to use for text generation.
20
+ * @param {FunctionOptions} [options] - Optional parameters for the function.
21
+ *
22
+ * @returns {ModelFunctionPromise<string>} - A promise that resolves to the generated text.
15
23
  */
16
24
  export function generateText(model, prompt, options) {
17
25
  return new ModelFunctionPromise(executeStandardCall({
@@ -3,6 +3,31 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.streamText = void 0;
4
4
  const AsyncIterableResultPromise_js_1 = require("../AsyncIterableResultPromise.cjs");
5
5
  const executeStreamCall_js_1 = require("../executeStreamCall.cjs");
6
+ /**
7
+ * Stream the generated text for a prompt as an async iterable.
8
+ *
9
+ * The prompt depends on the model used.
10
+ * For instance, OpenAI completion models expect a string prompt,
11
+ * whereas OpenAI chat models expect an array of chat messages.
12
+ *
13
+ * @see https://modelfusion.dev/guide/function/generate-text
14
+ *
15
+ * @example
16
+ * const textStream = await streamText(
17
+ * new OpenAICompletionModel(...),
18
+ * "Write a short story about a robot learning to love:\n\n"
19
+ * );
20
+ *
21
+ * for await (const textPart of textStream) {
22
+ * // ...
23
+ * }
24
+ *
25
+ * @param {TextStreamingModel<PROMPT>} model - The model to stream text from.
26
+ * @param {PROMPT} prompt - The prompt to use for text generation.
27
+ * @param {FunctionOptions} [options] - Optional parameters for the function.
28
+ *
29
+ * @returns {AsyncIterableResultPromise<string>} An async iterable promise that yields the generated text.
30
+ */
6
31
  function streamText(model, prompt, options) {
7
32
  let accumulatedText = "";
8
33
  let lastFullDelta;
@@ -1,4 +1,29 @@
1
1
  import { FunctionOptions } from "../../core/FunctionOptions.js";
2
2
  import { AsyncIterableResultPromise } from "../AsyncIterableResultPromise.js";
3
3
  import { TextStreamingModel } from "./TextGenerationModel.js";
4
+ /**
5
+ * Stream the generated text for a prompt as an async iterable.
6
+ *
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
12
+ *
13
+ * @example
14
+ * const textStream = await streamText(
15
+ * new OpenAICompletionModel(...),
16
+ * "Write a short story about a robot learning to love:\n\n"
17
+ * );
18
+ *
19
+ * for await (const textPart of textStream) {
20
+ * // ...
21
+ * }
22
+ *
23
+ * @param {TextStreamingModel<PROMPT>} model - The model to stream text from.
24
+ * @param {PROMPT} prompt - The prompt to use for text generation.
25
+ * @param {FunctionOptions} [options] - Optional parameters for the function.
26
+ *
27
+ * @returns {AsyncIterableResultPromise<string>} An async iterable promise that yields the generated text.
28
+ */
4
29
  export declare function streamText<PROMPT>(model: TextStreamingModel<PROMPT>, prompt: PROMPT, options?: FunctionOptions): AsyncIterableResultPromise<string>;
@@ -1,5 +1,30 @@
1
1
  import { AsyncIterableResultPromise } from "../AsyncIterableResultPromise.js";
2
2
  import { executeStreamCall } from "../executeStreamCall.js";
3
+ /**
4
+ * Stream the generated text for a prompt as an async iterable.
5
+ *
6
+ * The prompt depends on the model used.
7
+ * For instance, OpenAI completion models expect a string prompt,
8
+ * whereas OpenAI chat models expect an array of chat messages.
9
+ *
10
+ * @see https://modelfusion.dev/guide/function/generate-text
11
+ *
12
+ * @example
13
+ * const textStream = await streamText(
14
+ * new OpenAICompletionModel(...),
15
+ * "Write a short story about a robot learning to love:\n\n"
16
+ * );
17
+ *
18
+ * for await (const textPart of textStream) {
19
+ * // ...
20
+ * }
21
+ *
22
+ * @param {TextStreamingModel<PROMPT>} model - The model to stream text from.
23
+ * @param {PROMPT} prompt - The prompt to use for text generation.
24
+ * @param {FunctionOptions} [options] - Optional parameters for the function.
25
+ *
26
+ * @returns {AsyncIterableResultPromise<string>} An async iterable promise that yields the generated text.
27
+ */
3
28
  export function streamText(model, prompt, options) {
4
29
  let accumulatedText = "";
5
30
  let lastFullDelta;
@@ -4,18 +4,23 @@ exports.generateTranscription = void 0;
4
4
  const executeStandardCall_js_1 = require("../executeStandardCall.cjs");
5
5
  const ModelFunctionPromise_js_1 = require("../ModelFunctionPromise.cjs");
6
6
  /**
7
- * Transcribe audio data into text.
7
+ * Transcribe audio data into text. Also called speech-to-text (STT) or automatic speech recognition (ASR).
8
+ *
9
+ * @see https://modelfusion.dev/guide/function/generate-transcription
8
10
  *
9
11
  * @example
10
12
  * const data = await fs.promises.readFile("data/test.mp3");
11
13
  *
12
14
  * const transcription = await generateTranscription(
13
15
  * new OpenAITranscriptionModel({ model: "whisper-1" }),
14
- * {
15
- * type: "mp3",
16
- * data,
17
- * }
16
+ * { type: "mp3", data }
18
17
  * );
18
+ *
19
+ * @param {TranscriptionModel<DATA, TranscriptionModelSettings>} model - The model to use for transcription.
20
+ * @param {DATA} data - The data to transcribe.
21
+ * @param {FunctionOptions} [options] - Optional parameters for the function.
22
+ *
23
+ * @returns {ModelFunctionPromise<string>} A promise that resolves to the transcribed text.
19
24
  */
20
25
  function generateTranscription(model, data, options) {
21
26
  return new ModelFunctionPromise_js_1.ModelFunctionPromise((0, executeStandardCall_js_1.executeStandardCall)({
@@ -2,17 +2,22 @@ import { FunctionOptions } from "../../core/FunctionOptions.js";
2
2
  import { ModelFunctionPromise } from "../ModelFunctionPromise.js";
3
3
  import { TranscriptionModel, TranscriptionModelSettings } from "./TranscriptionModel.js";
4
4
  /**
5
- * Transcribe audio data into text.
5
+ * Transcribe audio data into text. Also called speech-to-text (STT) or automatic speech recognition (ASR).
6
+ *
7
+ * @see https://modelfusion.dev/guide/function/generate-transcription
6
8
  *
7
9
  * @example
8
10
  * const data = await fs.promises.readFile("data/test.mp3");
9
11
  *
10
12
  * const transcription = await generateTranscription(
11
13
  * new OpenAITranscriptionModel({ model: "whisper-1" }),
12
- * {
13
- * type: "mp3",
14
- * data,
15
- * }
14
+ * { type: "mp3", data }
16
15
  * );
16
+ *
17
+ * @param {TranscriptionModel<DATA, TranscriptionModelSettings>} model - The model to use for transcription.
18
+ * @param {DATA} data - The data to transcribe.
19
+ * @param {FunctionOptions} [options] - Optional parameters for the function.
20
+ *
21
+ * @returns {ModelFunctionPromise<string>} A promise that resolves to the transcribed text.
17
22
  */
18
23
  export declare function generateTranscription<DATA>(model: TranscriptionModel<DATA, TranscriptionModelSettings>, data: DATA, options?: FunctionOptions): ModelFunctionPromise<string>;
@@ -1,18 +1,23 @@
1
1
  import { executeStandardCall } from "../executeStandardCall.js";
2
2
  import { ModelFunctionPromise } from "../ModelFunctionPromise.js";
3
3
  /**
4
- * Transcribe audio data into text.
4
+ * Transcribe audio data into text. Also called speech-to-text (STT) or automatic speech recognition (ASR).
5
+ *
6
+ * @see https://modelfusion.dev/guide/function/generate-transcription
5
7
  *
6
8
  * @example
7
9
  * const data = await fs.promises.readFile("data/test.mp3");
8
10
  *
9
11
  * const transcription = await generateTranscription(
10
12
  * new OpenAITranscriptionModel({ model: "whisper-1" }),
11
- * {
12
- * type: "mp3",
13
- * data,
14
- * }
13
+ * { type: "mp3", data }
15
14
  * );
15
+ *
16
+ * @param {TranscriptionModel<DATA, TranscriptionModelSettings>} model - The model to use for transcription.
17
+ * @param {DATA} data - The data to transcribe.
18
+ * @param {FunctionOptions} [options] - Optional parameters for the function.
19
+ *
20
+ * @returns {ModelFunctionPromise<string>} A promise that resolves to the transcribed text.
16
21
  */
17
22
  export function generateTranscription(model, data, options) {
18
23
  return new ModelFunctionPromise(executeStandardCall({