Agentic applications require tool calling support. Let's take a look at the expanded support in the new models. One of Llama 3.2 and 3.1 most exciting new feature is its built-in support for tool calling. Also known as function calling, which enables more sophisticated tasks such as accessing real-time info, performing complex math or coding tasks, interacting with external data and systems. Building dynamic agents. In this lesson, you will learn about the Llama's special tokens and role for tool calling. built-in tool calling, that includes search, Wolfram alpha, and code interpreter. We will also learn how to use custom tool calling. Before getting into the code. Let's first take a look at how tool calling in Llama 3.2 and 2.1 works. The user informs Llama with a prompt that describes the available tools built-in and or custom tools. Along with the user question that may be answered correctly only with one of the tools. Llama responds with either a direct answer or an indication that a specific tool with appropriate parameters should be used to answer the user question. The user calls the tool with the parameters and sends the result back to Llama. Llama generates the final answer to the original user's question. You will work on several use cases in this lab. Let's get to code. Let's start by adding these two lines to ignore unnecessary warnings. Then you can load our API keys. In this lesson, you are going to use Tavily for search. Other API keys for this lesson, including together.AI and Wolfram Alpha are going to be part of the utils. So let's load the utils that will be needed in this lesson. You will pass your prompt to Llama three one function that is part of utils file, and you will use this C print for printing the Python dictionaries in a colorful format. And we will also need this json. Llama has a set of prompt templates For example, let's look at the system built-in tools only prompt template. As you see, we have this environment iPython that specify the tool call. These are set up built-in tools that are available. Brave Search and Wolfram Alpha. Also the cutoff knowledge date and today's date. We will use this prompt template several times in this notebook. So let's define it as a string so we can reuse it whenever needed. For this, let's first get the current date and reformat it to match the date format that is used in the system, Built-in tools only prompt template. Now you can use this prompt template and define the tool system prompt, which is exactly the same prompt template here, with the cutoff date and today's date, which is going to be the result of the formatted date. Now we can reuse this variable wherever needed. Let's ask Llama about the weather in Menlo Park, California. We can use our tool system prompt that we defined and then add users question to the system prompt. "What is the current weather in Menlo Park, California?" Now let's see the response. Based on user's question, the model returned a tool call to brave search with the query, current weather in Menlo Park, California. If you try another user query and this time ask about the population of California, then make another model call. You will see that this time, the result is the response by the model about the population of California. The model decided that this is something that doesn't need a tool call. All the further query about the current weather in Menlo Park, California, the model made a tool call to brave search. You're not limited to use a specific built in tool. In this lab you're going to use Tavily to do the search for this query. You first import Tavily client and then define our Tavily client and pass the API key. And now let's make our web search. So here are the results you read the search back from our search. We have multiple results with the title, URL, and the content, as well as the score for each of the search results that we have. Now let's re prompt Llama with the search tool response. Before that, let's take another look at the first result of the search. Let's add this search result to our prompt and then re-prompt Llama. For this you can define the prompt to include tool system prompt and then add users initial query. "What's the current weather?" And then the result of the tool call that was this search is added. And then with this end of turn we can ask Llama for the response. Let's print and see the full prompt here. Here's the result. This is the tool system prompt. Then you add the user's question. Then Llama's response which is a tool call to brave search. Note that the response of the tool search is added using a Python tag special token. Then the tool call is made and the result of the tool call, which is the search result is added. And then we can ask Llama for the response. Let's make a call to our Llama model and print the response. Now Llama, based on the search result, summarized the weather condition in Menlo Park, California and provided the temperature, humidity, and some other related information from the search response. The prompt you created so far for the tool call was in the raw prompt format. As you have seen in previous lessons, many of the hosting companies let you use a higher-level message format to prompt the model before making the call to the model, they convert your higher-level message to the Raw format and pass it to the model for the result. Let's see how you can do tool calling using the higher-level messages. Let's first define the system prompt content so you can reuse it when needed. Here is the system prompt content. The environment is set to iPython. To enable the function call. The built-in tools are given here. The cutoff knowledge date and today's date are set as you see here. Now let's see how you can build the same previous user query. about the weather in Menlo Park in the higher-level messages format. You set the role to be system and the content to be system prompt content. Then you set the role user and the content is the user's question. Let's make the call and see the response. And here's the result, which is the same tool call to the built-in brave search with the query current weather in Menlo Park, California. In previous cells, you used Tavily to do the search and then store the first result in the search underlying result variable. Now, in the same higher-level messages format, you can re-prompt and ask the model to give you the answer about the current weather in Menlo Park. So you have the same message that you earlier had with the system prompt content and the user's initial query and the initial response of the model, which is the brave search string is added. And finally, the result of the tool call that was already done in previous cells. And was stored in the search result, which is here. And then you pass it here as the content. And the role in this case is iPython, which indicates this is the result of a tool call. Now you can call and get the response from the model. And as you see the responses about weather in Menlo Park using tool call search result. As you see, using the high-level messages resulted in the same tool call and also a similar response from the model. Using the high-level messages can be easier to use. So in the following use cases in this lab, we are going to use this prompting format. I also recommend to pause the video changed this query here and here to another query of your own and explore the results. Now let's build another tool use case. Here you have this math problem. "Can you help me solve this equation?" And the equation is given here. And we are expecting this equation to have three roots. You define your higher-level messages using the same system prompt content that you used in previous prompts. And then also passing the math problem as the content to the role user. Now, you can make a call to the model and get the response. The response is a tool call to the Wolfram Alpha built-in tool using the query "solve equation", which is the same equation given in your question. So the model decided to make the Wolfram Alpha tool call to solve this problem. Now you can make the actual tool call. For this, you will use the Wolfram Alpha function that is in the utils file, and then you will pass the same query that you got in the same tool call response to the function. Here, based on the tool response, you can see this equation has three solutions positive one, negative one, and two. Let's double-check if this response is true here. Using SymPy you can solve the same equation and then print the solution. The solutions are negative one, one, and two, which are the same response you got from the Wolfram Alpha tool. Now you can re-prompt Llama with the Wolfram Alpha tool response. Here you have your original prompt that resulted in the tool call. And then you can add the initial response from the model, which was the tool call and the result of the tool call That was the solution of the equation. You will then pass it to the model to get the response. And as you see the response is accurately the solution of this equation that has three solutions that was passed to the model using the tool result here. Another tool calling use case you are going to implement in this lab is called interpreter built-in tool. You're going to calculate the monthly payment, total payment and total interest paid on this loan. You define the same higher-level messages using the system prompt content for the role system, and passing the loan question for the role user. Now you would make a call to the model and get the response. The result for this prompt is the code in Python. Let's see if the generated code can calculate the loan payments. The same code that was returned is used in this calculate loan function that is in the utils file. You can make a call to this function using the same loan amount that was in the prompt, and the interest rate 6%, and the loan term, which is 30 years. And with the down payment of 200k, which is 20% of the loan amount. And then here is the result for the monthly payment, total payment and total interest paid on the loan over the 30 years. The total interest paid on the loan is very close to the initial loan amount. The code that you got for solving the loan calculation problem was in Python. Llama can generate non python code if instructed. Let's ask Llama to generate the code this time in Java. Here is the same prompt where generate the code in Java is added to the system prompt content. You will make a call to Llama to get the response. And here's the loan calculation code in Java. To see the results, you can run this Java code on your machine, or use one of the online Java compilers to execute the generated code. Here you see you have the same monthly payment, total payment, and total interest paid. Now you can re-prompt Llama with the tool response which was the result of the executing the code that was generated by Llama. This is the result of running the code. And then in the same way you re-prompted in the previous use cases, now you have the system prompt content and the initial loan question and the response that was the Python code that was generated and the result of executing the code, which is this. Now you have these higher-level messages ready to be passed to the model. And here you get the full response from the model on the monthly payment, total payment and total interest paid on the loan that was given and the loan question. Besides the built-in tool, you can define your own custom tools inform Llama of the custom tool description and expect Llama to return custom tool calls based on users input. Let's say you have this custom tool, Trending Songs, that receives the country name and top number, and returns the top songs in the specified country. This function is available in the utils file. Let's import this function from utils and then see what are the top five songs in the US. These are the top five songs in the US. If you provide the trending songs custom tool to Llama and prompted to get the top five trending songs in the US, Llama should return the call to the right function. Let's see how you can prompt Llama for a custom tool call. You would start by defining this user prompt, which is: "answer the user's question by using the following functions if needed. If none of the functions can be used, please say so." Then, you would provide your functions here in json format. Let's say you have two functions. Your first function is "get boiling point", which has two input arguments liquid name and Celsius. And your second function is the same trending songs function that you already defined with country and "N" as input arguments. Now you add the question "Can you check the top trending songs in the US?" To prompt the model, here is the higher-level message you will have. For the role system, you have the content which has environment type iPython which specifies the tool call, the cutoff knowledge date and today's date. And for the role user, you will have the user prompt that has two custom tools you added there. Now, you would pass the messages and get the response, the response to the user's prompt which was can you check the top five trending songs in the US based on the tools you specified. Is calling trending songs. With parameters country to be US and the number N to be five. As you see, the model made the right call to the right custom tool. Now you can make the call to the custom tool and get the result. You can put the two custom tools trending songs and get boiling point in this dictionary to map the function name as a string to the function name itself for both of the functions. To extract the function name trending song and the parameters US and five from this json, you can first convert this json into a Python dictionary, and then get the function name and parameters and display them. The function name is trending songs and the parameters are US and five. And now using the function name that you extracted here, trending songs, and these two values passed as argument, you can make the tool call and get the tool result and display the answer. Here are the top five trending songs in the US. Now, in the same way you did with the previous tool calls, you can re-prompt Llama with the result of the tool call. This is the same initial high level messages for the role system and user with the initial user prompt, and now you add the result, which was the json string containing the function call and parameters, and then you add it to the result of calling the tool. Tool result. And now you can make the call and get the result. Here are the top five trending songs in the US. All right. In this lesson, you learned about the Llama's special tokens and the role for tool calling. You use built-in tool calling with search and other tools and also worked on a custom tool calling use case. In the next lesson, you will learn about Llama Stack, which is a series of APIs for customizing Llama models and building Llama based agentic applications. See you soon in the next lesson.