Function compiles stateless Python functions into self-contained binaries we call predictors.

Your Python code is lowered directly to native code, and does not rely on the Python runtime (or any other managed runtime).

Defining a Function

To define a prediction function, wrap a Python function with the @compile decorator, providing a predictor tag and short description:

greeting.py
from fxn import compile

@compile(
    tag="@your-username/greeting", # replace `your-username` with your Function username
    description="Say a friendly greeting."
)
def greeting (name: str) -> str:
    f"Hey there {name}! We're glad you're using Function and we hope you like it."

The predictor description is required and must be 100 characters or less.

The prediction function must specify parameter and return type annotations. Learn more.

Compiling the Function

Use the Function CLI to compile the function. First, make sure you are logged into the Function CLI:

# Login to the CLI
$ fxn auth login <access key>

Then run the following command:

# Compile the predictor
$ fxn compile greeting.py

Function will upload your code, perform code generation on your function and its dependencies, then compile for our supported platforms:

Creating a predictor with the Function CLI. This is realtime--not sped up.

Using the Function

Depending on the complexity of your function, it can take anywhere from a few seconds to a few minutes for the function to be compiled for all platforms. Once the function is compiled, you can run it everywhere:

import { Function } from "fxnjs"

// 💥 Create your Function client
const fxn = new Function({ accessKey: "..." });

// 🔥 Make a prediction
const prediction = await fxn.predictions.create({
    tag: "@your-username/greeting",
    inputs: { name: "Lina" }
});

// 🚀 Print the result
console.log(prediction.results[0]);

In the call to fxn.predictions.create, make sure to update the tag with your Function username.

You can check the compilation status of the predictor at fxn.ai/predictors.

Function currently takes longer to compile functions for macOS, iOS, and visionOS due to limited Mac compilation server capacity. We are working to provision more servers.

Try it Out

We have an open-source Playground project that shows the full workflow from writing and compiling a Python function; to using it across the following platforms:

  • React with Next.js (Browser, Node.js)
  • React Native with Expo (Android, iOS)
  • Kotlin in Android Studio (Android)
  • Swift in Xcode (iOS)
  • Python (Linux, macOS, Windows)
  • Unity Engine (Android, iOS, Linux, macOS, visionOS, WebGL, Windows)