1.0.0 • Published 5 months ago

llmtk-js v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

llmtk-js

A TypeScript library for registering functions with auto-generated Zod-based JSON schemas and validating payloads.

Features

  • Register a function with a description and get an auto-generated JSON schema.
  • Validate inputs using Zod.
  • Call and execute registered functions with proper payload validation.
  • No decorators required—simply register your function explicitly.

Without llmtk-js, you write this:

// Define your function
function getWeather(city: string): string {
  return `Weather in ${city}: Sunny`;
}

// Manually maintain OpenAI function schema
const weatherSchema = {
  type: "function",
  function: {
    name: "getWeather",
    description: "Get weather information for a city",
    parameters: {
      type: "object",
      properties: {
        city: {
          type: "string",
          description: "Name of the city"
        }
      },
      required: ["city"]
    }
  }
};

With llmtk-js, just write this:

import { registerFunction, getOpenAITools } from 'llmtk';

// Define your function normally.
function getWeather(city: string): string {
  return `Weather in ${city}: Sunny`;
}

// Explicitly register the function.
registerFunction("getWeather", "Get weather information for a city", getWeather);

// The schema is automatically generated based on your function's type metadata.
const tools = getOpenAITools();
console.log(tools);

Installation

Install llmtk-js with npm or yarn:

npm install llmtk-js
# or
yarn add llmtk-js
# or
pnpm add llmtk-js

Example Usage

import { registerFunction, callFunction, getOpenAITools } from 'llmtk';

// Define functions
function sayHello(name: string): string {
  return `Hello, ${name}!`;
}
function addNumbers(a: number, b: number): number {
  return a + b;
}

// Register functions
registerFunction("sayHello", "Say hello to a user", sayHello);
registerFunction("addNumbers", "Add two numbers", addNumbers);

// Call a function safely with payload validation.
const result = callFunction("sayHello", { name: "Jane" });
console.log(result);

// Access the generated schemas (e.g., for use with OpenAI)
console.log(JSON.stringify(getOpenAITools(), null, 2));

License

MIT License - feel free to use in your projects!

1.0.0

5 months ago