modelfusion 0.80.0 → 0.82.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 (66) hide show
  1. package/README.md +55 -44
  2. package/core/schema/UncheckedSchema.d.ts +2 -1
  3. package/core/schema/ZodSchema.cjs +5 -1
  4. package/core/schema/ZodSchema.d.ts +3 -1
  5. package/core/schema/ZodSchema.js +3 -0
  6. package/core/schema/index.cjs +0 -3
  7. package/core/schema/index.d.ts +0 -3
  8. package/core/schema/index.js +0 -3
  9. package/guard/fixStructure.cjs +14 -8
  10. package/guard/fixStructure.d.ts +14 -8
  11. package/guard/fixStructure.js +14 -8
  12. package/model-function/generate-structure/StructureFromTextGenerationModel.cjs +2 -3
  13. package/model-function/generate-structure/StructureFromTextGenerationModel.d.ts +8 -10
  14. package/model-function/generate-structure/StructureFromTextGenerationModel.js +2 -3
  15. package/model-function/generate-structure/StructureFromTextPromptFormat.d.ts +6 -0
  16. package/model-function/generate-structure/StructureFromTextStreamingModel.cjs +69 -0
  17. package/model-function/generate-structure/StructureFromTextStreamingModel.d.ts +22 -0
  18. package/model-function/generate-structure/StructureFromTextStreamingModel.js +65 -0
  19. package/model-function/generate-structure/StructureGenerationModel.d.ts +4 -3
  20. package/model-function/generate-structure/StructureParseError.cjs +2 -10
  21. package/model-function/generate-structure/StructureParseError.d.ts +1 -4
  22. package/model-function/generate-structure/StructureParseError.js +2 -10
  23. package/model-function/generate-structure/StructureValidationError.cjs +2 -10
  24. package/model-function/generate-structure/StructureValidationError.d.ts +1 -4
  25. package/model-function/generate-structure/StructureValidationError.js +2 -10
  26. package/model-function/generate-structure/generateStructure.cjs +4 -5
  27. package/model-function/generate-structure/generateStructure.d.ts +14 -20
  28. package/model-function/generate-structure/generateStructure.js +4 -5
  29. package/model-function/generate-structure/index.cjs +3 -0
  30. package/model-function/generate-structure/index.d.ts +3 -0
  31. package/model-function/generate-structure/index.js +3 -0
  32. package/model-function/generate-structure/jsonStructurePrompt.cjs +11 -0
  33. package/model-function/generate-structure/jsonStructurePrompt.d.ts +4 -0
  34. package/model-function/generate-structure/jsonStructurePrompt.js +7 -0
  35. package/model-function/generate-structure/streamStructure.cjs +4 -4
  36. package/model-function/generate-structure/streamStructure.d.ts +18 -26
  37. package/model-function/generate-structure/streamStructure.js +4 -4
  38. package/model-function/generate-text/PromptFormatTextGenerationModel.cjs +7 -0
  39. package/model-function/generate-text/PromptFormatTextGenerationModel.d.ts +3 -0
  40. package/model-function/generate-text/PromptFormatTextGenerationModel.js +7 -0
  41. package/model-function/generate-text/PromptFormatTextStreamingModel.cjs +7 -0
  42. package/model-function/generate-text/PromptFormatTextStreamingModel.d.ts +3 -0
  43. package/model-function/generate-text/PromptFormatTextStreamingModel.js +7 -0
  44. package/model-function/generate-text/prompt-format/ChatPrompt.d.ts +4 -4
  45. package/model-function/generate-text/prompt-format/Content.d.ts +4 -4
  46. package/model-function/generate-text/prompt-format/InstructionPrompt.d.ts +4 -4
  47. package/model-provider/openai/chat/OpenAIChatFunctionCallStructureGenerationModel.cjs +147 -0
  48. package/model-provider/openai/chat/OpenAIChatFunctionCallStructureGenerationModel.d.ts +89 -0
  49. package/model-provider/openai/chat/OpenAIChatFunctionCallStructureGenerationModel.js +140 -0
  50. package/model-provider/openai/chat/OpenAIChatModel.cjs +16 -56
  51. package/model-provider/openai/chat/OpenAIChatModel.d.ts +9 -53
  52. package/model-provider/openai/chat/OpenAIChatModel.js +17 -54
  53. package/model-provider/openai/chat/OpenAIChatPromptFormat.cjs +8 -1
  54. package/model-provider/openai/chat/OpenAIChatPromptFormat.d.ts +4 -0
  55. package/model-provider/openai/chat/OpenAIChatPromptFormat.js +6 -0
  56. package/package.json +1 -1
  57. package/tool/generate-tool-calls-or-text/generateToolCallsOrText.d.ts +2 -2
  58. package/core/schema/StructureDefinition.d.ts +0 -7
  59. package/core/schema/UncheckedStructureDefinition.cjs +0 -30
  60. package/core/schema/UncheckedStructureDefinition.d.ts +0 -12
  61. package/core/schema/UncheckedStructureDefinition.js +0 -26
  62. package/core/schema/ZodStructureDefinition.cjs +0 -30
  63. package/core/schema/ZodStructureDefinition.d.ts +0 -13
  64. package/core/schema/ZodStructureDefinition.js +0 -26
  65. /package/{core/schema/StructureDefinition.cjs → model-function/generate-structure/StructureFromTextPromptFormat.cjs} +0 -0
  66. /package/{core/schema/StructureDefinition.js → model-function/generate-structure/StructureFromTextPromptFormat.js} +0 -0
@@ -1,15 +1,9 @@
1
1
  import { getErrorMessage } from "../../util/getErrorMessage.js";
2
2
  export class StructureParseError extends Error {
3
- constructor({ structureName, valueText, cause, }) {
4
- super(`Structure parsing failed for '${structureName}'. ` +
3
+ constructor({ valueText, cause }) {
4
+ super(`Structure parsing failed. ` +
5
5
  `Value: ${valueText}.\n` +
6
6
  `Error message: ${getErrorMessage(cause)}`);
7
- Object.defineProperty(this, "structureName", {
8
- enumerable: true,
9
- configurable: true,
10
- writable: true,
11
- value: void 0
12
- });
13
7
  Object.defineProperty(this, "cause", {
14
8
  enumerable: true,
15
9
  configurable: true,
@@ -23,7 +17,6 @@ export class StructureParseError extends Error {
23
17
  value: void 0
24
18
  });
25
19
  this.name = "StructureParseError";
26
- this.structureName = structureName;
27
20
  this.cause = cause;
28
21
  this.valueText = valueText;
29
22
  }
@@ -33,7 +26,6 @@ export class StructureParseError extends Error {
33
26
  cause: this.cause,
34
27
  message: this.message,
35
28
  stack: this.stack,
36
- structureName: this.structureName,
37
29
  valueText: this.valueText,
38
30
  };
39
31
  }
@@ -3,16 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.StructureValidationError = void 0;
4
4
  const getErrorMessage_js_1 = require("../../util/getErrorMessage.cjs");
5
5
  class StructureValidationError extends Error {
6
- constructor({ structureName, value, valueText, cause, }) {
7
- super(`Structure validation failed for '${structureName}'. ` +
6
+ constructor({ value, valueText, cause, }) {
7
+ super(`Structure validation failed. ` +
8
8
  `Value: ${valueText}.\n` +
9
9
  `Error message: ${(0, getErrorMessage_js_1.getErrorMessage)(cause)}`);
10
- Object.defineProperty(this, "structureName", {
11
- enumerable: true,
12
- configurable: true,
13
- writable: true,
14
- value: void 0
15
- });
16
10
  Object.defineProperty(this, "cause", {
17
11
  enumerable: true,
18
12
  configurable: true,
@@ -32,7 +26,6 @@ class StructureValidationError extends Error {
32
26
  value: void 0
33
27
  });
34
28
  this.name = "StructureValidationError";
35
- this.structureName = structureName;
36
29
  this.cause = cause;
37
30
  this.value = value;
38
31
  this.valueText = valueText;
@@ -43,7 +36,6 @@ class StructureValidationError extends Error {
43
36
  message: this.message,
44
37
  cause: this.cause,
45
38
  stack: this.stack,
46
- structureName: this.structureName,
47
39
  value: this.value,
48
40
  valueText: this.valueText,
49
41
  };
@@ -1,10 +1,8 @@
1
1
  export declare class StructureValidationError extends Error {
2
- readonly structureName: string;
3
2
  readonly cause: unknown;
4
3
  readonly valueText: string;
5
4
  readonly value: unknown;
6
- constructor({ structureName, value, valueText, cause, }: {
7
- structureName: string;
5
+ constructor({ value, valueText, cause, }: {
8
6
  value: unknown;
9
7
  valueText: string;
10
8
  cause: unknown;
@@ -14,7 +12,6 @@ export declare class StructureValidationError extends Error {
14
12
  message: string;
15
13
  cause: unknown;
16
14
  stack: string | undefined;
17
- structureName: string;
18
15
  value: unknown;
19
16
  valueText: string;
20
17
  };
@@ -1,15 +1,9 @@
1
1
  import { getErrorMessage } from "../../util/getErrorMessage.js";
2
2
  export class StructureValidationError extends Error {
3
- constructor({ structureName, value, valueText, cause, }) {
4
- super(`Structure validation failed for '${structureName}'. ` +
3
+ constructor({ value, valueText, cause, }) {
4
+ super(`Structure validation failed. ` +
5
5
  `Value: ${valueText}.\n` +
6
6
  `Error message: ${getErrorMessage(cause)}`);
7
- Object.defineProperty(this, "structureName", {
8
- enumerable: true,
9
- configurable: true,
10
- writable: true,
11
- value: void 0
12
- });
13
7
  Object.defineProperty(this, "cause", {
14
8
  enumerable: true,
15
9
  configurable: true,
@@ -29,7 +23,6 @@ export class StructureValidationError extends Error {
29
23
  value: void 0
30
24
  });
31
25
  this.name = "StructureValidationError";
32
- this.structureName = structureName;
33
26
  this.cause = cause;
34
27
  this.value = value;
35
28
  this.valueText = valueText;
@@ -40,7 +33,6 @@ export class StructureValidationError extends Error {
40
33
  message: this.message,
41
34
  cause: this.cause,
42
35
  stack: this.stack,
43
- structureName: this.structureName,
44
36
  value: this.value,
45
37
  valueText: this.valueText,
46
38
  };
@@ -3,10 +3,10 @@ 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, structureDefinition, prompt, options) {
6
+ async function generateStructure(model, schema, prompt, options) {
7
7
  // Note: PROMPT must not be a function.
8
8
  const expandedPrompt = typeof prompt === "function"
9
- ? prompt(structureDefinition)
9
+ ? prompt(schema)
10
10
  : prompt;
11
11
  const fullResponse = await (0, executeStandardCall_js_1.executeStandardCall)({
12
12
  functionType: "generate-structure",
@@ -14,12 +14,11 @@ async function generateStructure(model, structureDefinition, prompt, options) {
14
14
  model,
15
15
  options,
16
16
  generateResponse: async (options) => {
17
- const result = await model.doGenerateStructure(structureDefinition, expandedPrompt, options);
17
+ const result = await model.doGenerateStructure(schema, expandedPrompt, options);
18
18
  const structure = result.value;
19
- const parseResult = structureDefinition.schema.validate(structure);
19
+ const parseResult = schema.validate(structure);
20
20
  if (!parseResult.success) {
21
21
  throw new StructureValidationError_js_1.StructureValidationError({
22
- structureName: structureDefinition.name,
23
22
  valueText: result.valueText,
24
23
  value: structure,
25
24
  cause: parseResult.error,
@@ -1,27 +1,21 @@
1
1
  import { FunctionOptions } from "../../core/FunctionOptions.js";
2
- import { StructureDefinition } from "../../core/schema/StructureDefinition.js";
2
+ import { JsonSchemaProducer } from "../../core/schema/JsonSchemaProducer.js";
3
+ import { Schema } from "../../core/schema/Schema.js";
3
4
  import { ModelCallMetadata } from "../ModelCallMetadata.js";
4
5
  import { StructureGenerationModel, StructureGenerationModelSettings } from "./StructureGenerationModel.js";
5
6
  /**
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.
7
+ * Generate a typed object for a prompt and a schema.
10
8
  *
11
9
  * @see https://modelfusion.dev/guide/function/generate-structure
12
10
  *
13
11
  * @example
14
12
  * const sentiment = await generateStructure(
15
- * openai.ChatTextGenerator(...),
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
- * }),
13
+ * openai.ChatTextGenerator(...).asFunctionCallStructureGenerationModel(...),
14
+ * new ZodSchema(z.object({
15
+ * sentiment: z
16
+ * .enum(["positive", "neutral", "negative"])
17
+ * .describe("Sentiment."),
18
+ * })),
25
19
  * [
26
20
  * OpenAIChatMessage.system(
27
21
  * "You are a sentiment evaluator. " +
@@ -35,18 +29,18 @@ import { StructureGenerationModel, StructureGenerationModelSettings } from "./St
35
29
  * );
36
30
  *
37
31
  * @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
32
+ * @param {Schema<STRUCTURE>} schema - The schema to be used.
33
+ * @param {PROMPT | ((schema: Schema<STRUCTURE>) => PROMPT)} prompt
40
34
  * The prompt to be used.
41
- * You can also pass a function that takes the structure definition as an argument and returns the prompt.
35
+ * You can also pass a function that takes the schema as an argument and returns the prompt.
42
36
  * @param {FunctionOptions} [options] - Optional function options.
43
37
  *
44
38
  * @returns {Promise<STRUCTURE>} - Returns a promise that resolves to the generated structure.
45
39
  */
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 & {
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 & {
47
41
  returnType?: "structure";
48
42
  }): Promise<STRUCTURE>;
49
- 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 & {
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 & {
50
44
  returnType: "full";
51
45
  }): Promise<{
52
46
  value: STRUCTURE;
@@ -1,9 +1,9 @@
1
1
  import { executeStandardCall } from "../executeStandardCall.js";
2
2
  import { StructureValidationError } from "./StructureValidationError.js";
3
- export async function generateStructure(model, structureDefinition, prompt, options) {
3
+ export async function generateStructure(model, schema, prompt, options) {
4
4
  // Note: PROMPT must not be a function.
5
5
  const expandedPrompt = typeof prompt === "function"
6
- ? prompt(structureDefinition)
6
+ ? prompt(schema)
7
7
  : prompt;
8
8
  const fullResponse = await executeStandardCall({
9
9
  functionType: "generate-structure",
@@ -11,12 +11,11 @@ export async function generateStructure(model, structureDefinition, prompt, opti
11
11
  model,
12
12
  options,
13
13
  generateResponse: async (options) => {
14
- const result = await model.doGenerateStructure(structureDefinition, expandedPrompt, options);
14
+ const result = await model.doGenerateStructure(schema, expandedPrompt, options);
15
15
  const structure = result.value;
16
- const parseResult = structureDefinition.schema.validate(structure);
16
+ const parseResult = schema.validate(structure);
17
17
  if (!parseResult.success) {
18
18
  throw new StructureValidationError({
19
- structureName: structureDefinition.name,
20
19
  valueText: result.valueText,
21
20
  value: structure,
22
21
  cause: parseResult.error,
@@ -15,10 +15,13 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./StructureFromTextGenerationModel.cjs"), exports);
18
+ __exportStar(require("./StructureFromTextPromptFormat.cjs"), exports);
19
+ __exportStar(require("./StructureFromTextStreamingModel.cjs"), exports);
18
20
  __exportStar(require("./StructureGenerationEvent.cjs"), exports);
19
21
  __exportStar(require("./StructureGenerationModel.cjs"), exports);
20
22
  __exportStar(require("./StructureParseError.cjs"), exports);
21
23
  __exportStar(require("./StructureStreamingEvent.cjs"), exports);
22
24
  __exportStar(require("./StructureValidationError.cjs"), exports);
23
25
  __exportStar(require("./generateStructure.cjs"), exports);
26
+ __exportStar(require("./jsonStructurePrompt.cjs"), exports);
24
27
  __exportStar(require("./streamStructure.cjs"), exports);
@@ -1,8 +1,11 @@
1
1
  export * from "./StructureFromTextGenerationModel.js";
2
+ export * from "./StructureFromTextPromptFormat.js";
3
+ export * from "./StructureFromTextStreamingModel.js";
2
4
  export * from "./StructureGenerationEvent.js";
3
5
  export * from "./StructureGenerationModel.js";
4
6
  export * from "./StructureParseError.js";
5
7
  export * from "./StructureStreamingEvent.js";
6
8
  export * from "./StructureValidationError.js";
7
9
  export * from "./generateStructure.js";
10
+ export * from "./jsonStructurePrompt.js";
8
11
  export * from "./streamStructure.js";
@@ -1,8 +1,11 @@
1
1
  export * from "./StructureFromTextGenerationModel.js";
2
+ export * from "./StructureFromTextPromptFormat.js";
3
+ export * from "./StructureFromTextStreamingModel.js";
2
4
  export * from "./StructureGenerationEvent.js";
3
5
  export * from "./StructureGenerationModel.js";
4
6
  export * from "./StructureParseError.js";
5
7
  export * from "./StructureStreamingEvent.js";
6
8
  export * from "./StructureValidationError.js";
7
9
  export * from "./generateStructure.js";
10
+ export * from "./jsonStructurePrompt.js";
8
11
  export * from "./streamStructure.js";
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.jsonStructurePrompt = void 0;
4
+ const parseJSON_js_1 = require("../../core/schema/parseJSON.cjs");
5
+ function jsonStructurePrompt(createPrompt) {
6
+ return {
7
+ createPrompt,
8
+ extractStructure: (response) => (0, parseJSON_js_1.parseJSON)({ text: response }),
9
+ };
10
+ }
11
+ exports.jsonStructurePrompt = jsonStructurePrompt;
@@ -0,0 +1,4 @@
1
+ import { JsonSchemaProducer } from "../../core/schema/JsonSchemaProducer.js";
2
+ import { Schema } from "../../core/schema/Schema.js";
3
+ import { StructureFromTextPromptFormat } from "./StructureFromTextPromptFormat.js";
4
+ export declare function jsonStructurePrompt<SOURCE_PROMPT, TARGET_PROMPT>(createPrompt: (prompt: SOURCE_PROMPT, schema: Schema<unknown> & JsonSchemaProducer) => TARGET_PROMPT): StructureFromTextPromptFormat<SOURCE_PROMPT, TARGET_PROMPT>;
@@ -0,0 +1,7 @@
1
+ import { parseJSON } from "../../core/schema/parseJSON.js";
2
+ export function jsonStructurePrompt(createPrompt) {
3
+ return {
4
+ createPrompt,
5
+ extractStructure: (response) => parseJSON({ text: response }),
6
+ };
7
+ }
@@ -3,10 +3,10 @@ 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, structureDefinition, prompt, options) {
6
+ async function streamStructure(model, schema, prompt, options) {
7
7
  // Note: PROMPT must not be a function.
8
8
  const expandedPrompt = typeof prompt === "function"
9
- ? prompt(structureDefinition)
9
+ ? prompt(schema)
10
10
  : prompt;
11
11
  let lastStructure;
12
12
  let lastFullDelta;
@@ -15,7 +15,7 @@ async function streamStructure(model, structureDefinition, prompt, options) {
15
15
  input: prompt,
16
16
  model,
17
17
  options,
18
- startStream: async (options) => model.doStreamStructure(structureDefinition, expandedPrompt, options),
18
+ startStream: async (options) => model.doStreamStructure(schema, expandedPrompt, options),
19
19
  processDelta: (delta) => {
20
20
  const latestFullDelta = delta.fullDelta;
21
21
  const latestStructure = delta.valueDelta;
@@ -32,7 +32,7 @@ async function streamStructure(model, structureDefinition, prompt, options) {
32
32
  },
33
33
  processFinished: () => {
34
34
  // process the final result (full type validation):
35
- const parseResult = structureDefinition.schema.validate(lastStructure);
35
+ const parseResult = schema.validate(lastStructure);
36
36
  if (!parseResult.success) {
37
37
  reportError(parseResult.error);
38
38
  throw parseResult.error;
@@ -1,5 +1,6 @@
1
1
  import { FunctionOptions } from "../../core/FunctionOptions.js";
2
- import { StructureDefinition } from "../../core/schema/StructureDefinition.js";
2
+ import { JsonSchemaProducer } from "../../core/schema/JsonSchemaProducer.js";
3
+ import { Schema } from "../../core/schema/Schema.js";
3
4
  import { ModelCallMetadata } from "../ModelCallMetadata.js";
4
5
  import { StructureStreamingModel } from "./StructureGenerationModel.js";
5
6
  export type StructureStreamPart<STRUCTURE> = {
@@ -25,26 +26,17 @@ export type StructureStreamPart<STRUCTURE> = {
25
26
  *
26
27
  * @example
27
28
  * const structureStream = await streamStructure(
28
- * openai.ChatTextGenerator({
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
- * }),
29
+ * openai.ChatTextGenerator(...).asFunctionCallStructureGenerationModel(...),
30
+ * new ZodSchema(
31
+ * z.array(
32
+ * z.object({
33
+ * name: z.string(),
34
+ * class: z
35
+ * .string()
36
+ * .describe("Character class, e.g. warrior, mage, or thief."),
37
+ * description: z.string(),
38
+ * })
39
+ * ),
48
40
  * [
49
41
  * OpenAIChatMessage.user(
50
42
  * "Generate 3 character descriptions for a fantasy role playing game."
@@ -64,10 +56,10 @@ export type StructureStreamPart<STRUCTURE> = {
64
56
  * }
65
57
  *
66
58
  * @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
59
+ * @param {Schema<STRUCTURE>} schema - The schema to be used.
60
+ * @param {PROMPT | ((schema: Schema<STRUCTURE>) => PROMPT)} prompt
69
61
  * The prompt to be used.
70
- * You can also pass a function that takes the structure definition as an argument and returns the prompt.
62
+ * You can also pass a function that takes the schema as an argument and returns the prompt.
71
63
  * @param {FunctionOptions} [options] - Optional function options
72
64
  *
73
65
  * @returns {AsyncIterableResultPromise<StructureStreamPart<STRUCTURE>>}
@@ -76,10 +68,10 @@ export type StructureStreamPart<STRUCTURE> = {
76
68
  * It contains a isComplete flag to indicate whether the structure is complete,
77
69
  * and a value that is either the partial structure or the final structure.
78
70
  */
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 & {
71
+ export declare function streamStructure<STRUCTURE, PROMPT>(model: StructureStreamingModel<PROMPT>, schema: Schema<STRUCTURE> & JsonSchemaProducer, prompt: PROMPT | ((schema: Schema<STRUCTURE>) => PROMPT), options?: FunctionOptions & {
80
72
  returnType?: "structure-stream";
81
73
  }): Promise<AsyncIterable<StructureStreamPart<STRUCTURE>>>;
82
- 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 & {
74
+ export declare function streamStructure<STRUCTURE, PROMPT>(model: StructureStreamingModel<PROMPT>, schema: Schema<STRUCTURE> & JsonSchemaProducer, prompt: PROMPT | ((schema: Schema<STRUCTURE>) => PROMPT), options: FunctionOptions & {
83
75
  returnType: "full";
84
76
  }): Promise<{
85
77
  value: AsyncIterable<StructureStreamPart<STRUCTURE>>;
@@ -1,9 +1,9 @@
1
1
  import { isDeepEqualData } from "../../util/isDeepEqualData.js";
2
2
  import { executeStreamCall } from "../executeStreamCall.js";
3
- export async function streamStructure(model, structureDefinition, prompt, options) {
3
+ export async function streamStructure(model, schema, prompt, options) {
4
4
  // Note: PROMPT must not be a function.
5
5
  const expandedPrompt = typeof prompt === "function"
6
- ? prompt(structureDefinition)
6
+ ? prompt(schema)
7
7
  : prompt;
8
8
  let lastStructure;
9
9
  let lastFullDelta;
@@ -12,7 +12,7 @@ export async function streamStructure(model, structureDefinition, prompt, option
12
12
  input: prompt,
13
13
  model,
14
14
  options,
15
- startStream: async (options) => model.doStreamStructure(structureDefinition, expandedPrompt, options),
15
+ startStream: async (options) => model.doStreamStructure(schema, expandedPrompt, options),
16
16
  processDelta: (delta) => {
17
17
  const latestFullDelta = delta.fullDelta;
18
18
  const latestStructure = delta.valueDelta;
@@ -29,7 +29,7 @@ export async function streamStructure(model, structureDefinition, prompt, option
29
29
  },
30
30
  processFinished: () => {
31
31
  // process the final result (full type validation):
32
- const parseResult = structureDefinition.schema.validate(lastStructure);
32
+ const parseResult = schema.validate(lastStructure);
33
33
  if (!parseResult.success) {
34
34
  reportError(parseResult.error);
35
35
  throw parseResult.error;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PromptFormatTextGenerationModel = void 0;
4
+ const StructureFromTextGenerationModel_js_1 = require("../../model-function/generate-structure/StructureFromTextGenerationModel.cjs");
4
5
  const TextGenerationToolCallModel_js_1 = require("../../tool/generate-tool-call/TextGenerationToolCallModel.cjs");
5
6
  const TextGenerationToolCallsOrGenerateTextModel_js_1 = require("../../tool/generate-tool-calls-or-text/TextGenerationToolCallsOrGenerateTextModel.cjs");
6
7
  class PromptFormatTextGenerationModel {
@@ -58,6 +59,12 @@ class PromptFormatTextGenerationModel {
58
59
  format: promptFormat,
59
60
  });
60
61
  }
62
+ asStructureGenerationModel(promptFormat) {
63
+ return new StructureFromTextGenerationModel_js_1.StructureFromTextGenerationModel({
64
+ model: this,
65
+ format: promptFormat,
66
+ });
67
+ }
61
68
  withPromptFormat(promptFormat) {
62
69
  return new PromptFormatTextGenerationModel({
63
70
  model: this.withSettings({
@@ -1,4 +1,6 @@
1
1
  import { FunctionOptions } from "../../core/FunctionOptions.js";
2
+ import { StructureFromTextGenerationModel } from "../../model-function/generate-structure/StructureFromTextGenerationModel.js";
3
+ import { StructureFromTextPromptFormat } from "model-function/generate-structure/StructureFromTextPromptFormat.js";
2
4
  import { TextGenerationToolCallModel, ToolCallPromptFormat } from "../../tool/generate-tool-call/TextGenerationToolCallModel.js";
3
5
  import { TextGenerationToolCallsOrGenerateTextModel, ToolCallsOrGenerateTextPromptFormat } from "../../tool/generate-tool-calls-or-text/TextGenerationToolCallsOrGenerateTextModel.js";
4
6
  import { TextGenerationModel, TextGenerationModelSettings } from "./TextGenerationModel.js";
@@ -27,6 +29,7 @@ export declare class PromptFormatTextGenerationModel<PROMPT, MODEL_PROMPT, SETTI
27
29
  get settingsForEvent(): Partial<SETTINGS>;
28
30
  asToolCallGenerationModel<INPUT_PROMPT>(promptFormat: ToolCallPromptFormat<INPUT_PROMPT, PROMPT>): TextGenerationToolCallModel<INPUT_PROMPT, PROMPT, this>;
29
31
  asToolCallsOrTextGenerationModel<INPUT_PROMPT>(promptFormat: ToolCallsOrGenerateTextPromptFormat<INPUT_PROMPT, PROMPT>): TextGenerationToolCallsOrGenerateTextModel<INPUT_PROMPT, PROMPT, this>;
32
+ asStructureGenerationModel<INPUT_PROMPT>(promptFormat: StructureFromTextPromptFormat<INPUT_PROMPT, PROMPT>): StructureFromTextGenerationModel<INPUT_PROMPT, PROMPT, this>;
30
33
  withPromptFormat<INPUT_PROMPT>(promptFormat: TextGenerationPromptFormat<INPUT_PROMPT, PROMPT>): PromptFormatTextGenerationModel<INPUT_PROMPT, PROMPT, SETTINGS, this>;
31
34
  withSettings(additionalSettings: Partial<SETTINGS>): this;
32
35
  }
@@ -1,3 +1,4 @@
1
+ import { StructureFromTextGenerationModel } from "../../model-function/generate-structure/StructureFromTextGenerationModel.js";
1
2
  import { TextGenerationToolCallModel, } from "../../tool/generate-tool-call/TextGenerationToolCallModel.js";
2
3
  import { TextGenerationToolCallsOrGenerateTextModel, } from "../../tool/generate-tool-calls-or-text/TextGenerationToolCallsOrGenerateTextModel.js";
3
4
  export class PromptFormatTextGenerationModel {
@@ -55,6 +56,12 @@ export class PromptFormatTextGenerationModel {
55
56
  format: promptFormat,
56
57
  });
57
58
  }
59
+ asStructureGenerationModel(promptFormat) {
60
+ return new StructureFromTextGenerationModel({
61
+ model: this,
62
+ format: promptFormat,
63
+ });
64
+ }
58
65
  withPromptFormat(promptFormat) {
59
66
  return new PromptFormatTextGenerationModel({
60
67
  model: this.withSettings({
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PromptFormatTextStreamingModel = void 0;
4
+ const StructureFromTextStreamingModel_js_1 = require("../../model-function/generate-structure/StructureFromTextStreamingModel.cjs");
4
5
  const PromptFormatTextGenerationModel_js_1 = require("./PromptFormatTextGenerationModel.cjs");
5
6
  class PromptFormatTextStreamingModel extends PromptFormatTextGenerationModel_js_1.PromptFormatTextGenerationModel {
6
7
  constructor(options) {
@@ -10,6 +11,12 @@ class PromptFormatTextStreamingModel extends PromptFormatTextGenerationModel_js_
10
11
  const mappedPrompt = this.promptFormat.format(prompt);
11
12
  return this.model.doStreamText(mappedPrompt, options);
12
13
  }
14
+ asStructureGenerationModel(promptFormat) {
15
+ return new StructureFromTextStreamingModel_js_1.StructureFromTextStreamingModel({
16
+ model: this,
17
+ format: promptFormat,
18
+ });
19
+ }
13
20
  withPromptFormat(promptFormat) {
14
21
  return new PromptFormatTextStreamingModel({
15
22
  model: this.withSettings({
@@ -1,4 +1,6 @@
1
1
  import { FunctionOptions } from "../../core/FunctionOptions.js";
2
+ import { StructureFromTextPromptFormat } from "model-function/generate-structure/StructureFromTextPromptFormat.js";
3
+ import { StructureFromTextStreamingModel } from "../../model-function/generate-structure/StructureFromTextStreamingModel.js";
2
4
  import { PromptFormatTextGenerationModel } from "./PromptFormatTextGenerationModel.js";
3
5
  import { TextGenerationModelSettings, TextStreamingModel } from "./TextGenerationModel.js";
4
6
  import { TextGenerationPromptFormat } from "./TextGenerationPromptFormat.js";
@@ -8,6 +10,7 @@ export declare class PromptFormatTextStreamingModel<PROMPT, MODEL_PROMPT, SETTIN
8
10
  promptFormat: TextGenerationPromptFormat<PROMPT, MODEL_PROMPT>;
9
11
  });
10
12
  doStreamText(prompt: PROMPT, options?: FunctionOptions): PromiseLike<AsyncIterable<import("../Delta.js").Delta<string>>>;
13
+ asStructureGenerationModel<INPUT_PROMPT>(promptFormat: StructureFromTextPromptFormat<INPUT_PROMPT, PROMPT>): StructureFromTextStreamingModel<INPUT_PROMPT, PROMPT, this>;
11
14
  withPromptFormat<INPUT_PROMPT>(promptFormat: TextGenerationPromptFormat<INPUT_PROMPT, PROMPT>): PromptFormatTextStreamingModel<INPUT_PROMPT, PROMPT, SETTINGS, this>;
12
15
  withSettings(additionalSettings: Partial<SETTINGS>): this;
13
16
  }
@@ -1,3 +1,4 @@
1
+ import { StructureFromTextStreamingModel } from "../../model-function/generate-structure/StructureFromTextStreamingModel.js";
1
2
  import { PromptFormatTextGenerationModel } from "./PromptFormatTextGenerationModel.js";
2
3
  export class PromptFormatTextStreamingModel extends PromptFormatTextGenerationModel {
3
4
  constructor(options) {
@@ -7,6 +8,12 @@ export class PromptFormatTextStreamingModel extends PromptFormatTextGenerationMo
7
8
  const mappedPrompt = this.promptFormat.format(prompt);
8
9
  return this.model.doStreamText(mappedPrompt, options);
9
10
  }
11
+ asStructureGenerationModel(promptFormat) {
12
+ return new StructureFromTextStreamingModel({
13
+ model: this,
14
+ format: promptFormat,
15
+ });
16
+ }
10
17
  withPromptFormat(promptFormat) {
11
18
  return new PromptFormatTextStreamingModel({
12
19
  model: this.withSettings({
@@ -24,18 +24,18 @@
24
24
  *
25
25
  * @see validateChatPrompt
26
26
  */
27
- export type ChatPrompt = {
27
+ export interface ChatPrompt {
28
28
  system?: string;
29
29
  messages: Array<ChatMessage>;
30
- };
30
+ }
31
31
  /**
32
32
  * A message in a chat prompt.
33
33
  * @see ChatPrompt
34
34
  */
35
- export type ChatMessage = {
35
+ export interface ChatMessage {
36
36
  role: "user" | "assistant";
37
37
  content: string;
38
- };
38
+ }
39
39
  /**
40
40
  * Checks if a chat prompt is valid. Throws a {@link ChatPromptValidationError} if it's not.
41
41
  *
@@ -1,13 +1,13 @@
1
1
  export type MultiModalInput = Array<Content>;
2
2
  export type Content = TextContent | ImageContent;
3
- export type TextContent = {
3
+ export interface TextContent {
4
4
  type: "text";
5
5
  /**
6
6
  * The text content.
7
7
  */
8
8
  text: string;
9
- };
10
- export type ImageContent = {
9
+ }
10
+ export interface ImageContent {
11
11
  type: "image";
12
12
  /**
13
13
  * Base-64 encoded image.
@@ -17,4 +17,4 @@ export type ImageContent = {
17
17
  * Optional mime type of the image.
18
18
  */
19
19
  mimeType?: string;
20
- };
20
+ }
@@ -4,7 +4,7 @@ import { MultiModalInput } from "./Content.js";
4
4
  * the role and behavior of the language model.
5
5
  * The instruction is a multi-model input (`array` of content).
6
6
  */
7
- export type InstructionPrompt = {
7
+ export interface InstructionPrompt {
8
8
  /**
9
9
  * Optional system message to provide context for the language model. Note that for some models,
10
10
  * changing the system message can impact the results, because the model may be trained on the default system message.
@@ -14,7 +14,7 @@ export type InstructionPrompt = {
14
14
  * The multi-modal instruction for the model.
15
15
  */
16
16
  instruction: MultiModalInput;
17
- };
17
+ }
18
18
  /**
19
19
  * A single text instruction prompt. It can contain an optional system message to define
20
20
  * the role and behavior of the language model.
@@ -27,7 +27,7 @@ export type InstructionPrompt = {
27
27
  * }
28
28
  * ```
29
29
  */
30
- export type TextInstructionPrompt = {
30
+ export interface TextInstructionPrompt {
31
31
  /**
32
32
  * Optional system message to provide context for the language model. Note that for some models,
33
33
  * changing the system message can impact the results, because the model may be trained on the default system message.
@@ -37,4 +37,4 @@ export type TextInstructionPrompt = {
37
37
  * The text instruction for the model.
38
38
  */
39
39
  instruction: string;
40
- };
40
+ }