callableFunction

inline fun <R> callableFunction(name: String, description: String? = null, noinline handler: suspend () -> R): CallableFunction

Makes a CallableFunction from a function that takes no parameters.

fun currentTime(): String = ...

val getTime = callableFunction("current_time", handler = ::currentTime)

Parameters

name

what the model refers to this function by.

description

what the function does, so the model knows when to ask for it.

handler

the function to run when the model asks for it.


inline fun <A, R> callableFunction(name: String, description: String? = null, paramName: String, noinline handler: suspend (A) -> R): CallableFunction

Makes a CallableFunction from a function that takes one parameter.

fun getWeather(city: String): String = ...

val weather = callableFunction("get_weather", paramName = "city", handler = ::getWeather)

Parameters

name

what the model refers to this function by.

description

what the function does, so the model knows when to ask for it.

paramName

the name the model sends the argument under. Required, because a function's own parameter names cannot be read back at runtime.

handler

the function to run when the model asks for it.


inline fun <A, B, R> callableFunction(name: String, description: String? = null, paramNames: List<String>, noinline handler: suspend (A, B) -> R): CallableFunction

Makes a CallableFunction from a function that takes two parameters.

fun getForecast(city: String, days: Int): String = ...

val forecast =
callableFunction("get_forecast", paramNames = listOf("city", "days"), handler = ::getForecast)

Parameters

name

what the model refers to this function by.

description

what the function does, so the model knows when to ask for it.

paramNames

the names the model sends the arguments under, in the order the function declares them.

handler

the function to run when the model asks for it.


inline fun <A, B, C, R> callableFunction(name: String, description: String? = null, paramNames: List<String>, noinline handler: suspend (A, B, C) -> R): CallableFunction

Makes a CallableFunction from a function that takes three parameters.

For a function with more parameters than this, take a single @Serializable class holding them instead.

fun route(from: String, to: String, metric: Boolean): String = ...

val plan =
callableFunction(
"plan_route",
paramNames = listOf("from", "to", "metric"),
handler = ::route,
)

Parameters

name

what the model refers to this function by.

description

what the function does, so the model knows when to ask for it.

paramNames

the names the model sends the arguments under, in the order the function declares them.

handler

the function to run when the model asks for it.


inline fun <A, R> callableFunction(name: String, description: String? = null, noinline handler: suspend (A) -> R): CallableFunction

Makes a CallableFunction from a function that takes one @Serializable class of parameters.

The class's fields are the parameters, so no names are needed. Use Describe to document them.

@Serializable
data class PlanTripArgs(
@Describe("Departure city.") val origin: String,
@Describe("Number of nights to stay.") val nights: Int = 2,
)

val planTrip = callableFunction("plan_trip") { args: PlanTripArgs ->
"Trip from ${args.origin} for ${args.nights} nights"
}

Parameters

name

what the model refers to this function by.

description

what the function does, so the model knows when to ask for it.

handler

the function to run when the model asks for it.