Skip to content

Latest commit

 

History

History
145 lines (99 loc) · 7.12 KB

File metadata and controls

145 lines (99 loc) · 7.12 KB
title titleSuffix description manager ms.service ms.topic ms.date
Quickstart: Use the OpenAI Service via PowerShell
Azure OpenAI Service
Walkthrough on how to get started with Azure OpenAI and make your first completions call with PowerShell.
nitinme
azure-ai-openai
include
08/28/2023

Prerequisites

Retrieve key and endpoint

To successfully make a call against the Azure OpenAI service, you'll need the following:

Variable name Value
ENDPOINT This value can be found in the Keys & Endpoint section when examining your resource from the Azure portal. You can also find the endpoint via the Deployments page in Azure AI Foundry portal. An example endpoint is: https://docs-test-001.openai.azure.com/.
API-KEY This value can be found in the Keys & Endpoint section when examining your resource from the Azure portal. You can use either KEY1 or KEY2.
DEPLOYMENT-NAME This value will correspond to the custom name you chose for your deployment when you deployed a model. This value can be found under Resource Management > Deployments in the Azure portal or via the Deployments page in Azure AI Foundry portal.

Go to your resource in the Azure portal. The Endpoint and Keys can be found in the Resource Management section. Copy your endpoint and access key as you'll need both for authenticating your API calls. You can use either KEY1 or KEY2. Always having two keys allows you to securely rotate and regenerate keys without causing a service disruption.

:::image type="content" source="../media/quickstarts/endpoint.png" alt-text="Screenshot of the overview blade for an Azure OpenAI resource in the Azure portal with the endpoint & access keys location circled in red." lightbox="../media/quickstarts/endpoint.png":::

Environment variables

Create and assign persistent environment variables for your key and endpoint.

[!INCLUDE Azure key vault]

$Env:AZURE_OPENAI_API_KEY = 'YOUR_KEY_VALUE'
$Env:AZURE_OPENAI_ENDPOINT = 'YOUR_ENDPOINT'
setx AZURE_OPENAI_API_KEY "REPLACE_WITH_YOUR_KEY_VALUE_HERE"
setx AZURE_OPENAI_ENDPOINT "REPLACE_WITH_YOUR_ENDPOINT_HERE"
echo export AZURE_OPENAI_API_KEY="REPLACE_WITH_YOUR_KEY_VALUE_HERE" >> /etc/environment && source /etc/environment
echo export AZURE_OPENAI_ENDPOINT="REPLACE_WITH_YOUR_ENDPOINT_HERE" >> /etc/environment && source /etc/environment

Create a new PowerShell script

  1. Create a new PowerShell file called quickstart.ps1. Then open it up in your preferred editor or IDE.

  2. Replace the contents of quickstart.ps1 with the following code. Modify the code to add your key, endpoint, and deployment name:

    # Azure OpenAI metadata variables
    $openai = @{
        api_key     = $Env:AZURE_OPENAI_API_KEY
        api_base    = $Env:AZURE_OPENAI_ENDPOINT # your endpoint should look like the following https://YOUR_RESOURCE_NAME.openai.azure.com/
        api_version = '2024-02-01' # this may change in the future
        name        = 'YOUR-DEPLOYMENT-NAME-HERE' #This will correspond to the custom name you chose for your deployment when you deployed a model.
    }
    
    # Completion text
    $prompt = 'Once upon a time...'
    
    # Header for authentication
    $headers = [ordered]@{
        'api-key' = $openai.api_key
    }
    
    # Adjust these values to fine-tune completions
    $body = [ordered]@{
        prompt      = $prompt
        max_tokens  = 10
        temperature = 2
        top_p       = 0.5
    } | ConvertTo-Json
    
    # Send a completion call to generate an answer
    $url = "$($openai.api_base)/openai/deployments/$($openai.name)/completions?api-version=$($openai.api_version)"
    
    $response = Invoke-RestMethod -Uri $url -Headers $headers -Body $body -Method Post -ContentType 'application/json'
    return "$prompt`n$($response.choices[0].text)"
    

    [!IMPORTANT] For production, use a secure way of storing and accessing your credentials like The PowerShell Secret Management with Azure Key Vault. For more information about credential security, see the Azure AI services security article.

  3. Run the script using PowerShell:

    ./quickstart.ps1
    

Output

The output will include response text following the Once upon a time prompt. Azure OpenAI returned There was a world beyond the mist...where a in this example.

Once upon a time...
 There was a world beyond the mist...where a

Run the code a few more times to see what other types of responses you get as the response won't always be the same.

Understanding your results

Since our example of Once upon a time... provides little context, it's normal for the model to not always return expected results. You can adjust the maximum number of tokens if the response seems unexpected or truncated.

Azure OpenAI also performs content moderation on the prompt inputs and generated outputs. The prompts or responses may be filtered if harmful content is detected. For more information, see the content filter article.

Clean up resources

If you want to clean up and remove an Azure OpenAI resource, you can delete the resource or resource group. Deleting the resource group also deletes any other resources associated with it.

Next steps