Function Calling
Pie leverages WebAssembly System Interface (WASI) to enable inferlets to directly initiate HTTP requests to external APIs. This saves the overhead of routing API calls to application servers, thereby reducing latency.
HTTP Request
The standard library embeds a wstd module that provides Rust-like APIs for making WASI API calls. For instance, you can use the wstd::http
module to make HTTP requests. Below is an example of how to make a GET request to fetch an image from a URL.
use inferlet::{Args, Result};
use image::{DynamicImage, load_from_memory};
use inferlet::wstd::http::{Client, Method, Request};
use inferlet::wstd::io::{AsyncRead, copy, empty};
/// Asynchronously fetches the contents of the given URL as a DynamicImage.
pub async fn fetch_image(url: &str) -> Result<DynamicImage> {
// Create a new HTTP client.
let client = Client::new();
// Build a GET request.
let request = Request::builder()
.uri(url)
.method(Method::GET)
.body(empty())?;
// Send the request and get the response.
let response = client.send(request).await?;
// Read the response body into a buffer.
let mut body = response.into_body();
let mut buf = Vec::new();
body.read_to_end(&mut buf).await?;
let img = load_from_memory(&buf)?;
// Convert the buffer into a UTF-8 string.
Ok(img)
}
#[inferlet::main]
async fn main(args:Args) -> Result<()> {
// Fetch an image from a URL
let url = "https://www.ilankelman.org/stopsigns/australia.jpg";
let image = fetch_image(url).await?;
Ok(())
}
Socket I/O and WebSocket
Pie currently does not support raw TCP/UDP socket I/O in inferlets. We are actively working on adding support for socket I/O in the near future.