Setting up

If you haven’t already, set up a cluster and project scaffolding by following the quick start.

In Inferable, services are a collection of functions that are connected to a cluster. runs are then created by connecting services together.

We will now extend the quickstart project with a new service.

Connecting a service with TypeScript

1

Create a service file

Start by creating a delivery.service.ts inside the src/services directory.

2

Write service functions

Let’s create a toy delivery service with two main functions:

  1. makeDelivery: Places a delivery for a given order id.
  2. checkDeliveryStatus: Checks the status of a delivery by order id.

Inferable functions are just plain TypeScript functions and can contain any side-effects.

import { z } from "zod";

// Define schemas and types
const DeliveryItemSchema = z.object({
  id: z.string(),
  name: z.string(),
  qty: z.number().int().positive(),
  status: z.enum(["pending", "delivered"]).optional().default("pending"),
});

type DeliveryItem = z.infer<typeof DeliveryItemSchema>;

// Simple in-memory database
const db: Record<string, { items: DeliveryItem[]; address: string }> = {};

// Service functions
function makeDelivery(params: {
  items: DeliveryItem[];
  address: string;
  orderId: string;
}) {
  const { items, address, orderId } = params;
  db[orderId] = { items, address };
  return { items, address };
}

function checkDeliveryStatus(params: { orderId: string }) {
  const { orderId } = params;
  if (!db[orderId]) {
    throw new Error(`Delivery with id ${orderId} not found`);
  }
  return db[orderId];
}
3

Connect functions to Inferable

Wire up the functions so AI can execute them alongside other services. Append this code to delivery.service.ts:

import { inferable } from "../util/inferable";

export const deliveryService = inferable.service({
  name: "delivery",
});

deliveryService.register({
  name: "makeDelivery",
  func: makeDelivery,
  description: "Makes a delivery to a given address",
  schema: {
    input: z.object({
      items: z.array(DeliveryItemSchema),
      address: z.string(),
      orderId: z.string(),
    }),
  },
});

deliveryService.register({
  name: "checkDeliveryStatus",
  func: checkDeliveryStatus,
  description: "Checks the status of a delivery for a given order address",
  schema: {
    input: z.object({
      orderId: z.string(),
    }),
  },
});
4

Start the service

Start the service with:

npm run dev

You can verify that the services are running by doing:

inf clusters info
5

Test the new function

Try out your new functions via the Assistant UI with this command:

Can you make an order for two lightsabers, and deliver them to 742 Evergreen Terrace, Springfield?

Inferable should execute multiple steps:

  1. Check the inventory
  2. Make the order
  3. Register a delivery for the order (using our new code)

Was this page helpful?