Azure OpenAI function calling
Quick answer
Use the
AzureOpenAI client from the openai package with your Azure endpoint and deployment name. Pass functions and function_call parameters in chat.completions.create() to enable function calling in Azure OpenAI.PREREQUISITES
Python 3.8+Azure OpenAI API keyAzure OpenAI endpoint URLpip install openai>=1.0
Setup
Install the official openai Python package version 1.0 or higher. Set environment variables for your Azure OpenAI API key, endpoint, and deployment name. Use the AzureOpenAI client to connect.
pip install openai>=1.0 Step by step
This example demonstrates how to call a function using Azure OpenAI's function calling feature. Define your function schema, then pass it in the functions parameter. Use function_call="auto" to let the model decide when to call the function.
import os
from openai import AzureOpenAI
client = AzureOpenAI(
api_key=os.environ["AZURE_OPENAI_API_KEY"],
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
api_version="2024-02-01"
)
def main():
functions = [
{
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "The city and state, e.g. San Francisco, CA"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
},
"required": ["location"]
}
}
]
messages = [
{"role": "user", "content": "What is the weather like in Seattle?"}
]
response = client.chat.completions.create(
model=os.environ["AZURE_OPENAI_DEPLOYMENT"],
messages=messages,
functions=functions,
function_call="auto"
)
message = response.choices[0].message
print("Model response:", message.content)
if message.function_call:
print("Function call requested:", message.function_call)
if __name__ == "__main__":
main() output
Model response:
Function call requested: {'name': 'get_current_weather', 'arguments': '{"location": "Seattle", "unit": "fahrenheit"}'} Common variations
- Use
function_call="none"to disable function calling. - Specify
function_call={"name": "function_name"}to force a specific function call. - Use async Python with
asyncioandawaitonclient.chat.completions.acreate(). - Change the model deployment name via environment variable for different Azure OpenAI models.
import asyncio
import os
from openai import AzureOpenAI
client = AzureOpenAI(
api_key=os.environ["AZURE_OPENAI_API_KEY"],
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
api_version="2024-02-01"
)
async def async_main():
functions = [
{
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
},
"required": ["location"]
}
}
]
messages = [{"role": "user", "content": "Tell me the weather in Boston."}]
response = await client.chat.completions.acreate(
model=os.environ["AZURE_OPENAI_DEPLOYMENT"],
messages=messages,
functions=functions,
function_call={"name": "get_current_weather"}
)
message = response.choices[0].message
print("Async model response:", message.content)
if message.function_call:
print("Function call:", message.function_call)
if __name__ == "__main__":
asyncio.run(async_main()) output
Async model response:
Function call: {'name': 'get_current_weather', 'arguments': '{"location": "Boston", "unit": "celsius"}'} Troubleshooting
- If you get authentication errors, verify your
AZURE_OPENAI_API_KEYandAZURE_OPENAI_ENDPOINTenvironment variables. - If function calling is ignored, ensure your model deployment supports function calling and you use
api_version="2024-02-01"or later. - Check that your function schema is valid JSON Schema and includes required fields.
Key Takeaways
- Use
AzureOpenAIclient withfunctionsandfunction_callparams to enable function calling. - Set
api_version="2024-02-01"or later for function calling support in Azure OpenAI. - You can control function calls with
function_call="auto","none", or specify a function name. - Async calls are supported via
acreate()method on the client. - Validate your function JSON schema carefully to avoid errors.