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;