learn.microsoft.com Open in urlscan Pro
2600:1400:d:48a::3544  Public Scan

Submitted URL: https://protect-us.mimecast.com/s/atCuCOY53jf50Bp6HjjuxZ?domain=nam06.safelinks.protection.outlook.com
Effective URL: https://learn.microsoft.com/en-us/azure/cognitive-services/openai/quickstart?pivots=programming-language-studio
Submission: On February 09 via manual from US — Scanned from US

Form analysis 0 forms found in the DOM

Text Content

Skip to main content


This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security
updates, and technical support.

Download Microsoft Edge More info about Internet Explorer and Microsoft Edge

Table of contents Exit focus mode

Read in English Save
Table of contents Read in English Save Edit Print

Twitter LinkedIn Facebook Email
Table of contents


QUICKSTART: GET STARTED GENERATING TEXT USING AZURE OPENAI

 * Article
 * 02/03/2023
 * 9 minutes to read
 * 4 contributors

Feedback


IN THIS ARTICLE

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


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 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.

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


GO TO THE AZURE OPENAI STUDIO

Navigate to the 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.



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


PLAYGROUND

Start exploring 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.



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.

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.

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 OpenAI service for text summarization in the GPT-3 Playground, follow
these steps:

 1. Sign in to the 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.
    
    

 6. Select Generate. OpenAI will grasp 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.

 * Portal
 * Azure CLI


NEXT STEPS

Learn more about how to generate the best completion in our How-to guide on
completions.

Library source code | Package (PyPi) |


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 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 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 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. 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.




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. The Azure OpenAI Service 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.

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.

 * Portal
 * Azure CLI


NEXT STEPS

Learn more about how to generate the best completion in our How-to guide on
completions.


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 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 Service 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 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 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. 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.




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.

 * Portal
 * Azure CLI


NEXT STEPS

Learn more about how to generate the best completion in our How-to guide on
completions.






FEEDBACK

Submit and view feedback for

This product This page
View all page feedback

--------------------------------------------------------------------------------


ADDITIONAL RESOURCES





Theme
 * Light
 * Dark
 * High contrast

 * 
 * Previous Versions
 * Blog
 * Contribute
 * Privacy
 * Terms of Use
 * Trademarks
 * © Microsoft 2023


ADDITIONAL RESOURCES






IN THIS ARTICLE



Theme
 * Light
 * Dark
 * High contrast

 * 
 * Previous Versions
 * Blog
 * Contribute
 * Privacy
 * Terms of Use
 * Trademarks
 * © Microsoft 2023