Tool

abstract class Tool<TArgs, TResult>(val argsSerializer: KSerializer<TArgs>, val resultSerializer: KSerializer<TResult>, val descriptor: ToolDescriptor)(source)

Base class representing a tool that can be invoked by the LLM. Tools are usually used to return results, make changes to the environment, or perform other actions.

Parameters

TArgs

The type of arguments the tool accepts.

TResult

The type of result the tool returns. Provides a textual explanation of what the tool does and how it can be used (for the LLM).

Inheritors

Constructors

Link copied to clipboard
constructor(argsSerializer: KSerializer<TArgs>, resultSerializer: KSerializer<TResult>, descriptor: ToolDescriptor)
constructor(argsSerializer: KSerializer<TArgs>, resultSerializer: KSerializer<TResult>, name: String, description: String)

Convenience constructor for the base tool class that generates ToolDescriptor from the provided name, description and argsSerializer using asToolDescriptor

Types

Link copied to clipboard
interface Args : ToolArgs

Base type, representing tool arguments.

Link copied to clipboard

Args implementation that can be used for tools that expect no arguments.

Properties

Link copied to clipboard

A KSerializer responsible for encoding and decoding the arguments required for the tool execution.

Link copied to clipboard

A ToolDescriptor representing the tool's schema, including its name, description, and parameters.

Link copied to clipboard

The name of the tool from the descriptor

Link copied to clipboard

A KSerializer responsible for encoding and decoding the result returned by the tool execution.

Functions

Link copied to clipboard
fun decodeArgs(rawArgs: JsonObject): TArgs

Decodes the provided raw JSON arguments into an instance of the specified arguments type.

Link copied to clipboard

Decodes the provided raw JSON element into an instance of the specified result type.

Link copied to clipboard

Encodes the given arguments into a JSON representation.

Link copied to clipboard

Encodes the provided arguments into a JSON string representation using the configured serializer.

Link copied to clipboard

Encodes the provided arguments into a JSON string representation without type safety checks.

Link copied to clipboard

Encodes the given arguments into a JSON representation without type safety checks.

Link copied to clipboard

Encodes the given result into a JSON representation using the configured result serializer.

Link copied to clipboard

Encodes the given result of type TResult to its string representation for the LLM.s

Link copied to clipboard

Encodes the provided result object into a JSON string representation without type safety checks.

Link copied to clipboard

Encodes the given result object into a JSON representation without type safety checks. This method casts the provided result to the expected TResult type and leverages the encodeResult method to produce the JSON output.

Link copied to clipboard
abstract suspend fun execute(args: TArgs): TResult

Executes the tool's logic with the provided arguments.