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
package/README.md CHANGED
@@ -193,31 +193,34 @@ Generate typed objects using a language model and a schema.
193
193
  Generate a structure that matches a schema.
194
194
 
195
195
  ```ts
196
+ import { zodSchema, generateStructure, openai } from "modelfusion";
197
+
196
198
  const sentiment = await generateStructure(
197
- openai.ChatTextGenerator({
198
- model: "gpt-3.5-turbo",
199
- temperature: 0,
200
- maxCompletionTokens: 50,
201
- }),
202
- new ZodStructureDefinition({
203
- name: "sentiment",
204
- description: "Write the sentiment analysis",
205
- schema: z.object({
199
+ openai
200
+ .ChatTextGenerator({
201
+ model: "gpt-3.5-turbo",
202
+ temperature: 0,
203
+ maxCompletionTokens: 50,
204
+ })
205
+ .asFunctionCallStructureGenerationModel({ fnName: "sentiment" })
206
+ .withInstructionPrompt(),
207
+
208
+ zodSchema(
209
+ z.object({
206
210
  sentiment: z
207
211
  .enum(["positive", "neutral", "negative"])
208
212
  .describe("Sentiment."),
209
- }),
210
- }),
211
- [
212
- OpenAIChatMessage.system(
213
+ })
214
+ ),
215
+
216
+ {
217
+ system:
213
218
  "You are a sentiment evaluator. " +
214
- "Analyze the sentiment of the following product review:"
215
- ),
216
- OpenAIChatMessage.user(
219
+ "Analyze the sentiment of the following product review:",
220
+ instruction:
217
221
  "After I opened the package, I was met by a very unpleasant smell " +
218
- "that did not disappear even after washing. Never again!"
219
- ),
220
- ]
222
+ "that did not disappear even after washing. Never again!",
223
+ }
221
224
  );
222
225
  ```
223
226
 
@@ -228,16 +231,19 @@ Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai)
228
231
  Stream a structure that matches a schema. Partial structures before the final part are untyped JSON.
229
232
 
230
233
  ```ts
234
+ import { zodSchema, openai, streamStructure } from "modelfusion";
235
+
231
236
  const structureStream = await streamStructure(
232
- openai.ChatTextGenerator({
233
- model: "gpt-3.5-turbo",
234
- temperature: 0,
235
- maxCompletionTokens: 2000,
236
- }),
237
- new ZodStructureDefinition({
238
- name: "generateCharacter" as const,
239
- description: "Generate character descriptions.",
240
- schema: z.object({
237
+ openai
238
+ .ChatTextGenerator(/* ... */)
239
+ .asFunctionCallStructureGenerationModel({
240
+ fnName: "generateCharacter",
241
+ fnDescription: "Generate character descriptions.",
242
+ })
243
+ .withTextPrompt(),
244
+
245
+ zodSchema(
246
+ z.object({
241
247
  characters: z.array(
242
248
  z.object({
243
249
  name: z.string(),
@@ -247,13 +253,10 @@ const structureStream = await streamStructure(
247
253
  description: z.string(),
248
254
  })
249
255
  ),
250
- }),
251
- }),
252
- [
253
- OpenAIChatMessage.user(
254
- "Generate 3 character descriptions for a fantasy role playing game."
255
- ),
256
- ]
256
+ })
257
+ ),
258
+
259
+ "Generate 3 character descriptions for a fantasy role playing game."
257
260
  );
258
261
 
259
262
  for await (const part of structureStream) {
@@ -320,10 +323,14 @@ Guard functions can be used to implement retry on error, redacting and changing
320
323
  const result = await guard(
321
324
  (input, options) =>
322
325
  generateStructure(
323
- openai.ChatTextGenerator({
324
- // ...
325
- }),
326
- new ZodStructureDefinition({
326
+ openai
327
+ .ChatTextGenerator({
328
+ // ...
329
+ })
330
+ .asFunctionCallStructureGenerationModel({
331
+ fnName: "myFunction",
332
+ }),
333
+ zodSchema({
327
334
  // ...
328
335
  }),
329
336
  input,
@@ -335,10 +342,14 @@ const result = await guard(
335
342
  fixStructure({
336
343
  modifyInputForRetry: async ({ input, error }) => [
337
344
  ...input,
338
- OpenAIChatMessage.functionCall(null, {
339
- name: error.structureName,
340
- arguments: error.valueText,
341
- }),
345
+ {
346
+ role: "assistant",
347
+ content: null,
348
+ function_call: {
349
+ name: "sentiment",
350
+ arguments: JSON.stringify(error.valueText),
351
+ },
352
+ } satisfies OpenAIChatMessage,
342
353
  OpenAIChatMessage.user(error.message),
343
354
  OpenAIChatMessage.user("Please fix the error and try again."),
344
355
  ],
@@ -361,7 +372,7 @@ const calculator = new Tool({
361
372
  name: "calculator",
362
373
  description: "Execute a calculation",
363
374
 
364
- parameters: new ZodSchema(
375
+ parameters: zodSchema(
365
376
  z.object({
366
377
  a: z.number().describe("The first number."),
367
378
  b: z.number().describe("The second number."),
@@ -1,5 +1,6 @@
1
+ import { JsonSchemaProducer } from "./JsonSchemaProducer.js";
1
2
  import { Schema } from "./Schema.js";
2
- export declare class UncheckedSchema<STRUCTURE> implements Schema<STRUCTURE> {
3
+ export declare class UncheckedSchema<STRUCTURE> implements Schema<STRUCTURE>, JsonSchemaProducer {
3
4
  private readonly jsonSchema?;
4
5
  constructor(jsonSchema?: unknown);
5
6
  validate(data: unknown): {
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ZodSchema = void 0;
3
+ exports.ZodSchema = exports.zodSchema = void 0;
4
4
  const zod_to_json_schema_1 = require("zod-to-json-schema");
5
+ function zodSchema(zodSchema) {
6
+ return new ZodSchema(zodSchema);
7
+ }
8
+ exports.zodSchema = zodSchema;
5
9
  class ZodSchema {
6
10
  constructor(zodSchema) {
7
11
  Object.defineProperty(this, "zodSchema", {
@@ -1,6 +1,8 @@
1
1
  import { z } from "zod";
2
+ import { JsonSchemaProducer } from "./JsonSchemaProducer.js";
2
3
  import { Schema } from "./Schema.js";
3
- export declare class ZodSchema<STRUCTURE> implements Schema<STRUCTURE> {
4
+ export declare function zodSchema<STRUCTURE>(zodSchema: z.Schema<STRUCTURE>): ZodSchema<STRUCTURE>;
5
+ export declare class ZodSchema<STRUCTURE> implements Schema<STRUCTURE>, JsonSchemaProducer {
4
6
  readonly zodSchema: z.Schema<STRUCTURE>;
5
7
  constructor(zodSchema: z.Schema<STRUCTURE>);
6
8
  validate(data: unknown): {
@@ -1,4 +1,7 @@
1
1
  import { zodToJsonSchema } from "zod-to-json-schema";
2
+ export function zodSchema(zodSchema) {
3
+ return new ZodSchema(zodSchema);
4
+ }
2
5
  export class ZodSchema {
3
6
  constructor(zodSchema) {
4
7
  Object.defineProperty(this, "zodSchema", {
@@ -17,9 +17,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./JSONParseError.cjs"), exports);
18
18
  __exportStar(require("./JsonSchemaProducer.cjs"), exports);
19
19
  __exportStar(require("./Schema.cjs"), exports);
20
- __exportStar(require("./StructureDefinition.cjs"), exports);
21
20
  __exportStar(require("./UncheckedSchema.cjs"), exports);
22
- __exportStar(require("./UncheckedStructureDefinition.cjs"), exports);
23
21
  __exportStar(require("./ZodSchema.cjs"), exports);
24
- __exportStar(require("./ZodStructureDefinition.cjs"), exports);
25
22
  __exportStar(require("./parseJSON.cjs"), exports);
@@ -1,9 +1,6 @@
1
1
  export * from "./JSONParseError.js";
2
2
  export * from "./JsonSchemaProducer.js";
3
3
  export * from "./Schema.js";
4
- export * from "./StructureDefinition.js";
5
4
  export * from "./UncheckedSchema.js";
6
- export * from "./UncheckedStructureDefinition.js";
7
5
  export * from "./ZodSchema.js";
8
- export * from "./ZodStructureDefinition.js";
9
6
  export * from "./parseJSON.js";
@@ -1,9 +1,6 @@
1
1
  export * from "./JSONParseError.js";
2
2
  export * from "./JsonSchemaProducer.js";
3
3
  export * from "./Schema.js";
4
- export * from "./StructureDefinition.js";
5
4
  export * from "./UncheckedSchema.js";
6
- export * from "./UncheckedStructureDefinition.js";
7
5
  export * from "./ZodSchema.js";
8
- export * from "./ZodStructureDefinition.js";
9
6
  export * from "./parseJSON.js";
@@ -29,12 +29,14 @@ const StructureValidationError_js_1 = require("../model-function/generate-struct
29
29
  * const result = await guard(
30
30
  * (input) =>
31
31
  * generateStructure(
32
- * openai.ChatTextGenerator({
33
- * // ...
34
- * }),
35
- * new ZodStructureDefinition({
32
+ * openai
33
+ * .ChatTextGenerator(/* ... * /)
34
+ * .asFunctionCallStructureGenerationModel(/* ... * /),
35
+ *
36
+ * new ZodSchema({
36
37
  * // ...
37
38
  * }),
39
+ *
38
40
  * input
39
41
  * ),
40
42
  * [
@@ -43,10 +45,14 @@ const StructureValidationError_js_1 = require("../model-function/generate-struct
43
45
  * fixStructure({
44
46
  * modifyInputForRetry: async ({ input, error }) => [
45
47
  * ...input,
46
- * OpenAIChatMessage.functionCall(null, {
47
- * name: error.structureName,
48
- * arguments: error.valueText,
49
- * }),
48
+ * {
49
+ * role: "assistant",
50
+ * content: null,
51
+ function_call: {
52
+ * name: "sentiment",
53
+ * arguments: JSON.stringify(error.valueText),
54
+ * },
55
+ * } satisfies OpenAIChatMessage,
50
56
  * OpenAIChatMessage.user(error.message),
51
57
  * OpenAIChatMessage.user("Please fix the error and try again."),
52
58
  * ],
@@ -27,12 +27,14 @@ import { Guard } from "./guard.js";
27
27
  * const result = await guard(
28
28
  * (input) =>
29
29
  * generateStructure(
30
- * openai.ChatTextGenerator({
31
- * // ...
32
- * }),
33
- * new ZodStructureDefinition({
30
+ * openai
31
+ * .ChatTextGenerator(/* ... * /)
32
+ * .asFunctionCallStructureGenerationModel(/* ... * /),
33
+ *
34
+ * new ZodSchema({
34
35
  * // ...
35
36
  * }),
37
+ *
36
38
  * input
37
39
  * ),
38
40
  * [
@@ -41,10 +43,14 @@ import { Guard } from "./guard.js";
41
43
  * fixStructure({
42
44
  * modifyInputForRetry: async ({ input, error }) => [
43
45
  * ...input,
44
- * OpenAIChatMessage.functionCall(null, {
45
- * name: error.structureName,
46
- * arguments: error.valueText,
47
- * }),
46
+ * {
47
+ * role: "assistant",
48
+ * content: null,
49
+ function_call: {
50
+ * name: "sentiment",
51
+ * arguments: JSON.stringify(error.valueText),
52
+ * },
53
+ * } satisfies OpenAIChatMessage,
48
54
  * OpenAIChatMessage.user(error.message),
49
55
  * OpenAIChatMessage.user("Please fix the error and try again."),
50
56
  * ],
@@ -26,12 +26,14 @@ import { StructureValidationError } from "../model-function/generate-structure/S
26
26
  * const result = await guard(
27
27
  * (input) =>
28
28
  * generateStructure(
29
- * openai.ChatTextGenerator({
30
- * // ...
31
- * }),
32
- * new ZodStructureDefinition({
29
+ * openai
30
+ * .ChatTextGenerator(/* ... * /)
31
+ * .asFunctionCallStructureGenerationModel(/* ... * /),
32
+ *
33
+ * new ZodSchema({
33
34
  * // ...
34
35
  * }),
36
+ *
35
37
  * input
36
38
  * ),
37
39
  * [
@@ -40,10 +42,14 @@ import { StructureValidationError } from "../model-function/generate-structure/S
40
42
  * fixStructure({
41
43
  * modifyInputForRetry: async ({ input, error }) => [
42
44
  * ...input,
43
- * OpenAIChatMessage.functionCall(null, {
44
- * name: error.structureName,
45
- * arguments: error.valueText,
46
- * }),
45
+ * {
46
+ * role: "assistant",
47
+ * content: null,
48
+ function_call: {
49
+ * name: "sentiment",
50
+ * arguments: JSON.stringify(error.valueText),
51
+ * },
52
+ * } satisfies OpenAIChatMessage,
47
53
  * OpenAIChatMessage.user(error.message),
48
54
  * OpenAIChatMessage.user("Please fix the error and try again."),
49
55
  * ],
@@ -29,8 +29,8 @@ class StructureFromTextGenerationModel {
29
29
  get settingsForEvent() {
30
30
  return this.model.settingsForEvent;
31
31
  }
32
- async doGenerateStructure(structure, prompt, options) {
33
- const { response, value } = await (0, generateText_js_1.generateText)(this.model, this.format.createPrompt(prompt, structure), {
32
+ async doGenerateStructure(schema, prompt, options) {
33
+ const { response, value } = await (0, generateText_js_1.generateText)(this.model, this.format.createPrompt(prompt, schema), {
34
34
  ...options,
35
35
  returnType: "full",
36
36
  });
@@ -43,7 +43,6 @@ class StructureFromTextGenerationModel {
43
43
  }
44
44
  catch (error) {
45
45
  throw new StructureParseError_js_1.StructureParseError({
46
- structureName: structure.name,
47
46
  valueText: value,
48
47
  cause: error,
49
48
  });
@@ -1,22 +1,20 @@
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 { TextGenerationModel, TextGenerationModelSettings } from "../generate-text/TextGenerationModel.js";
5
+ import { StructureFromTextPromptFormat } from "./StructureFromTextPromptFormat.js";
4
6
  import { StructureGenerationModel } from "./StructureGenerationModel.js";
5
- export type StructureFromTextPromptFormat<PROMPT> = {
6
- createPrompt: (prompt: PROMPT, structure: StructureDefinition<string, unknown>) => string;
7
- extractStructure: (response: string) => unknown;
8
- };
9
- export declare class StructureFromTextGenerationModel<PROMPT, MODEL extends TextGenerationModel<string, TextGenerationModelSettings>> implements StructureGenerationModel<PROMPT, MODEL["settings"]> {
10
- private readonly model;
11
- private readonly format;
7
+ export declare class StructureFromTextGenerationModel<SOURCE_PROMPT, TARGET_PROMPT, MODEL extends TextGenerationModel<TARGET_PROMPT, TextGenerationModelSettings>> implements StructureGenerationModel<SOURCE_PROMPT, MODEL["settings"]> {
8
+ protected readonly model: MODEL;
9
+ protected readonly format: StructureFromTextPromptFormat<SOURCE_PROMPT, TARGET_PROMPT>;
12
10
  constructor({ model, format, }: {
13
11
  model: MODEL;
14
- format: StructureFromTextPromptFormat<PROMPT>;
12
+ format: StructureFromTextPromptFormat<SOURCE_PROMPT, TARGET_PROMPT>;
15
13
  });
16
14
  get modelInformation(): import("../ModelInformation.js").ModelInformation;
17
15
  get settings(): TextGenerationModelSettings;
18
16
  get settingsForEvent(): Partial<MODEL["settings"]>;
19
- doGenerateStructure(structure: StructureDefinition<string, unknown>, prompt: PROMPT, options?: FunctionOptions): Promise<{
17
+ doGenerateStructure(schema: Schema<unknown> & JsonSchemaProducer, prompt: SOURCE_PROMPT, options?: FunctionOptions): Promise<{
20
18
  response: unknown;
21
19
  value: unknown;
22
20
  valueText: string;
@@ -26,8 +26,8 @@ export class StructureFromTextGenerationModel {
26
26
  get settingsForEvent() {
27
27
  return this.model.settingsForEvent;
28
28
  }
29
- async doGenerateStructure(structure, prompt, options) {
30
- const { response, value } = await generateText(this.model, this.format.createPrompt(prompt, structure), {
29
+ async doGenerateStructure(schema, prompt, options) {
30
+ const { response, value } = await generateText(this.model, this.format.createPrompt(prompt, schema), {
31
31
  ...options,
32
32
  returnType: "full",
33
33
  });
@@ -40,7 +40,6 @@ export class StructureFromTextGenerationModel {
40
40
  }
41
41
  catch (error) {
42
42
  throw new StructureParseError({
43
- structureName: structure.name,
44
43
  valueText: value,
45
44
  cause: error,
46
45
  });
@@ -0,0 +1,6 @@
1
+ import { JsonSchemaProducer } from "../../core/schema/JsonSchemaProducer.js";
2
+ import { Schema } from "../../core/schema/Schema.js";
3
+ export type StructureFromTextPromptFormat<SOURCE_PROMPT, TARGET_PROMPT> = {
4
+ createPrompt: (prompt: SOURCE_PROMPT, schema: Schema<unknown> & JsonSchemaProducer) => TARGET_PROMPT;
5
+ extractStructure: (response: string) => unknown;
6
+ };
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StructureFromTextStreamingModel = void 0;
4
+ const streamText_js_1 = require("../../model-function/generate-text/streamText.cjs");
5
+ const AsyncQueue_js_1 = require("../../util/AsyncQueue.cjs");
6
+ const generateText_js_1 = require("../generate-text/generateText.cjs");
7
+ const StructureFromTextGenerationModel_js_1 = require("./StructureFromTextGenerationModel.cjs");
8
+ const StructureParseError_js_1 = require("./StructureParseError.cjs");
9
+ const parsePartialJson_js_1 = require("./parsePartialJson.cjs");
10
+ class StructureFromTextStreamingModel extends StructureFromTextGenerationModel_js_1.StructureFromTextGenerationModel {
11
+ constructor(options) {
12
+ super(options);
13
+ }
14
+ async doStreamStructure(schema, prompt, options) {
15
+ const textStream = await (0, streamText_js_1.streamText)(this.model, this.format.createPrompt(prompt, schema), options);
16
+ const queue = new AsyncQueue_js_1.AsyncQueue();
17
+ // run async on purpose:
18
+ (async () => {
19
+ try {
20
+ let fullText = "";
21
+ for await (const deltaText of textStream) {
22
+ fullText += deltaText;
23
+ const deltaStructure = (0, parsePartialJson_js_1.parsePartialJson)(fullText);
24
+ // only publish parsable structures
25
+ if (deltaStructure != null) {
26
+ queue.push({
27
+ type: "delta",
28
+ fullDelta: fullText,
29
+ valueDelta: deltaStructure,
30
+ });
31
+ }
32
+ }
33
+ }
34
+ catch (error) {
35
+ queue.push({ type: "error", error });
36
+ }
37
+ finally {
38
+ queue.close();
39
+ }
40
+ })();
41
+ return queue;
42
+ }
43
+ async doGenerateStructure(schema, prompt, options) {
44
+ const { response, value } = await (0, generateText_js_1.generateText)(this.model, this.format.createPrompt(prompt, schema), {
45
+ ...options,
46
+ returnType: "full",
47
+ });
48
+ try {
49
+ return {
50
+ response,
51
+ value: this.format.extractStructure(value),
52
+ valueText: value,
53
+ };
54
+ }
55
+ catch (error) {
56
+ throw new StructureParseError_js_1.StructureParseError({
57
+ valueText: value,
58
+ cause: error,
59
+ });
60
+ }
61
+ }
62
+ withSettings(additionalSettings) {
63
+ return new StructureFromTextStreamingModel({
64
+ model: this.model.withSettings(additionalSettings),
65
+ format: this.format,
66
+ });
67
+ }
68
+ }
69
+ exports.StructureFromTextStreamingModel = StructureFromTextStreamingModel;
@@ -0,0 +1,22 @@
1
+ import { FunctionOptions } from "../../core/FunctionOptions.js";
2
+ import { JsonSchemaProducer } from "../../core/schema/JsonSchemaProducer.js";
3
+ import { Schema } from "../../core/schema/Schema.js";
4
+ import { Delta } from "../../model-function/Delta.js";
5
+ import { AsyncQueue } from "../../util/AsyncQueue.js";
6
+ import { TextGenerationModelSettings, TextStreamingModel } from "../generate-text/TextGenerationModel.js";
7
+ import { StructureFromTextGenerationModel } from "./StructureFromTextGenerationModel.js";
8
+ import { StructureFromTextPromptFormat } from "./StructureFromTextPromptFormat.js";
9
+ import { StructureStreamingModel } from "./StructureGenerationModel.js";
10
+ export declare class StructureFromTextStreamingModel<SOURCE_PROMPT, TARGET_PROMPT, MODEL extends TextStreamingModel<TARGET_PROMPT, TextGenerationModelSettings>> extends StructureFromTextGenerationModel<SOURCE_PROMPT, TARGET_PROMPT, MODEL> implements StructureStreamingModel<SOURCE_PROMPT, MODEL["settings"]> {
11
+ constructor(options: {
12
+ model: MODEL;
13
+ format: StructureFromTextPromptFormat<SOURCE_PROMPT, TARGET_PROMPT>;
14
+ });
15
+ doStreamStructure(schema: Schema<unknown> & JsonSchemaProducer, prompt: SOURCE_PROMPT, options?: FunctionOptions): Promise<AsyncQueue<Delta<unknown>>>;
16
+ doGenerateStructure(schema: Schema<unknown> & JsonSchemaProducer, prompt: SOURCE_PROMPT, options?: FunctionOptions): Promise<{
17
+ response: unknown;
18
+ value: unknown;
19
+ valueText: string;
20
+ }>;
21
+ withSettings(additionalSettings: Partial<MODEL["settings"]>): this;
22
+ }
@@ -0,0 +1,65 @@
1
+ import { streamText } from "../../model-function/generate-text/streamText.js";
2
+ import { AsyncQueue } from "../../util/AsyncQueue.js";
3
+ import { generateText } from "../generate-text/generateText.js";
4
+ import { StructureFromTextGenerationModel } from "./StructureFromTextGenerationModel.js";
5
+ import { StructureParseError } from "./StructureParseError.js";
6
+ import { parsePartialJson } from "./parsePartialJson.js";
7
+ export class StructureFromTextStreamingModel extends StructureFromTextGenerationModel {
8
+ constructor(options) {
9
+ super(options);
10
+ }
11
+ async doStreamStructure(schema, prompt, options) {
12
+ const textStream = await streamText(this.model, this.format.createPrompt(prompt, schema), options);
13
+ const queue = new AsyncQueue();
14
+ // run async on purpose:
15
+ (async () => {
16
+ try {
17
+ let fullText = "";
18
+ for await (const deltaText of textStream) {
19
+ fullText += deltaText;
20
+ const deltaStructure = parsePartialJson(fullText);
21
+ // only publish parsable structures
22
+ if (deltaStructure != null) {
23
+ queue.push({
24
+ type: "delta",
25
+ fullDelta: fullText,
26
+ valueDelta: deltaStructure,
27
+ });
28
+ }
29
+ }
30
+ }
31
+ catch (error) {
32
+ queue.push({ type: "error", error });
33
+ }
34
+ finally {
35
+ queue.close();
36
+ }
37
+ })();
38
+ return queue;
39
+ }
40
+ async doGenerateStructure(schema, prompt, options) {
41
+ const { response, value } = await generateText(this.model, this.format.createPrompt(prompt, schema), {
42
+ ...options,
43
+ returnType: "full",
44
+ });
45
+ try {
46
+ return {
47
+ response,
48
+ value: this.format.extractStructure(value),
49
+ valueText: value,
50
+ };
51
+ }
52
+ catch (error) {
53
+ throw new StructureParseError({
54
+ valueText: value,
55
+ cause: error,
56
+ });
57
+ }
58
+ }
59
+ withSettings(additionalSettings) {
60
+ return new StructureFromTextStreamingModel({
61
+ model: this.model.withSettings(additionalSettings),
62
+ format: this.format,
63
+ });
64
+ }
65
+ }
@@ -1,11 +1,12 @@
1
+ import { JsonSchemaProducer } from "../../core/schema/JsonSchemaProducer.js";
1
2
  import { FunctionOptions } from "../../core/FunctionOptions.js";
2
- import { StructureDefinition } from "../../core/schema/StructureDefinition.js";
3
+ import { Schema } from "../../core/schema/Schema.js";
3
4
  import { Delta } from "../../model-function/Delta.js";
4
5
  import { Model, ModelSettings } from "../Model.js";
5
6
  export interface StructureGenerationModelSettings extends ModelSettings {
6
7
  }
7
8
  export interface StructureGenerationModel<PROMPT, SETTINGS extends StructureGenerationModelSettings = StructureGenerationModelSettings> extends Model<SETTINGS> {
8
- doGenerateStructure(structure: StructureDefinition<string, unknown>, prompt: PROMPT, options?: FunctionOptions): PromiseLike<{
9
+ doGenerateStructure(schema: Schema<unknown> & JsonSchemaProducer, prompt: PROMPT, options?: FunctionOptions): PromiseLike<{
9
10
  response: unknown;
10
11
  valueText: string;
11
12
  value: unknown;
@@ -17,5 +18,5 @@ export interface StructureGenerationModel<PROMPT, SETTINGS extends StructureGene
17
18
  }>;
18
19
  }
19
20
  export interface StructureStreamingModel<PROMPT, SETTINGS extends StructureGenerationModelSettings = StructureGenerationModelSettings> extends StructureGenerationModel<PROMPT, SETTINGS> {
20
- doStreamStructure(structureDefinition: StructureDefinition<string, unknown>, prompt: PROMPT, options?: FunctionOptions): PromiseLike<AsyncIterable<Delta<unknown>>>;
21
+ doStreamStructure(schema: Schema<unknown> & JsonSchemaProducer, prompt: PROMPT, options?: FunctionOptions): PromiseLike<AsyncIterable<Delta<unknown>>>;
21
22
  }
@@ -3,16 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.StructureParseError = void 0;
4
4
  const getErrorMessage_js_1 = require("../../util/getErrorMessage.cjs");
5
5
  class StructureParseError extends Error {
6
- constructor({ structureName, valueText, cause, }) {
7
- super(`Structure parsing failed for '${structureName}'. ` +
6
+ constructor({ valueText, cause }) {
7
+ super(`Structure parsing 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,
@@ -26,7 +20,6 @@ class StructureParseError extends Error {
26
20
  value: void 0
27
21
  });
28
22
  this.name = "StructureParseError";
29
- this.structureName = structureName;
30
23
  this.cause = cause;
31
24
  this.valueText = valueText;
32
25
  }
@@ -36,7 +29,6 @@ class StructureParseError extends Error {
36
29
  cause: this.cause,
37
30
  message: this.message,
38
31
  stack: this.stack,
39
- structureName: this.structureName,
40
32
  valueText: this.valueText,
41
33
  };
42
34
  }
@@ -1,9 +1,7 @@
1
1
  export declare class StructureParseError extends Error {
2
- readonly structureName: string;
3
2
  readonly cause: unknown;
4
3
  readonly valueText: string;
5
- constructor({ structureName, valueText, cause, }: {
6
- structureName: string;
4
+ constructor({ valueText, cause }: {
7
5
  valueText: string;
8
6
  cause: unknown;
9
7
  });
@@ -12,7 +10,6 @@ export declare class StructureParseError extends Error {
12
10
  cause: unknown;
13
11
  message: string;
14
12
  stack: string | undefined;
15
- structureName: string;
16
13
  valueText: string;
17
14
  };
18
15
  }