modelfusion 0.41.3 → 0.43.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 (61) hide show
  1. package/README.md +55 -4
  2. package/guard/fixStructure.cjs +21 -0
  3. package/guard/fixStructure.d.ts +10 -0
  4. package/guard/fixStructure.js +17 -0
  5. package/guard/guard.cjs +69 -0
  6. package/guard/guard.d.ts +28 -0
  7. package/guard/guard.js +65 -0
  8. package/guard/index.cjs +18 -0
  9. package/guard/index.d.ts +2 -0
  10. package/guard/index.js +2 -0
  11. package/index.cjs +1 -0
  12. package/index.d.ts +1 -0
  13. package/index.js +1 -0
  14. package/model-function/generate-structure/StructureFromTextGenerationModel.cjs +15 -4
  15. package/model-function/generate-structure/StructureFromTextGenerationModel.d.ts +2 -1
  16. package/model-function/generate-structure/StructureFromTextGenerationModel.js +15 -4
  17. package/model-function/generate-structure/StructureGenerationModel.d.ts +2 -1
  18. package/model-function/generate-structure/StructureOrTextGenerationModel.d.ts +2 -0
  19. package/model-function/generate-structure/StructureParseError.cjs +34 -0
  20. package/model-function/generate-structure/StructureParseError.d.ts +10 -0
  21. package/model-function/generate-structure/StructureParseError.js +30 -0
  22. package/model-function/generate-structure/StructureValidationError.cjs +10 -3
  23. package/model-function/generate-structure/StructureValidationError.d.ts +3 -1
  24. package/model-function/generate-structure/StructureValidationError.js +10 -3
  25. package/model-function/generate-structure/generateStructure.cjs +2 -1
  26. package/model-function/generate-structure/generateStructure.js +2 -1
  27. package/model-function/generate-structure/generateStructureOrText.cjs +1 -0
  28. package/model-function/generate-structure/generateStructureOrText.js +1 -0
  29. package/model-function/generate-text/TextGenerationModel.d.ts +2 -4
  30. package/model-function/index.cjs +1 -0
  31. package/model-function/index.d.ts +1 -0
  32. package/model-function/index.js +1 -0
  33. package/model-provider/anthropic/AnthropicApiConfiguration.cjs +23 -0
  34. package/model-provider/anthropic/AnthropicApiConfiguration.d.ts +11 -0
  35. package/model-provider/anthropic/AnthropicApiConfiguration.js +19 -0
  36. package/model-provider/anthropic/AnthropicError.cjs +39 -0
  37. package/model-provider/anthropic/AnthropicError.d.ts +37 -0
  38. package/model-provider/anthropic/AnthropicError.js +31 -0
  39. package/model-provider/anthropic/AnthropicPromptFormat.cjs +66 -0
  40. package/model-provider/anthropic/AnthropicPromptFormat.d.ts +11 -0
  41. package/model-provider/anthropic/AnthropicPromptFormat.js +61 -0
  42. package/model-provider/anthropic/AnthropicTextGenerationModel.cjs +226 -0
  43. package/model-provider/anthropic/AnthropicTextGenerationModel.d.ts +102 -0
  44. package/model-provider/anthropic/AnthropicTextGenerationModel.js +219 -0
  45. package/model-provider/anthropic/index.cjs +23 -0
  46. package/model-provider/anthropic/index.d.ts +4 -0
  47. package/model-provider/anthropic/index.js +4 -0
  48. package/model-provider/huggingface/HuggingFaceTextGenerationModel.d.ts +1 -1
  49. package/model-provider/index.cjs +1 -0
  50. package/model-provider/index.d.ts +1 -0
  51. package/model-provider/index.js +1 -0
  52. package/model-provider/llamacpp/LlamaCppTextGenerationModel.cjs +0 -3
  53. package/model-provider/llamacpp/LlamaCppTextGenerationModel.d.ts +0 -1
  54. package/model-provider/llamacpp/LlamaCppTextGenerationModel.js +0 -3
  55. package/model-provider/openai/chat/OpenAIChatModel.cjs +47 -20
  56. package/model-provider/openai/chat/OpenAIChatModel.d.ts +36 -2
  57. package/model-provider/openai/chat/OpenAIChatModel.js +47 -20
  58. package/package.json +1 -1
  59. package/prompt/PromptFormatTextGenerationModel.cjs +3 -3
  60. package/prompt/PromptFormatTextGenerationModel.d.ts +1 -1
  61. package/prompt/PromptFormatTextGenerationModel.js +3 -3
package/README.md CHANGED
@@ -19,8 +19,9 @@ ModelFusion is a library for building AI apps, chatbots, and agents. It provides
19
19
 
20
20
  - **Multimodal Support**: Beyond just LLMs, ModelFusion encompasses a diverse array of models including text generation, text-to-speech, speech-to-text, and image generation, allowing you to build multifaceted AI applications with ease.
21
21
  - **Flexibility and control**: AI application development can be complex and unique to each project. With ModelFusion, you have complete control over the prompts and model settings, and you can access the raw responses from the models quickly to build what you need.
22
- - **Type inference and validation**: ModelFusion uses TypeScript and [Zod](https://github.com/colinhacks/zod) to infer types wherever possible and to validate model responses.
23
- - **Integrated support features**: Essential features like logging, retries, throttling, tracing, and error handling are built-in, helping you focus more on building your application.
22
+ - **Type inference and validation**: ModelFusion uses TypeScript to infer types wherever possible and to validate model responses. By default, [Zod](https://github.com/colinhacks/zod) is used for type validation, but you can also use other libraries.
23
+ - **Guards**: ModelFusion provides a guard function that you can use to implement retry on error, redacting and changing reponses, etc.
24
+ - **Integrated support features**: Essential features like **observability**, logging, retries, throttling, tracing, and error handling are built-in, helping you focus more on building your application.
24
25
 
25
26
  ## Quick Install
26
27
 
@@ -51,7 +52,7 @@ const text = await generateText(
51
52
  );
52
53
  ```
53
54
 
54
- Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai), [Cohere](https://modelfusion.dev/integration/model-provider/cohere), [Llama.cpp](https://modelfusion.dev/integration/model-provider/llamacpp), [Hugging Face](https://modelfusion.dev/integration/model-provider/huggingface)
55
+ Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai), [Anthropic](https://modelfusion.dev/integration/model-provider/anthropic), [Cohere](https://modelfusion.dev/integration/model-provider/cohere), [Llama.cpp](https://modelfusion.dev/integration/model-provider/llamacpp), [Hugging Face](https://modelfusion.dev/integration/model-provider/huggingface)
55
56
 
56
57
  #### streamText
57
58
 
@@ -68,7 +69,7 @@ for await (const textFragment of textStream) {
68
69
  }
69
70
  ```
70
71
 
71
- Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai), [Cohere](https://modelfusion.dev/integration/model-provider/cohere), [Llama.cpp](https://modelfusion.dev/integration/model-provider/llamacpp)
72
+ Providers: [OpenAI](https://modelfusion.dev/integration/model-provider/openai), [Anthropic](https://modelfusion.dev/integration/model-provider/anthropic), [Cohere](https://modelfusion.dev/integration/model-provider/cohere), [Llama.cpp](https://modelfusion.dev/integration/model-provider/llamacpp)
72
73
 
73
74
  #### Prompt Format
74
75
 
@@ -104,6 +105,7 @@ const textStream = await streamText(
104
105
  | Prompt Format | Instruction Prompt | Chat Prompt |
105
106
  | ------------- | ------------------ | ----------- |
106
107
  | OpenAI Chat | ✅ | ✅ |
108
+ | Anthropic | ✅ | ✅ |
107
109
  | Llama 2 | ✅ | ✅ |
108
110
  | Alpaca | ✅ | ❌ |
109
111
  | Vicuna | ❌ | ✅ |
@@ -453,6 +455,43 @@ const retrievedTexts = await retrieve(
453
455
 
454
456
  Available Vector Stores: [Memory](https://modelfusion.dev/integration/vector-index/memory), [Pinecone](https://modelfusion.dev/integration/vector-index/pinecone)
455
457
 
458
+ ### Guards
459
+
460
+ [Guards](https://github.com/lgrammel/modelfusion/tree/main/examples/basic/src/guard) can be used to implement retry on error, redacting and changing reponses, etc.
461
+
462
+ #### Retry structure parsing on error:
463
+
464
+ ```ts
465
+ const result = await guard(
466
+ (input) =>
467
+ generateStructure(
468
+ new OpenAIChatModel({
469
+ // ...
470
+ }),
471
+ new ZodStructureDefinition({
472
+ // ...
473
+ }),
474
+ input
475
+ ),
476
+ [
477
+ // ...
478
+ ],
479
+ [
480
+ fixStructure({
481
+ modifyInputForRetry: async ({ input, error }) => [
482
+ ...input,
483
+ OpenAIChatMessage.functionCall(null, {
484
+ name: error.structureName,
485
+ arguments: error.valueText,
486
+ }),
487
+ OpenAIChatMessage.user(error.message),
488
+ OpenAIChatMessage.user("Please fix the error and try again."),
489
+ ],
490
+ }),
491
+ ]
492
+ );
493
+ ```
494
+
456
495
  ### Observability
457
496
 
458
497
  Integrations: [Helicone](https://modelfusion.dev/integration/observability/helicone)
@@ -494,10 +533,18 @@ Integrations: [Helicone](https://modelfusion.dev/integration/observability/helic
494
533
 
495
534
  Examples for almost all of the individual functions and objects. Highly recommended to get started.
496
535
 
536
+ ### [StoryTeller](https://github.com/lgrammel/storyteller)
537
+
538
+ > _multi-modal_, _structure streaming_, _image generation_, _text to speech_, _speech to text_, _text generation_, _structure generation_, _embeddings_
539
+
540
+ StoryTeller is an exploratory web application that creates short audio stories for pre-school kids.
541
+
497
542
  ### [Chatbot (Terminal)](https://github.com/lgrammel/modelfusion/tree/main/examples/chatbot-terminal)
498
543
 
499
544
  > _Terminal app_, _chat_, _llama.cpp_
500
545
 
546
+ A chat with an AI assistant, implemented as a terminal app.
547
+
501
548
  ### [Chatbot (Next.JS)](https://github.com/lgrammel/modelfusion/tree/main/examples/chatbot-next-js)
502
549
 
503
550
  > _Next.js app_, _OpenAI GPT-3.5-turbo_, _streaming_, _abort handling_
@@ -551,3 +598,7 @@ Extracts information about a topic from a PDF and writes a tweet in your own sty
551
598
  ### [Contributing Guide](https://github.com/lgrammel/modelfusion/blob/main/CONTRIBUTING.md)
552
599
 
553
600
  Read the [ModelFusion contributing guide](https://github.com/lgrammel/modelfusion/blob/main/CONTRIBUTING.md) to learn about the development process, how to propose bugfixes and improvements, and how to build and test your changes.
601
+
602
+ ```
603
+
604
+ ```
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fixStructure = void 0;
4
+ const StructureParseError_js_1 = require("../model-function/generate-structure/StructureParseError.cjs");
5
+ const StructureValidationError_js_1 = require("../model-function/generate-structure/StructureValidationError.cjs");
6
+ const fixStructure = ({ modifyInputForRetry }) => async (result) => {
7
+ if (result.type === "error" &&
8
+ (result.error instanceof StructureValidationError_js_1.StructureValidationError ||
9
+ result.error instanceof StructureParseError_js_1.StructureParseError)) {
10
+ return {
11
+ action: "retry",
12
+ input: await modifyInputForRetry({
13
+ type: "error",
14
+ input: result.input,
15
+ error: result.error,
16
+ }),
17
+ };
18
+ }
19
+ return undefined;
20
+ };
21
+ exports.fixStructure = fixStructure;
@@ -0,0 +1,10 @@
1
+ import { StructureParseError } from "../model-function/generate-structure/StructureParseError.js";
2
+ import { StructureValidationError } from "../model-function/generate-structure/StructureValidationError.js";
3
+ import { Guard } from "./guard.js";
4
+ export declare const fixStructure: <INPUT, OUTPUT>(options: {
5
+ modifyInputForRetry: (options: {
6
+ type: "error";
7
+ input: INPUT;
8
+ error: StructureValidationError | StructureParseError;
9
+ }) => PromiseLike<INPUT>;
10
+ }) => Guard<INPUT, OUTPUT>;
@@ -0,0 +1,17 @@
1
+ import { StructureParseError } from "../model-function/generate-structure/StructureParseError.js";
2
+ import { StructureValidationError } from "../model-function/generate-structure/StructureValidationError.js";
3
+ export const fixStructure = ({ modifyInputForRetry }) => async (result) => {
4
+ if (result.type === "error" &&
5
+ (result.error instanceof StructureValidationError ||
6
+ result.error instanceof StructureParseError)) {
7
+ return {
8
+ action: "retry",
9
+ input: await modifyInputForRetry({
10
+ type: "error",
11
+ input: result.input,
12
+ error: result.error,
13
+ }),
14
+ };
15
+ }
16
+ return undefined;
17
+ };
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.guard = void 0;
4
+ async function guard(execute, input, guards, options) {
5
+ const maxRetries = options?.maxRetries ?? 1;
6
+ let attempts = 0;
7
+ while (attempts <= maxRetries) {
8
+ let result;
9
+ try {
10
+ result = {
11
+ type: "value",
12
+ input,
13
+ output: await execute(input),
14
+ };
15
+ }
16
+ catch (error) {
17
+ result = {
18
+ type: "error",
19
+ input,
20
+ error,
21
+ };
22
+ }
23
+ let isValid = true;
24
+ for (const guard of guards) {
25
+ const guardResult = await guard(result);
26
+ if (guardResult === undefined) {
27
+ continue;
28
+ }
29
+ switch (guardResult.action) {
30
+ case "passThrough": {
31
+ break;
32
+ }
33
+ case "retry": {
34
+ input = guardResult.input;
35
+ isValid = false;
36
+ break;
37
+ }
38
+ case "return": {
39
+ result = {
40
+ type: "value",
41
+ input,
42
+ output: guardResult.output,
43
+ };
44
+ break;
45
+ }
46
+ case "throwError": {
47
+ result = {
48
+ type: "error",
49
+ input,
50
+ error: guardResult.error,
51
+ };
52
+ break;
53
+ }
54
+ }
55
+ }
56
+ if (isValid) {
57
+ if (result.type === "value") {
58
+ return result.output;
59
+ }
60
+ else {
61
+ throw result.error;
62
+ }
63
+ }
64
+ attempts++;
65
+ }
66
+ throw new Error(`Maximum retry attempts of ${maxRetries} reached ` +
67
+ `without producing a valid output or handling an error after ${attempts} attempts.`);
68
+ }
69
+ exports.guard = guard;
@@ -0,0 +1,28 @@
1
+ type OutputResult<INPUT, OUTPUT> = {
2
+ type: "value";
3
+ input: INPUT;
4
+ output: OUTPUT;
5
+ error?: undefined;
6
+ } | {
7
+ type: "error";
8
+ input: INPUT;
9
+ output?: undefined;
10
+ error: unknown;
11
+ };
12
+ export type OutputValidator<INPUT, OUTPUT> = ({ type, input, output, error, }: OutputResult<INPUT, OUTPUT>) => PromiseLike<boolean>;
13
+ export type Guard<INPUT, OUTPUT> = ({ type, input, output, error, }: OutputResult<INPUT, OUTPUT>) => PromiseLike<{
14
+ action: "retry";
15
+ input: INPUT;
16
+ } | {
17
+ action: "return";
18
+ output: OUTPUT;
19
+ } | {
20
+ action: "throwError";
21
+ error: unknown;
22
+ } | {
23
+ action: "passThrough";
24
+ } | undefined>;
25
+ export declare function guard<INPUT, OUTPUT>(execute: (input: INPUT) => PromiseLike<OUTPUT>, input: INPUT, guards: Array<Guard<INPUT, OUTPUT>>, options?: {
26
+ maxRetries: number;
27
+ }): Promise<OUTPUT | undefined>;
28
+ export {};
package/guard/guard.js ADDED
@@ -0,0 +1,65 @@
1
+ export async function guard(execute, input, guards, options) {
2
+ const maxRetries = options?.maxRetries ?? 1;
3
+ let attempts = 0;
4
+ while (attempts <= maxRetries) {
5
+ let result;
6
+ try {
7
+ result = {
8
+ type: "value",
9
+ input,
10
+ output: await execute(input),
11
+ };
12
+ }
13
+ catch (error) {
14
+ result = {
15
+ type: "error",
16
+ input,
17
+ error,
18
+ };
19
+ }
20
+ let isValid = true;
21
+ for (const guard of guards) {
22
+ const guardResult = await guard(result);
23
+ if (guardResult === undefined) {
24
+ continue;
25
+ }
26
+ switch (guardResult.action) {
27
+ case "passThrough": {
28
+ break;
29
+ }
30
+ case "retry": {
31
+ input = guardResult.input;
32
+ isValid = false;
33
+ break;
34
+ }
35
+ case "return": {
36
+ result = {
37
+ type: "value",
38
+ input,
39
+ output: guardResult.output,
40
+ };
41
+ break;
42
+ }
43
+ case "throwError": {
44
+ result = {
45
+ type: "error",
46
+ input,
47
+ error: guardResult.error,
48
+ };
49
+ break;
50
+ }
51
+ }
52
+ }
53
+ if (isValid) {
54
+ if (result.type === "value") {
55
+ return result.output;
56
+ }
57
+ else {
58
+ throw result.error;
59
+ }
60
+ }
61
+ attempts++;
62
+ }
63
+ throw new Error(`Maximum retry attempts of ${maxRetries} reached ` +
64
+ `without producing a valid output or handling an error after ${attempts} attempts.`);
65
+ }
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./fixStructure.cjs"), exports);
18
+ __exportStar(require("./guard.cjs"), exports);
@@ -0,0 +1,2 @@
1
+ export * from "./fixStructure.js";
2
+ export * from "./guard.js";
package/guard/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./fixStructure.js";
2
+ export * from "./guard.js";
package/index.cjs CHANGED
@@ -18,6 +18,7 @@ __exportStar(require("./composed-function/index.cjs"), exports);
18
18
  __exportStar(require("./core/index.cjs"), exports);
19
19
  __exportStar(require("./cost/index.cjs"), exports);
20
20
  __exportStar(require("./event-source/index.cjs"), exports);
21
+ __exportStar(require("./guard/index.cjs"), exports);
21
22
  __exportStar(require("./model-function/index.cjs"), exports);
22
23
  __exportStar(require("./model-provider/index.cjs"), exports);
23
24
  __exportStar(require("./observability/index.cjs"), exports);
package/index.d.ts CHANGED
@@ -2,6 +2,7 @@ export * from "./composed-function/index.js";
2
2
  export * from "./core/index.js";
3
3
  export * from "./cost/index.js";
4
4
  export * from "./event-source/index.js";
5
+ export * from "./guard/index.js";
5
6
  export * from "./model-function/index.js";
6
7
  export * from "./model-provider/index.js";
7
8
  export * from "./observability/index.js";
package/index.js CHANGED
@@ -2,6 +2,7 @@ export * from "./composed-function/index.js";
2
2
  export * from "./core/index.js";
3
3
  export * from "./cost/index.js";
4
4
  export * from "./event-source/index.js";
5
+ export * from "./guard/index.js";
5
6
  export * from "./model-function/index.js";
6
7
  export * from "./model-provider/index.js";
7
8
  export * from "./observability/index.js";
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.StructureFromTextGenerationModel = void 0;
4
4
  const generateText_js_1 = require("../generate-text/generateText.cjs");
5
+ const StructureParseError_js_1 = require("./StructureParseError.cjs");
5
6
  class StructureFromTextGenerationModel {
6
7
  constructor({ model, format, }) {
7
8
  Object.defineProperty(this, "model", {
@@ -30,10 +31,20 @@ class StructureFromTextGenerationModel {
30
31
  }
31
32
  async doGenerateStructure(structure, prompt, options) {
32
33
  const { response, value } = await (0, generateText_js_1.generateText)(this.model, this.format.createPrompt(prompt, structure), options).asFullResponse();
33
- return {
34
- response,
35
- structure: this.format.extractStructure(value),
36
- };
34
+ try {
35
+ return {
36
+ response,
37
+ value: this.format.extractStructure(value),
38
+ valueText: value,
39
+ };
40
+ }
41
+ catch (error) {
42
+ throw new StructureParseError_js_1.StructureParseError({
43
+ structureName: structure.name,
44
+ valueText: value,
45
+ cause: error,
46
+ });
47
+ }
37
48
  }
38
49
  withSettings(additionalSettings) {
39
50
  return new StructureFromTextGenerationModel({
@@ -18,7 +18,8 @@ export declare class StructureFromTextGenerationModel<PROMPT, MODEL extends Text
18
18
  get settingsForEvent(): Partial<MODEL["settings"]>;
19
19
  doGenerateStructure(structure: StructureDefinition<string, unknown>, prompt: PROMPT, options?: FunctionOptions): Promise<{
20
20
  response: unknown;
21
- structure: unknown;
21
+ value: unknown;
22
+ valueText: string;
22
23
  }>;
23
24
  withSettings(additionalSettings: Partial<MODEL["settings"]>): this;
24
25
  }
@@ -1,4 +1,5 @@
1
1
  import { generateText } from "../generate-text/generateText.js";
2
+ import { StructureParseError } from "./StructureParseError.js";
2
3
  export class StructureFromTextGenerationModel {
3
4
  constructor({ model, format, }) {
4
5
  Object.defineProperty(this, "model", {
@@ -27,10 +28,20 @@ export class StructureFromTextGenerationModel {
27
28
  }
28
29
  async doGenerateStructure(structure, prompt, options) {
29
30
  const { response, value } = await generateText(this.model, this.format.createPrompt(prompt, structure), options).asFullResponse();
30
- return {
31
- response,
32
- structure: this.format.extractStructure(value),
33
- };
31
+ try {
32
+ return {
33
+ response,
34
+ value: this.format.extractStructure(value),
35
+ valueText: value,
36
+ };
37
+ }
38
+ catch (error) {
39
+ throw new StructureParseError({
40
+ structureName: structure.name,
41
+ valueText: value,
42
+ cause: error,
43
+ });
44
+ }
34
45
  }
35
46
  withSettings(additionalSettings) {
36
47
  return new StructureFromTextGenerationModel({
@@ -7,7 +7,8 @@ export interface StructureGenerationModelSettings extends ModelSettings {
7
7
  export interface StructureGenerationModel<PROMPT, SETTINGS extends StructureGenerationModelSettings = StructureGenerationModelSettings> extends Model<SETTINGS> {
8
8
  doGenerateStructure(structure: StructureDefinition<string, unknown>, prompt: PROMPT, options?: FunctionOptions): PromiseLike<{
9
9
  response: unknown;
10
- structure: unknown;
10
+ valueText: string;
11
+ value: unknown;
11
12
  usage?: {
12
13
  promptTokens: number;
13
14
  completionTokens: number;
@@ -9,10 +9,12 @@ export interface StructureOrTextGenerationModel<PROMPT, SETTINGS extends Structu
9
9
  structureAndText: {
10
10
  structure: null;
11
11
  value: null;
12
+ valueText: null;
12
13
  text: string;
13
14
  } | {
14
15
  structure: string;
15
16
  value: unknown;
17
+ valueText: string;
16
18
  text: string | null;
17
19
  };
18
20
  usage?: {
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StructureParseError = void 0;
4
+ const getErrorMessage_js_1 = require("../../util/getErrorMessage.cjs");
5
+ class StructureParseError extends Error {
6
+ constructor({ structureName, valueText, cause, }) {
7
+ super(`Structure parsing failed for '${structureName}'. ` +
8
+ `Value: ${valueText}.\n` +
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
+ Object.defineProperty(this, "cause", {
17
+ enumerable: true,
18
+ configurable: true,
19
+ writable: true,
20
+ value: void 0
21
+ });
22
+ Object.defineProperty(this, "valueText", {
23
+ enumerable: true,
24
+ configurable: true,
25
+ writable: true,
26
+ value: void 0
27
+ });
28
+ this.name = "StructureParseError";
29
+ this.structureName = structureName;
30
+ this.cause = cause;
31
+ this.valueText = valueText;
32
+ }
33
+ }
34
+ exports.StructureParseError = StructureParseError;
@@ -0,0 +1,10 @@
1
+ export declare class StructureParseError extends Error {
2
+ readonly structureName: string;
3
+ readonly cause: unknown;
4
+ readonly valueText: string;
5
+ constructor({ structureName, valueText, cause, }: {
6
+ structureName: string;
7
+ valueText: string;
8
+ cause: unknown;
9
+ });
10
+ }
@@ -0,0 +1,30 @@
1
+ import { getErrorMessage } from "../../util/getErrorMessage.js";
2
+ export class StructureParseError extends Error {
3
+ constructor({ structureName, valueText, cause, }) {
4
+ super(`Structure parsing failed for '${structureName}'. ` +
5
+ `Value: ${valueText}.\n` +
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
+ Object.defineProperty(this, "cause", {
14
+ enumerable: true,
15
+ configurable: true,
16
+ writable: true,
17
+ value: void 0
18
+ });
19
+ Object.defineProperty(this, "valueText", {
20
+ enumerable: true,
21
+ configurable: true,
22
+ writable: true,
23
+ value: void 0
24
+ });
25
+ this.name = "StructureParseError";
26
+ this.structureName = structureName;
27
+ this.cause = cause;
28
+ this.valueText = valueText;
29
+ }
30
+ }
@@ -3,9 +3,9 @@ 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, cause, }) {
7
- super(`Structure validation error for '${structureName}'. ` +
8
- `Value: ${JSON.stringify(value)}.\n` +
6
+ constructor({ structureName, value, valueText, cause, }) {
7
+ super(`Structure validation failed for '${structureName}'. ` +
8
+ `Value: ${valueText}.\n` +
9
9
  `Error message: ${(0, getErrorMessage_js_1.getErrorMessage)(cause)}`);
10
10
  Object.defineProperty(this, "structureName", {
11
11
  enumerable: true,
@@ -19,6 +19,12 @@ class StructureValidationError extends Error {
19
19
  writable: true,
20
20
  value: void 0
21
21
  });
22
+ Object.defineProperty(this, "valueText", {
23
+ enumerable: true,
24
+ configurable: true,
25
+ writable: true,
26
+ value: void 0
27
+ });
22
28
  Object.defineProperty(this, "value", {
23
29
  enumerable: true,
24
30
  configurable: true,
@@ -29,6 +35,7 @@ class StructureValidationError extends Error {
29
35
  this.structureName = structureName;
30
36
  this.cause = cause;
31
37
  this.value = value;
38
+ this.valueText = valueText;
32
39
  }
33
40
  }
34
41
  exports.StructureValidationError = StructureValidationError;
@@ -1,10 +1,12 @@
1
1
  export declare class StructureValidationError extends Error {
2
2
  readonly structureName: string;
3
3
  readonly cause: unknown;
4
+ readonly valueText: string;
4
5
  readonly value: unknown;
5
- constructor({ structureName, value, cause, }: {
6
+ constructor({ structureName, value, valueText, cause, }: {
6
7
  structureName: string;
7
8
  value: unknown;
9
+ valueText: string;
8
10
  cause: unknown;
9
11
  });
10
12
  }
@@ -1,8 +1,8 @@
1
1
  import { getErrorMessage } from "../../util/getErrorMessage.js";
2
2
  export class StructureValidationError extends Error {
3
- constructor({ structureName, value, cause, }) {
4
- super(`Structure validation error for '${structureName}'. ` +
5
- `Value: ${JSON.stringify(value)}.\n` +
3
+ constructor({ structureName, value, valueText, cause, }) {
4
+ super(`Structure validation failed for '${structureName}'. ` +
5
+ `Value: ${valueText}.\n` +
6
6
  `Error message: ${getErrorMessage(cause)}`);
7
7
  Object.defineProperty(this, "structureName", {
8
8
  enumerable: true,
@@ -16,6 +16,12 @@ export class StructureValidationError extends Error {
16
16
  writable: true,
17
17
  value: void 0
18
18
  });
19
+ Object.defineProperty(this, "valueText", {
20
+ enumerable: true,
21
+ configurable: true,
22
+ writable: true,
23
+ value: void 0
24
+ });
19
25
  Object.defineProperty(this, "value", {
20
26
  enumerable: true,
21
27
  configurable: true,
@@ -26,5 +32,6 @@ export class StructureValidationError extends Error {
26
32
  this.structureName = structureName;
27
33
  this.cause = cause;
28
34
  this.value = value;
35
+ this.valueText = valueText;
29
36
  }
30
37
  }
@@ -15,11 +15,12 @@ function generateStructure(model, structureDefinition, prompt, options) {
15
15
  options,
16
16
  generateResponse: async (options) => {
17
17
  const result = await model.doGenerateStructure(structureDefinition, expandedPrompt, options);
18
- const structure = result.structure;
18
+ const structure = result.value;
19
19
  const parseResult = structureDefinition.schema.validate(structure);
20
20
  if (!parseResult.success) {
21
21
  throw new StructureValidationError_js_1.StructureValidationError({
22
22
  structureName: structureDefinition.name,
23
+ valueText: result.valueText,
23
24
  value: structure,
24
25
  cause: parseResult.error,
25
26
  });