diff --git a/helpers/typescript.ts b/helpers/typescript.ts index ea1499bf..9678661b 100644 --- a/helpers/typescript.ts +++ b/helpers/typescript.ts @@ -166,7 +166,17 @@ export const installTSTemplate = async ({ cwd: path.join(compPath, "engines", "typescript", "agent"), }); - // TODO: action to install tools + // Write tools_config.json + const configContent: Record = {}; + tools.forEach((tool) => { + configContent[tool.name] = tool.config ?? {}; + }); + const configFilePath = path.join(enginePath, "tools_config.json"); + console.log(configFilePath) + await fs.writeFile( + configFilePath, + JSON.stringify(configContent, null, 2), + ); } else { await copy("**", enginePath, { parents: true, diff --git a/templates/components/engines/typescript/agent/chat.ts b/templates/components/engines/typescript/agent/chat.ts index 2d727e07..c0ed6472 100644 --- a/templates/components/engines/typescript/agent/chat.ts +++ b/templates/components/engines/typescript/agent/chat.ts @@ -5,6 +5,7 @@ import { } from "llamaindex"; import { getDataSource } from "./index"; import { STORAGE_CACHE_DIR } from "./constants.mjs"; +import ToolFactory from "./tools"; export async function createChatEngine(llm: OpenAI) { const index = await getDataSource(llm); @@ -17,8 +18,10 @@ export async function createChatEngine(llm: OpenAI) { }, }); + const externalTools = await ToolFactory.list(); + const agent = new OpenAIAgent({ - tools: [queryEngineTool], + tools: [queryEngineTool, ...externalTools], verbose: true, llm, }); diff --git a/templates/components/engines/typescript/agent/tools.ts b/templates/components/engines/typescript/agent/tools.ts index bf001e7e..de28b108 100644 --- a/templates/components/engines/typescript/agent/tools.ts +++ b/templates/components/engines/typescript/agent/tools.ts @@ -1 +1,35 @@ -// Load tools here \ No newline at end of file +import { BaseTool } from "llamaindex"; +import config from "./tool_config.json"; + +enum ExternalTool { + GoogleSearch = "google.GoogleSearchToolSpec", + Wikipedia = "wikipedia.WikipediaToolSpec", +} + +type ToolConfig = { [key in ExternalTool]: Record }; + +export default class ToolFactory { + private static async createTool( + key: ExternalTool, + options: Record + ): Promise { + if (key === ExternalTool.Wikipedia) { + const WikipediaTool = (await import("llamaindex")).WikipediaTool; + const tool = new WikipediaTool(); + return tool; + } + + // TODO: Implement GoogleSearchToolSpec + + throw new Error(`Sorry! Tool ${key} is not supported yet. Options: ${options}`); + } + + public static async list(): Promise { + const tools: BaseTool[] = []; + for (const [key, value] of Object.entries(config as ToolConfig)) { + const tool = await ToolFactory.createTool(key as ExternalTool, value); + tools.push(tool); + } + return tools; + } +}