Function Calling
AI Function Calling
It is known that, sometimes, AI models return incorrect results. With function calls, this means that there’s a risks that wrong functions calls have real-world impact. Double your attention when working with functions that do more than just reading/fetching data.
LLMs return unstructed data which is hard to use in applications other than chat. By invoking functions, you can make LLMs return structured data which can be used to interact with other systems. In practice, this means that the model will return a JSON instead of Natural Text, which can be parsed and passed as arguments to functions in your code. With this, LLM functions enable traditional use-cases such as rendering Web Pages, strucuring Mobile Application View Models, saving data to Database columns, passing it to API calls, among infinite other use cases.
OpenAI introduced Function Calling in their latest GPT Models, but open-source models did not get that feature
until recently. LlamaAPI allows you to seamlessly call functions (such as query_database()
or send_email()
)
from different LLMs, standardizing their outputs.
Use Cases
From a code perspective, function calling allows for:
- More deterministic control: Transform natural text into a structured output through functions.
- Structured data extraction: Using the resource to extract specific information from text.
- Development of custom functions: Connect the model to external APIs, databases, and internal tools.
From a product perspective you could:
- Integrate Personal Assistants into IoT devices.
- Add recurring reminders and to-do list features.
- Explore more complex interactions, such as online booking transactions.
Recommended Flow
- Send query and function definitions to the model
- Model returns JSON adhering to function schema (if it chooses to call one)
- Parse the JSON
- Validate it
- Call the function
- Send function result back to model to summarize for user
To learn more about Function Calling and how it works, see this OpenAI blog post.
Examples
As you will see on the following examples, an API Request must contain the following:
- Model used (eg.
llama-13b-chat
). See other models in this link - List of available functions.
- Function calls (
function_call
). - User messages.
Example 1: get_flight_info
- Objective: Get flight information between two locations.
- Parameters:
loc_origin
(departure airport),loc_destination
(destination airport). - Usage Example: User inquires about the next flight from Amsterdam to New York.
Request
Expected Response
Flight information method
Now we will create a method that will return information according to the extraction of information provided by the user.
Send the response back to the model to summarize
Expected reponse
Example 2: Person
- Objective: Identify information about a person.
- Parameters:
name
(person’s name),age
(person’s age),fav_food
(favorite food). - Usage Example: User provides information about John, and the function returns a JSON object.
Request
Expected Response
Example 3: get_weather_information
- Objective: Get the weather from a location
- Parameter:
location
(desired location) - Usage Example: User provides a location and receive local weather information
Request
Firstly, we extract the location where the user wants to receive information
Then, after retrieving the desired city, we make a request to an API to receive information about the local weather. The API used was WeatherAPI, as it has more fields, we filtered just a few for the example
Response
Then, we send the response to the model and summarize the final result
Response
Example 4: get_email_summary
For this example we will use gmail as an email service
- Objective: Create a summary of your e-mails
- Parameter:
value
(desired quantity of e-mails),login
(your e-mail) - Usage Example: User provides a location and receive local weather information
Request
Firstly, you need to create a password for less secure apps by following the link: https://support.google.com/a/answer/6260879?hl=en. With the password created, we can make the first request to extract the email, password and number of emails that will be summarized.
Let’s store this password in a variable, separate from the instruction for LLM
Response
So, after extracting the information, we need to access the email sent, for this it will be necessary to use a function to read and retrieve the desired emails
Response
So, we send the emails to a new function that will summarize
Response
Discord
Don’t forget to Join our Discord community! There, you’ll find tips, use-cases, help, and an opportunity to collaborate with our staff and users.