aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/chatbot/tools/DataAnalysisTool.ts
blob: 8c5e3d9cd7a1cbc6d0671cce59ded82eba55e9e6 (plain)
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
import { Observation } from '../types/types';
import { ParametersType, ToolInfo } from '../types/tool_types';
import { BaseTool } from './BaseTool';

const dataAnalysisToolParams = [
    {
        name: 'csv_file_names',
        type: 'string[]',
        description: 'List of names of the CSV files to analyze',
        required: true,
        max_inputs: 3,
    },
] as const;

type DataAnalysisToolParamsType = typeof dataAnalysisToolParams;

const dataAnalysisToolInfo: ToolInfo<DataAnalysisToolParamsType> = {
    name: 'dataAnalysis',
    description: 'Provides the full CSV file text for your analysis based on the user query and the available CSV file(s).',
    citationRules: 'No citation needed.',
    parameterRules: dataAnalysisToolParams,
};

export class DataAnalysisTool extends BaseTool<DataAnalysisToolParamsType> {
    private csv_files_function: () => { filename: string; id: string; text: string }[];

    constructor(csv_files: () => { filename: string; id: string; text: string }[]) {
        super(dataAnalysisToolInfo);
        this.csv_files_function = csv_files;
    }

    getFileContent(filename: string): string | undefined {
        const files = this.csv_files_function();
        const file = files.find(f => f.filename === filename);
        return file?.text;
    }

    getFileID(filename: string): string | undefined {
        const files = this.csv_files_function();
        const file = files.find(f => f.filename === filename);
        return file?.id;
    }

    async execute(args: ParametersType<DataAnalysisToolParamsType>): Promise<Observation[]> {
        const filenames = args.csv_file_names;
        const results: Observation[] = [];

        for (const filename of filenames) {
            const fileContent = this.getFileContent(filename);
            const fileID = this.getFileID(filename);

            if (fileContent && fileID) {
                results.push({
                    type: 'text',
                    text: `<chunk chunk_id="${fileID}" chunk_type="csv">${fileContent}</chunk>`,
                });
            } else {
                results.push({
                    type: 'text',
                    text: `File not found: ${filename}`,
                });
            }
        }

        return results;
    }
}