forked from run-llama/LlamaIndexTS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add tool factory (run-llama#663)
- Loading branch information
Showing
3 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"llamaindex": patch | ||
--- | ||
|
||
feat: add tool factory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters