Skip to content

Commit

Permalink
experimental[patch]: Improve AutoGPT's output_parser to extract JSON …
Browse files Browse the repository at this point in the history
…code block (#3656)

* Improve output_parser to extract JSON code block

Closes #3655

* Ran yarn format and lint
  • Loading branch information
mayt authored Dec 15, 2023
1 parent bb2fd42 commit 3e73fb7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
16 changes: 11 additions & 5 deletions langchain/src/experimental/autogpt/output_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@ import { BaseOutputParser } from "../../schema/output_parser.js";
import { AutoGPTAction } from "./schema.js";

/**
* Utility function used to preprocess a string to be parsed as JSON. It
* replaces single backslashes with double backslashes, while leaving
* Utility function used to preprocess a string to be parsed as JSON.
* It replaces single backslashes with double backslashes, while leaving
* already escaped ones intact.
* It also extracts the json code if it is inside a code block
*/
export function preprocessJsonInput(inputStr: string): string {
// Replace single backslashes with double backslashes,
// while leaving already escaped ones intact
const correctedStr = inputStr.replace(
/(?<!\\)\\(?!["\\/bfnrt]|u[0-9a-fA-F]{4})/g,
"\\\\"
);
return correctedStr;
const match = correctedStr.match(
/```(.*)(\r\n|\r|\n)(?<code>[\w\W\n]+)(\r\n|\r|\n)```/
);
if (match?.groups?.code) {
return match.groups.code.trim();
} else {
return correctedStr;
}
}

/**
Expand Down
12 changes: 12 additions & 0 deletions langchain/src/experimental/autogpt/tests/output_parser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { test, expect } from "@jest/globals";
import { preprocessJsonInput } from "../output_parser.js";

test("should parse outputs correctly", () => {
expect(preprocessJsonInput("{'escaped':'\\a'}")).toBe("{'escaped':'\\\\a'}");

expect(preprocessJsonInput("```\n{}\n```")).toBe("{}");
expect(preprocessJsonInput("```json\n{}\n```")).toBe("{}");
expect(
preprocessJsonInput("I will do the following:\n\n```json\n{}\n```")
).toBe("{}");
});

2 comments on commit 3e73fb7

@vercel
Copy link

@vercel vercel bot commented on 3e73fb7 Dec 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 3e73fb7 Dec 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

langchainjs-docs – ./docs/core_docs/

langchainjs-docs-langchain.vercel.app
langchainjs-docs-git-main-langchain.vercel.app
js.langchain.com
langchainjs-docs-ruddy.vercel.app

Please sign in to comment.