-
Notifications
You must be signed in to change notification settings - Fork 2k
/
output_parser.ts
83 lines (79 loc) · 2.46 KB
/
output_parser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { BaseOutputParser } from "@langchain/core/output_parsers";
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
* already escaped ones intact.
* It also extracts the json code if it is inside a code block
*/
export function preprocessJsonInput(inputStr: string): string {
const correctedStr = inputStr.replace(
/(?<!\\)\\(?!["\\/bfnrt]|u[0-9a-fA-F]{4})/g,
"\\\\"
);
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;
}
}
/**
* Class responsible for parsing the output of AutoGPT. It extends the
* BaseOutputParser class.
*/
export class AutoGPTOutputParser extends BaseOutputParser<AutoGPTAction> {
lc_namespace = ["langchain", "experimental", "autogpt"];
/**
* Method not implemented in the class and will throw an error if called.
* It is likely meant to be overridden in subclasses to provide specific
* format instructions.
* @returns Throws an error.
*/
getFormatInstructions(): string {
throw new Error("Method not implemented.");
}
/**
* Asynchronous method that takes a string as input and attempts to parse
* it into an AutoGPTAction object. If the input string cannot be parsed
* directly, the method tries to preprocess the string using the
* preprocessJsonInput function and parse it again. If parsing fails
* again, it returns an AutoGPTAction object with an error message.
* @param text The string to be parsed.
* @returns A Promise that resolves to an AutoGPTAction object.
*/
async parse(text: string): Promise<AutoGPTAction> {
let parsed: {
command: {
name: string;
args: Record<string, unknown>;
};
};
try {
parsed = JSON.parse(text);
} catch (error) {
const preprocessedText = preprocessJsonInput(text);
try {
parsed = JSON.parse(preprocessedText);
} catch (error) {
return {
name: "ERROR",
args: { error: `Could not parse invalid json: ${text}` },
};
}
}
try {
return {
name: parsed.command.name,
args: parsed.command.args,
};
} catch (error) {
return {
name: "ERROR",
args: { error: `Incomplete command args: ${parsed}` },
};
}
}
}