Skip to content

Tools

Tool calling with a calculator that performs arithmetic operations.

ts
import {
  LanguageModelSession,
  GenerationSchema,
  GenerationGuide,
  GeneratedContent,
  Tool,
} from "tsfm-sdk";

class CalculatorTool extends Tool {
  readonly name = "calculator";
  readonly description = "Performs basic arithmetic. Returns the result as a number.";

  readonly argumentsSchema = new GenerationSchema("CalculatorParams", "Calculator inputs")
    .property("operation", "string", {
      description: "One of: add, subtract, multiply, divide",
      guides: [GenerationGuide.anyOf(["add", "subtract", "multiply", "divide"])],
    })
    .property("a", "number", { description: "First operand" })
    .property("b", "number", { description: "Second operand" });

  async call(args: GeneratedContent): Promise<string> {
    const op = args.value<string>("operation");
    const a = args.value<number>("a");
    const b = args.value<number>("b");

    switch (op) {
      case "add":
        return String(a + b);
      case "subtract":
        return String(a - b);
      case "multiply":
        return String(a * b);
      case "divide":
        if (b === 0) throw new Error("Division by zero");
        return String(a / b);
      default:
        throw new Error(`Unknown operation: ${op}`);
    }
  }
}

async function main() {
  const calculator = new CalculatorTool();
  const session = new LanguageModelSession({
    instructions: "You are a helpful math assistant.",
    tools: [calculator],
  });

  const reply = await session.respond("What is 15% of 240?");
  console.log("Answer:", reply);

  session.dispose();
  calculator.dispose();
}

main().catch(console.error);

What This Shows

  1. Extend Tool with name, description, argumentsSchema, and call()
  2. Use GenerationGuide.anyOf() to constrain argument values
  3. Pass tools to a session via { tools: [calculator] }
  4. The model decides when to call the tool and incorporates the result
  5. Dispose both the session and tool when done

Released under the Apache 2.0 License.