ToolFunction

org.llm4s.toolapi.ToolFunction
See theToolFunction companion object
@Stable
final case class ToolFunction[T, R]

A fully-defined, executable tool that can be invoked by an LLM.

Bundles together a name, a natural-language description (shown to the model), a SchemaDefinition that describes the expected JSON parameters, and a handler function that performs the actual work.

Prefer constructing instances via ToolBuilder rather than directly.

Type parameters

R

Return type, must have a uPickle ReadWriter

T

Phantom type for the parameter schema (unused at runtime)

Value parameters

description

Natural-language description of what the tool does and when to use it

handler

Business logic; receives a SafeParameterExtractor and returns Right(result) or Left(errorMessage)

name

Unique tool identifier used by the LLM when calling the tool

schema

Parameter schema describing the expected JSON arguments

Attributes

Companion
object
Graph
Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all

Members list

Value members

Concrete methods

def execute(args: Value): Either[ToolCallError, Value]

Executes the tool with the given arguments

Executes the tool with the given arguments

Attributes

def execute(args: Value, call: ToolCallInfo): Either[ToolCallError, Value]

Executes the tool as the call call: the handler reads it as SafeParameterExtractor.call, so a tool with an external side effect can pass call.idempotencyKey on. An agent runs every ToolFunction this way.

Executes the tool as the call call: the handler reads it as SafeParameterExtractor.call, so a tool with an external side effect can pass call.idempotencyKey on. An agent runs every ToolFunction this way.

Attributes

def executeEnhanced(args: Value, enhancedHandler: SafeParameterExtractor => Either[List[ToolParameterError], R]): Either[ToolCallError, Value]

Executes the tool with enhanced error reporting. Uses SafeParameterExtractor in enhanced mode for better error messages.

Executes the tool with enhanced error reporting. Uses SafeParameterExtractor in enhanced mode for better error messages.

Value parameters

args

The arguments to pass to the tool

enhancedHandler

Handler that uses enhanced extraction methods

Attributes

Returns

Either an error or the result as JSON

def toOpenAITool(strict: Boolean): Value

Converts the tool definition to the format expected by OpenAI's API

Converts the tool definition to the format expected by OpenAI's API

Attributes

def withDescription(description: String): ToolFunction[T, R]

Replace the description shown to the LLM.

Replace the description shown to the LLM.

Attributes

def withHandler(handler: SafeParameterExtractor => Either[String, R]): ToolFunction[T, R]

Replace the handler.

Replace the handler.

Attributes

def withName(name: String): ToolFunction[T, R]

Replace the tool's name.

Replace the tool's name.

Attributes

def withSchema[U](schema: SchemaDefinition[U]): ToolFunction[U, R]

Replace the parameter schema.

Replace the parameter schema.

Attributes

Inherited methods

def productElementNames: Iterator[String]

Attributes

Inherited from:
Product
def productIterator: Iterator[Any]

Attributes

Inherited from:
Product