Skip to content

feat: build wasm with extism #1155

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

Open
wants to merge 31 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
ccfc11c
feat: build wasm with extism
thucpn Sep 6, 2024
02c8198
feat: detach packages for extism
thucpn Sep 6, 2024
ecc54cb
fix: lock
thucpn Sep 6, 2024
efbd61f
install extism-js
thucpn Sep 6, 2024
de4d8ff
install extism for all actions
thucpn Sep 6, 2024
fbb5ece
fix: install extism for all tests
thucpn Sep 6, 2024
5c34d3a
create abstract ExtismTool
thucpn Sep 6, 2024
9df0d63
feat: make agent example
thucpn Sep 6, 2024
dfb6c25
feat: add js example
thucpn Sep 6, 2024
4b74096
update readme
thucpn Sep 6, 2024
507012a
feat: ExtismToolFactory
thucpn Sep 9, 2024
54abd70
feat: example with agent
thucpn Sep 9, 2024
1eddb6e
export type
thucpn Sep 9, 2024
7710d96
add getMetadata to wiki python example
thucpn Sep 9, 2024
6cc8595
fix: typo and use same d.ts file for all tools
thucpn Sep 9, 2024
879c15f
fix: lint
thucpn Sep 9, 2024
d48724f
fix: update doc
thucpn Sep 9, 2024
f7b666e
refactor: add package.json to examples
thucpn Sep 9, 2024
8a73dac
test wiki
thucpn Sep 9, 2024
d95ed78
fix: lint depend on build
thucpn Sep 9, 2024
660eab8
fix: e2e
thucpn Sep 9, 2024
fda48fa
fix: wrong path ts config.json
thucpn Sep 9, 2024
f34ea45
Merge branch 'main' into feat/build-wasm-with-extism
thucpn Sep 11, 2024
d718f43
fix: upgrade extism to fix worker bug
thucpn Sep 12, 2024
191986b
refactor: create tool class params
thucpn Sep 12, 2024
f76abce
refactor: remove enum
thucpn Sep 12, 2024
92d0d09
add todo tool example
thucpn Sep 12, 2024
33d57d1
update test
thucpn Sep 12, 2024
3293c22
Merge branch 'main' into feat/build-wasm-with-extism
thucpn Sep 12, 2024
9447654
Create shy-bottles-shop.md
thucpn Sep 12, 2024
951f215
Merge branch 'main' into feat/build-wasm-with-extism
marcusschiesser Sep 16, 2024
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
32 changes: 32 additions & 0 deletions packages/wasm-tools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,35 @@ import { TestTool } from "@llamaindex/wasm-tools";
const testTool = new TestTool();
testTool.call("1"); // get post has id = 1 (url: https://jsonplaceholder.typicode.com/todos?id=1)
```

## Extism

### Prerequisites

- [Extism CLI](https://github.com/extism/cli?tab=readme-ov-file#installation)
- [Extism PDK](https://github.com/extism/js-pdk?tab=readme-ov-file#linux-macos)

### Build WASM files

```bash
cd packages/wasm-tools/extism/wiki
extism-js wiki.js -i wiki.d.ts -o wiki.wasm
```

### Test WASM files

```bash
extism call wiki.wasm wikiCall --wasi --allow-host "*.wikipedia.org" --input='{"query": "Ho Chi Minh City"}'
```

### Run WASM files in Node.js

```bash
node examples/wiki.js
```

### Run WASM files in Python

```bash
python examples/wiki.py
```
23 changes: 23 additions & 0 deletions packages/wasm-tools/examples/wiki.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import createPlugin from "@extism/extism";

async function main() {
const plugin = await createPlugin("../extism/wiki/wiki.wasm", {
useWasi: true,
functions: {},
runInWorker: true,
allowedHosts: ["*.wikipedia.org"],
memory: { maxHttpResponseBytes: 100 * 1024 * 1024 },
});

try {
const params = { query: "Ho Chi Minh City" };
const data = await plugin.call("wikiCall", JSON.stringify(params));
console.log(data.json());
} catch (e) {
console.error(e);
} finally {
await plugin.close();
}
}

void main();
27 changes: 27 additions & 0 deletions packages/wasm-tools/examples/wiki.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import extism
import json
from os.path import join, dirname


def read_local_wasm(file_name):
path = join(dirname(__file__), file_name)
with open(path, "rb") as wasm_file:
return wasm_file.read()


def _manifest(file_name):
wasm = read_local_wasm(file_name)
return {
"wasm": [{"data": wasm}],
"allowed_hosts": ["*.wikipedia.org"],
}


manifest = _manifest("wiki.wasm")
with extism.Plugin(manifest, wasi=True) as plugin:
data = plugin.call(
"wikiCall",
json.dumps({"query": "Ho Chi Minh City"}),
parse=lambda output: json.loads(bytes(output).decode("utf-8")),
)
print(data)
3 changes: 3 additions & 0 deletions packages/wasm-tools/extism/fake-data/fake-data.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module 'main' {
export function fakeData(): I32;
}
15 changes: 15 additions & 0 deletions packages/wasm-tools/extism/fake-data/fake-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const { wiki } = require("wikipedia");

function fakeData() {
const todoIndex = Host.inputString();
const request = {
method: "GET",
url: `https://jsonplaceholder.typicode.com/todos/${todoIndex}`,
};
const response = Http.request(request);
if (response.status != 200) throw new Error(`Got non 200 response ${response.status}`);
Host.outputString(response.body);
}

module.exports = { fakeData };
Binary file not shown.
3 changes: 3 additions & 0 deletions packages/wasm-tools/extism/wiki/wiki.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module 'main' {
export function wikiCall(): I32;
}
13 changes: 13 additions & 0 deletions packages/wasm-tools/extism/wiki/wiki.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function wikiCall() {
const params = JSON.parse(Host.inputString());
const query = params.query;
const request = {
method: "GET",
url: `https://en.wikipedia.org/api/rest_v1/page/summary/${encodeURIComponent(query)}`,
};
const response = Http.request(request);
if (response.status != 200) throw new Error(`Got non 200 response ${response.status}`);
Host.outputString(response.body);
}

module.exports = { wikiCall };
Binary file added packages/wasm-tools/extism/wiki/wiki.wasm
Binary file not shown.
4 changes: 2 additions & 2 deletions packages/wasm-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"license": "MIT",
"type": "module",
"dependencies": {
"@assemblyscript/loader": "^0.27.27",
"@types/node": "^22.5.1"
"@types/node": "^22.5.1",
"@extism/extism": "^2.0.0-rc7"
},
"devDependencies": {
"@swc/cli": "^0.4.0",
Expand Down
16 changes: 8 additions & 8 deletions pnpm-lock.yaml

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

Loading