This guide will walk you through creating a minimal NodeJS project to integrate with Inferable. This is an example that steps through the core concepts of Inferable.

While this guide uses NodeJS. The same steps and concepts are applicable for the other SDKs.

Prerequisites

Make sure you have Node.js installed and have set up your Inferable machine key as an environment variable (INFERABLE_API_SECRET).

You can get your API secret by running:

# npm install -g @inferable/cli
inf auth keys create --type=cluster_machine minimal_example_key
1

Install dependencies

Install the necessary packages:

npm init -y
npm install inferable zod
2

Initialize the Inferable client

Create a new file called index.ts and add the following code to initialize the Inferable client:

import { Inferable } from "inferable";
import { z } from "zod";

// Initialize the Inferable client
const client = new Inferable({
  apiSecret: process.env.INFERABLE_API_SECRET,
});
3

Register a function

Now, let’s add a function that greets users:

// Define the greeting function
client.default.register({
  name: "greetUser",
  description: "Greets a user by name",
  schema: {
    input: z.object({
    name: z.string(),
  }),
  },
  func: async (input: { name: string }) => {
    return `Hello, ${input.name}! Welcome to Inferable.`;
  },
});
4

Start the service

Finally, let’s start the service:

// Start the service
inferable.default.start().then(() => {
  console.log("Greeting service is running...");
})
5

Run the project

To run this example, save the file as app.js and execute it using Node.js:

INFERABLE_API_SECRET=****** tsx index.ts

Triggering runs

Now that you have the service running and your functions registered. You can interact with it by triggering a Run via:

const run = await inferable.run({
  initialPrompt: `Submit a support ticket with these details: ${details}`,
  attachedFunctions: [
    {
      function: "submitTicket",
      service: "default",
    },
    {
      function: "sendEmail",
      service: "default",
    },
    // ... Any other functions to make available to the run
  ],
  resultSchema: z.object({
    success: z.boolean(),
  }),
  // Optional: Subscribe an Inferable function to receive notifications when the run status changes
  // onStatusChange: { function: { function: "handler", service: "default" } },
});