Prompt Chain

Integrate Azure OpenAI with External Functions

Jupyter notebook demonstrating function calling with Azure OpenAI service for GPT-4 and GPT-3.5-turbo models.

Works with azureopenai

87
Spark score
out of 100
Updated 3 months 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)

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).

02
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.

03
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.

04
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.

Overview

Azure functions example

What it does

A Jupyter notebook tutorial

How it connects

when you need to implement function calling with Azure OpenAI chat models

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.

Step 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).

Step 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.

Step 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.

Step 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.

Discussion

Questions & comments · 0

Sign In Sign in to leave a comment.