From 6f50e25bf3cb6a51a3b0a9283da9a52f1283b2e3 Mon Sep 17 00:00:00 2001 From: ItzCrazyKns Date: Tue, 30 Jul 2024 10:08:29 +0530 Subject: [PATCH] feat(output-parsers): add line output parser --- src/lib/outputParsers/lineOutputParser.ts | 46 +++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/lib/outputParsers/lineOutputParser.ts diff --git a/src/lib/outputParsers/lineOutputParser.ts b/src/lib/outputParsers/lineOutputParser.ts new file mode 100644 index 0000000..b50a20e --- /dev/null +++ b/src/lib/outputParsers/lineOutputParser.ts @@ -0,0 +1,46 @@ +import { BaseOutputParser } from '@langchain/core/output_parsers'; + +interface LineOutputParserArgs { + key?: string; +} + +class LineOutputParser extends BaseOutputParser { + private key = 'questions'; + + constructor(args?: LineOutputParserArgs) { + super(); + this.key = args.key ?? this.key; + } + + static lc_name() { + return 'LineOutputParser'; + } + + lc_namespace = ['langchain', 'output_parsers', 'line_output_parser']; + + async parse(text: string): Promise { + const regex = /^(\s*(-|\*|\d+\.\s|\d+\)\s|\u2022)\s*)+/; + const startKeyIndex = text.indexOf(`<${this.key}>`); + const endKeyIndex = text.indexOf(``); + + if (startKeyIndex === -1 || endKeyIndex === -1) { + return ''; + } + + const questionsStartIndex = + startKeyIndex === -1 ? 0 : startKeyIndex + `<${this.key}>`.length; + const questionsEndIndex = endKeyIndex === -1 ? text.length : endKeyIndex; + const line = text + .slice(questionsStartIndex, questionsEndIndex) + .trim() + .replace(regex, ''); + + return line; + } + + getFormatInstructions(): string { + throw new Error('Not implemented.'); + } +} + +export default LineOutputParser;