PSOpenAI supports for Azure OpenAI Service.
For the following sections to work properly we first have to setup some things.
If you don't create an Azure OpenAI resource yet, you need to create it by following steps.
- Go to https://portal.azure.com/#create/Microsoft.CognitiveServicesOpenAI
- Fill out all mandatory parameters. then create resource.
- Go to resource page that has been created.
You have to get the access token and endpoint name to call the API.
- Go to resource page that has been created.
- Click on [Keys and Endpoint]
- Find your API key and Endpoint name.
- Set these to the variables for using by script.
Note: Two keys are provided as standard for rotation, but only one of them is needed.
Import-Module ..\PSOpenAI.psd1
$AuthType = 'azure'
$env:OPENAI_API_KEY = '<Put your api key here>'
$env:OPENAI_API_BASE = 'https://<resource-name>.openai.azure.com/'
You can get a user-based token from Entra ID by logging in with the Az.Accounts PowerShell module or Azure CLI tools. This way you are secured by MFA and no need for a API Key.
Users logging in with Entra ID must be assigned a Cognitive Services User
role or higher privileges.
Roles can be assigned from the [Access Control (IAM)] in the resource page.
# To run the following code, you need to install Az.Accounts PowerShell module.
# Install-Module Az.Accounts
Import-Module Az.Accounts
# Login with Entra ID
Connect-AzAccount
# Retrive access token
$MyToken = Get-AzAccessToken -ResourceUrl 'https://cognitiveservices.azure.com'
# Set to variables
$AuthType = 'azure_ad' # You need to set AuthType as "azure_ad".
$env:OPENAI_API_KEY = $MyToken.Token
$env:OPENAI_API_BASE = 'https://<resource-name>.openai.azure.com/'
In Azure, the AI model to be used must be deployed under an arbitrary name.
- Go to Azure OpenAI Studio page.
- Click on the [Deployments]
- Click [Create new deployment], give it a name, select a model and version, then click [Create].
$DeploymentName = '<Put your deployment name here>'
Now let's send a sample chat completion to the deployment.
# Need to set these variables properly in the above codes.
# $AuthType
# $DeploymentName
# $env:OPENAI_API_KEY
# $env:OPENAI_API_BASE
Request-ChatCompletion `
-ApiType Azure ` # This parameter switches to the Azure API.
-Message 'Hello Azure OpenAI Service.' `
-Model $DeploymentName `
-AuthType $AuthType