Data framework for your LLM application
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Tool integration system for extending agent capabilities with external functions and APIs in LlamaIndex.TS.
import { QueryEngineTool, FunctionTool } from "llamaindex/tools";
// Or from main package
import { QueryEngineTool, FunctionTool } from "llamaindex";The tools system allows agents and workflows to interact with external systems, perform calculations, access APIs, and execute custom functions through a standardized 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;
}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;
}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;
}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 */,
});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"],
},
}
);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"],
},
}
);// 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",
}
);