Skip to content

Commit

Permalink
feat: add tool factory (run-llama#663)
Browse files Browse the repository at this point in the history
  • Loading branch information
thucpn authored Mar 22, 2024
1 parent 48e2878 commit fececd8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/lemon-windows-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"llamaindex": patch
---

feat: add tool factory
33 changes: 33 additions & 0 deletions packages/core/src/tools/ToolFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type { BaseTool } from "../types.js";
import { WikipediaTool } from "./WikipediaTool.js";

enum Tools {
Wikipedia = "wikipedia.WikipediaToolSpec",
}

type ToolConfig = { [key in Tools]: Record<string, any> };

export class ToolFactory {
private static async createTool(
key: Tools,
options: Record<string, any>,
): Promise<BaseTool> {
if (key === Tools.Wikipedia) {
const tool = new WikipediaTool();
return tool;
}

throw new Error(
`Sorry! Tool ${key} is not supported yet. Options: ${options}`,
);
}

public static async createTools(config: ToolConfig): Promise<BaseTool[]> {
const tools: BaseTool[] = [];
for (const [key, value] of Object.entries(config as ToolConfig)) {
const tool = await ToolFactory.createTool(key as Tools, value);
tools.push(tool);
}
return tools;
}
}
1 change: 1 addition & 0 deletions packages/core/src/tools/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./QueryEngineTool.js";
export * from "./ToolFactory.js";
export * from "./WikipediaTool.js";
export * from "./functionTool.js";
export * from "./types.js";
Expand Down

0 comments on commit fececd8

Please sign in to comment.