Azure OpenAI SDK を使用する

完了

前のユニットで説明した REST API に加えて、ユーザーは C# と Python SDK を使って Azure OpenAI モデルにアクセスすることもできます。 REST とこれらの SDK のどちらでも同じ機能を使用できます。

Note

いずれかの SDK を使って API を操作する前に、Azure portal で Azure OpenAI リソースを作成し、そのリソースにモデルをデプロイして、エンドポイントとキーを取得する必要があります。 その方法については、「Azure OpenAI Service で作業を開始する」をご覧ください。

このユニットで説明する両方の SDK では、Azure OpenAI リソースのエンドポイントとキー、そしてデプロイしたモデルに指定した名前が必要です。 以下のコード スニペットでは、次のプレースホルダーが使われます。

プレースホルダー名 Value
YOUR_ENDPOINT_NAME このベース エンドポイントは、Azure portal 内の [キーとエンドポイント] セクション内にあります。 これは、リソースのベース エンドポイントです (https://sample.openai.azure.com/ など)。
YOUR_API_KEY キーは、Azure portal 内の [キーとエンドポイント] セクション内にあります。 リソースにはどちらのキーを使っても構いません。
YOUR_DEPLOYMENT_NAME このデプロイ名は、Azure OpenAI Studio でモデルをデプロイしたときに指定した名前です。

ライブラリのインストール

最初に、使用する言語のクライアント ライブラリをインストールします。 C# SDK は REST API を .NET に適応したものであり、Azure OpenAI 専用に構築されていますが、Azure OpenAI リソースまたは Azure 以外の OpenAI エンドポイントへの接続に使用できます。 Python SDK は OpenAI によって構築および保守されています。

dotnet add package Azure.AI.OpenAI --prerelease
pip install openai

Azure OpenAI リソースにアクセスするようにアプリを構成する

構成は言語ごとに若干異なりますが、どちらでも同じパラメーターを設定する必要があります。 必要なパラメーターは endpointkey、およびデプロイの名前です。これは、モデルにプロンプトを送信するときは engine と呼ばれます。

ライブラリをアプリに追加し、クライアントに必要なパラメーターを設定します。

// Add OpenAI library
using Azure.AI.OpenAI;

// Define parameters and initialize the client
string endpoint = "<YOUR_ENDPOINT_NAME>";
string key = "<YOUR_API_KEY>";
string deploymentName = "<YOUR_DEPLOYMENT_NAME>"; 

OpenAIClient client = new OpenAIClient(new Uri(endpoint), new AzureKeyCredential(key));
# Add OpenAI library
from openai import AzureOpenAI

deployment_name = '<YOUR_DEPLOYMENT_NAME>' 

# Initialize the Azure OpenAI client
client = AzureOpenAI(
        azure_endpoint = '<YOUR_ENDPOINT_NAME>', 
        api_key='<YOUR_API_KEY>',  
        api_version="20xx-xx-xx" #  Target version of the API, such as 2024-02-15-preview
        )

Azure OpenAI リソースを呼び出す

Azure OpenAI への接続を構成したら、モデルにプロンプトを送信します。

// Build completion options object
ChatCompletionsOptions chatCompletionsOptions = new ChatCompletionsOptions()
{
    Messages =
    {
        new ChatRequestSystemMessage("You are a helpful AI bot."),
        new ChatRequestUserMessage("What is Azure OpenAI?"),
    },
    DeploymentName = deploymentName
};

// Send request to Azure OpenAI model
ChatCompletions response = client.GetChatCompletions(chatCompletionsOptions);

// Print the response
string completion = response.Choices[0].Message.Content;
Console.WriteLine("Response: " + completion + "\n");
response = client.chat.completions.create(
    model=deployment_name,
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "What is Azure OpenAI?"}
    ]
)
generated_text = response.choices[0].message.content

# Print the response
print("Response: " + generated_text + "\n")

応答オブジェクトには、total_tokensfinish_reason など、いくつかの値が含まれています。 応答オブジェクトからの入力候補は、次の入力候補のようになります。

"Azure OpenAI is a cloud-based artificial intelligence (AI) service that offers a range of tools and services for developing and deploying AI applications. Azure OpenAI provides a variety of services for training and deploying machine learning models, including a managed service for training and deploying deep learning models, a managed service for deploying machine learning models, and a managed service for managing and deploying machine learning models."

C# と Python のどちらでも、呼び出しに、temperaturemax_tokens などの省略可能なパラメーターを含めることができます。 このモジュールのラボには、それらのパラメーターの使用例が含まれます。