Learning Library

← Back to Library

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.

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
0:00Hi, my name is Erika and this is how to build function calling with watsonx.ai. 0:05Step one is setting up your environment. 0:07Check out this video to set up your IBM account 0:10and get your API key and project ID credentials. 0:13Step two installing relevant libraries. 0:17We'll need a few packages for this tutorial. 0:18Make sure to install the following libraries. 0:21Step three importing libraries and setting up our credentials. 0:26Next, we'll import the following packages. 0:29For this tutorial, the API request will require bearer authentication. 0:33To get our bearer token, we need to run the following commands in our terminal 0:37and insert our watsonx API key from step one here. 0:41The token will begin with bearer and will be followed 0:43by a long string of characters. 0:45It should look something like this. 0:48Note that this token expires an hour after generation. 0:53Next, we can prepare our environment by setting the model ID 0:56for the granite three eight, the instruct model, 0:58the URL needed for making Watson Access API requests, 1:02and the tokenizer for the granted 20b function calling model. 1:07To set our credentials, we'll need the project ID we generated in 1:10step one and the bearer token output from the previous commands. 1:14The Get stock price function in this tutorial will need an AV 1:17stock API key to get a free AV stock API key. 1:21Please visit the Alpha Vantage website and fill out this form. 1:24And finally, the get current weather function requires a weather API key. 1:29To generate one, please create an account at home.open weathermap.org/users/signup. 1:36After creating an account, select the API keys tab to display your free key. 1:41Please store all four of these private keys in a separate env file 1:46in the same level as your directory for this notebook. 1:50Step four defining the functions. 1:53First, we'll write the get stock price function, 1:56which uses the stock market data API from Alpha Vantage. 1:59Given a ticker and the date, it returns the high and the low prices 2:03for that ticker on a given day. 2:08Next, the current weather function gets the real time weather 2:11in a given location using the current weather data API from open weather. 2:17Step five setting up the API request. 2:20Now that we've defined our stock and weather retrieving functions, let's 2:24make a third function to make a Watson API request for a set of instructions. 2:28We'll use this function each time we make an API request. 2:31Finally, let's create a list of the two available functions to call. 2:35Here we declare our function definitions, which require the function names, 2:39descriptions, parameters, and required properties. 2:43The model will use function descriptions and function 2:45parameters to determine the relevant function to call. 2:49Step six performing function calling. 2:52First, let's 2:53call the get stock price function to prepare for the API request. 2:56We'll set up our query and a JSON list of available functions for payload use 3:01in the tokenizer chat template. 3:05Here's what our payload looks like. 3:07Next, we'll use our tokenizers apply chart 3:09template to create our first set of instructions. 3:13Apply chat template is useful for breaking up long 3:16strings of texts into one or more messages with corresponding labels. 3:20This allows the LLM to process the input in a format that it expects. 3:24Since we want our output to be in string format, 3:27we can set the tokenize parameter to false. 3:30The add generation prompt can be set to true 3:32to indicate the beginning of an assistant message to the output. 3:36This will be useful when generating chart completions with the model. 3:42Here's what our instructions look like. 3:46Now we can call the make API request function 3:49and pass through the instructions. 3:51We just need. 3:55And here's the API response. 3:57As you can see by the function call name in the JSON object in the output, 4:01the correct function get stock price was selected from the set of two available functions. 4:07Now to run the stock price function, let's extract 4:10the necessary arguments from the output. 4:14Here are the arguments. 4:16We'll use the ticker and the date 4:18with the function name, ticker and date extracted. 4:21We can set these variables and call the function 4:24to call the function using its name as a string. 4:27We can use the global function. 4:32We see in the output that our function ran and returned the low 4:35and high stock prices for IBM on October 7th, 2024. 4:40To make a 4:40clean final response with our granite model, we can pass 4:44another prompt along with the information collected from function calling. 4:49And here's the final clean response from our model. 4:55Finally, let's use our Get current 4:58Weather function to ask about the current weather in San Francisco. 5:01We can follow the same steps as before, but adjust our query to what is the current weather in San Francisco? 5:07Our payload looks almost the same 5:09as our previous payload, with the exception of our new query. 5:12Let's make our instructions again using apply chart template. 5:18Here's what our instructions look like. 5:20Now, like we did before, we call the make API request function and pass 5:24through the instructions we just made. 5:28Here's the API response. 5:30As you can see, the model picked the correct function to use. 5:34Get current weather from the set of two functions. 5:39Now to run, the weather function will fetch the needed arguments from the output. 5:44Here's the argument we'll use the location. 5:48Now let's call the function with the argument generated by the model. 5:52The function correctly describes the current weather in San Francisco. 5:56And to complete, let's generate the final response of this function. 6:02And here is the final response from our model. 6:05To summarize, in this tutorial, we built two custom functions 6:09and used the granite 3.08 instruct model to determine 6:12which function to call based on key information from user queries. 6:17With this information, we call the function with the arguments from the model response. 6:21These function calls produce the desired output. 6:24Finally, we call the granted 3.08b instruct model again 6:28to synthesize the information returned by the functions.