From the course: Gemini API: Tool and Function Calling
Function calling in action - Gemini Tutorial
From the course: Gemini API: Tool and Function Calling
Function calling in action
- [Instructor] Let's kick things off by running a simple function calling example. Here in VS code, I have a Python script that prompts a Gemini model for an addition operation. But instead of trying to use the model's knowledge base to answer the question, the model delegates this operation to a add numbers function that we have written. This has been achieved by providing the model with a declaration of the function and also defining the actual function to be called. But don't worry about how this works yet. That's what you'll be learning step by step throughout this course and even more. To run our example, you need to make sure that you have the Gemini API key as an environment variable. We're going to be learning how to do that very, very soon. For this demo, I've already added the environment variable, so that you can see function calling in action. If you scroll through the code, you can see the actual function that runs the operation. You can see how we are passing the function to the Gemini model and down here, you can see our request. And when the response comes back, we run the function. Now, let's see this in action and pull up my command line, and I'm just going to run Python. The name of the file is intro example, so we'll run intro example.py. Hit Enter, and we have some print statements. Welcome to Gemini function calling. This example shows how Gemini can call functions to solve problems. So, our function is add numbers. What does it do? It adds two numbers together, and now, we have given Gemini to use this function. The user asks, what is 25 + 37? Gemini thinks I need to call the add numbers function. Then it calls and returns it, gives us this argument, second number 37, first number 25, which are the two numbers in our prompt. And then the function is executed and returns to number 62 as a submission of 25 and 37. And there we have our result. How amazing is that? Now, this may not look like much at first glance, but it is more about what's happening here. We just used a local function to execute a request that was sent to the AI model using natural language. We just created a bridge between a powerful language processing and response generating model and an actual, real-world operation. This way, Gemini models can take requests to perform actions and use code, APIs, or any other available tool to interact with their environment in order to fulfill a task. This is the power of function calling. No more chat-only AI applications. Now, we have powerful assistant that can actually perform reward operations. In the next video, we'll be taking a look at how this capability works in order to understand the inner workings of our function calling creates a bridge between AI models and their environment.