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[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
- Source
- Tool.scala
- Graph
-
- Supertypes
- Self type
-
Tool.type
Members list
Value members
Concrete methods
Builds a tool from a method, eta-expanded: Tool.fromMethod(orders.cancelOrder). Nothing is written by hand: the method's parameters are the tool's, as the fields of an argument case class are for of, and its Scaladoc (or description) describes it.
Builds a tool from a method, eta-expanded: Tool.fromMethod(orders.cancelOrder). Nothing is written by hand: the method's parameters are the tool's, as the fields of an argument case class are for of, and its Scaladoc (or description) describes it.
trait Orders:
@description("Cancel an order that has not shipped yet")
def cancelOrder(@description("The order's id, for example O-17") orderId: String): Either[String, Cancellation]
val cancel: Result[ToolFunction[Cancellation]] = Tool.fromMethod(orders.cancelOrder)
- '''Name''': the method's toolName, else its name in
snake_case(cancelOrderiscancel_order). - '''Description''': the method's description, else the text of its Scaladoc before the first tag, else its name. A parameter's: its description, else its Scaladoc
@param, else its type's description, else its name. Scaladoc is read from the source when the method is compiled in the same run, and from the TASTy of a class compiled separately only when the calling project compiles with-Xread-docs. A method without Scaladoc or annotations of its own takes those of the method it overrides. - '''Parameters''': each needs a SchemaOf and a uPickle
Reader; the schema, and the checks and errors for a call's arguments, are those of of for a case class with the same fields. A parameter with a default value is still required, as a case class field with one is. - '''Result''':
Either[String, R](aLeftis the error the model sees),Result[R](anLLMErroris the error, formatted),Try[R](a failure's message is the error), or any otherR, each written withR'sReadWriter. An exception the method throws propagates, as from an of handler.
Refused at compile time, with a message naming the method: a parameter type without a SchemaOf or Reader, a result without a ReadWriter, a polymorphic method, a by-name, repeated or context parameter, several parameter lists, and an argument that is not a method reference (Tool.fromMethod((x: Int) => x + 1)). A method with an empty parameter list is passed as a function: Tool.fromMethod(() => service.ping()).
Value parameters
- method
-
the method, eta-expanded:
service.method
Attributes
- Returns
-
the tool (a
ToolFunction[R]for the method's resultR), or aValidationErrorwhen a parameter's schema cannot be built (see SchemaOf) - Source
- Tool.scala
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(whichToolFunctionrequires)
Value parameters
- description
-
what the tool does, for the model
- handler
-
the tool's logic; a
Leftis the error the model sees, as withToolBuilder.withHandler - name
-
the tool's name, as the model calls it
Attributes
- Returns
-
the tool, or a
ValidationErrorwhenA's schema is not a JSON object (an enum, for instance) or cannot be built (a recursion through hand-written givens; see SchemaOf) - Source
- Tool.scala
Builds a tool from a function over its arguments that runs in a ToolScope: it returns its result directly, and ends the call early with reject, ensure or ? (on a Result or an Either[String, T]), whose message is the error the model sees, as a Left from a of handler is. It reads the call it serves as ToolScope.call, and the key to pass to a system with a side effect as ToolScope.idempotencyKey.
Builds a tool from a function over its arguments that runs in a ToolScope: it returns its result directly, and ends the call early with reject, ensure or ? (on a Result or an Either[String, T]), whose message is the error the model sees, as a Left from a of handler is. It reads the call it serves as ToolScope.call, and the key to pass to a system with a side effect as ToolScope.idempotencyKey.
Tool.scoped[Charge, Receipt]("charge_card", "Charge a customer's saved card") { c =>
ensure(c.amountCents > 0, "amountCents must be positive")
val customer = customers.find(c.customerId).? // a Result: its LLMError becomes the tool's error
if (customer.blocked) reject("this customer cannot be charged")
Receipt(payments.charge(customer, c.amountCents, ToolScope.idempotencyKey).?)
}
The arguments are decoded, and refused, exactly as by of. An exception the handler throws propagates, as from an of handler; a ToolRegistry turns it into the tool's error.
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(whichToolFunctionrequires)
Value parameters
- description
-
what the tool does, for the model
- handler
-
the tool's logic, given the call's ToolScope
- name
-
the tool's name, as the model calls it
Attributes
- Returns
-
the tool, or a
ValidationErroras from of - Source
- Tool.scala
