top of page

Function Calling with Azure OpenAI Service

Updated: Sep 14, 2023

Imagine you have a powerful tool like Azure OpenAI Service, but using it effectively can be a bit like solving a puzzle. The problem we're tackling is how to make it easy for people to use the service and call functions without getting lost in complexity.

Function Calling with Azure OpenAI Service

So, let's explore how we're simplifying this process to make harnessing the AI magic of Azure OpenAI Service a breeze!


Table of Contents:

STEP 1: Define a Function

STEP 2: Create a Request to Azure OpenAI Service API

STEP 3: Parse the response from the API

Limitations of Function Calling


What is Function Calling?

Function calling is a feature of the Azure OpenAI Service that allows you to call external functions from within your conversations with the model. This can be used to extend the capabilities of the model and to provide more complex and sophisticated responses.


To use function calling, you first need to define the functions that you want to call. These functions can be written in any language that the Azure OpenAI Service supports, such as Python, JavaScript, or Java. Once you have defined your functions, you can make them available to the model by uploading them to the Azure OpenAI Service.


When you are having a conversation with the model, you can tell the model to call a function by using the function_call prompt. For example, you could say "Tell me the weather in San Francisco" and then use the function_call prompt to call a function that retrieves the weather forecast from a weather API.


Also Read:


Why use Function calling with Azure OpenAI Service

There are several reasons why you might want to use function calling with Azure OpenAI Service:

  1. It allows you to extend the capabilities of the model. For example, you could use function calling to add the ability to translate languages, search the web, or perform other tasks that are not natively supported by the model.

  2. It can be used to provide more complex and sophisticated responses. For example, you could use function calling to generate a personalized birthday message or to create a custom story.

  3. It can be used to improve the efficiency of your conversations with the model. For example, you could use function calling to cache the results of frequently-used functions, so that the model does not have to re-compute them each time you ask for them.


How to call a function with Azure OpenAI Service

Let's walk through the process of invoking a function using Azure OpenAI Service with these steps.


STEP 1: Define the function

The first step is to define the function that you want to call. The function can be any Python function that takes some input and returns some output. The function should be defined in a file that is accessible to the Azure OpenAI Service API.


Here is an example of a function that you can use to search for hotels:

def search_hotels(city, number_of_rooms):
    """     
    Searches for hotels in the specified city and returns a list of hotels.      
    
    Args:         
        city: The city to search for hotels in.         
        number_of_rooms: The number of rooms to search for.      
    
    Returns:         
        A list of hotels.     
    """
    
    # Make a request to the hotel search API.     
    response = requests.get("https://api.example.com/hotels", params={         
        "city": city,         
        "number_of_rooms": number_of_rooms     
    })      
    
    # Parse the response.     
    hotels = response.json()["hotels"]      

    return hotels 

STEP 2: Create a request to the Azure OpenAI Service API

The next step is to create a request to the Azure OpenAI Service API. The request should include the following information:

  • The ID of the deployment that you want to use

  • The list of messages that you want to send to the model

  • The list of functions that you want to make available to the model

  • The function_call parameter, which specifies whether the model should automatically call a function

Here is an example of a request to the Azure OpenAI Service API:

import openai  

# Define the deployment ID. 
deployment_id = "gpt-35-turbo-0613"

# Define the messages. 
messages = ["I want to find a hotel in New York City"]  

# Define the functions. 
functions = [search_hotels]  

# Create the request. 
response = openai.ChatCompletion.create(     
    deployment_id=deployment_id,     
    messages=messages,     
    functions=functions,     
    function_call="auto" 
) 

STEP 3: Parse the response from the API

The final step is to parse the response from the API. The response will be a JSON object that contains the following information:

  • The list of choices that the model generated

  • The confidence scores for each choice

  • The function_call property, which specifies whether the model called a function

If the model called a function, the function_call property will be set to true. The response will also include a function_response property, which contains the response from the function.

Here is an example of how to parse the response from the API:

# Get the list of choices. 
choices = response["choices"]  

# Get the confidence scores for each choice. 
confidence_scores = [choice["confidence"] for choice in choices]  

# Check if the model called a function.
if response["function_call"]:      
    # Get the function name.     
    function_name = response["function_call"]["name"]      
    
    # Get the function response.     
    function_response = response["function_response"]

Different Ways to define a function for Azure OpenAI Service

There are two different ways to define a function for Azure OpenAI Service:

  • Using a function file: This is the most common way to define a function. A function file is a Python file that contains the definition of the function. The function file must be uploaded to the Azure OpenAI Service before it can be used.

  • Using a function object: This is a less common way to define a function. A function object is a Python object that represents a function. The function object can be created in your code and then passed to the Azure OpenAI Service.

Here is an example of a function file that defines a function to get the weather forecast for a city:

def get_weather_forecast(city):
    """     Get the weather forecast for the specified city.      
    Args:         
        city: The city to get the weather forecast for.      
    Returns:         
        A string containing the weather forecast.     
    """
    
    # Make a request to the weather API.     
    response = requests.get("https://api.example.com/weather", params={         
        "city": city     
    })      
    
    # Parse the response.     
    weather_forecast = response.json()["weather_forecast"]  
        
    return weather_forecast 

This function file can be uploaded to the Azure OpenAI Service and then used to get the weather forecast for a city.


Here is an example of how to define a function object in your code:

def get_weather_forecast_function(city):
    """     
    Get the weather forecast for the specified city.      
    
    Args:         
        city: The city to get the weather forecast for.
              
    Returns:         
        A string containing the weather forecast.     
    """
    
    # Make a request to the weather API.     
    response = requests.get("https://api.example.com/weather", params={         
        "city": city     
    })      
    
    # Parse the response.     
    weather_forecast = response.json()["weather_forecast"]  
        
    return weather_forecast   

function_object = get_weather_forecast_function 

This function object can then be passed to the Azure OpenAI Service to get the weather forecast for a city.


The best way to define a function for Azure OpenAI Service depends on your specific needs. If you need to define a complex function that requires multiple lines of code, then it is best to define a function file. If you need to define a simple function that can be defined in a few lines of code, then it is best to define a function object.



Types of responses that the Azure OpenAI Service API can return

The Azure OpenAI Service API can return different types of responses, depending on the request that is made.

  • Choices: This is the most common type of response. It returns a list of possible responses, along with their confidence scores. The client can then choose the response that they want to use.

  • Completion: This type of response returns a single string that is generated by the model. The client can then use this string as they see fit.

  • Logs: This type of response returns a log of the steps that the model took to generate its response. This can be useful for debugging or for understanding how the model works.

  • Function call: This type of response indicates that the model has called an external function. The response will contain the name of the function that was called and the response from the function.


The different ways to parse the response from the API depend on the type of response that is returned.


1. Choices: The response can be parsed using the choices property. This property is a list of objects, each of which contains the following information:

  • text: The text of the response.

  • confidence: The confidence score for the response.

2. Completion: The response can be parsed using the content property. This property is a string that contains the generated text.


3. Logs: The response can be parsed using the logs property. This property is a list of objects, each of which contains the following information:

  • step: The step number.

  • action: The action that was taken.

  • inputs: The inputs to the action.

  • outputs: The outputs from the action.


4. Function call: The response can be parsed using the function_call property. This property is an object that contains the following information:

  • name: The name of the function that was called.

  • arguments: The arguments that were passed to the function.

  • response: The response from the function.


Security considerations for using function calling with Azure OpenAI Service

Here are some security considerations for using function calling with Azure OpenAI Service:


1. Sensitive data: When you use function calling, you are essentially giving the model access to the code in your function. This means that the model could potentially access sensitive data, such as passwords or credit card numbers if it is not properly secured.


For example, if you are writing a function that retrieves the user's password from a database, you should make sure that the function is properly secured so that the model cannot access the password.


2. Malicious code: If you are not careful, you could accidentally upload malicious code to the Azure OpenAI Service. This code could then be executed by the model, which could potentially damage your system or steal your data.


For example, if you are writing a function that takes user input, you should make sure that the function is properly sanitized so that the model cannot execute malicious code.


3. Unauthorized access: If you do not properly secure your function, it could be accessed by unauthorized users. This could allow them to execute malicious code or steal your data.


For example, if you are writing a function that is accessible to the public, you should make sure that the function is properly secured so that only authorized users can access it.


Here are some additional tips for using function calling safely and securely:

  • Only call functions that you trust. This means that you should only call functions that have been written by trusted developers and that have been properly reviewed for security vulnerabilities.

  • Use secure coding practices when writing your functions. This means that you should use secure coding practices, such as input validation and escaping, to help protect your functions from malicious code.

  • Encrypt sensitive data before it is passed to the function. This means that you should encrypt sensitive data, such as passwords or credit card numbers before it is passed to the function. This will help to protect the data from being stolen if the function is compromised.

  • Use a firewall to protect your system from unauthorized access. This means that you should use a firewall to block unauthorized users from accessing your system.

  • Monitor your system for suspicious activity. This means that you should monitor your system for suspicious activity, such as unauthorized access or changes to your functions. This will help you to detect and respond to security incidents quickly.


Limitations of function calling with Azure OpenAI Service

Here are some of the limitations of function calling with Azure OpenAI Service:

  • Performance: Function calling can add latency to your conversations with the model. This is because the model has to first call the function and then wait for the function to return a response.

  • Complexity: Function calling can make your conversations with the model more complex. This is because you have to learn how to use the function calling API and you have to define the functions that you want to call.

  • Cost: Function calls can add to the cost of using the Azure OpenAI Service. This is because you are charged for the number of times that your functions are called.


Conclusion

Function calling lets us use Azure OpenAI effectively. To do this, we send it clear instructions. Simplifying this process is great because it makes AI accessible. But remember, AI has limits too, so understanding them is important for successful use.

bottom of page