As a developer, you want to understand and adopt all potential grounding methods. Function calling will enable you to improve the design of your SQL agent. In this lesson, you have the opportunity to work with Azure OpenAI's Function Calling feature. Compared to the previous one, the idea is to leverage the pre-built functions to send the queries to databases, without the need to expose the SQL queries during the translation process. Okay, let's get to it. We have already explored several interesting aspects in the previous lessons. We have to use LangChain to connect to CSV files and two SQL databases with SQL Lite. Now, what's next? One of the new capabilities of the Azure OpenAI models is the function calling feature. You might wonder why we need this if our agent is already working great and fetching the information correctly. What's the additional value that this functionality brings? The main point of using function calling is to provide our system or agent with specific instructions for finding information from any specific topic. These include suggesting the type of queries to prioritize, and to get the search results and format that you need. This approach adds a level of deterministic behavior to the agent, allowing you to control the process more precisely. Unlike the previous LangChain agent approach, function call encapsulates queries within functions, providing more structure and predictability. You might wonder why we need this if our agent is already working great and fetching the information correctly. Let's see how this works. You have all the information in the notebook and we will reuse the previous setup. Here is what you need. As before, we are using our Azure OpenAI endpoint with the key and a preview version. This version includes the function calling features we will use in this and the next lesson. Now, let's move to the second step. Depending on your background, you may know how to create functions in Python. I'll show you how to create a simple test function. In this case, you will ask a question regarding the weather in different cities. Then the code includes the instructions to check if they are available. Suitable predefined functions. So let's continue with our development to get you to create a second database agent. So basically what we are saying is that we will be defining a message right, from the user is like the human the message that we had before. But basically a message that we are sending that is containing what's the weather like in San Francisco, New York and Las Vegas? And then we tell the system that there is this get current weather function and how to use it, and what's the kind of behavior right, of the function and what is the required information that we need is the location. So we are teaching the system how to use the function. And having more pieces. Here, we are saying is that we will have the same way we have the message, we have the response, and the response is based on the chat completion interaction. The model. And the message is what we have here. And the message is depending on the notion of the tools the tools are just to explain to a chat completion engine that we are using these tools and they will rely on existing functions. Okay. So we are saying this is interaction. These are the tools you can use. Perfect. Right now we are playing with a function for the weather. But after that, we will be applying this to our database. And again you have all the content on the notebook but you can modify stats, you can change the prompts. same as before. You can just do this. But now this part is important. Now because we are saying okay, the response message will be a based on the message that we had before. And while the response that we have before that contains the message, the model, the tools, etcetera. And then we are saying tool calls, if we call this tool, basically we need to say, hey, if you are using this kind of functions for whatever purpose, you should know that there are available functions here. They get current weather, and this is related to the one that we have created before. Right? So you are telling the system you could use this. In this example because this is the first one. You have only one function. But the reality is the system is going to have like a I don't know, 10,20 different functions with different behaviors, different results. And you will guide the system to choose one or the other depending on the on the prompt that you will get. So let's see what the result of these interactions. That we are calling the function. Okay. So basically here we are getting information and I understand that here is difficult to read. But you can check it from the actual notebook. Basically, we are saying okay, we are looking for information from San Francisco, from New York, from the other city. And basically this information we are getting and we are using the get current weather. So this is good because we are understanding that this system based on the prompt that we said before, which is let me get to get the current weather in different locations, will be performing different calls here. So you may think, okay, we have already the result, what we are looking for. We are interacting with function. But no. This process follows a two step kind of scenario. The first one is what we just did, which is just telling the system what we have available. And this is the system based on the problem that we have said. As we were asking for the information from three different locations, we have called the function three different times the same function because we have only one for different locations. So at this point, the system knows that you have to use this function and that you will use it three times. And then based on this information, what we will do is to say, hey, now go and actually retrieve the answer we are looking for. So we are creating a second response kind of parameter. And we are saying, okay, based on the information we have, based on the messages that are explaining what we have to do and the model that we have on Azure, OpenAI, let us create a chat completion. So you are building the request, the final request based on the instructions that the system learned from the proper functions to be used. So basically now we are getting a chat completion kind of answer. We are saying okay, we get information. We have the finished result, which is a stop. We say that we could be a stop because the request is complete, or we could have a content safety kind of filter if there is something. But you have more information here about these kind of content filters for Yeah, for hate, for violence. And we are calling three times and we are getting the current weather in San Francisco is 50 degrees. This was an interesting example. Let's go now with the database. Remember we are doing that on lesson three is basically here. We are recording the information from the CSV file. We are putting that in data frame. Now we will have all our information. So this is good. We have reloaded again the information of the CSV file into SQL Lite kind of instance. We have it here. Good. What are we doing now? Well, we'll be creating, same as we create before, a function for the weather. That's not useful for this purpose. We'll create here, is a couple of functions that are related to the content that we have on the database. Right. So we can educate the agent. But basically we have get hospitalized increase for the state and the specific date. Very similar to what we did before. But now we add in some of the structure, some parameters, we can add here the state, we can add the specified date. These are the expected kind of parameters. Right. And we are saying okay for that kind of request, is your natural language request is talking about getting hospitalizations. Then we will need to teach the assistant to go to a right to this kind of a function. And this function will play or will run the query this SQL query by using these kind of parameters. So we are becoming more deterministic. We are controlling the output of the system, which in an implementation probably is what we are looking for. Right? To be more precise and more aligned with what we are expecting. So basically here we are saying that we are reading the SQL, we are connected to SQL, we are sending the query, we are performing the query, and then we are returning the result. The second one is for positive cases. Why we are bringing a second function. So you see that we actually have several functions and these kind of implementations. The previous example we were there was one function. Here we are adding two. And here we are talking about positive cases for a specific state, so that the dynamic is very similar. You can post here and take a look and check the details. But basically we are saying we have two cases hospitalized and positive. So, we have the function. So let me just try to see that this is working. Perfect. So this is working. That means that all the previous steps are there. So we are saying that for Alaska, for this specific date, we have three hospitalized people. But again there is no magic on this. And just calling the the function directly. We are not using natural language. Now as we this is where we will be starting to define how to use this. So we will follow the same kind of procedure. We'll create the user message. I'll put here a look very familiar to what we have before. So basically and now we will go more into detail than before. We are saying like, if we have something that is related to how many hospitalized people we had in Alaska in 2021, what they were the month we should follow this pattern. So this is once again giving an example to the system. So the agent understands that when you are getting some question that is similar to these hospitalized, or when you are saying like, get positive cases, right, like this one retrieve, you will be following these function and not the other one. Right? So we are just teaching the system to leverage the function that we have previously created. We will add the response. Then in this case the response. What we are saying is we are leveraging the model. We are leveraging the messages inside user message with a question about hospitalized people in the specific location will use the tools that we have defined. So hey, for one other purpose, you have this function is available and that's all. Now let's go into the first part of the two step process that we mentioned. This one is to say, hey, if we get a call, if we are using these tools because we are recognizing the intent of the user from the natural language request, we need to consider that the available functions in this case is get positive cases from the state, and date and get the hospitalized increase and then the assistant should figure out. Okay? This is not like calling one or the other is just asking the question and the agent will work internally to decide one or the other. And, okay, we have the same kind of, of, specification. Here we have the functional arguments that are the state and the date. We prepare everything to work. Let me play here. Okay. So if you remember from the previous one, what we are doing here is to realize the necessary functions that we need to call to find the results. For what? For the system to answer once again to this message. And this message is about hospitalizations in Alaska. And the 5th March 2021. We are able to send the request and the system will choose the function and prepare the arguments. But once again, this doesn't mean that we have already the answer to that query here is just the internal thought of the system. Same as the LangChain. Agents were explaining their thought and the thought process, right? Here, in order to get the answer, will use the second response. So you see the chat completion, the answer basically to our prompt is containing an answer that says On March 5th in 2021, there were three additional hospitalizations due to Covid 19 reported in Alaska. And we are seeing that the reason is to stop. So there is no filter. We don't have any of the filters activated. They are all false. Remember the filters for content safety? I'll give you seven seconds, review everything, review the system and you can even try you can even try with a different message and see the results. And State of Alaska can be Arizona can be Texas can be another date. Or you could try obviously with, something else related to the second function, which is the positive cases. Just take your time. I will go to the last lesson, and create the last piece of our wonderful database agent.