diff options
| author | bobzel <zzzman@gmail.com> | 2024-11-19 10:36:59 -0500 |
|---|---|---|
| committer | bobzel <zzzman@gmail.com> | 2024-11-19 10:36:59 -0500 |
| commit | 7b38bbc4d845fa524e8310a0ec05b0e776b47c82 (patch) | |
| tree | 958609fc079523803345a74e33b16c164c226fd8 /src/client/views/nodes/chatbot/tools | |
| parent | 196b92cb84095780d2b36244831cac03e9b66d8e (diff) | |
| parent | 9e447814b551c352709296ae562f1f50480320f5 (diff) | |
Merge remote-tracking branch 'origin/ajs-finalagent'
Diffstat (limited to 'src/client/views/nodes/chatbot/tools')
13 files changed, 233 insertions, 92 deletions
diff --git a/src/client/views/nodes/chatbot/tools/BaseTool.ts b/src/client/views/nodes/chatbot/tools/BaseTool.ts index 58cd514d9..8efba2d28 100644 --- a/src/client/views/nodes/chatbot/tools/BaseTool.ts +++ b/src/client/views/nodes/chatbot/tools/BaseTool.ts @@ -1,5 +1,5 @@ import { Observation } from '../types/types'; -import { Parameter, Tool, ParametersType } from './ToolTypes'; +import { Parameter, ParametersType } from '../types/tool_types'; /** * @file BaseTool.ts @@ -14,7 +14,7 @@ import { Parameter, Tool, ParametersType } from './ToolTypes'; * It is generic over a type parameter `P`, which extends `ReadonlyArray<Parameter>`. * This means `P` is a readonly array of `Parameter` objects that cannot be modified (immutable). */ -export abstract class BaseTool<P extends ReadonlyArray<Parameter>> implements Tool<P> { +export abstract class BaseTool<P extends ReadonlyArray<Parameter>> { // The name of the tool (e.g., "calculate", "searchTool") name: string; // A description of the tool's functionality @@ -59,6 +59,7 @@ export abstract class BaseTool<P extends ReadonlyArray<Parameter>> implements To return { tool: this.name, description: this.description, + citationRules: this.citationRules, parameters: this.parameterRules.reduce( (acc, param) => { // Build an object for each parameter without the 'name' property, since it's used as the key diff --git a/src/client/views/nodes/chatbot/tools/CalculateTool.ts b/src/client/views/nodes/chatbot/tools/CalculateTool.ts index e96c9a98a..139ede8f0 100644 --- a/src/client/views/nodes/chatbot/tools/CalculateTool.ts +++ b/src/client/views/nodes/chatbot/tools/CalculateTool.ts @@ -1,5 +1,5 @@ import { Observation } from '../types/types'; -import { ParametersType } from './ToolTypes'; +import { ParametersType } from '../types/tool_types'; import { BaseTool } from './BaseTool'; const calculateToolParams = [ diff --git a/src/client/views/nodes/chatbot/tools/CreateAnyDocTool.ts b/src/client/views/nodes/chatbot/tools/CreateAnyDocTool.ts new file mode 100644 index 000000000..6f61b77d4 --- /dev/null +++ b/src/client/views/nodes/chatbot/tools/CreateAnyDocTool.ts @@ -0,0 +1,153 @@ +import { v4 as uuidv4 } from 'uuid'; +import { BaseTool } from './BaseTool'; +import { Observation } from '../types/types'; +import { ParametersType, Parameter } from '../types/tool_types'; +import { DocumentOptions, Docs } from '../../../../documents/Documents'; + +/** + * List of supported document types that can be created via text LLM. + */ +type supportedDocumentTypesType = 'text' | 'html' | 'equation' | 'functionPlot' | 'dataviz' | 'noteTaking' | 'rtf' | 'message'; +const supportedDocumentTypes: supportedDocumentTypesType[] = ['text', 'html', 'equation', 'functionPlot', 'dataviz', 'noteTaking', 'rtf', 'message']; + +/** + * Description of document options and data field for each type. + */ +const documentTypesInfo = { + text: { + options: ['title', 'backgroundColor', 'fontColor', 'text_align', 'layout'], + dataDescription: 'The text content of the document.', + }, + html: { + options: ['title', 'backgroundColor', 'layout'], + dataDescription: 'The HTML-formatted text content of the document.', + }, + equation: { + options: ['title', 'backgroundColor', 'fontColor', 'layout'], + dataDescription: 'The equation content as a string.', + }, + functionPlot: { + options: ['title', 'backgroundColor', 'layout', 'function_definition'], + dataDescription: 'The function definition(s) for plotting. Provide as a string or array of function definitions.', + }, + dataviz: { + options: ['title', 'backgroundColor', 'layout', 'chartType'], + dataDescription: 'A string of comma-separated values representing the CSV data.', + }, + noteTaking: { + options: ['title', 'backgroundColor', 'layout'], + dataDescription: 'The initial content or structure for note-taking.', + }, + rtf: { + options: ['title', 'backgroundColor', 'layout'], + dataDescription: 'The rich text content in RTF format.', + }, + message: { + options: ['title', 'backgroundColor', 'layout'], + dataDescription: 'The message content of the document.', + }, +}; + +const createAnyDocumentToolParams = [ + { + name: 'document_type', + type: 'string', + description: `The type of the document to create. Supported types are: ${supportedDocumentTypes.join(', ')}`, + required: true, + }, + { + name: 'data', + type: 'string', + description: 'The content or data of the document. The exact format depends on the document type.', + required: true, + }, + { + name: 'options', + type: 'string', + description: `A JSON string representing the document options. Available options depend on the document type. For example: +${supportedDocumentTypes + .map( + docType => ` +- For '${docType}' documents, options include: ${documentTypesInfo[docType].options.join(', ')}` + ) + .join('\n')}`, + required: false, + }, +] as const; + +type CreateAnyDocumentToolParamsType = typeof createAnyDocumentToolParams; + +export class CreateAnyDocumentTool extends BaseTool<CreateAnyDocumentToolParamsType> { + private _addLinkedDoc: (doc_type: string, data: string | undefined, options: DocumentOptions, id: string) => void; + + constructor(addLinkedDoc: (doc_type: string, data: string | undefined, options: DocumentOptions, id: string) => void) { + super( + 'createAnyDocument', + `Creates any type of document with the provided options and data. Supported document types are: ${supportedDocumentTypes.join(', ')}. dataviz is a csv table tool, so for CSVs, use dataviz. Here are the options for each type: + <supported_document_types> + ${supportedDocumentTypes + .map( + docType => ` + <document_type name="${docType}"> + <data_description>${documentTypesInfo[docType].dataDescription}</data_description> + <options> + ${documentTypesInfo[docType].options.map(option => `<option>${option}</option>`).join('\n')} + </options> + </document_type> + ` + ) + .join('\n')} + </supported_document_types>`, + createAnyDocumentToolParams, + 'Provide the document type, data, and options for the document. Options should be a valid JSON string containing the document options specific to the document type.', + `Creates any type of document with the provided options and data. Supported document types are: ${supportedDocumentTypes.join(', ')}.` + ); + this._addLinkedDoc = addLinkedDoc; + } + + async execute(args: ParametersType<CreateAnyDocumentToolParamsType>): Promise<Observation[]> { + try { + const documentType: supportedDocumentTypesType = args.document_type.toLowerCase() as supportedDocumentTypesType; + let options: DocumentOptions = {}; + + if (!supportedDocumentTypes.includes(documentType)) { + throw new Error(`Unsupported document type: ${documentType}. Supported types are: ${supportedDocumentTypes.join(', ')}.`); + } + + if (!args.data) { + throw new Error(`Data is required for ${documentType} documents. ${documentTypesInfo[documentType].dataDescription}`); + } + + if (args.options) { + try { + options = JSON.parse(args.options as string) as DocumentOptions; + } catch (e) { + throw new Error('Options must be a valid JSON string.'); + } + } + + const data = args.data as string; + const id = uuidv4(); + + // Set default options if not provided + options.title = options.title || `New ${documentType.charAt(0).toUpperCase() + documentType.slice(1)} Document`; + + // Call the function to add the linked document + this._addLinkedDoc(documentType, data, options, id); + + return [ + { + type: 'text', + text: `Created ${documentType} document with ID ${id}.`, + }, + ]; + } catch (error) { + return [ + { + type: 'text', + text: 'Error creating document: ' + (error as Error).message, + }, + ]; + } + } +} diff --git a/src/client/views/nodes/chatbot/tools/CreateCSVTool.ts b/src/client/views/nodes/chatbot/tools/CreateCSVTool.ts index b321d98ba..2cc513d6c 100644 --- a/src/client/views/nodes/chatbot/tools/CreateCSVTool.ts +++ b/src/client/views/nodes/chatbot/tools/CreateCSVTool.ts @@ -1,7 +1,7 @@ import { BaseTool } from './BaseTool'; import { Networking } from '../../../../Network'; import { Observation } from '../types/types'; -import { ParametersType } from './ToolTypes'; +import { ParametersType } from '../types/tool_types'; const createCSVToolParams = [ { diff --git a/src/client/views/nodes/chatbot/tools/CreateTextDocumentTool.ts b/src/client/views/nodes/chatbot/tools/CreateTextDocumentTool.ts new file mode 100644 index 000000000..fae78aa49 --- /dev/null +++ b/src/client/views/nodes/chatbot/tools/CreateTextDocumentTool.ts @@ -0,0 +1,61 @@ +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'; +import { DocumentOptions } from '../../../../documents/Documents'; +import { RTFCast, StrCast } from '../../../../../fields/Types'; + +const createTextDocToolParams = [ + { + name: 'text_content', + type: 'string', + description: 'The text content that the document will display', + required: true, + }, + { + name: 'title', + type: 'string', + description: 'The title of the document', + required: true, + }, + { + name: 'background_color', + type: 'string', + description: 'The background color of the document as a hex string', + required: false, + }, + { + name: 'font_color', + type: 'string', + description: 'The font color of the document as a hex string', + required: false, + }, +] as const; + +type CreateTextDocToolParamsType = typeof createTextDocToolParams; + +export class CreateTextDocTool extends BaseTool<CreateTextDocToolParamsType> { + private _addLinkedDoc: (doc_type: string, data: string, options: DocumentOptions, id: string) => void; + + constructor(addLinkedDoc: (text_content: string, data: string, options: DocumentOptions, id: string) => void) { + super( + 'createTextDoc', + 'Creates a text document with the provided content and title (and of specified other options if wanted)', + createTextDocToolParams, + 'Provide the text content and title (and optionally color) for the document.', + 'Creates a text document with the provided content and title (and of specified other options if wanted). Use if the user wants to create a textbox or text document of some sort. Can use after a search or other tool to save information.' + ); + this._addLinkedDoc = addLinkedDoc; + } + + async execute(args: ParametersType<CreateTextDocToolParamsType>): Promise<Observation[]> { + try { + console.log(RTFCast(args.text_content)); + this._addLinkedDoc('text', args.text_content, { title: args.title, backgroundColor: args.background_color, text_fontColor: args.font_color }, uuidv4()); + return [{ type: 'text', text: 'Created text document.' }]; + } catch (error) { + return [{ type: 'text', text: 'Error creating text document, ' + error }]; + } + } +} diff --git a/src/client/views/nodes/chatbot/tools/DataAnalysisTool.ts b/src/client/views/nodes/chatbot/tools/DataAnalysisTool.ts index d9b75219d..97b9ee023 100644 --- a/src/client/views/nodes/chatbot/tools/DataAnalysisTool.ts +++ b/src/client/views/nodes/chatbot/tools/DataAnalysisTool.ts @@ -1,5 +1,5 @@ import { Observation } from '../types/types'; -import { ParametersType } from './ToolTypes'; +import { ParametersType } from '../types/tool_types'; import { BaseTool } from './BaseTool'; const dataAnalysisToolParams = [ diff --git a/src/client/views/nodes/chatbot/tools/GetDocsTool.ts b/src/client/views/nodes/chatbot/tools/GetDocsTool.ts index 26756522c..4286e7ffe 100644 --- a/src/client/views/nodes/chatbot/tools/GetDocsTool.ts +++ b/src/client/views/nodes/chatbot/tools/GetDocsTool.ts @@ -1,5 +1,5 @@ import { Observation } from '../types/types'; -import { ParametersType } from './ToolTypes'; +import { ParametersType } from '../types/tool_types'; import { BaseTool } from './BaseTool'; import { DocServer } from '../../../../DocServer'; import { Docs } from '../../../../documents/Documents'; diff --git a/src/client/views/nodes/chatbot/tools/NoTool.ts b/src/client/views/nodes/chatbot/tools/NoTool.ts index a92e3fa23..5d652fd8d 100644 --- a/src/client/views/nodes/chatbot/tools/NoTool.ts +++ b/src/client/views/nodes/chatbot/tools/NoTool.ts @@ -1,6 +1,6 @@ import { BaseTool } from './BaseTool'; import { Observation } from '../types/types'; -import { ParametersType } from './ToolTypes'; +import { ParametersType } from '../types/tool_types'; const noToolParams = [] as const; diff --git a/src/client/views/nodes/chatbot/tools/RAGTool.ts b/src/client/views/nodes/chatbot/tools/RAGTool.ts index 482069f36..fcd93a07a 100644 --- a/src/client/views/nodes/chatbot/tools/RAGTool.ts +++ b/src/client/views/nodes/chatbot/tools/RAGTool.ts @@ -1,6 +1,6 @@ import { Networking } from '../../../../Network'; import { Observation, RAGChunk } from '../types/types'; -import { ParametersType } from './ToolTypes'; +import { ParametersType } from '../types/tool_types'; import { Vectorstore } from '../vectorstore/Vectorstore'; import { BaseTool } from './BaseTool'; diff --git a/src/client/views/nodes/chatbot/tools/SearchTool.ts b/src/client/views/nodes/chatbot/tools/SearchTool.ts index fd5144dd6..d22f4c189 100644 --- a/src/client/views/nodes/chatbot/tools/SearchTool.ts +++ b/src/client/views/nodes/chatbot/tools/SearchTool.ts @@ -2,11 +2,11 @@ import { v4 as uuidv4 } from 'uuid'; import { Networking } from '../../../../Network'; import { BaseTool } from './BaseTool'; import { Observation } from '../types/types'; -import { ParametersType } from './ToolTypes'; +import { ParametersType } from '../types/tool_types'; const searchToolParams = [ { - name: 'query', + name: 'queries', type: 'string[]', description: 'The search query or queries to use for finding websites', required: true, @@ -20,7 +20,7 @@ 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) { + 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', @@ -33,8 +33,9 @@ export class SearchTool extends BaseTool<SearchToolParamsType> { } async execute(args: ParametersType<SearchToolParamsType>): Promise<Observation[]> { - const queries = args.query; + 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 { @@ -44,9 +45,10 @@ export class SearchTool extends BaseTool<SearchToolParamsType> { }); 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="text"><url>${result.url}</url><overview>${result.snippet}</overview></chunk>`, + text: `<chunk chunk_id="${id}" chunk_type="url"><url>${result.url}</url><overview>${result.snippet}</overview></chunk>`, }; }); return data; diff --git a/src/client/views/nodes/chatbot/tools/ToolTypes.ts b/src/client/views/nodes/chatbot/tools/ToolTypes.ts deleted file mode 100644 index d47a38952..000000000 --- a/src/client/views/nodes/chatbot/tools/ToolTypes.ts +++ /dev/null @@ -1,76 +0,0 @@ -import { Observation } from '../types/types'; - -/** - * The `Tool` interface represents a generic tool in the system. - * It is generic over a type parameter `P`, which extends `ReadonlyArray<Parameter>`. - * @template P - An array of `Parameter` objects defining the tool's parameters. - */ -export interface Tool<P extends ReadonlyArray<Parameter>> { - // The name of the tool (e.g., "calculate", "searchTool") - name: string; - // A description of the tool's functionality - description: string; - // An array of parameter definitions for the tool - parameterRules: P; - // Guidelines for how to handle citations when using the tool - citationRules: string; - // A brief summary of the tool's purpose - briefSummary: string; - /** - * Executes the tool's main functionality. - * @param args - The arguments for execution, with types inferred from `ParametersType<P>`. - * @returns A promise that resolves to an array of `Observation` objects. - */ - execute: (args: ParametersType<P>) => Promise<Observation[]>; - /** - * Generates an action rule object that describes the tool's usage. - * @returns An object representing the tool's action rules. - */ - getActionRule: () => Record<string, unknown>; -} - -/** - * The `Parameter` type defines the structure of a parameter configuration. - */ -export type Parameter = { - // The type of the parameter; constrained to the types 'string', 'number', 'boolean', 'string[]', 'number[]' - readonly type: 'string' | 'number' | 'boolean' | 'string[]' | 'number[]'; - // The name of the parameter - readonly name: string; - // A description of the parameter - readonly description: string; - // Indicates whether the parameter is required - readonly required: boolean; - // (Optional) The maximum number of inputs (useful for array types) - readonly max_inputs?: number; -}; - -/** - * A utility type that maps string representations of types to actual TypeScript types. - * This is used to convert the `type` field of a `Parameter` into a concrete TypeScript type. - */ -type TypeMap = { - string: string; - number: number; - boolean: boolean; - 'string[]': string[]; - 'number[]': number[]; -}; - -/** - * The `ParamType` type maps a `Parameter`'s `type` field to the corresponding TypeScript type. - * If the `type` field matches a key in `TypeMap`, it returns the associated type. - * Otherwise, it returns `unknown`. - * @template P - A `Parameter` object. - */ -export type ParamType<P extends Parameter> = P['type'] extends keyof TypeMap ? TypeMap[P['type']] : unknown; - -/** - * The `ParametersType` type transforms an array of `Parameter` objects into an object type - * where each key is the parameter's name, and the value is the corresponding TypeScript type. - * This is used to define the types of the arguments passed to the `execute` method of a tool. - * @template P - An array of `Parameter` objects. - */ -export type ParametersType<P extends ReadonlyArray<Parameter>> = { - [K in P[number] as K['name']]: ParamType<K>; -}; diff --git a/src/client/views/nodes/chatbot/tools/WebsiteInfoScraperTool.ts b/src/client/views/nodes/chatbot/tools/WebsiteInfoScraperTool.ts index f2e3863a6..ce659e344 100644 --- a/src/client/views/nodes/chatbot/tools/WebsiteInfoScraperTool.ts +++ b/src/client/views/nodes/chatbot/tools/WebsiteInfoScraperTool.ts @@ -2,7 +2,7 @@ import { v4 as uuidv4 } from 'uuid'; import { Networking } from '../../../../Network'; import { BaseTool } from './BaseTool'; import { Observation } from '../types/types'; -import { ParametersType } from './ToolTypes'; +import { ParametersType } from '../types/tool_types'; const websiteInfoScraperToolParams = [ { diff --git a/src/client/views/nodes/chatbot/tools/WikipediaTool.ts b/src/client/views/nodes/chatbot/tools/WikipediaTool.ts index 4fcffe2ed..f2dbf3cfd 100644 --- a/src/client/views/nodes/chatbot/tools/WikipediaTool.ts +++ b/src/client/views/nodes/chatbot/tools/WikipediaTool.ts @@ -2,7 +2,7 @@ import { v4 as uuidv4 } from 'uuid'; import { Networking } from '../../../../Network'; import { BaseTool } from './BaseTool'; import { Observation } from '../types/types'; -import { ParametersType } from './ToolTypes'; +import { ParametersType } from '../types/tool_types'; const wikipediaToolParams = [ { @@ -38,7 +38,7 @@ export class WikipediaTool extends BaseTool<WikipediaToolParamsType> { return [ { type: 'text', - text: `<chunk chunk_id="${id}" chunk_type="text"> ${text} </chunk>`, + text: `<chunk chunk_id="${id}" chunk_type="url"> ${text} </chunk>`, }, ]; } catch (error) { |
