Skip to content

Commit 43e82d4

Browse files
committed
feat: init structure for llamaindex tools
1 parent 484a710 commit 43e82d4

File tree

12 files changed

+356
-23
lines changed

12 files changed

+356
-23
lines changed

examples/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@
1818
"module": "commonjs"
1919
}
2020
},
21-
"include": ["./**/*.ts"]
21+
"include": ["./**/*.ts", "assembly-tools/add.mjs"]
2222
}

packages/tools/asconfig.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"targets": {
3+
"debug": {
4+
"outFile": "build/debug.wasm",
5+
"textFile": "build/debug.wat",
6+
"sourceMap": true,
7+
"debug": true
8+
},
9+
"release": {
10+
"outFile": "build/release.wasm",
11+
"textFile": "build/release.wat",
12+
"sourceMap": true,
13+
"optimizeLevel": 3,
14+
"shrinkLevel": 0,
15+
"converge": false,
16+
"noAssert": false
17+
}
18+
},
19+
"options": {
20+
"bindings": "esm"
21+
}
22+
}

packages/tools/assembly/base.ts

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
export class ToolParameterProperty {
2+
type: string;
3+
description: string | null;
4+
5+
constructor(type: string, description: string | null) {
6+
this.type = type;
7+
this.description = description;
8+
}
9+
}
10+
11+
export class ToolParameters {
12+
type: string;
13+
properties: Map<string, ToolParameterProperty>;
14+
required: string[] | null;
15+
16+
constructor(
17+
type: string,
18+
properties: Map<string, ToolParameterProperty>,
19+
required: string[] | null,
20+
) {
21+
this.type = type;
22+
this.properties = properties;
23+
this.required = required;
24+
}
25+
}
26+
27+
export class ToolMetadata {
28+
name: string;
29+
description: string;
30+
parameters: ToolParameters | null;
31+
argsKwargs: Map<string, Object> | null;
32+
33+
constructor(
34+
name: string,
35+
description: string,
36+
parameters: ToolParameters | null,
37+
argsKwargs: Map<string, Object> | null,
38+
) {
39+
this.name = name;
40+
this.description = description;
41+
this.parameters = parameters;
42+
this.argsKwargs = argsKwargs;
43+
}
44+
}
45+
46+
export class BaseTool {
47+
call(_args: Object): Object {
48+
return {};
49+
}
50+
metadata: ToolMetadata | null = null;
51+
52+
constructor(metadata: ToolMetadata) {
53+
this.metadata = metadata;
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {
2+
BaseTool,
3+
ToolMetadata,
4+
ToolParameterProperty,
5+
ToolParameters,
6+
} from "../base";
7+
8+
function getDefaultMetadata(): ToolMetadata {
9+
const properties = new Map<string, ToolParameterProperty>();
10+
const paramProperty = new ToolParameterProperty(
11+
"string",
12+
"The query to search for",
13+
);
14+
properties.set("query", paramProperty);
15+
return new ToolMetadata(
16+
"query_engine_tool",
17+
"Useful for running a natural language query against a knowledge base and get back a natural language response.",
18+
new ToolParameters("object", properties, ["query"]),
19+
null,
20+
);
21+
}
22+
23+
class QueryEngineTool extends BaseTool {
24+
call(_args: Object): string {
25+
// TODO: Implement this tool later
26+
return "QueryEngineTool.call";
27+
}
28+
29+
constructor() {
30+
super(getDefaultMetadata());
31+
}
32+
}
33+
34+
export function getInstance(): QueryEngineTool {
35+
return new QueryEngineTool();
36+
}

packages/tools/assembly/tsconfig.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "assemblyscript/std/assembly.json",
3+
"include": [
4+
"./**/*.ts"
5+
]
6+
}
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {
2+
BaseTool,
3+
ToolMetadata,
4+
ToolParameterProperty,
5+
ToolParameters,
6+
} from "../base";
7+
8+
function getDefaultMetadata(): ToolMetadata {
9+
const properties = new Map<string, ToolParameterProperty>();
10+
const paramProperty = new ToolParameterProperty(
11+
"string",
12+
"The query to search for",
13+
);
14+
properties.set("query", paramProperty);
15+
return new ToolMetadata(
16+
"wiki_tool",
17+
"Summary of the Wikipedia page for the given query.",
18+
new ToolParameters("object", properties, ["query"]),
19+
null,
20+
);
21+
}
22+
23+
class WikiTool extends BaseTool {
24+
call(_args: Object): string {
25+
// TODO: Implement this tool later
26+
return "WikiTool.call";
27+
}
28+
29+
constructor() {
30+
super(getDefaultMetadata());
31+
}
32+
}
33+
34+
export function getInstance(): WikiTool {
35+
return new WikiTool();
36+
}

packages/tools/download.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { execSync } from "child_process";
2+
3+
// npm run download add
4+
const args = process.argv.slice(2);
5+
const tool = args[0];
6+
7+
if (!tool) {
8+
console.error("Error: Please provide a module name.");
9+
process.exit(1);
10+
}
11+
12+
try {
13+
execSync(
14+
`asc assembly/tools/${tool}/index.ts -o build/${tool}.D -t build/${tool}.wat --target release`,
15+
);
16+
console.log(`Module ${tool} downloaded successfully.`);
17+
} catch (error) {
18+
console.error(`Error downloading module ${tool}:`, error.message);
19+
process.exit(1);
20+
}

packages/tools/index.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<script type="module">
5+
import { add } from "./build/release.js";
6+
document.body.innerText = add(1, 2);
7+
</script>
8+
</head>
9+
<body></body>
10+
</html>

packages/tools/index.ts

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { execSync } from "child_process";
2+
import fs from "fs";
3+
import { BaseTool } from "./assembly/base";
4+
5+
type ToolInfo = {
6+
id: string;
7+
name: string;
8+
};
9+
10+
export interface IToolFactory {
11+
getToolList(): ToolInfo[];
12+
downloadTool(toolId: string, storageDir?: string): void;
13+
loadTool(filePath: string): Promise<BaseTool>;
14+
}
15+
16+
export default class ToolFactory implements IToolFactory {
17+
getToolList(): ToolInfo[] {
18+
return [
19+
{
20+
id: "query-engine-tool",
21+
name: "Query Engine Tool",
22+
},
23+
{
24+
id: "wiki-tool",
25+
name: "Wikipedia Summary",
26+
},
27+
];
28+
}
29+
30+
async downloadTool(toolId: string, storageDir: string = "build") {
31+
try {
32+
execSync(
33+
`asc assembly/tools/${toolId}/index.ts -o ${storageDir}/${toolId}.wasm -t ${storageDir}/${toolId}.wat --target release`,
34+
);
35+
console.log(`Module ${toolId} downloaded successfully.`);
36+
} catch (error) {
37+
console.error(`Error downloading module ${toolId}:`, error.message);
38+
}
39+
}
40+
41+
async loadTool(filePath: string): Promise<BaseTool> {
42+
const mod = new WebAssembly.Module(fs.readFileSync(filePath));
43+
const ins = new WebAssembly.Instance(mod);
44+
return ins.exports as unknown as BaseTool;
45+
}
46+
}

packages/tools/package.json

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "@llamaindex/tools",
3+
"version": "0.0.1",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "node tests",
8+
"asbuild:debug": "asc assembly/index.ts --target debug",
9+
"asbuild:release": "asc assembly/index.ts --target release",
10+
"asbuild": "npm run asbuild:debug && npm run asbuild:release",
11+
"start": "npx serve ."
12+
},
13+
"author": "",
14+
"license": "ISC",
15+
"devDependencies": {
16+
"@types/node": "^20.11.25",
17+
"assemblyscript": "^0.27.24"
18+
},
19+
"dependencies": {
20+
"as-bind": "^0.8.2"
21+
},
22+
"type": "module",
23+
"repository": {
24+
"type": "git",
25+
"url": "https://github.com/run-llama/LlamaIndexTS.git",
26+
"directory": "packages/tools"
27+
},
28+
"exports": {
29+
".": {
30+
"import": "./build/release.js",
31+
"types": "./build/release.d.ts"
32+
}
33+
}
34+
}

packages/tools/tests/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { getInstance } from "../build/debug.js";
2+
3+
const instance = getInstance();
4+
console.log(instance);

0 commit comments

Comments
 (0)