跳到主要内容

LLM Tool Spec

Tool spec describes tools used by LLM.

Spec

The tool spec is a JSON schema file. The JSON schema has two properties, definition and configuration.

Tool spec
{
"$schema" : "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/JavaAIDev/easy-llm-tools/raw/refs/heads/main/llm-tool-spec.json",
"type": "object",
"title": "LLM Tool Spec",
"description": "Tools for LLMs to use",
"properties": {
"definition": {
"type": "object",
"properties": {
"id": {
"type": "string",
"description": "Unique id of this tool"
},
"name": {
"type": "string",
"description": "Name of this tool"
},
"description": {
"type": "string",
"description": "Description of this tool"
},
"parameters": {
"description": "Parameters of this tool",
"$ref": "https://json-schema.org/draft/2020-12/schema"
},
"returnType": {
"description": "Return type of this tool",
"$ref": "https://json-schema.org/draft/2020-12/schema"
},
"examples": {
"description": "Examples of using tool",
"type": "array",
"items": {
"type": "object",
"properties": {
"description": {
"description": "Description of this example",
"type": "string"
},
"parameters": {
"description": "Input parameters",
"type": "object"
},
"returnValue": {
"description": "Return value",
"type": "object"
}
}
}
}
},
"required": ["name", "description"]
},
"configuration": {
"description": "Configuration of this tool",
"$ref": "https://json-schema.org/draft/2020-12/schema"
},
"required": ["definition"]
}
}

Definition

The definition property defines the definition of a tool. It has the following properties.

PropertyDescription
idId of this tool, used as the identifier
nameName of this tool, sent to LLM
descriptionDescription of this tool
parametersSchema of parameters of this tool
returnTypeSchema of return type of this tool
examplesExamples of using this tool

name and description are required.

For the id property, it usually has the same value as name property. The name of a tool will be sent to an LLM. The id is used for identification. For example, for tools that perform web searches, webSearch is a good name. However, there may be tools using different search engines. So the id of tools may be googleWebSearch or bingWebSearch.

Configuration

configuration is a JSON schema which defines the configuration to create a tool. There is no schema for configurations. Tools can be configured in different ways.

A typical configuration is the API key to call an external service.

Java API

Easy LLM Tools provides a Java API to create tools.

Tool

Tool is the interface of a tool. It has two methods to implement:

  • getRequestType to return the type of request.
  • call to perform the tool's action.
Tool
package com.javaaidev.easyllmtools.llmtoolspec;

import java.lang.reflect.Type;

/**
* A tool used by LLM
*
* @param <Request> Request type
* @param <Response> Response type
*/
public interface Tool<Request, Response> extends ToolDefinition {

/**
* Type of the request
*
* @return type
*/
Type getRequestType();

/**
* Call this tool
*
* @param request Request
* @return Response
*/
Response call(Request request);
}

ToolDefinition

ToolDefinition is the super-interface of Tool. It contains definition of this tool. Implementation of this interface will have these values generated from the spec.

ToolDefinition
package com.javaaidev.easyllmtools.llmtoolspec;

/**
* Definition of a tool
*/
public interface ToolDefinition {

/**
* Tool id, default to tool's name
*
* @return tool id
*/
default String getId() {
return this.getName();
}

/**
* Tool name
*
* @return tool name
*/
String getName();

/**
* Tool description
*
* @return tool description
*/
String getDescription();

/**
* JSON schema of tool's parameters
*
* @return Parameters schema
*/
default String getParametersSchema() {
return "{}";
}

/**
* JSON schema of tool's return type
*
* @return Return type schema
*/
default String getReturnTypeSchema() {
return "{}";
}

/**
* Examples to call this tool
*
* @return Examples
*/
default String getExamples() {
return "{}";
}
}

ToolFactory

ToolFactory creates tools. The toolId method returns the id of tools created from this factory.

ToolFactory
package com.javaaidev.easyllmtools.llmtoolspec;

/**
* Factory to create a tool
*/
public interface ToolFactory {

/**
* ID of tool created by this factory
*
* @return tool id
*/
String toolId();
}

There are two types of ToolFactorys.

ConfigurableToolFactory

If a tool requires a configuration object to create, then ConfigurableToolFactory should be used. Its create method takes a configuration object as the input.

ConfigurableToolFactory
package com.javaaidev.easyllmtools.llmtoolspec;

/**
* Factory to create tools
*
* @param <T> Tool type
* @param <Config> Configuration type
*/
public interface ConfigurableToolFactory<Request, Response, T extends Tool<? super Request, ? extends Response>, Config> extends
ToolFactory {

/**
* Create a tool
*
* @param config Configuration
* @return the tool
*/
T create(Config config);
}

UnconfigurableToolFactory

If a tool doesn't require a configuration object to create, then UnconfigurableToolFactory should be used. Its create method has no parameters.

UnconfigurableToolFactory
package com.javaaidev.easyllmtools.llmtoolspec;

/**
* Factory to create a tool without configuration
*
* @param <T> tool
*/
public interface UnconfigurableToolFactory<Request, Response, T extends Tool<? super Request, ? extends Response>> extends
ToolFactory {

/**
* Create a tool
*
* @return the tool
*/
T create();
}

See the JavaDoc javadoc