Nestjs Llm Tools

A NestJS module that provides decorators and utilities for defining LLM function calling tools using TypeScript decorators.

LLM Mart 6 views 344k listing impressions

A NestJS module that provides decorators and utilities for defining LLM (Large Language Model) function calling tools using TypeScript decorators. This library enables you to easily expose NestJS service methods as structured tools that can be used with LLM APIs.

Features

  • 🎯 Decorator-based Tool Definition: Use simple decorators to mark methods as LLM tools
  • 🔍 Automatic Discovery: Automatically discovers and registers tools from providers and controllers
  • 📝 Type Safety: Leverages TypeScript decorators and Zod for type validation
  • ⚙️ Configurable: Customize tool naming and behavior
  • 🧪 Well Tested: Includes comprehensive test coverage

Installation

npm install nestjs-llm-tools
# or
yarn add nestjs-llm-tools
# or
pnpm add nestjs-llm-tools
# or
bun add nestjs-llm-tools

Peer Dependencies

This package requires the following peer dependencies:

npm install @nestjs/common @nestjs/core reflect-metadata zod

Usage

1. Import the Module

Import LlmToolsModule in your NestJS application:

import { Module } from '@nestjs/common';
import { LlmToolsModule } from 'nestjs-llm-tools';

@Module({
  imports: [
    LlmToolsModule.forRoot({
      prefixClassName: true, // Optional: prefix tool names with class name (default: true)
    }),
  ],
})
export class AppModule {}

2. Define Tools Using Decorators

Use the @LlmTool() and @ToolParam() decorators to define tools in your services:

import { Injectable } from '@nestjs/common';
import { LlmTool, ToolParam } from 'nestjs-llm-tools';
import { z } from 'zod';

@Injectable()
export class WeatherService {
  @LlmTool('Get the current weather for a specific location')
  getWeather(
    @ToolParam({ description: 'The city and state, e.g., San Francisco, CA' })
    location: string,

    @ToolParam({
      type: z.enum(['celsius', 'fahrenheit']).optional(),
      description: 'Temperature unit'
    })
    units?: string
  ) {
    // Your implementation
    return {
      location,
      temperature: 72,
      units: units || 'fahrenheit'
    };
  }
}

Type Inference Rules

The @ToolParam() decorator supports automatic type and name inference:

  • Parameter names are automatically inferred from function signatures
  • Primitive types (string, number, boolean) are automatically inferred from TypeScript types
  • Complex types (enums, objects, arrays, unions) require explicit Zod schemas
// ✅ Primitive types - no explicit type needed
@ToolParam({ description: 'User name' })
name: string

@ToolParam({ description: 'User age' })
age: number

@ToolParam({ description: 'Is active' })
isActive: boolean

// ✅ Complex types - explicit Zod schema required
@ToolParam({
  type: z.enum(['admin', 'user', 'guest']),
  description: 'User role'
})
role: string

@ToolParam({
  type: z.object({ street: z.string(), city: z.string() }),
  description: 'User address'
})
address: { street: string; city: string }

3. Use the LlmToolsService

The module automatically discovers and registers all tools. Inject LlmToolsService to access and execute them:

import { Injectable } from '@nestjs/common';
import { LlmToolsService } from 'nestjs-llm-tools';

@Injectable()
export class LlmService {
  constructor(private readonly llmToolsService: LlmToolsService) {}

  async chat(userMessage: string) {
    // Get all available tools
    const tools = this.llmToolsService.getTools();

    // Send to your LLM API (e.g., Anthropic, OpenAI)
    const response = await yourLlmApi.sendMessage({
      message: userMessage,
      // WARN: You need to transform the tool definitions accordingly
      tools: tools
    });

    // If the LLM wants to call a tool
    if (response.toolCalls) {
      for (const toolCall of response.toolCalls) {
        const result = await this.llmToolsService.callTool(
          toolCall.name,
          toolCall.parameters
        );

From the project's README.

Comments (0)

Sign in to join the conversation.

No comments yet.