How to use Azure OpenAI GPT-4o with Function calling (2024)

Introduction

In this article we will demonstrate how we leverage GPT-4o capabilities, using images with function calling to unlock multimodal use cases.

We will simulate a package routing service that routes packages based on the shipping label using OCR with GPT-4o.

The model will identify the appropriate function to call based on the image analysis and the predefined actions for routing to the appropriate continent.

Background

The new GPT-4o (“o” for “omni”) can reason across audio, vision, and text in real time.

  • It can respond to audio inputs in as little as 232 milliseconds, with an average of 320 milliseconds, which is similar tohuman response timein a conversation.
  • It matches GPT-4 Turbo performance on text in English and code, with significant improvement on text in non-English languages, while also being much faster and 50% cheaper in the API.
  • GPT-4o is especially better at vision and audio understanding compared to existing models.
  • GPT-4o now enables function calling.

The application

We will run a Jupyter notebook that connects to GPT-4o to sort packages based on the printed labels with the shipping address.

Here are some sample labels we will be using GPT-4o for OCR to get the country this is being shipped to and GPT-4o functions to route the packages.

How to use Azure OpenAI GPT-4o with Function calling (1)

How to use Azure OpenAI GPT-4o with Function calling (2)

How to use Azure OpenAI GPT-4o with Function calling (3)

The environment

The code can be found here - Azure OpenAI code examples

Make sure you create your python virtual environment and fill the environment variables as stated in the README.md file.

The code

Connecting to Azure OpenAI GPT-4o deployment.

from dotenv import load_dotenvfrom IPython.display import display, HTML, Imageimport osfrom openai import AzureOpenAIimport jsonload_dotenv()GPT4o_API_KEY = os.getenv("GPT4o_API_KEY")GPT4o_DEPLOYMENT_ENDPOINT = os.getenv("GPT4o_DEPLOYMENT_ENDPOINT")GPT4o_DEPLOYMENT_NAME = os.getenv("GPT4o_DEPLOYMENT_NAME")client = AzureOpenAI( azure_endpoint = GPT4o_DEPLOYMENT_ENDPOINT, api_key=GPT4o_API_KEY, api_version="2024-02-01")

Defining the functions to be called after GPT-4o answers.

# Defining the functions - in this case a toy example of a shipping functiondef ship_to_Oceania(location): return f"Shipping to Oceania based on location {location}"def ship_to_Europe(location): return f"Shipping to Europe based on location {location}"def ship_to_US(location): return f"Shipping to Americas based on location {location}"

Defining the available functions to be called to send to GPT-4o.

It is very IMPORTANT to send the function's and parameters descriptions so GPT-4o will know which method to call.

tools = [ { "type": "function", "function": { "name": "ship_to_Oceania", "description": "Shipping the parcel to any country in Oceania", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The country to ship the parcel to.", } }, "required": ["location"], }, }, }, { "type": "function", "function": { "name": "ship_to_Europe", "description": "Shipping the parcel to any country in Europe", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The country to ship the parcel to.", } }, "required": ["location"], }, }, }, { "type": "function", "function": { "name": "ship_to_US", "description": "Shipping the parcel to any country in the United States", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The country to ship the parcel to.", } }, "required": ["location"], }, }, },]available_functions = { "ship_to_Oceania": ship_to_Oceania, "ship_to_Europe": ship_to_Europe, "ship_to_US": ship_to_US,}

Function to base64 encode our images, this is the format accepted by GPT-4o.

# Encoding the images to send to GPT-4-Oimport base64def encode_image(image_path): with open(image_path, "rb") as image_file: return base64.b64encode(image_file.read()).decode("utf-8")

The method to call GPT-4o.

Notice below that we send the parameter "tools" with the JSON describing the functions to be called.

def call_OpenAI(messages, tools, available_functions): # Step 1: send the prompt and available functions to GPT response = client.chat.completions.create( model=GPT4o_DEPLOYMENT_NAME, messages=messages, tools=tools, tool_choice="auto", ) response_message = response.choices[0].message # Step 2: check if GPT wanted to call a function if response_message.tool_calls: print("Recommended Function call:") print(response_message.tool_calls[0]) print() # Step 3: call the function # Note: the JSON response may not always be valid; be sure to handle errors function_name = response_message.tool_calls[0].function.name # verify function exists if function_name not in available_functions: return "Function " + function_name + " does not exist" function_to_call = available_functions[function_name] # verify function has correct number of arguments function_args = json.loads(response_message.tool_calls[0].function.arguments) if check_args(function_to_call, function_args) is False: return "Invalid number of arguments for function: " + function_name # call the function function_response = function_to_call(**function_args) print("Output of function call:") print(function_response) print()

Please note that WEand not GPT-4o call the methods in our code based on the answer by GTP4-o.

# call the function function_response = function_to_call(**function_args)

Iterate through all the images in the folder.

Notice the system prompt where we ask GPT-4o what we need it to do, sort labels for packages routing calling functions.

# iterate through all the images in the data folderimport osdata_folder = "./data"for image in os.listdir(data_folder): if image.endswith(".png"): IMAGE_PATH = os.path.join(data_folder, image) base64_image = encode_image(IMAGE_PATH) display(Image(IMAGE_PATH)) messages = [ {"role": "system", "content": "You are a customer service assistant for a delivery service, equipped to analyze images of package labels. Based on the country to ship the package to, you must always ship to the corresponding continent. You must always use tools!"}, {"role": "user", "content": [ {"type": "image_url", "image_url": { "url": f"data:image/png;base64,{base64_image}"} } ]} ] call_OpenAI(messages, tools, available_functions)

Let’s run our notebook!!!

How to use Azure OpenAI GPT-4o with Function calling (4)

Running our code for the label above produces the following output:

Recommended Function call:ChatCompletionMessageToolCall(id='call_lH2G1bh2j1IfBRzZcw84wg0x', function=Function(arguments='{"location":"United States"}', name='ship_to_US'), type='function')Output of function call:Shipping to Americas based on location United States

That’s all folks!

Thanks

Denise

How to use Azure OpenAI GPT-4o with Function calling (2024)

FAQs

How to use gpt-4 in Azure OpenAI? ›

Use this article to get started using the Azure OpenAI REST APIs to deploy and use the GPT-4 Turbo with Vision model.
  1. Prerequisites. An Azure subscription. ...
  2. Retrieve key and endpoint. ...
  3. Create a new Python application. ...
  4. Clean up resources.
May 1, 2024

Does Azure OpenAI support functions? ›

Azure Functions extension for OpenAI, now in public preview, enables developers to build Function applications that integrate with OpenAI.

How does GPT function calling work? ›

Once GPT determines it should call this function to generate a better response, it will reply to you (the thing/system sending/processing these Open AI API calls) with a function message that essentially tells your code - “Hey, run this function and return the results to me”.

What is function calling in ChatGPT API? ›

Function calling is a new way to use the ChatGPT API. Instead of getting a message back from the language model, you get a request to call a function. If you've ever used plugins in the ChatGPT UI, Function Calling is the feature behind the scenes that allow plugins to be integrated with the LLM's responses.

Is Azure GPT-4 faster? ›

On average, GPT-4 via OpenAI API was 2.8 times slower than Microsoft Azure.

What is the difference between GPT-4 and GPT-4 32K? ›

GPT-4 Turbo, developed by OpenAI, features a large context window of 128,000 tokens. The model costs 1.0 cent per thousand tokens for input and 3.0 cents per thousand tokens for output. It is set to be released on November 6, 2023. GPT-4 32K, developed by OpenAI, features a context window of 32768 tokens.

What is the difference between Azure OpenAI and OpenAI? ›

Wrapping Up. Overall, Azure OpenAI is a service that offers advanced language AI with OpenAI models while providing the security and enterprise promise of Azure. It is co-developed by Microsoft and OpenAI, ensuring compatibility and a smooth transition between the two.

Which two tools can you use to call the Azure OpenAI service? ›

You can use either API Keys or Microsoft Entra ID.

How to call Azure OpenAI API from Python? ›

Click Create to provision your Azure OpenAI instance.
  1. Deploy a Model: Once your Azure OpenAI instance is created, navigate to it. Under the Models section, click Deploy Model.
  2. Access Your Model Programmatically: Retrieve the necessary credentials (API keys or tokens) from your Azure OpenAI instance.
Feb 19, 2024

How to improve OpenAI function calling? ›

To ensure effective function calling in OpenAI, consider the following tips: Provide Clear and Concise System Prompts: Provide a clear and concise system prompt that describes the purpose of the chatbot and the functions it can call. This helps the model understand the context and generate more accurate responses.

How do I call GPT-4 API? ›

To use GPT-4, check OpenAI's documentation on accessing GPT-4 and ensure your API key has the necessary permissions. Verify that the model name and key are correctly specified in your API request. If issues persist, contact OpenAI support for assistance.

What is the use case of function calling in OpenAI? ›

The model uses the function call results to build a more accurate and consistent response. Potential use cases for function calling include: Answering questions by calling external APIs, for example, sending emails or getting the weather forecast.

How do I call an API using Azure function? ›

Describes how to use Azure Functions as the basis of a cohesive set of serverless APIs. Use Visual Studio to create an HTTP triggered function along with an OpenAPI definition, which enables Azure API Management integration so that other apps and services can call your serverless function-based API.

What is the difference between function call and plugin in ChatGPT? ›

The difference is: Plugins are solely for the ChatGPT User Interface. Functions are for local or remote systems outside of ChatGPT. But you can use functions also in utilizing plugin development whereas you can not directly access plugins from the API.

What is difference between API and function call? ›

The main difference are in terms of behavior and the way it use in marketing. Mostly API are complete pack of protocol,rules and functions which can be shared with third parties where as functions call are just internal calls within certain scope.

How to use GPT-4 with OpenAI API? ›

Getting Started With GPT-4
  1. Step 1: The OpenAI GPT-4 API Key. The first thing we need to do is define our GPT-4 API key. ...
  2. Step 2: Installing OpenAI and W&B. To make GPT-4 work, you only need OpenAI. ...
  3. Step 3: Import Libraries And Pass The OpenAI API Key. ...
  4. Step 4: Generating Content with GPT-4. ...
  5. Step 5: Save to Weights & Biases.
Dec 14, 2023

Is OpenAI working on GPT-4? ›

We are beginning to roll out GPT-4o to ChatGPT Plus and Team users, with availability for Enterprise users coming soon. We are also starting to roll out to ChatGPT Free with usage limits today.

What is the token limit for Azure OpenAI GPT-4? ›

Increase the max_tokens parameter value to avoid truncated responses. GPT-4o max tokens defaults to 4096.

How to integrate ChatGPT with azure? ›

Go to the ChatGPT portal and log in with your credentials. Select “Integrations” and click on “Azure Cloud Services” from the list. Finally, provide your Azure account credentials and authorize the integration of ChatGPT with Azure cloud services. That's it!

Top Articles
Latest Posts
Article information

Author: Frankie Dare

Last Updated:

Views: 6222

Rating: 4.2 / 5 (73 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Frankie Dare

Birthday: 2000-01-27

Address: Suite 313 45115 Caridad Freeway, Port Barabaraville, MS 66713

Phone: +3769542039359

Job: Sales Manager

Hobby: Baton twirling, Stand-up comedy, Leather crafting, Rugby, tabletop games, Jigsaw puzzles, Air sports

Introduction: My name is Frankie Dare, I am a funny, beautiful, proud, fair, pleasant, cheerful, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.