-
Notifications
You must be signed in to change notification settings - Fork 337
/
OutputParser.ts
83 lines (74 loc) · 2.28 KB
/
OutputParser.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 type { SubQuestion } from "./engines/query/types.js";
import type { BaseOutputParser, StructuredOutput } from "./types.js";
/**
* Error class for output parsing. Due to the nature of LLMs, anytime we use LLM
* to generate structured output, it's possible that it will hallucinate something
* that doesn't match the expected output format. So make sure to catch these
* errors in production.
*/
class OutputParserError extends Error {
cause: Error | undefined;
output: string | undefined;
constructor(
message: string,
options: { cause?: Error; output?: string } = {},
) {
// @ts-ignore
super(message, options); // https://github.com/tc39/proposal-error-cause
this.name = "OutputParserError";
if (!this.cause) {
// Need to check for those environments that have implemented the proposal
this.cause = options.cause;
}
this.output = options.output;
// This line is to maintain proper stack trace in V8
// (https://v8.dev/docs/stack-trace-api)
if (Error.captureStackTrace) {
Error.captureStackTrace(this, OutputParserError);
}
}
}
/**
*
* @param text A markdown block with JSON
* @returns parsed JSON object
*/
export function parseJsonMarkdown(text: string) {
text = text.trim();
const left_square = text.indexOf("[");
const left_brace = text.indexOf("{");
let left: number;
let right: number;
if (left_square < left_brace && left_square != -1) {
left = left_square;
right = text.lastIndexOf("]");
} else {
left = left_brace;
right = text.lastIndexOf("}");
}
const jsonText = text.substring(left, right + 1);
try {
//Single JSON object case
if (left_square === -1) {
return [JSON.parse(jsonText)];
}
//Multiple JSON object case.
return JSON.parse(jsonText);
} catch (e) {
throw new OutputParserError("Not a json markdown", { output: text });
}
}
/**
* SubQuestionOutputParser is used to parse the output of the SubQuestionGenerator.
*/
export class SubQuestionOutputParser
implements BaseOutputParser<StructuredOutput<SubQuestion[]>>
{
parse(output: string): StructuredOutput<SubQuestion[]> {
const parsed = parseJsonMarkdown(output);
return { rawOutput: output, parsedOutput: parsed };
}
format(output: string): string {
return output;
}
}