From 01d7ae700c3a5fb2103edfb64b3b56092fc8acec Mon Sep 17 00:00:00 2001 From: Marviel Date: Wed, 12 Apr 2023 20:38:08 -0400 Subject: [PATCH 1/3] fixup structured parser --- langchain/src/output_parsers/structured.ts | 35 ++++++++++++++-------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/langchain/src/output_parsers/structured.ts b/langchain/src/output_parsers/structured.ts index 2ec354991d31..bee66a33e9a9 100644 --- a/langchain/src/output_parsers/structured.ts +++ b/langchain/src/output_parsers/structured.ts @@ -1,7 +1,10 @@ /* eslint-disable no-instanceof/no-instanceof */ import { z } from "zod"; -import { BaseOutputParser, OutputParserException } from "../schema/index.js"; +import { + BaseOutputParser, + OutputParserException, +} from "../schema/index.js"; function printSchema(schema: z.ZodTypeAny, depth = 0): string { if ( @@ -22,6 +25,12 @@ function printSchema(schema: z.ZodTypeAny, depth = 0): string { if (schema instanceof z.ZodDate) { return "date"; } + if (schema instanceof z.ZodNullable) { + return `${printSchema(schema._def.innerType, depth)} // Nullable`; + } + if (schema instanceof z.ZodTransformer) { + return `${printSchema(schema._def.schema, depth)}`; + } if (schema instanceof z.ZodOptional) { return `${printSchema(schema._def.innerType, depth)} // Optional`; } @@ -33,18 +42,18 @@ function printSchema(schema: z.ZodTypeAny, depth = 0): string { const indentIn = "\t".repeat(depth + 1); return `{${schema._def.description ? ` // ${schema._def.description}` : ""} ${Object.entries(schema.shape) - .map( - ([key, value]) => - `${indentIn}"${key}": ${printSchema(value as z.ZodTypeAny, depth + 1)}${ - (value as z.ZodTypeAny)._def.description - ? ` // ${(value as z.ZodTypeAny)._def.description}` - : "" - }` - ) - .join("\n")} + .map( + ([key, value]) => + `${indentIn}"${key}": ${printSchema(value as z.ZodTypeAny, depth + 1)}${(value as z.ZodTypeAny)._def.description + ? ` // ${(value as z.ZodTypeAny)._def.description}` + : "" + }` + ) + .join("\n")} ${indent}}`; } - throw new Error(`Unsupported type: ${schema._def.innerType}`); + + throw new Error(`Unsupported type: ${schema._def.innerType.typeName}`); } export class StructuredOutputParser< @@ -79,6 +88,8 @@ export class StructuredOutputParser< \`\`\`json ${printSchema(this.schema)} \`\`\` + +Including the leading and trailing "\`\`\`json" and "\`\`\`" `; } @@ -88,7 +99,7 @@ ${printSchema(this.schema)} return this.schema.parse(JSON.parse(json)); } catch (e) { throw new OutputParserException( - `Failed to parse. Text: ${text}. Error: ${e}` + `Failed to parse. Text: "${text}". Error: ${e}` ); } } From 254cee16adb3cc5cb4bc73199c2604daaa658469 Mon Sep 17 00:00:00 2001 From: Marviel Date: Wed, 12 Apr 2023 20:45:29 -0400 Subject: [PATCH 2/3] format fix --- langchain/src/output_parsers/structured.ts | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/langchain/src/output_parsers/structured.ts b/langchain/src/output_parsers/structured.ts index bee66a33e9a9..d8a85b285b1d 100644 --- a/langchain/src/output_parsers/structured.ts +++ b/langchain/src/output_parsers/structured.ts @@ -1,10 +1,7 @@ /* eslint-disable no-instanceof/no-instanceof */ import { z } from "zod"; -import { - BaseOutputParser, - OutputParserException, -} from "../schema/index.js"; +import { BaseOutputParser, OutputParserException } from "../schema/index.js"; function printSchema(schema: z.ZodTypeAny, depth = 0): string { if ( @@ -42,14 +39,15 @@ function printSchema(schema: z.ZodTypeAny, depth = 0): string { const indentIn = "\t".repeat(depth + 1); return `{${schema._def.description ? ` // ${schema._def.description}` : ""} ${Object.entries(schema.shape) - .map( - ([key, value]) => - `${indentIn}"${key}": ${printSchema(value as z.ZodTypeAny, depth + 1)}${(value as z.ZodTypeAny)._def.description - ? ` // ${(value as z.ZodTypeAny)._def.description}` - : "" - }` - ) - .join("\n")} + .map( + ([key, value]) => + `${indentIn}"${key}": ${printSchema(value as z.ZodTypeAny, depth + 1)}${ + (value as z.ZodTypeAny)._def.description + ? ` // ${(value as z.ZodTypeAny)._def.description}` + : "" + }` + ) + .join("\n")} ${indent}}`; } From b4d18d9e335532d91f1a118db11d099190a6c8b6 Mon Sep 17 00:00:00 2001 From: Marviel Date: Wed, 12 Apr 2023 21:24:31 -0400 Subject: [PATCH 3/3] fix tests --- langchain/src/output_parsers/tests/structured.test.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/langchain/src/output_parsers/tests/structured.test.ts b/langchain/src/output_parsers/tests/structured.test.ts index b55631fd0239..1d379aaac3b6 100644 --- a/langchain/src/output_parsers/tests/structured.test.ts +++ b/langchain/src/output_parsers/tests/structured.test.ts @@ -1,6 +1,7 @@ -import { test, expect } from "@jest/globals"; import { z } from "zod"; +import { expect, test } from "@jest/globals"; + import { StructuredOutputParser } from "../structured.js"; test("StructuredOutputParser.fromNamesAndDescriptions", async () => { @@ -20,6 +21,8 @@ test("StructuredOutputParser.fromNamesAndDescriptions", async () => { "url": string // A link to the resource } \`\`\` + +Including the leading and trailing "\`\`\`json" and "\`\`\`" " `); }); @@ -41,6 +44,8 @@ test("StructuredOutputParser.fromZodSchema", async () => { "url": string // A link to the resource } \`\`\` + +Including the leading and trailing "\`\`\`json" and "\`\`\`" " `); }); @@ -73,6 +78,8 @@ test("StructuredOutputParser.fromZodSchema", async () => { "sources": string[] // sources used to answer the question, should be websites. } \`\`\` + +Including the leading and trailing "\`\`\`json" and "\`\`\`" " `); }); @@ -136,6 +143,8 @@ test("StructuredOutputParser.fromZodSchema", async () => { }[] } \`\`\` + +Including the leading and trailing "\`\`\`json" and "\`\`\`" " `); });