Skip to content
Snippets Groups Projects
Commit c77b150c authored by Elliot Kang's avatar Elliot Kang
Browse files

hardcoding single JSON object case

parent 3cf27bb8
No related branches found
No related tags found
No related merge requests found
......@@ -56,11 +56,15 @@ class OutputParserError extends Error {
function parseJsonMarkdown(text: string) {
text = text.trim();
//This code is more general than the previous version, and should be faster.
const beginIndex = text.indexOf("[");
const endIndex = text.lastIndexOf("]");
const jsonText = text.substring(beginIndex, endIndex + 1);
try {
//Single JSON object case.
if(beginIndex === -1 || endIndex === -1){
return [JSON.parse(text)];
}
//Multiple JSON object case.
return JSON.parse(jsonText);
} catch (e) {
throw new OutputParserError("Not a json markdown", { output: text });
......
......@@ -49,4 +49,42 @@ ${data_str}
expect(parser.parse(JSON.stringify(data))).toEqual(real_answer);
});
test("parses null single response", () => {
const parser = new SubQuestionOutputParser();
const data_str =
"[\n" +
" {\n" +
` "subQuestion": "Sorry, I don't have any relevant information to answer your question",\n` +
' "toolName": ""\n' +
" }\n" +
"]";
const data = [
{
subQuestion:
"Sorry, I don't have any relevant information to answer your question",
toolName: "",
},
];
const real_answer = { parsedOutput: data, rawOutput: data_str };
expect(parser.parse(data_str)).toEqual(real_answer);
});
test("Single JSON object case", () => {
const parser = new SubQuestionOutputParser();
const data_str =
" {\n" +
` "subQuestion": "Sorry, I don't have any relevant information to answer your question",\n` +
' "toolName": ""\n' +
" }\n";
const data = [
{
subQuestion:
"Sorry, I don't have any relevant information to answer your question",
toolName: "",
},
];
const real_answer = { parsedOutput: data, rawOutput: data_str };
expect(parser.parse(data_str)).toEqual(real_answer);
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment