Using Functions in Generated Speech Nodes
This feature allows Large Language Models (LLMs), such as GPTs, to use user-defined functions. In broader terms, it provides the capability for GPTs to utilize a set of tools to solve the problem at hand. This is particularly useful when you need the LLM to carry out specific, well-defined tasks within your application.
One key area where this functionality is leveraged is within the Generated Speech Node. However, using functions in this context can be a bit tricky and requires some basic knowledge of Kotlin programming. Below you will find a detailed example and an explanation of how to implement and use these functions.
Example Code
To apply user-defined functions to a generated speech node, simply use the function.
Here's a code snippet.
Let's break down the example code step-by-step:
Data Class Definition
This represents input for one function.
Explanation:
data class GetCurrentWeatherInput
: This defines a Kotlin data class namedGetCurrentWeatherInput
. You can name it anything you like, for consistency, we suffix withInput
.@Description("The city and state, e.g. San Francisco, CA")
: This annotation provides a description for thelocation
parameter. It helps document the purpose of the field and what kind of data it expects.val location: String
: This defines a single property,location
, of typeString
. It signifies the city and state for which the weather is to be fetched. These data types are also supported, such asString,
numbers (Integer, Float, Double
) andBoolean
.
List of Functions
Explanation:
val myFunctions
: Declares a variablemyFunctions
which contains a list of functions.listOf(...)
: Creates a list with the specified elements—in this case, a single function created by thellmFunction
method. You can add as many functions as needed.llmFunction(...) { ... }
: Defines a function that the LLM can call. It specifies a unique name and description for the function and provides its implementation.
Inside the llmFunction
call:
llmFunction
call:name = "get_current_weather"
: Assigns the nameget_current_weather
to the function. This is the name used to invoke the function via the LLM.description = "Get the current weather in a given location"
: Provides a description that explains the purpose of the function.Function Body: Defined inside the curly braces
{...}
, it is a lambda expression that has input and output types. In this example, the input type isGetCurrentWeatherInput
and the output is a simpleString
.
Last updated