Azure OpenAI
This notebook goes over how to use Langchain with Azure OpenAI.
The Azure OpenAI API is compatible with OpenAIβs API. The openai
Python package makes it easy to use both OpenAI and Azure OpenAI. You
can call Azure OpenAI the same way you call OpenAI with the exceptions
noted below.
API configurationβ
You can configure the openai
package to use Azure OpenAI using
environment variables. The following is for bash
:
# Set this to `azure`
export OPENAI_API_TYPE=azure
# The API version you want to use: set this to `2023-05-15` for the released version.
export OPENAI_API_VERSION=2023-05-15
# The base URL for your Azure OpenAI resource. You can find this in the Azure portal under your Azure OpenAI resource.
export OPENAI_API_BASE=https://your-resource-name.openai.azure.com
# The API key for your Azure OpenAI resource. You can find this in the Azure portal under your Azure OpenAI resource.
export OPENAI_API_KEY=<your Azure OpenAI API key>
Alternatively, you can configure the API right within your running Python environment:
import os
os.environ["OPENAI_API_TYPE"] = "azure"
Azure Active Directory Authenticationβ
There are two ways you can authenticate to Azure OpenAI: - API Key - Azure Active Directory (AAD)
Using the API key is the easiest way to get started. You can find your API key in the Azure portal under your Azure OpenAI resource.
However, if you have complex security requirements - you may want to use Azure Active Directory. You can find more information on how to use AAD with Azure OpenAI here.
If you are developing locally, you will need to have the Azure CLI
installed and be logged in. You can install the Azure CLI
here.
Then, run az login
to log in.
Add a role an Azure role assignment Cognitive Services OpenAI User
scoped to your Azure OpenAI resource. This will allow you to get a token
from AAD to use with Azure OpenAI. You can grant this role assignment to
a user, group, service principal, or managed identity. For more
information about Azure OpenAI RBAC roles see
here.
To use AAD in Python with LangChain, install the azure-identity
package. Then, set OPENAI_API_TYPE
to azure_ad
. Next, use the
DefaultAzureCredential
class to get a token from AAD by calling
get_token
as shown below. Finally, set the OPENAI_API_KEY
environment variable to the token value.
import os
from azure.identity import DefaultAzureCredential
# Get the Azure Credential
credential = DefaultAzureCredential()
# Set the API type to `azure_ad`
os.environ["OPENAI_API_TYPE"] = "azure_ad"
# Set the API_KEY to the token from the Azure credential
os.environ["OPENAI_API_KEY"] = credential.get_token("https://cognitiveservices.azure.com/.default").token
The DefaultAzureCredential
class is an easy way to get started with
AAD authentication. You can also customize the credential chain if
necessary. In the example shown below, we first try Managed Identity,
then fall back to the Azure CLI. This is useful if you are running your
code in Azure, but want to develop locally.
from azure.identity import ChainedTokenCredential, ManagedIdentityCredential, AzureCliCredential
credential = ChainedTokenCredential(
ManagedIdentityCredential(),
AzureCliCredential()
)
Deploymentsβ
With Azure OpenAI, you set up your own deployments of the common GPT-3 and Codex models. When calling the API, you need to specify the deployment you want to use.
*Note: These docs are for the Azure text completion models. Models
like GPT-4 are chat models. They have a slightly different interface,
and can be accessed via the AzureChatOpenAI
class. For docs on Azure
chat see Azure Chat OpenAI
documentation.*
Letβs say your deployment name is text-davinci-002-prod
. In the
openai
Python API, you can specify this deployment with the engine
parameter. For example:
import openai
response = openai.Completion.create(
engine="text-davinci-002-prod",
prompt="This is a test",
max_tokens=5
)
!pip install openai
import os
os.environ["OPENAI_API_TYPE"] = "azure"
os.environ["OPENAI_API_VERSION"] = "2023-05-15"
os.environ["OPENAI_API_BASE"] = "..."
os.environ["OPENAI_API_KEY"] = "..."
# Import Azure OpenAI
from langchain.llms import AzureOpenAI
# Create an instance of Azure OpenAI
# Replace the deployment name with your own
llm = AzureOpenAI(
deployment_name="td2",
model_name="gpt-3.5-turbo-instruct",
)
# Run the LLM
llm("Tell me a joke")
"\n\nWhy couldn't the bicycle stand up by itself? Because it was...two tired!"
We can also print the LLM and see its custom print.
print(llm)
AzureOpenAI
Params: {'deployment_name': 'text-davinci-002', 'model_name': 'text-davinci-002', 'temperature': 0.7, 'max_tokens': 256, 'top_p': 1, 'frequency_penalty': 0, 'presence_penalty': 0, 'n': 1, 'best_of': 1}