Building Function Calls with watsonx.ai
Key Points
- The tutorial walks through building function calling with watsonx.ai, outlining a step‑by‑step workflow from environment setup to execution.
- First, you create an IBM account, obtain an API key and project ID, install required Python libraries, and configure authentication by generating a short‑lived bearer token.
- Next, you prepare the environment by specifying the Granite 3.8 model, API endpoint, tokenizer, and storing all private keys (IBM, Alpha Vantage, OpenWeather) in a separate `.env` file.
- You then define two utility functions: one that fetches high/low stock prices from Alpha Vantage given a ticker and date, and another that retrieves real‑time weather data from OpenWeather for a specified location.
- Finally, you implement a generic Watson API request function, list the available functions with their descriptions and parameters, and demonstrate calling the stock‑price function by constructing a JSON payload and applying the tokenizer’s chat template.
Sections
- Building Function Calls with Watsonx.ai - A step-by-step guide showing how to set up the environment, install libraries, obtain credentials, and configure APIs for function calling using Watsonx.ai's Granite 3.8 model.
- Applying Chat Template for Function Calls - The passage walks through constructing a tokenized payload with apply_chat_template, sending it via the make API request, automatically selecting the appropriate stock‑price function, extracting its arguments, invoking the function, and formatting the final response.
- Chaining Model Calls with Functions - The passage outlines invoking a function using arguments generated by the model, capturing its output, and then prompting a 3.08b instruct model to synthesize the returned information.
Full Transcript
# Building Function Calls with watsonx.ai **Source:** [https://www.youtube.com/watch?v=cjCYcTPryw8](https://www.youtube.com/watch?v=cjCYcTPryw8) **Duration:** 00:06:31 ## Summary - The tutorial walks through building function calling with watsonx.ai, outlining a step‑by‑step workflow from environment setup to execution. - First, you create an IBM account, obtain an API key and project ID, install required Python libraries, and configure authentication by generating a short‑lived bearer token. - Next, you prepare the environment by specifying the Granite 3.8 model, API endpoint, tokenizer, and storing all private keys (IBM, Alpha Vantage, OpenWeather) in a separate `.env` file. - You then define two utility functions: one that fetches high/low stock prices from Alpha Vantage given a ticker and date, and another that retrieves real‑time weather data from OpenWeather for a specified location. - Finally, you implement a generic Watson API request function, list the available functions with their descriptions and parameters, and demonstrate calling the stock‑price function by constructing a JSON payload and applying the tokenizer’s chat template. ## Sections - [00:00:00](https://www.youtube.com/watch?v=cjCYcTPryw8&t=0s) **Building Function Calls with Watsonx.ai** - A step-by-step guide showing how to set up the environment, install libraries, obtain credentials, and configure APIs for function calling using Watsonx.ai's Granite 3.8 model. - [00:03:04](https://www.youtube.com/watch?v=cjCYcTPryw8&t=184s) **Applying Chat Template for Function Calls** - The passage walks through constructing a tokenized payload with apply_chat_template, sending it via the make API request, automatically selecting the appropriate stock‑price function, extracting its arguments, invoking the function, and formatting the final response. - [00:06:16](https://www.youtube.com/watch?v=cjCYcTPryw8&t=376s) **Chaining Model Calls with Functions** - The passage outlines invoking a function using arguments generated by the model, capturing its output, and then prompting a 3.08b instruct model to synthesize the returned information. ## Full Transcript
Hi, my name is Erika and this is how to build function calling with watsonx.ai.
Step one is setting up your environment.
Check out this video to set up your IBM account
and get your API key and project ID credentials.
Step two installing relevant libraries.
We'll need a few packages for this tutorial.
Make sure to install the following libraries.
Step three importing libraries and setting up our credentials.
Next, we'll import the following packages.
For this tutorial, the API request will require bearer authentication.
To get our bearer token, we need to run the following commands in our terminal
and insert our watsonx API key from step one here.
The token will begin with bearer and will be followed
by a long string of characters.
It should look something like this.
Note that this token expires an hour after generation.
Next, we can prepare our environment by setting the model ID
for the granite three eight, the instruct model,
the URL needed for making Watson Access API requests,
and the tokenizer for the granted 20b function calling model.
To set our credentials, we'll need the project ID we generated in
step one and the bearer token output from the previous commands.
The Get stock price function in this tutorial will need an AV
stock API key to get a free AV stock API key.
Please visit the Alpha Vantage website and fill out this form.
And finally, the get current weather function requires a weather API key.
To generate one, please create an account at home.open weathermap.org/users/signup.
After creating an account, select the API keys tab to display your free key.
Please store all four of these private keys in a separate env file
in the same level as your directory for this notebook.
Step four defining the functions.
First, we'll write the get stock price function,
which uses the stock market data API from Alpha Vantage.
Given a ticker and the date, it returns the high and the low prices
for that ticker on a given day.
Next, the current weather function gets the real time weather
in a given location using the current weather data API from open weather.
Step five setting up the API request.
Now that we've defined our stock and weather retrieving functions, let's
make a third function to make a Watson API request for a set of instructions.
We'll use this function each time we make an API request.
Finally, let's create a list of the two available functions to call.
Here we declare our function definitions, which require the function names,
descriptions, parameters, and required properties.
The model will use function descriptions and function
parameters to determine the relevant function to call.
Step six performing function calling.
First, let's
call the get stock price function to prepare for the API request.
We'll set up our query and a JSON list of available functions for payload use
in the tokenizer chat template.
Here's what our payload looks like.
Next, we'll use our tokenizers apply chart
template to create our first set of instructions.
Apply chat template is useful for breaking up long
strings of texts into one or more messages with corresponding labels.
This allows the LLM to process the input in a format that it expects.
Since we want our output to be in string format,
we can set the tokenize parameter to false.
The add generation prompt can be set to true
to indicate the beginning of an assistant message to the output.
This will be useful when generating chart completions with the model.
Here's what our instructions look like.
Now we can call the make API request function
and pass through the instructions.
We just need.
And here's the API response.
As you can see by the function call name in the JSON object in the output,
the correct function get stock price was selected from the set of two available functions.
Now to run the stock price function, let's extract
the necessary arguments from the output.
Here are the arguments.
We'll use the ticker and the date
with the function name, ticker and date extracted.
We can set these variables and call the function
to call the function using its name as a string.
We can use the global function.
We see in the output that our function ran and returned the low
and high stock prices for IBM on October 7th, 2024.
To make a
clean final response with our granite model, we can pass
another prompt along with the information collected from function calling.
And here's the final clean response from our model.
Finally, let's use our Get current
Weather function to ask about the current weather in San Francisco.
We can follow the same steps as before, but adjust our query to what is the current weather in San Francisco?
Our payload looks almost the same
as our previous payload, with the exception of our new query.
Let's make our instructions again using apply chart template.
Here's what our instructions look like.
Now, like we did before, we call the make API request function and pass
through the instructions we just made.
Here's the API response.
As you can see, the model picked the correct function to use.
Get current weather from the set of two functions.
Now to run, the weather function will fetch the needed arguments from the output.
Here's the argument we'll use the location.
Now let's call the function with the argument generated by the model.
The function correctly describes the current weather in San Francisco.
And to complete, let's generate the final response of this function.
And here is the final response from our model.
To summarize, in this tutorial, we built two custom functions
and used the granite 3.08 instruct model to determine
which function to call based on key information from user queries.
With this information, we call the function with the arguments from the model response.
These function calls produce the desired output.
Finally, we call the granted 3.08b instruct model again
to synthesize the information returned by the functions.