modelfusion 0.121.2 → 0.123.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 (110) hide show
  1. package/CHANGELOG.md +78 -1
  2. package/README.md +127 -85
  3. package/index.cjs +0 -1
  4. package/index.d.ts +0 -1
  5. package/index.js +0 -1
  6. package/model-function/ModelCallEvent.d.ts +6 -2
  7. package/model-function/classify/Classifier.cjs +2 -0
  8. package/model-function/classify/Classifier.d.ts +10 -0
  9. package/model-function/classify/Classifier.js +1 -0
  10. package/model-function/classify/ClassifyEvent.cjs +2 -0
  11. package/model-function/classify/ClassifyEvent.d.ts +20 -0
  12. package/model-function/classify/ClassifyEvent.js +1 -0
  13. package/model-function/classify/EmbeddingSimilarityClassifier.cjs +97 -0
  14. package/model-function/classify/EmbeddingSimilarityClassifier.d.ts +40 -0
  15. package/model-function/classify/EmbeddingSimilarityClassifier.js +93 -0
  16. package/model-function/classify/classify.cjs +27 -0
  17. package/model-function/classify/classify.d.ts +17 -0
  18. package/model-function/classify/classify.js +23 -0
  19. package/{classifier → model-function/classify}/index.cjs +4 -1
  20. package/model-function/classify/index.d.ts +4 -0
  21. package/model-function/classify/index.js +4 -0
  22. package/model-function/embed/embed.cjs +14 -14
  23. package/model-function/embed/embed.d.ts +24 -18
  24. package/model-function/embed/embed.js +14 -14
  25. package/model-function/generate-image/generateImage.cjs +6 -6
  26. package/model-function/generate-image/generateImage.d.ts +12 -9
  27. package/model-function/generate-image/generateImage.js +6 -6
  28. package/model-function/generate-speech/generateSpeech.cjs +7 -7
  29. package/model-function/generate-speech/generateSpeech.d.ts +12 -9
  30. package/model-function/generate-speech/generateSpeech.js +7 -7
  31. package/model-function/generate-speech/streamSpeech.cjs +6 -6
  32. package/model-function/generate-speech/streamSpeech.d.ts +12 -8
  33. package/model-function/generate-speech/streamSpeech.js +6 -6
  34. package/model-function/generate-structure/StructureFromTextGenerationModel.cjs +5 -3
  35. package/model-function/generate-structure/StructureFromTextGenerationModel.d.ts +1 -1
  36. package/model-function/generate-structure/StructureFromTextGenerationModel.js +5 -3
  37. package/model-function/generate-structure/StructureFromTextStreamingModel.cjs +5 -1
  38. package/model-function/generate-structure/StructureFromTextStreamingModel.js +5 -1
  39. package/model-function/generate-structure/StructureGenerationModel.d.ts +1 -1
  40. package/model-function/generate-structure/generateStructure.cjs +8 -8
  41. package/model-function/generate-structure/generateStructure.d.ts +17 -10
  42. package/model-function/generate-structure/generateStructure.js +8 -8
  43. package/model-function/generate-structure/streamStructure.cjs +6 -6
  44. package/model-function/generate-structure/streamStructure.d.ts +16 -10
  45. package/model-function/generate-structure/streamStructure.js +6 -6
  46. package/model-function/generate-text/generateText.cjs +6 -6
  47. package/model-function/generate-text/generateText.d.ts +12 -9
  48. package/model-function/generate-text/generateText.js +6 -6
  49. package/model-function/generate-text/streamText.cjs +6 -6
  50. package/model-function/generate-text/streamText.d.ts +12 -8
  51. package/model-function/generate-text/streamText.js +6 -6
  52. package/model-function/generate-transcription/generateTranscription.cjs +3 -3
  53. package/model-function/generate-transcription/generateTranscription.d.ts +12 -9
  54. package/model-function/generate-transcription/generateTranscription.js +3 -3
  55. package/model-function/index.cjs +1 -0
  56. package/model-function/index.d.ts +1 -0
  57. package/model-function/index.js +1 -0
  58. package/model-provider/cohere/CohereTextGenerationModel.d.ts +12 -12
  59. package/model-provider/cohere/CohereTextGenerationModel.test.cjs +7 -4
  60. package/model-provider/cohere/CohereTextGenerationModel.test.js +7 -4
  61. package/model-provider/llamacpp/LlamaCppCompletionModel.d.ts +10 -10
  62. package/model-provider/llamacpp/LlamaCppCompletionModel.test.cjs +4 -1
  63. package/model-provider/llamacpp/LlamaCppCompletionModel.test.js +4 -1
  64. package/model-provider/mistral/MistralChatModel.test.cjs +15 -8
  65. package/model-provider/mistral/MistralChatModel.test.js +15 -8
  66. package/model-provider/mistral/MistralTextEmbeddingModel.d.ts +13 -13
  67. package/model-provider/ollama/OllamaChatModel.d.ts +9 -9
  68. package/model-provider/ollama/OllamaChatModel.test.cjs +6 -1
  69. package/model-provider/ollama/OllamaChatModel.test.js +6 -1
  70. package/model-provider/ollama/OllamaCompletionModel.test.cjs +31 -16
  71. package/model-provider/ollama/OllamaCompletionModel.test.js +31 -16
  72. package/model-provider/openai/OpenAIChatFunctionCallStructureGenerationModel.cjs +4 -4
  73. package/model-provider/openai/OpenAIChatFunctionCallStructureGenerationModel.d.ts +1 -1
  74. package/model-provider/openai/OpenAIChatFunctionCallStructureGenerationModel.js +4 -4
  75. package/model-provider/openai/OpenAIChatModel.test.cjs +21 -14
  76. package/model-provider/openai/OpenAIChatModel.test.js +21 -14
  77. package/model-provider/openai/OpenAICompletionModel.test.cjs +15 -9
  78. package/model-provider/openai/OpenAICompletionModel.test.js +15 -9
  79. package/model-provider/openai/OpenAITextEmbeddingModel.d.ts +12 -12
  80. package/package.json +1 -1
  81. package/tool/execute-tool/executeTool.cjs +5 -5
  82. package/tool/execute-tool/executeTool.d.ts +8 -4
  83. package/tool/execute-tool/executeTool.js +5 -5
  84. package/tool/execute-tool/safeExecuteToolCall.cjs +1 -1
  85. package/tool/execute-tool/safeExecuteToolCall.js +1 -1
  86. package/tool/generate-tool-call/TextGenerationToolCallModel.cjs +4 -2
  87. package/tool/generate-tool-call/TextGenerationToolCallModel.js +4 -2
  88. package/tool/generate-tool-call/generateToolCall.cjs +7 -7
  89. package/tool/generate-tool-call/generateToolCall.d.ts +11 -5
  90. package/tool/generate-tool-call/generateToolCall.js +7 -7
  91. package/tool/generate-tool-calls/TextGenerationToolCallsModel.cjs +4 -2
  92. package/tool/generate-tool-calls/TextGenerationToolCallsModel.js +4 -2
  93. package/tool/generate-tool-calls/generateToolCalls.cjs +3 -3
  94. package/tool/generate-tool-calls/generateToolCalls.d.ts +11 -5
  95. package/tool/generate-tool-calls/generateToolCalls.js +3 -3
  96. package/tool/use-tool/useTool.cjs +2 -2
  97. package/tool/use-tool/useTool.d.ts +5 -1
  98. package/tool/use-tool/useTool.js +2 -2
  99. package/tool/use-tools/useTools.cjs +8 -2
  100. package/tool/use-tools/useTools.d.ts +5 -1
  101. package/tool/use-tools/useTools.js +8 -2
  102. package/vector-index/VectorIndexRetriever.cjs +5 -1
  103. package/vector-index/VectorIndexRetriever.js +5 -1
  104. package/vector-index/upsertIntoVectorIndex.cjs +5 -1
  105. package/vector-index/upsertIntoVectorIndex.js +5 -1
  106. package/classifier/SemanticClassifier.cjs +0 -75
  107. package/classifier/SemanticClassifier.d.ts +0 -25
  108. package/classifier/SemanticClassifier.js +0 -71
  109. package/classifier/index.d.ts +0 -1
  110. package/classifier/index.js +0 -1
@@ -8,24 +8,27 @@ import { SpeechGenerationModel, SpeechGenerationModelSettings } from "./SpeechGe
8
8
  * @see https://modelfusion.dev/guide/function/generate-speech
9
9
  *
10
10
  * @example
11
- * const speech = await generateSpeech(
12
- * lmnt.SpeechGenerator(...),
13
- * "Good evening, ladies and gentlemen! Exciting news on the airwaves tonight " +
11
+ * const speech = await generateSpeech({
12
+ * model: lmnt.SpeechGenerator(...),
13
+ * text: "Good evening, ladies and gentlemen! Exciting news on the airwaves tonight " +
14
14
  * "as The Rolling Stones unveil 'Hackney Diamonds.'
15
- * );
15
+ * });
16
16
  *
17
17
  * @param {SpeechGenerationModel<SpeechGenerationModelSettings>} model - The speech generation model.
18
18
  * @param {string} text - The text to be converted to speech.
19
- * @param {FunctionOptions} [options] - Optional function options.
20
19
  *
21
20
  * @returns {Promise<Buffer>} - A promise that resolves to a buffer containing the synthesized speech.
22
21
  */
23
- export declare function generateSpeech(model: SpeechGenerationModel<SpeechGenerationModelSettings>, text: string, options?: FunctionOptions & {
22
+ export declare function generateSpeech(args: {
23
+ model: SpeechGenerationModel<SpeechGenerationModelSettings>;
24
+ text: string;
24
25
  fullResponse?: false;
25
- }): Promise<Buffer>;
26
- export declare function generateSpeech(model: SpeechGenerationModel<SpeechGenerationModelSettings>, text: string, options: FunctionOptions & {
26
+ } & FunctionOptions): Promise<Buffer>;
27
+ export declare function generateSpeech(args: {
28
+ model: SpeechGenerationModel<SpeechGenerationModelSettings>;
29
+ text: string;
27
30
  fullResponse: true;
28
- }): Promise<{
31
+ } & FunctionOptions): Promise<{
29
32
  speech: Buffer;
30
33
  rawResponse: unknown;
31
34
  metadata: ModelCallMetadata;
@@ -1,6 +1,6 @@
1
1
  import { executeStandardCall } from "../executeStandardCall.js";
2
- export async function generateSpeech(model, text, options) {
3
- const fullResponse = await executeStandardCall({
2
+ export async function generateSpeech({ model, text, fullResponse, ...options }) {
3
+ const callResponse = await executeStandardCall({
4
4
  functionType: "generate-speech",
5
5
  input: text,
6
6
  model,
@@ -13,11 +13,11 @@ export async function generateSpeech(model, text, options) {
13
13
  };
14
14
  },
15
15
  });
16
- return options?.fullResponse
16
+ return fullResponse
17
17
  ? {
18
- speech: fullResponse.value,
19
- rawResponse: fullResponse.rawResponse,
20
- metadata: fullResponse.metadata,
18
+ speech: callResponse.value,
19
+ rawResponse: callResponse.rawResponse,
20
+ metadata: callResponse.metadata,
21
21
  }
22
- : fullResponse.value;
22
+ : callResponse.value;
23
23
  }
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.streamSpeech = void 0;
4
4
  const AsyncQueue_js_1 = require("../../util/AsyncQueue.cjs");
5
5
  const executeStreamCall_js_1 = require("../executeStreamCall.cjs");
6
- async function streamSpeech(model, text, options) {
6
+ async function streamSpeech({ model, text, fullResponse, ...options }) {
7
7
  let textStream;
8
8
  // simulate a stream with a single value for a string input:
9
9
  if (typeof text === "string") {
@@ -15,7 +15,7 @@ async function streamSpeech(model, text, options) {
15
15
  else {
16
16
  textStream = text;
17
17
  }
18
- const fullResponse = await (0, executeStreamCall_js_1.executeStreamCall)({
18
+ const callResponse = await (0, executeStreamCall_js_1.executeStreamCall)({
19
19
  functionType: "stream-speech",
20
20
  input: text,
21
21
  model,
@@ -23,11 +23,11 @@ async function streamSpeech(model, text, options) {
23
23
  startStream: async (options) => model.doGenerateSpeechStreamDuplex(textStream, options),
24
24
  processDelta: (delta) => delta.deltaValue,
25
25
  });
26
- return options?.fullResponse
26
+ return fullResponse
27
27
  ? {
28
- speechStream: fullResponse.value,
29
- metadata: fullResponse.metadata,
28
+ speechStream: callResponse.value,
29
+ metadata: callResponse.metadata,
30
30
  }
31
- : fullResponse.value;
31
+ : callResponse.value;
32
32
  }
33
33
  exports.streamSpeech = streamSpeech;
@@ -11,10 +11,10 @@ import { SpeechGenerationModelSettings, StreamingSpeechGenerationModel } from ".
11
11
  * @example
12
12
  * const textStream = await streamText(...);
13
13
  *
14
- * const speechStream = await streamSpeech(
15
- * elevenlabs.SpeechGenerator(...),
16
- * textStream
17
- * );
14
+ * const speechStream = await streamSpeech({
15
+ * model: elevenlabs.SpeechGenerator(...),
16
+ * text: textStream
17
+ * });
18
18
  *
19
19
  * for await (const speechPart of speechStream) {
20
20
  * // ...
@@ -26,12 +26,16 @@ import { SpeechGenerationModelSettings, StreamingSpeechGenerationModel } from ".
26
26
  *
27
27
  * @returns {AsyncIterableResultPromise<Buffer>} An async iterable promise that contains the synthesized speech chunks.
28
28
  */
29
- export declare function streamSpeech(model: StreamingSpeechGenerationModel<SpeechGenerationModelSettings>, text: AsyncIterable<string> | string, options?: FunctionOptions & {
29
+ export declare function streamSpeech(args: {
30
+ model: StreamingSpeechGenerationModel<SpeechGenerationModelSettings>;
31
+ text: AsyncIterable<string> | string;
30
32
  fullResponse?: false;
31
- }): Promise<AsyncIterable<Buffer>>;
32
- export declare function streamSpeech(model: StreamingSpeechGenerationModel<SpeechGenerationModelSettings>, text: AsyncIterable<string> | string, options: FunctionOptions & {
33
+ } & FunctionOptions): Promise<AsyncIterable<Buffer>>;
34
+ export declare function streamSpeech(args: {
35
+ model: StreamingSpeechGenerationModel<SpeechGenerationModelSettings>;
36
+ text: AsyncIterable<string> | string;
33
37
  fullResponse: true;
34
- }): Promise<{
38
+ } & FunctionOptions): Promise<{
35
39
  speechStream: AsyncIterable<Buffer>;
36
40
  metadata: Omit<ModelCallMetadata, "durationInMs" | "finishTimestamp">;
37
41
  }>;
@@ -1,6 +1,6 @@
1
1
  import { AsyncQueue } from "../../util/AsyncQueue.js";
2
2
  import { executeStreamCall } from "../executeStreamCall.js";
3
- export async function streamSpeech(model, text, options) {
3
+ export async function streamSpeech({ model, text, fullResponse, ...options }) {
4
4
  let textStream;
5
5
  // simulate a stream with a single value for a string input:
6
6
  if (typeof text === "string") {
@@ -12,7 +12,7 @@ export async function streamSpeech(model, text, options) {
12
12
  else {
13
13
  textStream = text;
14
14
  }
15
- const fullResponse = await executeStreamCall({
15
+ const callResponse = await executeStreamCall({
16
16
  functionType: "stream-speech",
17
17
  input: text,
18
18
  model,
@@ -20,10 +20,10 @@ export async function streamSpeech(model, text, options) {
20
20
  startStream: async (options) => model.doGenerateSpeechStreamDuplex(textStream, options),
21
21
  processDelta: (delta) => delta.deltaValue,
22
22
  });
23
- return options?.fullResponse
23
+ return fullResponse
24
24
  ? {
25
- speechStream: fullResponse.value,
26
- metadata: fullResponse.metadata,
25
+ speechStream: callResponse.value,
26
+ metadata: callResponse.metadata,
27
27
  }
28
- : fullResponse.value;
28
+ : callResponse.value;
29
29
  }
@@ -39,13 +39,15 @@ class StructureFromTextGenerationModel {
39
39
  return this.model;
40
40
  }
41
41
  async doGenerateStructure(schema, prompt, options) {
42
- const { rawResponse: response, text } = await (0, generateText_js_1.generateText)(this.getModelWithJsonOutput(schema), this.template.createPrompt(prompt, schema), {
43
- ...options,
42
+ const { rawResponse, text } = await (0, generateText_js_1.generateText)({
43
+ model: this.model,
44
+ prompt: this.template.createPrompt(prompt, schema),
44
45
  fullResponse: true,
46
+ ...options,
45
47
  });
46
48
  try {
47
49
  return {
48
- response,
50
+ rawResponse,
49
51
  value: this.template.extractStructure(text),
50
52
  valueText: text,
51
53
  };
@@ -16,7 +16,7 @@ export declare class StructureFromTextGenerationModel<SOURCE_PROMPT, TARGET_PROM
16
16
  get settingsForEvent(): Partial<MODEL["settings"]>;
17
17
  getModelWithJsonOutput(schema: Schema<unknown> & JsonSchemaProducer): MODEL;
18
18
  doGenerateStructure(schema: Schema<unknown> & JsonSchemaProducer, prompt: SOURCE_PROMPT, options?: FunctionOptions): Promise<{
19
- response: unknown;
19
+ rawResponse: unknown;
20
20
  value: unknown;
21
21
  valueText: string;
22
22
  }>;
@@ -36,13 +36,15 @@ export class StructureFromTextGenerationModel {
36
36
  return this.model;
37
37
  }
38
38
  async doGenerateStructure(schema, prompt, options) {
39
- const { rawResponse: response, text } = await generateText(this.getModelWithJsonOutput(schema), this.template.createPrompt(prompt, schema), {
40
- ...options,
39
+ const { rawResponse, text } = await generateText({
40
+ model: this.model,
41
+ prompt: this.template.createPrompt(prompt, schema),
41
42
  fullResponse: true,
43
+ ...options,
42
44
  });
43
45
  try {
44
46
  return {
45
- response,
47
+ rawResponse,
46
48
  value: this.template.extractStructure(text),
47
49
  valueText: text,
48
50
  };
@@ -10,7 +10,11 @@ class StructureFromTextStreamingModel extends StructureFromTextGenerationModel_j
10
10
  super(options);
11
11
  }
12
12
  async doStreamStructure(schema, prompt, options) {
13
- const textStream = await (0, streamText_js_1.streamText)(this.getModelWithJsonOutput(schema), this.template.createPrompt(prompt, schema), options);
13
+ const textStream = await (0, streamText_js_1.streamText)({
14
+ model: this.model,
15
+ prompt: this.template.createPrompt(prompt, schema),
16
+ ...options,
17
+ });
14
18
  const queue = new AsyncQueue_js_1.AsyncQueue();
15
19
  // run async on purpose:
16
20
  (async () => {
@@ -7,7 +7,11 @@ export class StructureFromTextStreamingModel extends StructureFromTextGeneration
7
7
  super(options);
8
8
  }
9
9
  async doStreamStructure(schema, prompt, options) {
10
- const textStream = await streamText(this.getModelWithJsonOutput(schema), this.template.createPrompt(prompt, schema), options);
10
+ const textStream = await streamText({
11
+ model: this.model,
12
+ prompt: this.template.createPrompt(prompt, schema),
13
+ ...options,
14
+ });
11
15
  const queue = new AsyncQueue();
12
16
  // run async on purpose:
13
17
  (async () => {
@@ -7,7 +7,7 @@ export interface StructureGenerationModelSettings extends ModelSettings {
7
7
  }
8
8
  export interface StructureGenerationModel<PROMPT, SETTINGS extends StructureGenerationModelSettings = StructureGenerationModelSettings> extends Model<SETTINGS> {
9
9
  doGenerateStructure(schema: Schema<unknown> & JsonSchemaProducer, prompt: PROMPT, options?: FunctionOptions): PromiseLike<{
10
- response: unknown;
10
+ rawResponse: unknown;
11
11
  valueText: string;
12
12
  value: unknown;
13
13
  usage?: {
@@ -3,12 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateStructure = void 0;
4
4
  const executeStandardCall_js_1 = require("../executeStandardCall.cjs");
5
5
  const StructureValidationError_js_1 = require("./StructureValidationError.cjs");
6
- async function generateStructure(model, schema, prompt, options) {
6
+ async function generateStructure({ model, schema, prompt, fullResponse, ...options }) {
7
7
  // Note: PROMPT must not be a function.
8
8
  const expandedPrompt = typeof prompt === "function"
9
9
  ? prompt(schema)
10
10
  : prompt;
11
- const fullResponse = await (0, executeStandardCall_js_1.executeStandardCall)({
11
+ const callResponse = await (0, executeStandardCall_js_1.executeStandardCall)({
12
12
  functionType: "generate-structure",
13
13
  input: {
14
14
  schema,
@@ -29,18 +29,18 @@ async function generateStructure(model, schema, prompt, options) {
29
29
  }
30
30
  const value = parseResult.data;
31
31
  return {
32
- rawResponse: result.response,
32
+ rawResponse: result.rawResponse,
33
33
  extractedValue: value,
34
34
  usage: result.usage,
35
35
  };
36
36
  },
37
37
  });
38
- return options?.fullResponse
38
+ return fullResponse
39
39
  ? {
40
- structure: fullResponse.value,
41
- rawResponse: fullResponse.rawResponse,
42
- metadata: fullResponse.metadata,
40
+ structure: callResponse.value,
41
+ rawResponse: callResponse.rawResponse,
42
+ metadata: callResponse.metadata,
43
43
  }
44
- : fullResponse.value;
44
+ : callResponse.value;
45
45
  }
46
46
  exports.generateStructure = generateStructure;
@@ -9,14 +9,16 @@ import { StructureGenerationModel, StructureGenerationModelSettings } from "./St
9
9
  * @see https://modelfusion.dev/guide/function/generate-structure
10
10
  *
11
11
  * @example
12
- * const sentiment = await generateStructure(
13
- * openai.ChatTextGenerator(...).asFunctionCallStructureGenerationModel(...),
14
- * zodSchema(z.object({
12
+ * const sentiment = await generateStructure({
13
+ * model: openai.ChatTextGenerator(...).asFunctionCallStructureGenerationModel(...),
14
+ *
15
+ * schema: zodSchema(z.object({
15
16
  * sentiment: z
16
17
  * .enum(["positive", "neutral", "negative"])
17
18
  * .describe("Sentiment."),
18
19
  * })),
19
- * [
20
+ *
21
+ * prompt: [
20
22
  * openai.ChatMessage.system(
21
23
  * "You are a sentiment evaluator. " +
22
24
  * "Analyze the sentiment of the following product review:"
@@ -26,23 +28,28 @@ import { StructureGenerationModel, StructureGenerationModelSettings } from "./St
26
28
  * "that did not disappear even after washing. Never again!"
27
29
  * ),
28
30
  * ]
29
- * );
31
+ * });
30
32
  *
31
33
  * @param {StructureGenerationModel<PROMPT, SETTINGS>} model - The model to generate the structure.
32
34
  * @param {Schema<STRUCTURE>} schema - The schema to be used.
33
35
  * @param {PROMPT | ((schema: Schema<STRUCTURE>) => PROMPT)} prompt
34
36
  * The prompt to be used.
35
37
  * You can also pass a function that takes the schema as an argument and returns the prompt.
36
- * @param {FunctionOptions} [options] - Optional function options.
37
38
  *
38
39
  * @returns {Promise<STRUCTURE>} - Returns a promise that resolves to the generated structure.
39
40
  */
40
- export declare function generateStructure<STRUCTURE, PROMPT, SETTINGS extends StructureGenerationModelSettings>(model: StructureGenerationModel<PROMPT, SETTINGS>, schema: Schema<STRUCTURE> & JsonSchemaProducer, prompt: PROMPT | ((schema: Schema<STRUCTURE>) => PROMPT), options?: FunctionOptions & {
41
+ export declare function generateStructure<STRUCTURE, PROMPT, SETTINGS extends StructureGenerationModelSettings>(args: {
42
+ model: StructureGenerationModel<PROMPT, SETTINGS>;
43
+ schema: Schema<STRUCTURE> & JsonSchemaProducer;
44
+ prompt: PROMPT | ((schema: Schema<STRUCTURE>) => PROMPT);
41
45
  fullResponse?: false;
42
- }): Promise<STRUCTURE>;
43
- export declare function generateStructure<STRUCTURE, PROMPT, SETTINGS extends StructureGenerationModelSettings>(model: StructureGenerationModel<PROMPT, SETTINGS>, schema: Schema<STRUCTURE> & JsonSchemaProducer, prompt: PROMPT | ((schema: Schema<STRUCTURE>) => PROMPT), options: FunctionOptions & {
46
+ } & FunctionOptions): Promise<STRUCTURE>;
47
+ export declare function generateStructure<STRUCTURE, PROMPT, SETTINGS extends StructureGenerationModelSettings>(args: {
48
+ model: StructureGenerationModel<PROMPT, SETTINGS>;
49
+ schema: Schema<STRUCTURE> & JsonSchemaProducer;
50
+ prompt: PROMPT | ((schema: Schema<STRUCTURE>) => PROMPT);
44
51
  fullResponse: true;
45
- }): Promise<{
52
+ } & FunctionOptions): Promise<{
46
53
  structure: STRUCTURE;
47
54
  rawResponse: unknown;
48
55
  metadata: ModelCallMetadata;
@@ -1,11 +1,11 @@
1
1
  import { executeStandardCall } from "../executeStandardCall.js";
2
2
  import { StructureValidationError } from "./StructureValidationError.js";
3
- export async function generateStructure(model, schema, prompt, options) {
3
+ export async function generateStructure({ model, schema, prompt, fullResponse, ...options }) {
4
4
  // Note: PROMPT must not be a function.
5
5
  const expandedPrompt = typeof prompt === "function"
6
6
  ? prompt(schema)
7
7
  : prompt;
8
- const fullResponse = await executeStandardCall({
8
+ const callResponse = await executeStandardCall({
9
9
  functionType: "generate-structure",
10
10
  input: {
11
11
  schema,
@@ -26,17 +26,17 @@ export async function generateStructure(model, schema, prompt, options) {
26
26
  }
27
27
  const value = parseResult.data;
28
28
  return {
29
- rawResponse: result.response,
29
+ rawResponse: result.rawResponse,
30
30
  extractedValue: value,
31
31
  usage: result.usage,
32
32
  };
33
33
  },
34
34
  });
35
- return options?.fullResponse
35
+ return fullResponse
36
36
  ? {
37
- structure: fullResponse.value,
38
- rawResponse: fullResponse.rawResponse,
39
- metadata: fullResponse.metadata,
37
+ structure: callResponse.value,
38
+ rawResponse: callResponse.rawResponse,
39
+ metadata: callResponse.metadata,
40
40
  }
41
- : fullResponse.value;
41
+ : callResponse.value;
42
42
  }
@@ -3,14 +3,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.streamStructure = void 0;
4
4
  const isDeepEqualData_js_1 = require("../../util/isDeepEqualData.cjs");
5
5
  const executeStreamCall_js_1 = require("../executeStreamCall.cjs");
6
- async function streamStructure(model, schema, prompt, options) {
6
+ async function streamStructure({ model, schema, prompt, fullResponse, ...options }) {
7
7
  // Note: PROMPT must not be a function.
8
8
  const expandedPrompt = typeof prompt === "function"
9
9
  ? prompt(schema)
10
10
  : prompt;
11
11
  let accumulatedText = "";
12
12
  let lastStructure;
13
- const fullResponse = await (0, executeStreamCall_js_1.executeStreamCall)({
13
+ const callResponse = await (0, executeStreamCall_js_1.executeStreamCall)({
14
14
  functionType: "stream-structure",
15
15
  input: {
16
16
  schema,
@@ -49,11 +49,11 @@ async function streamStructure(model, schema, prompt, options) {
49
49
  };
50
50
  },
51
51
  });
52
- return options?.fullResponse
52
+ return fullResponse
53
53
  ? {
54
- structureStream: fullResponse.value,
55
- metadata: fullResponse.metadata,
54
+ structureStream: callResponse.value,
55
+ metadata: callResponse.metadata,
56
56
  }
57
- : fullResponse.value;
57
+ : callResponse.value;
58
58
  }
59
59
  exports.streamStructure = streamStructure;
@@ -25,9 +25,9 @@ export type StructureStreamPart<STRUCTURE> = {
25
25
  * @see https://modelfusion.dev/guide/function/generate-structure
26
26
  *
27
27
  * @example
28
- * const structureStream = await streamStructure(
29
- * openai.ChatTextGenerator(...).asFunctionCallStructureGenerationModel(...),
30
- * zodSchema(
28
+ * const structureStream = await streamStructure({
29
+ * structureGenerator: openai.ChatTextGenerator(...).asFunctionCallStructureGenerationModel(...),
30
+ * schema: zodSchema(
31
31
  * z.array(
32
32
  * z.object({
33
33
  * name: z.string(),
@@ -37,12 +37,12 @@ export type StructureStreamPart<STRUCTURE> = {
37
37
  * description: z.string(),
38
38
  * })
39
39
  * ),
40
- * [
40
+ * prompt: [
41
41
  * openai.ChatMessage.user(
42
42
  * "Generate 3 character descriptions for a fantasy role playing game."
43
43
  * ),
44
44
  * ]
45
- * );
45
+ * });
46
46
  *
47
47
  * for await (const part of structureStream) {
48
48
  * if (!part.isComplete) {
@@ -55,7 +55,7 @@ export type StructureStreamPart<STRUCTURE> = {
55
55
  * }
56
56
  * }
57
57
  *
58
- * @param {StructureStreamingModel<PROMPT>} model - The model to use for streaming
58
+ * @param {StructureStreamingModel<PROMPT>} structureGenerator - The model to use for streaming
59
59
  * @param {Schema<STRUCTURE>} schema - The schema to be used.
60
60
  * @param {PROMPT | ((schema: Schema<STRUCTURE>) => PROMPT)} prompt
61
61
  * The prompt to be used.
@@ -68,12 +68,18 @@ export type StructureStreamPart<STRUCTURE> = {
68
68
  * It contains a isComplete flag to indicate whether the structure is complete,
69
69
  * and a value that is either the partial structure or the final structure.
70
70
  */
71
- export declare function streamStructure<STRUCTURE, PROMPT>(model: StructureStreamingModel<PROMPT>, schema: Schema<STRUCTURE> & JsonSchemaProducer, prompt: PROMPT | ((schema: Schema<STRUCTURE>) => PROMPT), options?: FunctionOptions & {
71
+ export declare function streamStructure<STRUCTURE, PROMPT>(args: {
72
+ model: StructureStreamingModel<PROMPT>;
73
+ schema: Schema<STRUCTURE> & JsonSchemaProducer;
74
+ prompt: PROMPT | ((schema: Schema<STRUCTURE>) => PROMPT);
72
75
  fullResponse?: false;
73
- }): Promise<AsyncIterable<StructureStreamPart<STRUCTURE>>>;
74
- export declare function streamStructure<STRUCTURE, PROMPT>(model: StructureStreamingModel<PROMPT>, schema: Schema<STRUCTURE> & JsonSchemaProducer, prompt: PROMPT | ((schema: Schema<STRUCTURE>) => PROMPT), options: FunctionOptions & {
76
+ } & FunctionOptions): Promise<AsyncIterable<StructureStreamPart<STRUCTURE>>>;
77
+ export declare function streamStructure<STRUCTURE, PROMPT>(args: {
78
+ model: StructureStreamingModel<PROMPT>;
79
+ schema: Schema<STRUCTURE> & JsonSchemaProducer;
80
+ prompt: PROMPT | ((schema: Schema<STRUCTURE>) => PROMPT);
75
81
  fullResponse: true;
76
- }): Promise<{
82
+ } & FunctionOptions): Promise<{
77
83
  structureStream: AsyncIterable<StructureStreamPart<STRUCTURE>>;
78
84
  metadata: Omit<ModelCallMetadata, "durationInMs" | "finishTimestamp">;
79
85
  }>;
@@ -1,13 +1,13 @@
1
1
  import { isDeepEqualData } from "../../util/isDeepEqualData.js";
2
2
  import { executeStreamCall } from "../executeStreamCall.js";
3
- export async function streamStructure(model, schema, prompt, options) {
3
+ export async function streamStructure({ model, schema, prompt, fullResponse, ...options }) {
4
4
  // Note: PROMPT must not be a function.
5
5
  const expandedPrompt = typeof prompt === "function"
6
6
  ? prompt(schema)
7
7
  : prompt;
8
8
  let accumulatedText = "";
9
9
  let lastStructure;
10
- const fullResponse = await executeStreamCall({
10
+ const callResponse = await executeStreamCall({
11
11
  functionType: "stream-structure",
12
12
  input: {
13
13
  schema,
@@ -46,10 +46,10 @@ export async function streamStructure(model, schema, prompt, options) {
46
46
  };
47
47
  },
48
48
  });
49
- return options?.fullResponse
49
+ return fullResponse
50
50
  ? {
51
- structureStream: fullResponse.value,
52
- metadata: fullResponse.metadata,
51
+ structureStream: callResponse.value,
52
+ metadata: callResponse.metadata,
53
53
  }
54
- : fullResponse.value;
54
+ : callResponse.value;
55
55
  }
@@ -2,8 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateText = void 0;
4
4
  const executeStandardCall_js_1 = require("../executeStandardCall.cjs");
5
- async function generateText(model, prompt, options) {
6
- const fullResponse = await (0, executeStandardCall_js_1.executeStandardCall)({
5
+ async function generateText({ model, prompt, fullResponse, ...options }) {
6
+ const callResponse = await (0, executeStandardCall_js_1.executeStandardCall)({
7
7
  functionType: "generate-text",
8
8
  input: prompt,
9
9
  model,
@@ -66,16 +66,16 @@ async function generateText(model, prompt, options) {
66
66
  };
67
67
  },
68
68
  });
69
- const textGenerationResults = fullResponse.value;
69
+ const textGenerationResults = callResponse.value;
70
70
  const firstResult = textGenerationResults[0];
71
- return options?.fullResponse
71
+ return fullResponse
72
72
  ? {
73
73
  text: firstResult.text,
74
74
  finishReason: firstResult.finishReason,
75
75
  texts: textGenerationResults.map((textGeneration) => textGeneration.text),
76
76
  textGenerationResults,
77
- rawResponse: fullResponse.rawResponse,
78
- metadata: fullResponse.metadata,
77
+ rawResponse: callResponse.rawResponse,
78
+ metadata: callResponse.metadata,
79
79
  }
80
80
  : firstResult.text;
81
81
  }
@@ -12,23 +12,26 @@ import { TextGenerationFinishReason, TextGenerationResult } from "./TextGenerati
12
12
  * @see https://modelfusion.dev/guide/function/generate-text
13
13
  *
14
14
  * @example
15
- * const text = await generateText(
16
- * openai.CompletionTextGenerator(...),
17
- * "Write a short story about a robot learning to love:\n\n"
18
- * );
15
+ * const text = await generateText({
16
+ * model: openai.CompletionTextGenerator(...),
17
+ * prompt: "Write a short story about a robot learning to love:\n\n"
18
+ * });
19
19
  *
20
20
  * @param {TextGenerationModel<PROMPT, TextGenerationModelSettings>} model - The text generation model to use.
21
21
  * @param {PROMPT} prompt - The prompt to use for text generation.
22
- * @param {FunctionOptions} [options] - Optional parameters for the function.
23
22
  *
24
23
  * @returns {Promise<string>} - A promise that resolves to the generated text.
25
24
  */
26
- export declare function generateText<PROMPT>(model: TextGenerationModel<PROMPT, TextGenerationModelSettings>, prompt: PROMPT, options?: FunctionOptions & {
25
+ export declare function generateText<PROMPT>(args: {
26
+ model: TextGenerationModel<PROMPT, TextGenerationModelSettings>;
27
+ prompt: PROMPT;
27
28
  fullResponse?: false;
28
- }): Promise<string>;
29
- export declare function generateText<PROMPT>(model: TextGenerationModel<PROMPT, TextGenerationModelSettings>, prompt: PROMPT, options: FunctionOptions & {
29
+ } & FunctionOptions): Promise<string>;
30
+ export declare function generateText<PROMPT>(args: {
31
+ model: TextGenerationModel<PROMPT, TextGenerationModelSettings>;
32
+ prompt: PROMPT;
30
33
  fullResponse: true;
31
- }): Promise<{
34
+ } & FunctionOptions): Promise<{
32
35
  text: string;
33
36
  finishReason: TextGenerationFinishReason;
34
37
  texts: string[];
@@ -1,6 +1,6 @@
1
1
  import { executeStandardCall } from "../executeStandardCall.js";
2
- export async function generateText(model, prompt, options) {
3
- const fullResponse = await executeStandardCall({
2
+ export async function generateText({ model, prompt, fullResponse, ...options }) {
3
+ const callResponse = await executeStandardCall({
4
4
  functionType: "generate-text",
5
5
  input: prompt,
6
6
  model,
@@ -63,16 +63,16 @@ export async function generateText(model, prompt, options) {
63
63
  };
64
64
  },
65
65
  });
66
- const textGenerationResults = fullResponse.value;
66
+ const textGenerationResults = callResponse.value;
67
67
  const firstResult = textGenerationResults[0];
68
- return options?.fullResponse
68
+ return fullResponse
69
69
  ? {
70
70
  text: firstResult.text,
71
71
  finishReason: firstResult.finishReason,
72
72
  texts: textGenerationResults.map((textGeneration) => textGeneration.text),
73
73
  textGenerationResults,
74
- rawResponse: fullResponse.rawResponse,
75
- metadata: fullResponse.metadata,
74
+ rawResponse: callResponse.rawResponse,
75
+ metadata: callResponse.metadata,
76
76
  }
77
77
  : firstResult.text;
78
78
  }
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.streamText = void 0;
4
4
  const executeStreamCall_js_1 = require("../executeStreamCall.cjs");
5
- async function streamText(model, prompt, options) {
5
+ async function streamText({ model, prompt, fullResponse, ...options }) {
6
6
  const shouldTrimWhitespace = model.settings.trimWhitespace ?? true;
7
7
  let accumulatedText = "";
8
8
  let isFirstDelta = true;
@@ -11,7 +11,7 @@ async function streamText(model, prompt, options) {
11
11
  const textPromise = new Promise((resolve) => {
12
12
  resolveText = resolve;
13
13
  });
14
- const fullResponse = await (0, executeStreamCall_js_1.executeStreamCall)({
14
+ const callResponse = await (0, executeStreamCall_js_1.executeStreamCall)({
15
15
  functionType: "stream-text",
16
16
  input: prompt,
17
17
  model,
@@ -43,12 +43,12 @@ async function streamText(model, prompt, options) {
43
43
  resolveText(accumulatedText);
44
44
  },
45
45
  });
46
- return options?.fullResponse
46
+ return fullResponse
47
47
  ? {
48
- textStream: fullResponse.value,
48
+ textStream: callResponse.value,
49
49
  text: textPromise,
50
- metadata: fullResponse.metadata,
50
+ metadata: callResponse.metadata,
51
51
  }
52
- : fullResponse.value;
52
+ : callResponse.value;
53
53
  }
54
54
  exports.streamText = streamText;