aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/chatbot/tools/SearchTool.ts
blob: d22f4c189c764d421fefdc8967a6d0af22b94d0c (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
68
69
70
import { v4 as uuidv4 } from 'uuid';
import { Networking } from '../../../../Network';
import { BaseTool } from './BaseTool';
import { Observation } from '../types/types';
import { ParametersType } from '../types/tool_types';

const searchToolParams = [
    {
        name: 'queries',
        type: 'string[]',
        description: 'The search query or queries to use for finding websites',
        required: true,
        max_inputs: 3,
    },
] as const;

type SearchToolParamsType = typeof searchToolParams;

export class SearchTool extends BaseTool<SearchToolParamsType> {
    private _addLinkedUrlDoc: (url: string, id: string) => void;
    private _max_results: number;

    constructor(addLinkedUrlDoc: (url: string, id: string) => void, max_results: number = 4) {
        super(
            'searchTool',
            'Search the web to find a wide range of websites related to a query or multiple queries',
            searchToolParams,
            'Provide up to 3 search queries to find a broad range of websites.',
            'Returns a list of websites and their overviews based on the search queries.'
        );
        this._addLinkedUrlDoc = addLinkedUrlDoc;
        this._max_results = max_results;
    }

    async execute(args: ParametersType<SearchToolParamsType>): Promise<Observation[]> {
        const queries = args.queries;

        console.log(`Searching the web for queries: ${queries[0]}`);
        // Create an array of promises, each one handling a search for a query
        const searchPromises = queries.map(async query => {
            try {
                const { results } = await Networking.PostToServer('/getWebSearchResults', {
                    query,
                    max_results: this._max_results,
                });
                const data = results.map((result: { url: string; snippet: string }) => {
                    const id = uuidv4();
                    this._addLinkedUrlDoc(result.url, id);
                    return {
                        type: 'text',
                        text: `<chunk chunk_id="${id}" chunk_type="url"><url>${result.url}</url><overview>${result.snippet}</overview></chunk>`,
                    };
                });
                return data;
            } catch (error) {
                console.log(error);
                return [
                    {
                        type: 'text',
                        text: `An error occurred while performing the web search for query: ${query}`,
                    },
                ];
            }
        });

        const allResultsArrays = await Promise.all(searchPromises);

        return allResultsArrays.flat();
    }
}