In this lesson, you will learn how to enhance language models by integrating real-time data using function calling. You will create function declarations, handle API responses, and seamlessly integrate these functions into language models improving the accuracy and automation of your applications. Let's start coding. Okay. Let's start with lesson six on function calling. And let's jump into this use case to help understand what is function calling. And why it is useful. Imagine you want to know the current exchange rate for euros to dollars. Instead of manually searching for this information, you can ask a model to provide it for you. We will ask the model, What is the exchange rate for euros to dollars today? In order to do this, first, we need to run our helper functions so that we can use the Gemini API. Our authentication code. We'll use the same region again. We need to use the open-source SDK and then initialize the SDK. Exactly how we've done it before. Secondly, we have to select our model. In this case, we're going to use the flash model that we talked about in lesson one. Next, we have to set up our prompt and call the model. As mentioned, we're going to ask the model what is the exchange rate for euros to dollars today. Today's important because we don't want an outdated exchange rate. Let's see how up-to-date our model is. And let's see if it knows the exchange rates for the euros to dollars today. Okay. Let's unpack this response. I do not have access to real-time information, including financial data like exchange rate. The model doesn't have access to today's exchange rate for euros to dollars. This is not very helpful. So have you ever interacted with a large language model or generative AI model and asked it about real-time or current information, only to get a response with outdated information or inaccurate information? Or in this case, with no information at all? Large language models are powerful at solving many types of problems. However, they are constrained by the following limitations. They are frozen after training, leading to stale knowledge. And they can't query or modify external data. Function calling can address these shortcomings. Let's go into a few slides. As I just mentioned, function calling can address some of these shortcomings. So you can use function calling to define custom functions and provide these to the model. For example, you can define a function called get underscore whether that takes a location parameter and returns information about the current weather conditions at that location. So what happens, the user interacts with the application gives a prompt. This prompt and the function declaration is go into the model while processing a prompt. The model can choose to delegate certain data processing tasks to the function that you identify. The model does not call the function directly. Instead, the model provides structured data output that includes the function to call and parameter values to use. For example, for a prompt. What is the weather like in Boston? The model can delegate the processing to the get weather function and provide the location parameter value, Boston. You can use the structured output from the model to invoke external APIs. Example, you could connect to a weather service API, provide the location, Boston, and receive information about things like temperature, clouds, cover and wind conditions for that location. You can then provide the API output back to the model. Allowing it to complete its response to the prompt. For the weather example, the model may provide the following response. It is currently 38°F in Boston with partly cloudy skies. And this is a response that goes to the user. The advantage of not having the model call the API directly is that you can use any code language, any framework, any API that you would like to call. Typically this functionality is part of the application backend system. Some of the use cases of where you can use function calling are, for example, helping customers interact with businesses. You can create functions that connect to a business API, allowing the model to provide accurate answer to queries such as is product A still available. Or can I get the latest price for product B? Or you can build generative AI applications by connecting to public APIs. Like the example that I just discussed getting the latest weather data for a certain location. And of course, there's many, many more use cases. Think about it this way. If you have a use case where you want to include up-to-date or latest information and provide that to the model to get a more accurate response. This is a typical use case where you can use function calling. Okay. Now let's learn how you can use function calling to query for information external services and return relevant responses to your end users. For this application, you will use Frankfurt. No, no, not the sausage. In this case, Frankfurter is an open source API for current and historical foreign exchange rates published by the European Central Bank. These are up-to-date exchange rates. Since a LLMs are frozen after training, it might not know the latest exchange rates. Okay, let's specify the URL for the API. So we set URL. And this is where we will specify the path to the API. Okay. First let's call the API to check its output. For this, you will use requests. So the request library in Python is like your personal mailman for the internet. When you want to communicate with a website or online service. You write a message called a request explaining what you need. The requests library takes care of delivering your message and bringing back the response from your website. Import requests. And then we can Get a response. By using requests the get on the URL. Next. You can output the response. This is a response we're getting from the API. Remember the response you get is of the current date and time when you execute this request. In my case this is information about the current exchange rate on 12/7/2024. Since the rates are that of the European Central Bank, the base currency is the euro. This means the amount listed are what you would get if you exchanged €1 for that currency. For example, I live in Singapore. So, if I would give €1 I would get 1.4623 Singaporean dollars. Okay, now it's time to build a function that lets you call the API. We need some additional SDK classes that lets us implement our function calling. Let's import these additional classes. The function declaration lets you formally describe a function for the large language model. Now you can start with defining an API specification and register the function in a tool. Okay. So imagine you have chosen a restaurant. And the restaurant in this case is our rest API. And we want to order food from this restaurant. Now you need to write down what dishes you want. And this is what we call the API specification. And then you give that list of dishes to the waiter. So that they can get the order right. The model uses function descriptions to understand the purpose of the functions, and to determine whether these functions are useful in processing user queries. The application must declare a set of functions that the model can use to process the prompt. Each function declaration must include a function name and a function parameter. In this case, you can go up to 128. Okay, referring back to the rest API We can see that it accepts the following input parameters. From, which is a string, which is the currency to convert from to, which is also a string the currency that we would like to convert to. And then there's the date, which is also the string, the date that we're using to fetch the exchange rate for. You can now go ahead and specify your function declaration. This function is designed to fetch the exchange rate between two currencies. So we have the from and we have to to use it you need to provide at least the currency date. So the date you want the exchange rate for. You have to also specify the currency from. So the currency you're converting from. So this could be euros or US dollars. And then of course our currency to. This is the currency you're converting to. So this could be euros, Australian dollars, Singapore dollars. Be sure that you use as much detail as possible in the function and the parameter descriptions since the model will use this information to determine which function to select and how to fill the parameters in the function calling. So the more detail, the better. Finally, you'll define a tool that includes the function declaration that the model uses to generate the response. So you will set up your exchange rate tool. And this tool uses the function declaration that we just specified which is the get exchange rate function. You've now completed the configuration of your function and tool definitions. Now you can prompt the model and include the tool that you just defined. Okay. First, let's specify a prompt. In this case, what is the exchange rate from Australian dollars to Swedish krona? How much is 500 AUD worth in Swedish krona? So we're not only asking for the exchange rate, we're also asking to tell us how much $500 AUD is worth in Swedish krona. Now we can call the API. Response. And we can generate the content. As you can see, we're not only passing the prompt, we're also specifying the tool that you just created. Okay. Let's call the model with this configuration. And now let's have a look at the response. It looks like the models selected the one available function and returned a function call for the get underscore exchange underscore rate function, along with the parameters. So we can see Australian dollar. And we can see Swedish krona, for the latest date. So today's date. The parameters are in the correct format that we want it. Hooray for getting a structured response from the model. The model doesn't actually make the external API call for you. Rather, you are free to use any language, library, or framework that you would like to use for your specific use case. This gives you more flexibility. In this use case, you will use the request library that I talked about earlier to call the exchange rate rest API. Okay. Let's first unpack the response into a Python dictionary. Okay. So we're taking the response and the first candidate. And we're unpacking this in a Python dictionary. Here, you can see the Python dictionary. Now we can use the requests library or any other method that you would like to call the API. Okay. Let's specify the URL again and make sure it can leverage our parameters. We can then call the API and get a response. Let's output the response. Okay. Here you can see the response of our API call. So the base currency is the Australian dollar. Now we have today's date. And then we have the exchange rate for the Swedish krona. The next step is we have to pass this information back to the model. So that the model can generate relevant responses for the users. So just as something's up, what we've done until now. So what we've done is we specified our function declaration. So we've set the parameters. We specified the type. The description. All this information is used by the model to make a decision which function it will use. Next, you defined a tool that included the function declaration that the model uses to generate a response. Then we call the API using the prompt and the tool, and we got a structured response from the model. We then use that structured response to call the external API to get the relevant data. And now we're calling the model again using this relevant data to get the final response. Okay. Now you can call the model again with the relevant information. So we call the model and provide the latest information. So the model generate underscore content. This is the main function that orchestrates all of the steps. Each content block represents a turn a step. So we have our role. We have our parts, which is actually the text or the data that the conversation is stored in. Finally, you can generate a response for the user by passing back the function response to the model. In the next conversation turn. So we're passing here the initial prompt. Which is what is the exchange rate from Australia dollars to Swedish krona. And how much is 500 AUD worth in Swedish krona? We're providing the function call and we're providing the response of the API. And we can run this and call the model. So once we pass the function response back to the model it will respond to the user prompt along with the relevant information from the API. So let's have a look at the response. Okay. Here we have the response from the model. So the current exchange rate is 1 AUD to 7.1513 Swedish krona and 500 AUD is worth 3575 and $0.65 Swedish krona. You can get out your calculator and see if this is correct. Function calling is great for use cases where you need to access the latest external data in order for your LLM to provide an up-to-date response. I've seen developers use function calling to get the latest data, like when you want to leverage the latest prices of flights. For example, a user types book a flight from Singapore to Tokyo for July 25th. Then the model calls a predefined function in the travel app's code base like book underscore, flight with the parameters origin, destination, date, passing the extracted information. Then the app's function executes, searching for flights and potentially completing the booking process seamlessly within the apps. Some of these key advantages of using function calling are streamlining user experience, so you eliminate the need for users to copy-paste information between the model and, for example, the app. It reduces the errors, so you minimize the risk of users entering incorrect details during manual input. And it helps with increasing automation. So it opens opportunities for more complex actions like hotel bookings. Or it's itenarary suggestions to be triggered directly by the user's request to the Gemini model. This was it for lesson six on function calling. Thank you so much for taking this course and see you in the next one.