Hello World
In this tutorial, we will write and run a simple "Hello, World!" inferlet. We will assume that the Pie server is running locally. Please make sure you have pie installed, following the installation instructions.
Writing your first inferlet
Currently, inferlets are written in Rust.
We'll assume you have Rust installed, along with the wasm32-wasip2
target. If not, please revisit the installation instructions.
Yes, you can use any language that compiles to WebAssembly (Wasm).
You may create Pie API bindings for your favorite language using WIT bindgen on the pie/inferlet-api
WIT definition.
However, Pie currently provides standard library only for Rust to make it easy to get started.
We are working on providing first-class support for other languages, such as Go and JavaScript.
First, create a new Rust project:
cargo new hello-world
cd hello-world
Then add the inferlet
dependency to your Cargo.toml
:
[dependencies]
inferlet = "0.1"
Now, replace the contents of src/main.rs
with the following code:
use inferlet::{Args, Result};
#[inferlet::main]
async fn main(mut args: Args) -> Result<()> {
println!("Hello, world!");
Ok(())
}
This is a minimal inferlet that prints "Hello, world!" when executed. To compile the inferlet to Wasm, run:
cargo build --target wasm32-wasip2 --release
This will produce a Wasm binary at target/wasm32-wasip2/release/hello_world.wasm
.
Running the inferlet
You can run the inferlet in two ways: using the Pie CLI or the Python/JavaScript client APIs.
Using the Pie CLI
To run the inferlet using the Pie CLI, use the following command:
pie run target/wasm32-wasip2/release/hello_world.wasm
This will execute the inferlet and you should see "Hello, world!" printed in the console.
Using the Python API
You can also run the inferlet using the Python API. First, make sure you have the pie-client
pip package installed:
pip install pie-client
Then, you can use the following Python script to run the inferlet:
import asyncio
from pathlib import Path
from blake3 import blake3
from pie import PieClient, Instance
async def main():
program_path = Path(f"target/wasm32-wasip2/release/hello_world.wasm")
async with PieClient("ws://127.0.0.1:8080") as client:
with open(program_path, "rb") as f:
program_bytes = f.read()
program_hash = blake3(program_bytes).hexdigest()
await client.upload_program(program_bytes)
await client.launch_instance(program_hash, arguments=[])
if __name__ == "__main__":
asyncio.run(main())
Using the JavaScript API
You can also run the inferlet using the JavaScript API. First, make sure you have the pie-client
npm package installed:
npm install pie-client
Then, you can use the following JavaScript code to run the inferlet:
import { PieClient } from 'pie-client';
import { promises as fs } from 'fs';
import { blake3 } from 'blake3';
const client = new PieClient("ws://127.0.0.1:8080");
await client.connect();
const programPath = "target/wasm32-wasip2/release/hello_world.wasm";
const programBytes = await fs.readFile(programPath);
const programHash = blake3(programBytes).toString('hex');
await client.uploadProgram(programBytes);
await client.launchInstance(programHash, []);
await client.close();