Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: add tools module #621

Merged
merged 23 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/tools/asconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"targets": {
"debug": {
"outFile": "build/debug.wasm",
"textFile": "build/debug.wat",
"sourceMap": true,
"debug": true
},
"release": {
"outFile": "build/release.wasm",
"textFile": "build/release.wat",
"sourceMap": true,
"optimizeLevel": 3,
"shrinkLevel": 0,
"converge": false,
"noAssert": false
}
},
"options": {
"bindings": "esm"
}
}
55 changes: 55 additions & 0 deletions packages/tools/assembly/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
export class ToolParameterProperty {
type: string;
description: string | null;

constructor(type: string, description: string | null) {
this.type = type;
this.description = description;
}
}

export class ToolParameters {
type: string;
properties: Map<string, ToolParameterProperty>;
required: string[] | null;

constructor(
type: string,
properties: Map<string, ToolParameterProperty>,
required: string[] | null,
) {
this.type = type;
this.properties = properties;
this.required = required;
}
}

export class ToolMetadata {
name: string;
description: string;
parameters: ToolParameters | null;
argsKwargs: Map<string, Object> | null;

constructor(
name: string,
description: string,
parameters: ToolParameters | null,
argsKwargs: Map<string, Object> | null,
) {
this.name = name;
this.description = description;
this.parameters = parameters;
this.argsKwargs = argsKwargs;
}
}

export class BaseTool {
call(_args: Object): Object {
return {};
}
metadata: ToolMetadata | null = null;

constructor(metadata: ToolMetadata) {
this.metadata = metadata;
}
}
36 changes: 36 additions & 0 deletions packages/tools/assembly/query-engine-tool/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {
marcusschiesser marked this conversation as resolved.
Show resolved Hide resolved
BaseTool,
ToolMetadata,
ToolParameterProperty,
ToolParameters,
} from "../base";

function getDefaultMetadata(): ToolMetadata {
const properties = new Map<string, ToolParameterProperty>();
const paramProperty = new ToolParameterProperty(
"string",
"The query to search for",
);
properties.set("query", paramProperty);
return new ToolMetadata(
"query_engine_tool",
"Useful for running a natural language query against a knowledge base and get back a natural language response.",
new ToolParameters("object", properties, ["query"]),
null,
);
}

class QueryEngineTool extends BaseTool {
call(_args: Object): string {
// TODO: Implement this tool later
return "QueryEngineTool.call";
}

constructor() {
super(getDefaultMetadata());
}
}

export function getInstance(): QueryEngineTool {
return new QueryEngineTool();
}
6 changes: 6 additions & 0 deletions packages/tools/assembly/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "assemblyscript/std/assembly.json",
"include": [
"./**/*.ts"
]
}
36 changes: 36 additions & 0 deletions packages/tools/assembly/wiki-tool/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {
BaseTool,
ToolMetadata,
ToolParameterProperty,
ToolParameters,
} from "../base";

function getDefaultMetadata(): ToolMetadata {
const properties = new Map<string, ToolParameterProperty>();
const paramProperty = new ToolParameterProperty(
"string",
"The query to search for",
);
properties.set("query", paramProperty);
return new ToolMetadata(
"wiki_tool",
"Summary of the Wikipedia page for the given query.",
new ToolParameters("object", properties, ["query"]),
null,
);
}

class WikiTool extends BaseTool {
call(_args: Object): string {
// TODO: Implement this tool later
return "WikiTool.call";
}

constructor() {
super(getDefaultMetadata());
}
}

export function getInstance(): WikiTool {
return new WikiTool();
}
52 changes: 52 additions & 0 deletions packages/tools/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import asc from "assemblyscript/bin/asc";
import fs from "fs";
import { BaseTool } from "./assembly/base";
marcusschiesser marked this conversation as resolved.
Show resolved Hide resolved

type ToolInfo = {
id: string;
name: string;
};

export interface IToolFactory {
getToolList(): ToolInfo[];
downloadTool(toolId: string, storageDir?: string): void;
loadTool(filePath: string): Promise<BaseTool>;
marcusschiesser marked this conversation as resolved.
Show resolved Hide resolved
}

export default class ToolFactory implements IToolFactory {
getToolList(): ToolInfo[] {
return [
{
id: "query-engine-tool",
name: "Query Engine Tool",
},
{
id: "wiki-tool",
name: "Wikipedia Summary",
},
];
}

async downloadTool(toolId: string, storageDir: string = "build") {
const { error, stdout, stderr } = await asc.main([
marcusschiesser marked this conversation as resolved.
Show resolved Hide resolved
`assembly/tools/${toolId}/index.ts`,
"--outFile",
`${storageDir}/${toolId}.wasm`,
"--optimize",
"--sourceMap",
"--stats",
]);
if (error) {
console.log("Download failed: " + error.message);
console.log(stderr.toString());
} else {
console.log(stdout.toString());
}
}

async loadTool(filePath: string): Promise<BaseTool> {
const mod = new WebAssembly.Module(fs.readFileSync(filePath));
const ins = new WebAssembly.Instance(mod);
return ins.exports as unknown as BaseTool;
}
}
33 changes: 33 additions & 0 deletions packages/tools/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "@llamaindex/tools",
"version": "0.0.1",
"description": "",
"main": "index.js",
"scripts": {
"test": "node tests",
"asbuild:debug": "asc assembly/index.ts --target debug",
"asbuild:release": "asc assembly/index.ts --target release",
"asbuild": "npm run asbuild:debug && npm run asbuild:release",
"start": "npx serve ."
},
"author": "",
"license": "ISC",
marcusschiesser marked this conversation as resolved.
Show resolved Hide resolved
"devDependencies": {
"assemblyscript": "^0.27.24"
},
"dependencies": {
"as-bind": "^0.8.2"
},
"type": "module",
"repository": {
"type": "git",
"url": "https://github.com/run-llama/LlamaIndexTS.git",
"directory": "packages/tools"
},
"exports": {
".": {
"import": "./build/release.js",
"types": "./build/release.d.ts"
}
}
}
4 changes: 4 additions & 0 deletions packages/tools/tests/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { getInstance } from "../build/debug.js";

const instance = getInstance();
console.log(instance);
59 changes: 57 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading