見出し画像

Using Azure OpenAI Service①

Azure OpenAI Service is a powerful wrapper for the OpenAI API, provided by Microsoft Azure. This service allows you to leverage OpenAI's advanced AI models combined with Azure's security and management features. Today, I'll guide you through creating a chatbot using this service with Python.

Step 1: Create an Azure OpenAI Resource

First, log in to the Azure portal. Click on the "Create a resource" button, select the "AI + Machine Learning" category, and choose "OpenAI Service". Complete the necessary configurations and proceed to "Review + create" to establish your resource. This sets the stage for using OpenAI within the Azure ecosystem.

Step 2: Obtain the API Key

Once your resource is created, navigate to the "Keys and Endpoint" section and copy your API key. This key will serve as your access pass to the API.

Step 3: Prepare the Python Code

Next, we need to adjust our Python code to utilize the Azure API key. Take a look at the following code snippet.

import openai

# Set the Azure OpenAI API key and endpoint
openai.api_key = "your_azure_openai_api_key"
endpoint = "your_azure_openai_endpoint"

# Use the GPT-3.5-turbo model to answer a question
response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Please introduce yourself."}
    ],
    temperature=0,
    api_base=endpoint  # Specify the Azure OpenAI endpoint
)

print(response["choices"][0]["message"]["content"])

This code uses the Azure OpenAI Service to send a question to the GPT-3.5-turbo model and retrieve its response. Replace `your_azure_openai_api_key` and `your_azure_openai_endpoint` with your actual values.

Step 4: Install the Library

Finally, install the `openai` library, which will act as a bridge to the Azure OpenAI Service.

pip install --quiet openai

Now, you're ready to create a chatbot using the Azure OpenAI Service with Python. Through this process, you can take advantage of Azure's security and management features while exploring the capabilities of OpenAI's powerful AI models.

この記事が気に入ったらサポートをしてみませんか?