asToolDescriptor

fun SerialDescriptor.asToolDescriptor(toolName: String, toolDescription: String? = null, valueDescription: String? = null): ToolDescriptor(source)

Converts a SerialDescriptor into a ToolDescriptor with metadata about a tool, including its name, description, and parameters.

Return

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

Example: if the current SerialDescriptor represents the following class:

@Serializable
class Person(
val name: String,
@property:LLMDescription("Age of the user (between 5 and 99)")
val age: Int
)

,then

serializer<Person>().descriptor
.asToolDescriptor(
toolName = "getLocation",
toolDescription = "Finds where the given Person is located"
)

would return the following ToolDescriptor :

ToolDescriptor(
name = "getLocation",
description = "Finds where the given Person is located",
requiredParameters = listOf(
ToolParameterDescriptor(
name = "name",
description = "name",
type = ToolParameterType.String
),
ToolParameterDescriptor(
name = "age",
description = "Age of the user (between 5 and 99)",
type = ToolParameterType.Integer
)
)
)

Or, alternatively, you can ommit the toolDescription parameter but provide it via @LLMDescription annotation of your class:

@Serializable
@LLMDescription("A tool to compile the final plan of the trip accepted by the user")
class TripPlan(
@property:LLMDescription("Steps of the plan, containing destination, start date and end date of each jorney")
val steps: List<PlanStep>,
)

,then

serializer<TripPlan>().descriptor
.asToolDescriptor(toolName = "provideTripPlan")

would return the following ToolDescriptor :

ToolDescriptor(
name = "provideTripPlan",
description = "A tool to compile the final plan of the trip accepted by the user",
requiredParameters = listOf(
ToolParameterDescriptor(
name = "steps",
description = "Steps of the plan, containing destination, start date and end date of each jorney",
type = ToolParameterType.List(itemType = ToolParameterType.Object(
... // fields of `PlanStep`
))
)
)
)

Parameters

toolName

The name to assign to the resulting tool descriptor.

toolDescription

An optional custom description for the tool. Defaults to the descriptor's annotation-based description if null.

valueDescription

An optional description for value parameters of primitive types (not required for class-based parameters with @LLMDescription annotation but recommended for String/Int/Float/List etc. tool parameters). If not specified for a primitive input type, an empty description will be passed to LLM.