Tool

org.llm4s.schema.Tool
@Experimental
object Tool

Tools defined from a function over a case class: the parameter schema is derived from the argument type (SchemaOf), the arguments are decoded into it, and the result is written with its uPickle writer, so the schema the model sees, the argument parsing and the function cannot drift apart.

@description("Where to look up the weather")
case class WeatherArgs(@description("City name") city: String, @description("Days ahead") days: Int)
 derives SchemaOf, ReadWriter

val weather: Result[ToolFunction[WeatherArgs, Forecast]] =
 Tool.of[WeatherArgs, Forecast]("get_weather", "Gets a forecast")(args => forecast(args.city, args.days))

The result is the ToolFunction that ToolBuilder builds, registered in a ToolRegistry like any other.

Attributes

Graph
Supertypes
class Object
trait Matchable
class Any
Self type
Tool.type

Members list

Value members

Concrete methods

def of[A, R](name: String, description: String)(handler: A => Either[String, R])(using schemaOf: SchemaOf[A], reader: Reader[A], writer: ReadWriter[R]): Result[ToolFunction[A, R]]

Builds a tool from a function over its arguments.

Builds a tool from a function over its arguments.

Before handler runs, the call's arguments are checked against the derived schema, every field at once, and each problem is reported as the ToolParameterError that SafeParameterExtractor reports for a hand-written tool: MissingParameter (with the keys that were sent), NullParameter, TypeMismatch, and InvalidNesting for each field when the arguments are not a JSON object. A nested field is named by its path (address.city, items[0].name). An Int must be a whole number in the Int range, as for getInt; a value outside an enum's cases is a TypeMismatch that lists them. Keys the type has no field for are ignored, as a hand-written tool ignores them. A failure reaches the model as the tool's error, as a hand-written handler's Left does: one problem as its own message, several as ToolParameterError.MultipleErrors's.

An Option field may be absent or null (None); every other field is required, including one with a default value, as the schema says.

Type parameters

A

the arguments: a case class with a SchemaOf (derives SchemaOf) and a uPickle reader

R

the result, written with its uPickle ReadWriter (which ToolFunction requires)

Value parameters

description

what the tool does, for the model

handler

the tool's logic; a Left is the error the model sees, as with ToolBuilder.withHandler

name

the tool's name, as the model calls it

Attributes

Returns

the tool, or a ValidationError when A's schema is not a JSON object (an enum, for instance) or cannot be built (a recursion through hand-written givens; see SchemaOf)