From 6f9b8f9b393d411a17f7954b6cc36618efe698e2 Mon Sep 17 00:00:00 2001 From: "A.J. Shulman" Date: Thu, 15 Aug 2024 13:16:32 -0400 Subject: implemented search tool and other tools but scraping doesn't work --- src/client/views/nodes/ChatBox/tools/BaseTool.ts | 19 +++------ .../views/nodes/ChatBox/tools/DataAnalysisTool.ts | 17 ++++++-- src/client/views/nodes/ChatBox/tools/SearchTool.ts | 47 ++++++++++++++++++++++ .../nodes/ChatBox/tools/WebsiteInfoScraperTool.ts | 35 ++++++++++++++++ .../views/nodes/ChatBox/tools/WikipediaTool.ts | 10 ++++- 5 files changed, 109 insertions(+), 19 deletions(-) create mode 100644 src/client/views/nodes/ChatBox/tools/SearchTool.ts create mode 100644 src/client/views/nodes/ChatBox/tools/WebsiteInfoScraperTool.ts (limited to 'src/client/views/nodes/ChatBox/tools') diff --git a/src/client/views/nodes/ChatBox/tools/BaseTool.ts b/src/client/views/nodes/ChatBox/tools/BaseTool.ts index c7942e359..2e2267653 100644 --- a/src/client/views/nodes/ChatBox/tools/BaseTool.ts +++ b/src/client/views/nodes/ChatBox/tools/BaseTool.ts @@ -5,26 +5,19 @@ export abstract class BaseTool = Record, - public useRules: string, + public citationRules: string, public briefSummary: string ) {} abstract execute(args: T): Promise; - getActionRule(isCurrentTool: boolean): Record { - if (isCurrentTool) { - return { - [this.name]: { - name: this.name, - useRules: this.useRules, - description: this.description, - parameters: this.parameters, - }, - }; - } + getActionRule(): Record { return { [this.name]: { - description: 'This tool is not currently selected.', + name: this.name, + citationRules: this.citationRules, + description: this.description, + parameters: this.parameters, }, }; } diff --git a/src/client/views/nodes/ChatBox/tools/DataAnalysisTool.ts b/src/client/views/nodes/ChatBox/tools/DataAnalysisTool.ts index d2edc4847..b45733639 100644 --- a/src/client/views/nodes/ChatBox/tools/DataAnalysisTool.ts +++ b/src/client/views/nodes/ChatBox/tools/DataAnalysisTool.ts @@ -1,8 +1,9 @@ import { BaseTool } from './BaseTool'; export class DataAnalysisTool extends BaseTool<{ csv_file_name: string }> { - private csv_files_function: () => { [filename: string]: string }; - constructor(csv_files: () => { [filename: string]: string }) { + private csv_files_function: () => { filename: string; id: string; text: string }[]; + + constructor(csv_files: () => { filename: string; id: string; text: string }[]) { super( 'dataAnalysis', 'Analyzes, and provides insights, from a CSV file', @@ -21,10 +22,18 @@ export class DataAnalysisTool extends BaseTool<{ csv_file_name: string }> { getFileContent(filename: string): string | undefined { const files = this.csv_files_function(); - return files[filename]; + 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: { csv_file_name: string }): Promise { - return [{ type: 'text', text: this.getFileContent(args.csv_file_name) }]; + console.log(this.csv_files_function()); + return [{ type: 'text', text: `` + this.getFileContent(args.csv_file_name) + '' }]; } } diff --git a/src/client/views/nodes/ChatBox/tools/SearchTool.ts b/src/client/views/nodes/ChatBox/tools/SearchTool.ts new file mode 100644 index 000000000..91ecc71ff --- /dev/null +++ b/src/client/views/nodes/ChatBox/tools/SearchTool.ts @@ -0,0 +1,47 @@ +import { Networking } from '../../../../Network'; +import { BaseTool } from './BaseTool'; +import { v4 as uuidv4 } from 'uuid'; + +export class SearchTool extends BaseTool<{ query: string }> { + private _addLinkedUrlDoc: (url: string, id: string) => void; + + constructor(addLinkedUrlDoc: (url: string, id: string) => void) { + super( + 'searchTool', + 'Search the web to find a wide range of websites related to a query', + { + query: { + type: 'string', + description: 'The search query to use for finding websites', + required: true, + }, + }, + 'Provide a search query 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 query, helping to identify which website might contain the most relevant information.' + ); + this._addLinkedUrlDoc = addLinkedUrlDoc; + } + + async execute(args: { query: string }): Promise { + try { + const { results } = await Networking.PostToServer('/getWebSearchResults', { query: args.query }); + console.log(results); + const data: { type: string; text: string }[] = results.map((result: { url: string; snippet: string }) => { + console.log; + const id = uuidv4(); + this._addLinkedUrlDoc(result.url, id); + return { + type: 'text', + text: ` + ${result.url} + ${result.snippet} + `, + }; + }); + return data; + } catch (error) { + console.log(error); + return [{ type: 'text', text: 'An error occurred while performing the web search.' }]; + } + } +} diff --git a/src/client/views/nodes/ChatBox/tools/WebsiteInfoScraperTool.ts b/src/client/views/nodes/ChatBox/tools/WebsiteInfoScraperTool.ts new file mode 100644 index 000000000..59fd47b7a --- /dev/null +++ b/src/client/views/nodes/ChatBox/tools/WebsiteInfoScraperTool.ts @@ -0,0 +1,35 @@ +import { Networking } from '../../../../Network'; +import { BaseTool } from './BaseTool'; +import { v4 as uuidv4 } from 'uuid'; + +export class WebsiteInfoScraperTool extends BaseTool<{ url: string }> { + private _addLinkedUrlDoc: (url: string, id: string) => void; + + constructor(addLinkedUrlDoc: (url: string, id: string) => void) { + super( + 'websiteInfoScraper', + 'Scrape detailed information from a specific website identified as the most relevant', + { + url: { + type: 'string', + description: 'The URL of the website to scrape', + required: true, + }, + }, + 'Provide the URL of the website that you have identified as the most relevant from the previous search. This tool will scrape and process detailed information from that specific website. It will also create a document from the scraped content for future reference.', + 'Returns the full HTML content from the provided URL and creates a document from the content for further analysis.' + ); + this._addLinkedUrlDoc = addLinkedUrlDoc; + } + + async execute(args: { url: string }): Promise { + try { + const { html } = await Networking.PostToServer('/scrapeWebsite', { url: args.url }); + const id = uuidv4(); + this._addLinkedUrlDoc(args.url, id); + return [{ type: 'text', text: ` ${html} ` }]; + } catch (error) { + return [{ type: 'text', text: 'An error occurred while scraping the website.' }]; + } + } +} diff --git a/src/client/views/nodes/ChatBox/tools/WikipediaTool.ts b/src/client/views/nodes/ChatBox/tools/WikipediaTool.ts index e2c5009a1..143d91d80 100644 --- a/src/client/views/nodes/ChatBox/tools/WikipediaTool.ts +++ b/src/client/views/nodes/ChatBox/tools/WikipediaTool.ts @@ -2,9 +2,11 @@ import { title } from 'process'; import { Networking } from '../../../../Network'; import { BaseTool } from './BaseTool'; import axios from 'axios'; +import { v4 as uuidv4 } from 'uuid'; export class WikipediaTool extends BaseTool<{ title: string }> { - constructor() { + private _addLinkedUrlDoc: (url: string, id: string) => void; + constructor(addLinkedUrlDoc: (url: string, id: string) => void) { super( 'wikipedia', 'Search Wikipedia and return a summary', @@ -18,12 +20,16 @@ export class WikipediaTool extends BaseTool<{ title: string }> { 'Provide simply the title you want to search on Wikipedia and nothing more. If re-using this tool, try a different title for different information.', 'Returns a summary from searching an article title on Wikipedia' ); + this._addLinkedUrlDoc = addLinkedUrlDoc; } async execute(args: { title: string }): Promise { try { const { text } = await Networking.PostToServer('/getWikipediaSummary', { title: args.title }); - return [{ type: 'text', text: text }]; + const id = uuidv4(); + const url = `https://en.wikipedia.org/wiki/${args.title.replace(/ /g, '_')}`; + this._addLinkedUrlDoc(url, id); + return [{ type: 'text', text: ` ${text} ` }]; } catch (error) { return [{ type: 'text', text: 'An error occurred while fetching the article.' }]; } -- cgit v1.2.3-70-g09d2