Contents

A guide to Azure OpenAI Service – Full guide + code samples

Contents

A guide to Azure OpenAI Service – Full guide + code samples

Azure OpenAI Service is a cloud-based artificial intelligence platform developed by Microsoft that allows developers to easily build, deploy, and manage AI models. This platform provides access to powerful machine learning algorithms and pre-trained models to help you create various applications, from chatbots and natural language processing to image recognition.

This post will guide you through getting started with Azure OpenAI Service. We will cover everything you need to know, from setting up your Azure account and creating a new project to using the service to make API calls. We will also provide some examples and working code you can copy/paste and run.

Table of Contents

  1. Setting up Your Azure Account
  2. Creating a New Project
  3. Creating an OpenAI Resource
  4. Authentication and API Keys
  5. Making API Calls
  6. Examples and Working Code

To get started, you must first sign up for an Azure account if you don’t already have one. You can sign up for a free trial account here, which gives you access to various Azure services.

  1. Creating a New Project

After signing up for an account, you need to create a new project. Follow these steps:

  1. Log in to the Azure Portal.
  2. Click on ‘Create a resource’ in the left-hand menu.
  3. Search for ‘OpenAI’ in the search bar and select the ‘OpenAI Service’ resource.
  4. Click on the ‘Create’ button to start creating a new OpenAI Service project.

3. Creating an OpenAI Resource

To create an OpenAI resource, follow these steps:

  1. Fill in the necessary details in the ‘Basics’ tab, such as subscription, resource group, and resource name.
  2. Choose the region closest to your location for better performance.
  3. Select the pricing tier that suits your needs.
  4. Click on the ‘Review + create’ button to review your settings and then click ‘Create’ to deploy your OpenAI Service resource.

/a-guide-to-azure-openai-service-full-guide-code-samples/assets/createopenai.png

  1. Authentication and API Keys

You must obtain an API key to interact with the OpenAI Service API. Follow these steps:

  1. In the Azure portal, navigate to your OpenAI Service resource.
  2. Click on the ‘Keys and Endpoint’ tab in the left-hand menu.
  3. Copy the primary or secondary API key.
  4. Making API Calls

To make API calls, you can use the Azure OpenAI Python SDK. Install the SDK with the following command:

1
pip install azure-openai

The following Python code demonstrates how to initialize the OpenAI Service client and make an API call:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
from azure.core.credentials import AzureKeyCredential
from azure.openai import OpenAIServiceClient

# Replace with your API key
api_key = "<your_api_key>"

client = OpenAIServiceClient(
    endpoint="https://api.openai.azure.com",
    credential=AzureKeyCredential(api_key)
)

response = client.completion(
    model="text-davinci-002",
    prompt="Translate the following English text to French: 'Hello, how are you?'",
    max_tokens=20
)

print(response.choices[0].text.strip())

Here are some common tasks with the Azure OpenAI Service API. You can copy/paste and run these code snippets after replacing the API key with your own.

6. Examples and Working

Example 1: Text Summarization

This example demonstrates how to use the OpenAI Service API to summarise a given text.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
def summarize_text(text, max_tokens=50):
    response = client.completion(
        model="text-davinci-002",
        prompt=f"Please provide a summary of the following text: '{text}'",
        max_tokens=max_tokens
    )

    return response.choices[0].text.strip()

text = """
The Azure OpenAI Service is a cloud-based AI platform developed by Microsoft. It allows developers to build, deploy, and manage AI models with ease. The platform provides access to powerful machine learning algorithms and pre-trained models for various applications.
"""

summary = summarize_text(text)
print("Summary:", summary)

Example 2: Sentiment Analysis

This example shows how to perform sentiment analysis on a given text using the OpenAI Service API.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
def analyze_sentiment(text):
    response = client.completion(
        model="text-davinci-002",
        prompt=f"What is the sentiment of the following text: '{text}'? Positive, Negative or Neutral?",
        max_tokens=10
    )

    return response.choices[0].text.strip()

text = "I love using the Azure OpenAI Service. It's incredibly easy to use and has a lot of powerful features."
sentiment = analyze_sentiment(text)
print("Sentiment:", sentiment)

Example 3: Question-Answering

This example demonstrates how to use the OpenAI Service API to answer a question based on a provided context.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
def answer_question(question, context, max_tokens=50):
    response = client.completion(
        model="text-davinci-002",
        prompt=f"{context}\nQuestion: {question}\nAnswer:",
        max_tokens=max_tokens
    )

    return response.choices[0].text.strip()

context = """
Azure OpenAI Service is a cloud-based artificial intelligence platform developed by Microsoft. The platform provides access to powerful machine learning algorithms and pre-trained models for various applications, such as chatbots, natural language processing, and image recognition.
"""

question = "What are some applications of Azure OpenAI Service?"
answer = answer_question(question, context)
print("Answer:", answer)

Conclusion

In this blog post, we provided a comprehensive guide to help you get started with Azure OpenAI Service. We covered setting up your Azure account, creating a new project and resource, authenticating with API keys, and making API calls using the Python SDK. Additionally, we provided some examples and working code snippets that you can use as a starting point for your projects.

Now that you have a solid understanding of Azure OpenAI Service, you can begin exploring its capabilities and integrating it into your applications. Happy coding!