asTool
Converts a KFunction into a code-engine tool that works by reflection.
The function can be annotated with Tool annotation where the name of the tool can be overridden. If the custom name is not provided, the name of the function is used.
The function can be annotated with LLMDescription annotation to provide a description of the tool. If not provided, the name of the function is used as a description.
The callable parameters can be annotated with LLMDescription annotation to provide a description of the parameter. If not provided, the name of the parameter is used.
If the function is a method that overrides or implements another method from a base class or an interface, Tool annotation can be extracted from one of the base methods in the case when it's missing on this method. In this case LLMDescription` annotation will be also extracted from the base method where Tool annotation was found.
Both suspend and non-suspend functions are supported
Default parameters are supported (calling site can omit them in the argument Json)
Parameters
The Json instance to use for serialization.
The object instance to use as the 'this' object for the callable.
The name of the tool. If not provided, the name will be obtained from Tool.customName property. In the case of the missing attribute or empty name the name of the function is used.
The description of the tool. If not provided, the description will be obtained from LLMDescription.description property. In the case of the missing attribute or empty description the name of the function is used as a description.
Note
If you get the callable as a reference to an instance method like myTools::my_best_tool
you don't need to pass thisRef argument, but if the callable is a reference to an instance method obtained via class you have to provide the proper value (MyTools::my_best_tool
])
class MyTools {
@Tool
fun my_best_tool(arg1: String, arg2: Int) {
// ...
}
}
val myTools = MyTools()
val tool = myTools::my_best_tool.asTool(json = Json)
// or
val tool = MyTools::my_best_tool.asTool(json = Json, thisRef = myTools)