Prompt Chain

Integrate Azure OpenAI with External Functions

A notebook showing Azure OpenAI function calling end to end: API key or Active Directory auth, function definition, and response handling.

Works with azureopenai

69
Spark score
out of 100
Updated 12 days ago
Version 1.0.0
Models
gpt 4ogpt 4

Add to Favorites

Why it matters

Enable your AI models to interact with external tools and data sources by leveraging Azure OpenAI's function calling capabilities. Extend model functionality into your existing systems and workflows.

Outcomes

What it gets done

01

Define custom functions for the AI model.

02

Pass function definitions to the Azure OpenAI API.

03

Process function calls and their arguments.

04

Feed function responses back to the AI for continued conversation.

Install

Add it to your toolbox

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/oai-functions | bash

Steps

Steps in the chain

01
Define the function(s)
02
Pass function definition(s) into chat completions API
03
Call function with arguments from the response
04
Feed function response back into chat completions API

Overview

Azure functions example

A notebook showing Azure OpenAI function calling end to end: Azure API key or Active Directory authentication, function definition with JSON schema, and feeding results back into the model. Use it as a reference for wiring function calling into an Azure OpenAI deployment specifically, covering Azure's own authentication paths.

What it does

Demonstrates function calling with the Azure OpenAI service, letting a chat completions caller define capabilities the model can use to reach external tools and data sources - a capability that requires model versions labeled -0613 or later on gpt-4 and gpt-35-turbo (older versions don't support it). After setting up an Azure resource and retrieving its endpoint from the Azure Portal's Keys and Endpoints section, the notebook covers two authentication paths: an Azure API key (api_type=azure, api_key from the portal, ideally set via environment variables), or Microsoft Active Directory authentication, where a time-limited token needs to be refreshed by hooking into requests.auth before it expires. The function-calling flow itself has four steps: define one or more functions with a name, optional description, and a JSON-schema parameter definition; pass those definitions into the chat completions API, where the model either auto-decides (function_call="auto", the default) or is forced (function_call={"name": ...}) to call one, surfacing a finish_reason of "function_call" plus the chosen function and its arguments; call that function locally with the JSON arguments matching the defined schema; and feed the function's result back into the chat completions API as a new message with role="function", so the model can incorporate it into its final answer.

When to use - and when NOT to

Use it as a reference for wiring function calling into an Azure OpenAI deployment specifically - covering Azure's own authentication paths, API key or Active Directory - rather than the standard OpenAI API's function-calling setup, which differs in configuration even though the calling pattern itself is the same.

The notebook also flags that a newer version of the openai Python library is available, pointing to the library's own GitHub discussion for migration details rather than assuming this exact setup is current.

Inputs and outputs

Input is an Azure OpenAI resource endpoint, authentication credentials (API key or Active Directory token), and one or more function definitions with JSON-schema parameters. Output is a chat completion that either answers directly or returns a function_call with the function name and arguments to execute, and, after the function runs, a final model answer incorporating the function's result.

Integrations

Built on the Azure OpenAI Service and the OpenAI Python SDK configured for Azure (api_type, api_base, api_key, api_version), with Active Directory as an alternative authentication path to API keys.

Who it's for

For developers wiring OpenAI function calling into an Azure-hosted deployment who need the Azure-specific authentication and configuration steps, not just the generic function-calling pattern.

OPENAI_API_BASE
OPENAI_API_KEY
OPENAI_API_TYPE
OPENAI_API_VERSION
Source README

Azure functions example

Note: There is a newer version of the openai library available. See https://github.com/openai/openai-python/discussions/742

This notebook shows how to use the function calling capability with the Azure OpenAI service. Functions allow a caller of chat completions to define capabilities that the model can use to extend its
functionality into external tools and data sources.

You can read more about chat functions on OpenAI's blog: https://openai.com/blog/function-calling-and-other-api-updates

NOTE: Chat functions require model versions beginning with gpt-4 and gpt-35-turbo's -0613 labels. They are not supported by older versions of the models.

Setup

First, we install the necessary dependencies.

Additionally, to properly access the Azure OpenAI Service, we need to create the proper resources at the Azure Portal (you can check a detailed guide on how to do this in the Microsoft Docs)

Once the resource is created, the first thing we need to use is its endpoint. You can get the endpoint by looking at the "Keys and Endpoints" section under the "Resource Management" section. Having this, we will set up the SDK using this information:

Authentication

The Azure OpenAI service supports multiple authentication mechanisms that include API keys and Azure credentials.

Authentication using API key

To set up the OpenAI SDK to use an Azure API Key, we need to set up the api_type to azure and set api_key to a key associated with your endpoint (you can find this key in "Keys and Endpoints" under "Resource Management" in the Azure Portal)

Note: In this example, we configured the library to use the Azure API by setting the variables in code. For development, consider setting the environment variables instead:

OPENAI_API_BASE
OPENAI_API_KEY
OPENAI_API_TYPE
OPENAI_API_VERSION
Authentication using Microsoft Active Directory

Let's now see how we can get a key via Microsoft Active Directory Authentication.

A token is valid for a period of time, after which it will expire. To ensure a valid token is sent with every request, you can refresh an expiring token by hooking into requests.auth:

Functions

With setup and authentication complete, you can now use functions with the Azure OpenAI service. This will be split into a few steps:

  1. Define the function(s)
  2. Pass function definition(s) into chat completions API
  3. Call function with arguments from the response
  4. Feed function response back into chat completions API
1. Define the function(s)

A list of functions can be defined, each containing the name of the function, an optional description, and the parameters the function accepts (described as a JSON schema).

2. Pass function definition(s) into chat completions API

Now we can pass the function into the chat completions API. If the model determines it should call the function, a finish_reason of "function_call" will be populated on the choice and the details of which function to call and its arguments will be present in the message. Optionally, you can set the function_call keyword argument to force the model to call a particular function (e.g. function_call={"name": get_current_weather}). By default, this is set to auto, allowing the model to choose whether to call the function or not.

3. Call function with arguments from the response

The name of the function call will be one that was provided initially and the arguments will include JSON matching the schema included in the function definition.

4. Feed function response back into chat completions API

The response from the function should be serialized into a new message with the role set to "function". Now the model will use the response data to formulate its answer.

FAQ

Common questions

Discussion

Questions & comments · 0

Sign In Sign in to leave a comment.