CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-llamaindex

Data framework for your LLM application

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

tools.mddocs/

Tools and Utilities

Tool integration system for extending agent capabilities with external functions and APIs in LlamaIndex.TS.

Import

import { QueryEngineTool, FunctionTool } from "llamaindex/tools";
// Or from main package
import { QueryEngineTool, FunctionTool } from "llamaindex";

Overview

The tools system allows agents and workflows to interact with external systems, perform calculations, access APIs, and execute custom functions through a standardized interface.

Base Tool Interface

interface BaseTool {
  metadata: ToolMetadata;
  call(input: string): Promise<ToolOutput>;
}

interface ToolMetadata {
  name: string;
  description: string;
  parameters?: {
    type: "object";
    properties: Record<string, any>;
    required?: string[];
  };
}

interface ToolOutput {
  content: string;
  tool: BaseTool;
  rawInput: any;
  rawOutput?: any;
  isError?: boolean;
}

QueryEngineTool

Wraps query engines as tools for use with agents.

class QueryEngineTool implements BaseTool {
  constructor(options: {
    queryEngine: BaseQueryEngine;
    metadata: ToolMetadata;
  });
  
  call(input: string): Promise<ToolOutput>;
  
  metadata: ToolMetadata;
  queryEngine: BaseQueryEngine;
}

FunctionTool

Creates tools from JavaScript functions.

class FunctionTool implements BaseTool {
  static from<T extends (...args: any[]) => any>(
    fn: T,
    metadata: ToolMetadata
  ): FunctionTool;
  
  constructor(options: {
    fn: Function;
    metadata: ToolMetadata;
  });
  
  call(input: string): Promise<ToolOutput>;
  
  metadata: ToolMetadata;
}

Basic Usage

Query Engine Tool

import { VectorStoreIndex, QueryEngineTool } from "llamaindex";

// Create knowledge base
const index = await VectorStoreIndex.fromDocuments(documents);

// Create query engine tool
const queryTool = new QueryEngineTool({
  queryEngine: index.asQueryEngine(),
  metadata: {
    name: "knowledge_search",
    description: "Search the company knowledge base for information",
  },
});

// Use with agent
const agent = new ReActAgent({
  tools: [queryTool],
  llm: /* your LLM */,
});

Function Tool

import { FunctionTool } from "llamaindex/tools";

// Create calculator tool
const calculatorTool = FunctionTool.from(
  ({ a, b, operation }: { a: number; b: number; operation: string }) => {
    switch (operation) {
      case "add": return a + b;
      case "subtract": return a - b;
      case "multiply": return a * b;
      case "divide": return a / b;
      default: throw new Error("Unknown operation");
    }
  },
  {
    name: "calculator",
    description: "Perform basic arithmetic operations",
    parameters: {
      type: "object",
      properties: {
        a: { type: "number", description: "First number" },
        b: { type: "number", description: "Second number" },
        operation: { 
          type: "string", 
          description: "Operation to perform",
          enum: ["add", "subtract", "multiply", "divide"]
        },
      },
      required: ["a", "b", "operation"],
    },
  }
);

Advanced Tool Creation

API Tool

const apiTool = FunctionTool.from(
  async ({ endpoint, method = "GET" }: { endpoint: string; method?: string }) => {
    const response = await fetch(endpoint, { method });
    const data = await response.json();
    return JSON.stringify(data, null, 2);
  },
  {
    name: "api_call",
    description: "Make HTTP requests to APIs",
    parameters: {
      type: "object",
      properties: {
        endpoint: { type: "string", description: "API endpoint URL" },
        method: { type: "string", enum: ["GET", "POST"], description: "HTTP method" },
      },
      required: ["endpoint"],
    },
  }
);

Best Practices

// Error handling in tools
const robustTool = FunctionTool.from(
  async (params: any) => {
    try {
      // Tool logic here
      return "Success";
    } catch (error) {
      throw new Error(`Tool failed: ${error.message}`);
    }
  },
  {
    name: "robust_tool",
    description: "A tool with proper error handling",
  }
);

docs

chat-engines.md

document-processing.md

embeddings.md

index.md

llm-integration.md

query-engines.md

response-synthesis.md

settings.md

storage.md

tools.md

vector-indexing.md

tile.json