File size: 1,747 Bytes
e706de2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import {Runnable} from '../core/runnable.js';

/**

 * Base class for all output parsers

 * Transforms LLM text output into structured data

 */
export class BaseOutputParser extends Runnable {
    constructor() {
        super();
        this.name = this.constructor.name;
    }

    /**

     * Parse the LLM output into structured data

     * @abstract

     * @param {string} text - Raw LLM output

     * @returns {Promise<any>} Parsed data

     */
    async parse(text) {
        throw new Error(`${this.name} must implement parse()`);
    }

    /**

     * Get instructions for the LLM on how to format output

     * @returns {string} Format instructions

     */
    getFormatInstructions() {
        return '';
    }

    /**

     * Runnable interface: parse the output

     */
    async _call(input, config) {
        // Input can be a string or a Message
        const text = typeof input === 'string'
            ? input
            : input.content;

        return await this.parse(text);
    }

    /**

     * Parse with error handling

     */
    async parseWithPrompt(text, prompt) {
        try {
            return await this.parse(text);
        } catch (error) {
            throw new OutputParserException(
                `Failed to parse output from prompt: ${error.message}`,
                text,
                error
            );
        }
    }
}

/**

 * Exception thrown when parsing fails

 */
export class OutputParserException extends Error {
    constructor(message, llmOutput, originalError) {
        super(message);
        this.name = 'OutputParserException';
        this.llmOutput = llmOutput;
        this.originalError = originalError;
    }
}