Using Microsoft Azure's OpenAI Endpoints
Image source: https://azure.microsoft.com/en-us/services/openai/
Did you know that Microsoft Azure provides OpenAI endpoints? With these endpoints, you get the security and enterprise promises that come with Azure! If you already use Azure for your infrastructure, then you also get the benefit of not having to manage an OpenAI account alongside your Azure account.
In this tutorial, we'll show you how to configure an Azure OpenAI endpoint with PhaseLLM.
We will skip the steps of creating an Azure account and model deployment. You can follow the tutorial about how to do that here. Once you have your model deployed, continue reading.
The Setup
If you followed the tutorial linked above, you should have a resource and a model deployment. The resource name you created, as well as the model deployment ID are used to construct the endpoint.
Broadly, there are two different methods you can use to authenticate your Azure OpenAI endpoint. The first is to use an API key, and the second is to use Azure Active Directory, Azure's identity access management solution. We'll show you how to use both methods.
Phasellm provides a set of configuration classes which clearly define the parameters needed for a particular API provider. These classes are located in the phasellm.configurations module. You can read the docs for these classes here.
To configure a PhaseLLM wrapper to use a different API provider, simply pass the appropriate configuration class to the wrapper's constructor. See the examples below.
Using an Azure API Key
from phasellm.configurations import AzureAPIConfiguration
llm = StreamingOpenAIGPTWrapper(api_config=AzureAPIConfiguration(
apikey='your-azure-api-key',
api_base='https://{your-resource-name}.openai.azure.com/',
api_version='2023-05-15',
deployment_id='your-deployment-id'
))
llm.text_completion(prompt="Hello, my name is")
Notice how we pass the AzureAPIConfiguration class to the api_config parameter of the wrapper's constructor.
Simply replace your-azure-api-key, your-resource-name, and your-deployment-id with the values you created in the Azure portal.
Using Azure Active Directory
from phasellm.configurations import AzureActiveDirectoryConfiguration
llm = StreamingOpenAIGPTWrapper(api_config=AzureActiveDirectoryConfiguration(
api_base='https://{your-resource-name}.openai.azure.com/',
api_version='2023-05-15',
deployment_id='your-deployment-id'
))
llm.text_completion(prompt="Hello, my name is")
Notice how we pass the AzureActiveDirectoryConfiguration class to the api_config parameter of the wrapper's constructor.
Simply replace your-resource-name and your-deployment-id with the values you created in the Azure portal.
The Azure Active Directory configuration is slightly more compact since you don't need to explicitly define an API key. Instead, authentication credentials will be automatically extracted from your environment. You can read more about the authentication flow here.
That's it! You should now be able to use your Azure OpenAI endpoint with PhaseLLM.
As always, reach out at w (at) phaseai (dot) com if you want to collaborate. You can also follow PhaseLLM on Twitter.