diff options
| author | bobzel <zzzman@gmail.com> | 2024-10-17 17:19:25 -0400 |
|---|---|---|
| committer | bobzel <zzzman@gmail.com> | 2024-10-17 17:19:25 -0400 |
| commit | 8ac260db2fdffc37ff9b6e91971f287df6a70528 (patch) | |
| tree | c4bad3d44cb4c374b84834a39f5fc664345784f7 /src/client/views/nodes/chatbot/tools/SearchTool.ts | |
| parent | 3067940f28563d1217056f6eb428d377365077a8 (diff) | |
| parent | dd93f5175064850c6c0e47f025cd7bbba1f23106 (diff) | |
Merge branch 'master' into alyssa-starter
Diffstat (limited to 'src/client/views/nodes/chatbot/tools/SearchTool.ts')
| -rw-r--r-- | src/client/views/nodes/chatbot/tools/SearchTool.ts | 65 |
1 files changed, 40 insertions, 25 deletions
diff --git a/src/client/views/nodes/chatbot/tools/SearchTool.ts b/src/client/views/nodes/chatbot/tools/SearchTool.ts index 3a4668422..fd5144dd6 100644 --- a/src/client/views/nodes/chatbot/tools/SearchTool.ts +++ b/src/client/views/nodes/chatbot/tools/SearchTool.ts @@ -1,53 +1,68 @@ import { v4 as uuidv4 } from 'uuid'; import { Networking } from '../../../../Network'; import { BaseTool } from './BaseTool'; +import { Observation } from '../types/types'; +import { ParametersType } from './ToolTypes'; -export class SearchTool extends BaseTool<{ query: string | string[] }> { +const searchToolParams = [ + { + name: 'query', + 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 = 5) { super( 'searchTool', 'Search the web to find a wide range of websites related to a query or multiple queries', - { - query: { - type: 'string', - description: 'The search query or queries to use for finding websites', - required: 'true', - max_inputs: '3', - }, - }, - 'Provide up to 3 search queries to find a broad range of websites. This tool is intended to help you identify relevant websites, but not to be used for providing the final answer. Use this information to determine which specific website to investigate further.', - 'Returns a list of websites and their overviews based on the search queries, helping to identify which websites might contain relevant information.' + 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: { query: string | string[] }): Promise<unknown> { - const queries = Array.isArray(args.query) ? args.query : [args.query]; - const allResults = []; + async execute(args: ParametersType<SearchToolParamsType>): Promise<Observation[]> { + const queries = args.query; - for (const query of queries) { + // 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: { type: string; text: string }[] = results.map((result: { url: string; snippet: string }) => { + const { results } = await Networking.PostToServer('/getWebSearchResults', { + query, + max_results: this._max_results, + }); + const data = results.map((result: { url: string; snippet: string }) => { const id = uuidv4(); return { type: 'text', - text: `<chunk chunk_id="${id}" chunk_type="text"> - <url>${result.url}</url> - <overview>${result.snippet}</overview> - </chunk>`, + text: `<chunk chunk_id="${id}" chunk_type="text"><url>${result.url}</url><overview>${result.snippet}</overview></chunk>`, }; }); - allResults.push(...data); + return data; } catch (error) { console.log(error); - allResults.push({ type: 'text', text: `An error occurred while performing the web search for query: ${query}` }); + return [ + { + type: 'text', + text: `An error occurred while performing the web search for query: ${query}`, + }, + ]; } - } + }); + + const allResultsArrays = await Promise.all(searchPromises); - return allResults; + return allResultsArrays.flat(); } } |
