Quickstart: Get started generating text using Azure OpenAI Service

Use this article to get started making your first calls to Azure OpenAI.

Prerequisites

  • An Azure subscription - Create one for free.

  • Access granted to Azure OpenAI in the desired Azure subscription.

    Currently, access to this service is granted only by application. You can apply for access to Azure OpenAI by completing the form at https://aka.ms/oai/access. Open an issue on this repo to contact us if you have an issue.

  • An Azure OpenAI resource with a model deployed. For more information about model deployment, see the resource deployment guide.

Go to the Azure OpenAI Studio

Navigate to Azure OpenAI Studio at https://oai.azure.com/ and sign-in with credentials that have access to your OpenAI resource. During or after the sign-in workflow, select the appropriate directory, Azure subscription, and Azure OpenAI resource.

From the Azure OpenAI Studio landing page navigate further to explore examples for prompt completion, manage your deployments and models, and find learning resources such as documentation and community forums.

Screenshot of the Azure OpenAI Studio landing page.

Go to the Playground for experimentation and fine-tuning workflow.

Playground

Start exploring Azure OpenAI capabilities with a no-code approach through the GPT-3 Playground. It's simply a text box where you can submit a prompt to generate a completion. From this page, you can quickly iterate and experiment with the capabilities.

Screenshot of the playground page of the Azure OpenAI Studio with sections highlighted.

You can select a deployment and choose from a few pre-loaded examples to get started. If your resource doesn't have a deployment, select Create a deployment and follow the instructions provided by the wizard. For more information about model deployment, see the resource deployment guide.

You can experiment with the configuration settings such as temperature and pre-response text to improve the performance of your task. You can read more about each parameter in the REST API.

  • Selecting the Generate button will send the entered text to the completions API and stream the results back to the text box.
  • Select the Undo button to undo the prior generation call.
  • Select the Regenerate button to complete an undo and generation call together.

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.

In the GPT-3 playground you can also view Python and curl code samples pre-filled according to your selected settings. Just select View code next to the examples dropdown. You can write an application to complete the same task with the OpenAI Python SDK, curl, or other REST API client.

Try text summarization

To use the Azure OpenAI for text summarization in the GPT-3 Playground, follow these steps:

  1. Sign in to Azure OpenAI Studio.

  2. Select the subscription and OpenAI resource to work with.

  3. Select GPT-3 Playground at the top of the landing page.

  4. Select your deployment from the Deployments dropdown. If your resource doesn't have a deployment, select Create a deployment and then revisit this step.

  5. Select Summarize Text from the Examples dropdown.

    Screenshot of the playground page of the Azure OpenAI Studio with the Summarize Text dropdown selection visible

  6. Select Generate. Azure OpenAI will attempt to capture the context of text and rephrase it succinctly. You should get a result that resembles the following text:

    Tl;dr A neutron star is the collapsed core of a supergiant star. These incredibly dense objects are incredibly fascinating due to their strange properties and their potential for phenomena such as extreme gravitational forces and a strong magnetic field.
    

The accuracy of the response can vary per model. The Davinci based model in this example is well-suited to this type of summarization, whereas a Codex based model wouldn't perform as well at this particular task.

Clean up resources

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

Next steps

Source code | Package (NuGeT) | Samples

Prerequisites

  • An Azure subscription - Create one for free
  • Access granted to the Azure OpenAI service in the desired Azure subscription. Currently, access to this service is granted only by application. You can apply for access to Azure OpenAI Service by completing the form at https://aka.ms/oai/access.
  • The current version of .NET Core
  • An Azure OpenAI Service resource with the text-davinci-003 model deployed. For more information about model deployment, see the resource deployment guide.

Set up

Create a new .NET Core application

In a console window (such as cmd, PowerShell, or Bash), use the dotnet new command to create a new console app with the name anomaly-detector-quickstart. This command creates a simple "Hello World" project with a single C# source file: Program.cs.

dotnet new console -n azure-openai-quickstart

Change your directory to the newly created app folder. You can build the application with:

dotnet build

The build output should contain no warnings or errors.

...
Build succeeded.
 0 Warning(s)
 0 Error(s)
...

Install the OpenAI .NET client library with:

dotnet add package Azure.AI.OpenAI --prerelease

Retrieve key and endpoint

To successfully make a call against Azure OpenAI, you'll need an endpoint and a key.

Variable name Value
ENDPOINT This value can be found in the Keys & Endpoint section when examining your resource from the Azure portal. Alternatively, you can find the value in the Azure OpenAI Studio > Playground > Code View. 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.

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.

Screenshot of the overview blade for an OpenAI Resource in the Azure portal with the endpoint & access keys location circled in red.

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

Environment variables

setx OPENAI_API_KEY "REPLACE_WITH_YOUR_KEY_VALUE_HERE" 
setx OPENAI_API_BASE "REPLACE_WITH_YOUR_ENDPOINT_HERE" 

Create .NET application

From the project directory, open the program.cs file and replace with the following code:

using Azure;
using Azure.AI.OpenAI;
using static System.Environment;

namespace azure_openai_quickstart
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string endpoint = GetEnvironmentVariable("OPENAI_API_BASE");
            string key = GetEnvironmentVariable("OPENAI_API_KEY");
            string engine = "text-davinci-003"; //Enter the deployment name you chose when you deployed the model.

            OpenAIClient client = new OpenAIClient(new Uri(endpoint), new AzureKeyCredential(key));

            string prompt = "When was Microsoft founded?";
            Console.Write($"Input: {prompt}\n");

            Response<Completions> completionsResponse = client.GetCompletions(engine, prompt);
            string completion = completionsResponse.Value.Choices[0].Text;
            Console.WriteLine($"Chatbot: {completion}");

        }  
    }
}

Important

For production, use a secure way of storing and accessing your credentials like Azure Key Vault. For more information about credential security, see the Cognitive Services security article.

dotnet run program.cs

Output

Input: When was Microsoft founded?
Chatbot:

Microsoft was founded on April 4, 1975

Clean up resources

If you want to clean up and remove an OpenAI resource, you can delete the resource. Before deleting the resource you must first delete any deployed models.

Next steps

Library source code | Package (PyPi) |

Prerequisites

  • An Azure subscription - Create one for free

  • Access granted to Azure OpenAI in the desired Azure subscription

    Currently, access to this service is granted only by application. You can apply for access to Azure OpenAI by completing the form at https://aka.ms/oai/access. Open an issue on this repo to contact us if you have an issue.

  • Python 3.7.1 or later version

  • The following Python libraries: os, requests, json

  • An Azure OpenAI Service resource with a model deployed. For more information about model deployment, see the resource deployment guide.

Set up

Install the OpenAI Python client library with:

pip install openai

Note

This library is maintained by OpenAI and is currently in preview. Refer to the release history or the version.py commit history to track the latest updates to the library.

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. Alternatively, you can find the value in Azure OpenAI Studio > Playground > Code View. 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 alternatively under Management > Deployments in Azure OpenAI Studio.

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.

Screenshot of the overview blade for an OpenAI Resource in the Azure portal with the endpoint & access keys location circled in red.

Create a new Python application

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

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

    import os
    import requests
    import json
    import openai
    
    openai.api_key = "REPLACE_WITH_YOUR_API_KEY_HERE"
    openai.api_base =  "REPLACE_WITH_YOUR_ENDPOINT_HERE" # your endpoint should look like the following https://YOUR_RESOURCE_NAME.openai.azure.com/
    openai.api_type = 'azure'
    openai.api_version = '2022-12-01' # this may change in the future
    
    deployment_name='REPLACE_WITH_YOUR_DEPLOYMENT_NAME' #This will correspond to the custom name you chose for your deployment when you deployed a model. 
    
    # Send a completion call to generate an answer
    print('Sending a test completion job')
    start_phrase = 'Write a tagline for an ice cream shop. '
    response = openai.Completion.create(engine=deployment_name, prompt=start_phrase, max_tokens=10)
    text = response['choices'][0]['text'].replace('\n', '').replace(' .', '.').strip()
    print(start_phrase+text)
    

    Important

    Remember to remove the key from your code when you're done, and never post it publicly. For production, use a secure way of storing and accessing your credentials like Azure Key Vault. See the Cognitive Services security article for more information.

  3. Run the application with the python command on your quickstart file:

    python quickstart.py
    

Output

The output will include response text following the Write a tagline for an ice cream shop. prompt. Azure OpenAI returned The coldest ice cream in town! in this example.

Sending a test completion job
Write a tagline for an ice cream shop. The coldest ice cream in town!

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 Write a tagline for an ice cream shop. 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 OpenAI resource, you can delete the resource or resource group. Deleting the resource group also deletes any other resources associated with it.

Next steps

Prerequisites

  • An Azure subscription - Create one for free

  • Access granted to Azure OpenAI in the desired Azure subscription

    Currently, access to this service is granted only by application. You can apply for access to the Azure OpenAI service by completing the form at https://aka.ms/oai/access. Open an issue on this repo to contact us if you have an issue.

  • Python 3.7.1 or later version

  • The following Python libraries: os, requests, json

  • An Azure OpenAI resource with a model deployed. For more information about model deployment, see the resource deployment guide.

Retrieve key and endpoint

To successfully make a call against Azure OpenAI, 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. Alternatively, you can find the value in Azure OpenAI Studio > Playground > Code View. 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 alternatively under Management > Deployments in Azure OpenAI Studio.

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.

Screenshot of the overview blade for an OpenAI Resource in the Azure portal with the endpoint & access keys location circled in red.

Create a new Python application

Create a new Python file called quickstart.py. Then open it up in your preferred editor or IDE.

  1. Replace the contents of quickstart.py with the following code.

    import os
    import requests
    import json
    
    api_key = "REPLACE_WITH_YOUR_API_KEY_HERE"
    base_url = "REPLACE_WITH_YOUR_ENDPOINT_HERE"
    deployment_name ="REPLACE_WITH_YOUR_DEPLOYMENT_NAME_HERE"
    
    url = base_url + "/openai/deployments/" + deployment_name + "/completions?api-version=2022-12-01"
    prompt = "Once upon a time"
    payload = {        
        "prompt":prompt
        }
    
    r = requests.post(url, 
          headers={
            "api-key": api_key,
            "Content-Type": "application/json"
          },
          json = payload
        )
    
    response = json.loads(r.text)
    formatted_response = json.dumps(response, indent=4)
    
    print(formatted_response)
    

    Important

    Remember to remove the key from your code when you're done, and never post it publicly. For production, use a secure way of storing and accessing your credentials. For example, Azure Key Vault.

  2. Run the application with the python command on your quickstart file:

    python quickstart.py
    

Output

The output from the completions API will look as follows.

{
    "id": "ID of your call",
    "object": "text_completion",
    "created": 1675444965,
    "model": "text-davinci-002",
    "choices": [
        {
            "text": " there lived in a little village a woman who was known as the meanest",
            "index": 0,
            "finish_reason": "length",
            "logprobs": null
        }
    ],
    "usage": {
        "completion_tokens": 16,
        "prompt_tokens": 3,
        "total_tokens": 19
    }
}

The Azure OpenAI Service 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 OpenAI resource, you can delete the resource or resource group. Deleting the resource group also deletes any other resources associated with it.

Next steps