🗣️

本家 OpenAI と Azure OpenAI Service の ChatGPT API の比較

2023/03/10に公開

はじめに

待ちに待った Azure OpenAI Service の ChatGPT API が公開されましたので、本家 OpenAI 版との違いに着目しながら情報をまとめました。本家で API が公開されて 2 週間足らずでの公開ですので、かなり早いペースだと感じます。

なお、サービス自体の比較は以前の記事でまとめています。

【追記】Azure OpenAI Service に本家 OpenAI とほぼ同じ操作性の API が追加されましたので、情報を追記しました。

利用申請

既に Azure OpenAI Service の利用申請が承認されている場合は、ChatGPT API の利用のために特に追加で必要な申請はありません。

参考

本家 OpenAI 版との比較

1. 利用規約

良く話題に上がりそうなデータの取り扱い部分についてピックアップしました。更新される可能性もあるため、必ず原文も確認してください。

本家 OpenAI

利用規約はこちらです。2023 年 3 月の ChatGPT API 公開に合わせて更新されました。

(c) Use of Content to Improve Services. We do not use Content that you provide to or receive from our API (“API Content”) to develop and improve our Services. API Content is only used to provide and maintain our API Services. We may use Content from Services other than our API (“Non-API Content”) to help develop and improve our Services. You can read more here about how Non-API Content may be used to improve model performance. If you do not want your Non-API Content used to improve Services, you can opt out by filling out this form. Please note that in some cases this may limit the ability of our Services to better address your specific use case.

データの取り扱いについて、API 経由で与えられたコンテンツについてはサービスの開発や改善に使われることはないとされています。一方で Non-API 経由で与えられたコンテンツについては、サービスの開発や改善に使われることもあるとされています。Non-API とはブラウザからの利用を指しているものと思われます。なお、Non-API に関しても、申請を行うことでサービスの開発や改善への使用をオプトアウトできるようです。

Azure OpenAI Service

利用規約はこちらです。

データの取り扱いについては別のドキュメントに書かれています。

Training data provided by the customer is only used to fine-tune the customer’s model and is not used by Microsoft to train or improve any Microsoft models.

利用者から提供されたトレーニングデータは その利用者のモデルの Fine-tuning のみに使用されます。

The Azure OpenAI Service stores prompts & completions from the service to monitor for abusive use and to develop and improve the quality of Azure OpenAI’s content management systems.

Azure OpenAI Service では悪用や有害コンテンツ生成の監視目的で入出力を一定期間保存して人間によるレビュープロセスが実行されます。

Can a customer opt out of the logging and human review process?
Some customers in highly regulated industries with low risk use cases process sensitive data with less likelihood of misuse. Because of the nature of the data or use case, these customers do not want or do not have the right to permit Microsoft to process such data for abuse detection due to their internal policies or applicable legal regulations.

To empower its enterprise customers and to strike a balance between regulatory / privacy needs and abuse prevention, the Azure Open AI Service will include a set of Limited Access features to provide potential customers with the option to modify following:

abuse monitoring
content filtering
These Limited Access features will enable potential customers to opt out of the human review and data logging processes subject to eligibility criteria governed by Microsoft’s Limited Access framework. Customers who meet Microsoft’s Limited Access eligibility criteria and have a low-risk use case can apply for the ability to opt-out of both data logging and human review process. This allows trusted customers with low-risk scenarios the data and privacy controls they require while also allowing us to offer AOAI models to all other customers in a way that minimizes the risk of harm and abuse.

データの保存とレビュープロセスは、規制の厳しい業界におけるリスクの低いユースケースに限りオプトアウトできるようです。オプトアウトには申請と承認が必要になります。

参考

2. リージョン

Azure OpenAI Service の ChatGPT API は執筆時点で East US (米国東部)South Central US (米国中南部) で利用可能です。一方、本家 OpenAI の ChatGPT にリージョンの概念はありません。

参考

3. 価格

執筆時点でいずれも $0.002 / 1K tokens で同じでした。ちなみに、これは GPT-3 の Curie モデルと同じ価格設定です。

本家 OpenAI

Azure OpenAI Service

4. モデル名

API で指定するモデル名が微妙に異なりますが、同じモデルのことを示しています。

本家 / Azure モデル名
本家 OpenAI gpt-3.5-turbo
Azure OpenAI Service gpt-35-turbo

参考

本家 OpenAI

ChatGPT モデルは更新され続けるため、本家 OpenAI で単に gpt-3.5-turbo を指定すると常に最新バージョンのモデルが使用されます。一方で、モデルバージョンを固定したい場合向けに、2023 年 3 月 1 日時点のスナップショットバージョンである gpt-3.5-turbo-0301 も提供されています。なお、執筆時点では gpt-3.5-turbogpt-3.5-turbo-0301 のどちらを指定しても gpt-3.5-turbo-0301 が使われる挙動をしました。

gpt-3.5-turbo を指定:

import os
import openai

openai.api_key = os.getenv("OPENAI_API_KEY")

response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    messages=[
        {"role": "system", "content": "You are a friendly assistant."},
        {"role": "user", "content": "What's up?"}
    ]
)

print(response["model"])

レスポンス:

gpt-3.5-turbo-0301

gpt-3.5-turbo-0301 を指定:

import os
import openai

openai.api_key = os.getenv("OPENAI_API_KEY")

response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo-0301",
    messages=[
        {"role": "system", "content": "You are a friendly assistant."},
        {"role": "user", "content": "What's up?"}
    ]
)

print(response["model"])

レスポンス:

gpt-3.5-turbo-0301

参考

Azure OpenAI Service

Azure OpenAI Service では、ChatGPT モデルのバージョンは デプロイ で選択します。
以下の例では、gpt-35-turbo (version 0301) (本家でいう gpt-3.5-turbo-0301) を、gpt-35-turbo-0301 という名前を付けてデプロイしています。API からモデルを使用する際はこのデプロイ名を指定します。 なお、Azure OpenAI Service では本家の gpt-3.5-turbo に相当する選択肢はありませんでした。


※ East US リージョンの例。グレーアウトしているのはデプロイ済みのモデル。

5. Playground

本家 OpenAI

かなりシンプルな作りです。パラメーターの設定もモデルの挙動を設定するシステムメッセージ の入力もできません。

Azure OpenAI Service

Azure OpenAI Service には ChatGPT API の公開に合わせてチャット専用の Playground が追加されました。この Playground では 前述のパラメーターやシステムメッセージに加えて Few-shot の例を指定するウィンドウも追加されていて、実用的な開発環境という印象です。

チャット向けの例もいくつか追加されています。


開発結果をそのままソースコードとしてコピーすることもできます。

6. Python API

6.1. 本家 OpenAI

公式ドキュメントの例にある 2020 年のワールドシリーズ の会話を行う場合、以下のようなコードとレスポンスになります。API キーは事前に取得して環境変数に登録しておきます。

import os
import openai

openai.api_key = os.getenv("OPENAI_API_KEY")

response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo-0301",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Who won the world series in 2020?"},
        {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
        {"role": "user", "content": "Where was it played?"}
    ],
    max_tokens=800,
    temperature=0
)

# print(response["choices"][0]["message"]["content"]) # レスポンスのテキスト部分のみ取得する場合
print(response)
{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "message": {
        "content": "The 2020 World Series was played at Globe Life Field in Arlington, Texas.",
        "role": "assistant"
      }
    }
  ],
  "created": 1678443255,
  "id": "chatcmpl-6sU3Lv5RCT8UP9WaOC9bzjnbd7V6S",
  "model": "gpt-3.5-turbo-0301",
  "object": "chat.completion",
  "usage": {
    "completion_tokens": 19,
    "prompt_tokens": 56,
    "total_tokens": 75
  }
}

参考

6.2. Azure OpenAI Service (Chat Markup Language)

Azure OpenAI Service のドキュメントに書かれている操作方法を参考にして同じ会話を行ってみます。キーとエンドポイントは事前に取得して環境変数に登録しておきます。

【追記】後述する Chat Completions API (本家とほぼ同じ操作性) の一般提供 (GA) 開始により、あえてこちらの Chat Markup Language を選択する理由は無くなりました。

import os
import openai

openai.api_type = "azure"
openai.api_base = os.getenv("AOAI_API_BASE")
openai.api_version = "2022-12-01"
openai.api_key = os.getenv("AOAI_API_KEY")

prompt="""<|im_start|>system
You are a helpful assistant.
<|im_end|>
<|im_start|>user
Who won the world series in 2020?
<|im_end|>
<|im_start|>assistant
The Los Angeles Dodgers won the World Series in 2020.
<|im_end|>
<|im_start|>user
Where was it played?
<|im_end|>
<|im_start|>assistant
"""

response = openai.Completion.create(
    engine="gpt-35-turbo-0301",  # デプロイ名
    prompt=prompt,
    max_tokens=800,
    temperature=0,
    stop=["<|im_end|>"]
)

# print(response["choices"][0]["text"]) # レスポンスのテキスト部分のみ取得する場合
print(response)
{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "logprobs": null,
      "text": "The 2020 World Series was played at Globe Life Field in Arlington, Texas."
    }
  ],
  "created": 1678443897,
  "id": "cmpl-6sUDh0pD6xGzD9IGZFq1hMDJFcB7S",
  "model": "gpt-35-turbo",
  "object": "text_completion",
  "usage": {
    "completion_tokens": 17,
    "prompt_tokens": 57,
    "total_tokens": 74
  }
}

本家と異なる点をピックアップします。

設定項目

本家 OpenAI は API キーの指定のみです。

openai.api_key = os.getenv("OPENAI_API_KEY")

一方、Azure OpenAI Service では利用者のリソース固有の エンドポイント (URL) などいくつか追加で必要な項目があります。

openai.api_type = "azure" # 執筆時点で固定値
openai.api_base = os.getenv("AOAI_API_BASE")
openai.api_version = "2022-12-01"
openai.api_key = os.getenv("AOAI_API_KEY")

入力フォーマット

最も異なる点です。本家 OpenAI では入力を以下のように辞書のリストopenai.Completion.create 関数に与えられます。その際の引数名は messages です。

messages=[
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Who won the world series in 2020?"},
    {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
    {"role": "user", "content": "Where was it played?"}
]

参考

一方、Azure OpenAI Service では以下のように Chat Markup Language (ChatML) というフォーマットの文字列として与える必要があります。(ここでの ML は紛らわしい..)
Chat Markup Language はワンライナーで書くこともできますが、ここでは読みやすさを重視してヒアドキュメントで書いています。ややクセが強いですが、さほど難しいフォーマットではありません。また、openai.Completion.create 関数へ与える際の引数名は prompt です。

prompt="""<|im_start|>system
You are a helpful assistant.
<|im_end|>
<|im_start|>user
Who won the world series in 2020?
<|im_end|>
<|im_start|>assistant
The Los Angeles Dodgers won the World Series in 2020.
<|im_end|>
<|im_start|>user
Where was it played?
<|im_end|>
<|im_start|>assistant
"""

参考

[補足] Chat Markup Language (ChatML) について

以下のドキュメントによると、ChatGPT モデル (Chat Completions ができるモデル) の生の入出力は ChatML フォーマット によって行われているそうです。
つまり、Azure OpenAI Service 版は開発者に対してモデルの生の入出力を見せているのに対して、本家 OpenAI 版ではシンタックスシュガーが追加されているため、このような入出力の違いが出ているのだと思われます。

レスポンス

レスポンスも異なる部分があります。本家 OpenAI ではレスポンスも辞書で返ってきますが、一方の Azure OpenAI Service では文字列で返ってきます。

本家 OpenAI

{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "message": {
        "content": "The 2020 World Series was played at Globe Life Field in Arlington, Texas.",
        "role": "assistant"
      }
    }
  ],
...
  }
}

Azure OpenAI Service

{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "logprobs": null,
      "text": "The 2020 World Series was played at Globe Life Field in Arlington, Texas."
    }
  ],
...
  }
}

6.3. Azure OpenAI Service (Chat Completions)

Azure OpenAI Service での GPT-4 登録制プレビュー開始 と同じタイミングで、本家 OpenAI の Chat Completions API とほぼ同じ操作性の API がプレビュー版として公開されました。

Microsoft Tech Community の以下の記事によると、今後はこちらの API が主流になっていくようです。 (ChatML とは何だったのか..)

NOTE: gpt-35-turbo can also be used with the existing Text Completions API. However, we recommend that you use the Chat Completions API moving forward. If you do use gpt-35-turbo with the text completions API, make sure to specify your prompts using Chat Markup Language (ChatML).

【追記】2023 年 5 月に Chat Completion API は一般提供 (GA) が開始しました。

Chat Completions API を使ったコードは以下のようになります。いくつか Azure OpenAI Service 固有の設定項目がある点と、モデル名ではなくデプロイ名を指定する点は ChatML を使う場合と同じですが、入力フォーマットは本家 OpenAI と同じ辞書のリストになっています。
Chat Completions API は openai.api_version"2023-05-15" を指定することで利用可能になります。

参考

import os
import openai

openai.api_type = "azure"
openai.api_base = os.getenv("AOAI_API_BASE")
openai.api_version = "2023-05-15"
openai.api_key = os.getenv("AOAI_API_KEY")

response = openai.ChatCompletion.create(
    engine="gpt-35-turbo-0301",  # デプロイ名
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Who won the world series in 2020?"},
        {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
        {"role": "user", "content": "Where was it played?"}
    ],
    max_tokens=800,
    temperature=0
)

# print(response["choices"][0]["message"]["content"])  # レスポンスのテキスト部分のみ取得する場合
print(response)

レスポンスも本家と全く同じフォーマットになりました。

{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "message": {
        "content": "The 2020 World Series was played at Globe Life Field in Arlington, Texas.",
        "role": "assistant"
      }
    }
  ],
  "created": 1679906338,
  "id": "chatcmpl-6ycfSP3RM14U3eUECZKLbmZaRF8Ui",
  "model": "gpt-35-turbo",
  "object": "chat.completion",
  "usage": {
    "completion_tokens": 19,
    "prompt_tokens": 56,
    "total_tokens": 75
  }
}

7. REST API

本家 OpenAI

以下のようなリクエストとレスポンスになります。事前に API キーを取得して <your-openai-api-key> と置き換えます。

リクエスト:

curl https://api.openai.com/v1/chat/completions \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer <your-openai-api-key>' \
  -d '{
  "model": "gpt-3.5-turbo",
  "messages": [{"role": "system", "content": "You are a friendly assistant."},{"role": "user", "content": "What''s up?"}],
  "max_tokens": 800,
  "temperature": 0
}'

レスポンス:

HTTP/1.1 200 OK
Date: Fri, 10 Mar 2023 11:56:08 GMT
Content-Type: application/json
Content-Length: 412
Connection: close
Access-Control-Allow-Origin: *
Cache-Control: no-cache, must-revalidate
Openai-Model: gpt-3.5-turbo-0301
Openai-Organization: user-xttkbqdmglib1amy51evvapj
Openai-Processing-Ms: 1803
Openai-Version: 2020-10-01
Strict-Transport-Security: max-age=15724800; includeSubDomains
X-Request-Id: 611a516d2b6c6082ec028b35a5c0be4d

{
  "id": "chatcmpl-6sVdu59vNwOKdNlx4kfyUvtpryoDK",
  "object": "chat.completion",
  "created": 1678449366,
  "model": "gpt-3.5-turbo-0301",
  "usage": {
    "prompt_tokens": 21,
    "completion_tokens": 34,
    "total_tokens": 55
  },
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": "Hello! As an AI assistant, I don't have emotions, but I'm here to help you with anything you need. How can I assist you today?"
      },
      "finish_reason": "stop",
      "index": 0
    }
  ]
}

Azure OpenAI Service (Chat Markup Language)

以下のようなリクエストとレスポンスになります。事前に API キーを取得 して <your-aoai-api-key> と置き換えます。また、<your-aoai-resource-name> は Azure OpenAI Service のリソース名、<your-deployment-name> は ChatGPT モデルのデプロイ名と置き換えます。さらに、入力のフォーマットは Chat Markup Language で記述します。

【追記】Python API と同様に Chat Completions API の一般提供 (GA) 開始により、あえて Chat Markup Language を選択する理由は無くなりました。

リクエスト:

curl https://<your-aoai-resource-name>.openai.azure.com/openai/deployments/<your-deployment-name>/completions?api-version=2022-12-01 \
  -H "Content-Type: application/json" \
  -H "api-key: <your-aoai-api-key>" \
  -d '{
  "prompt": "<|im_start|>system\nYou are a friendly assistant.\n<|im_end|>\n<|im_start|>user\nWhat''s up?\n<|im_end|>\n<|im_start|>assistant",
  "max_tokens": 800,
  "temperature": 0,
  "stop": ["<|im_end|>"]
}'

Python API の場合と同じく、Azure OpenAI Service 固有の設定項目と入力フォーマットの部分が異なります。

レスポンス:

HTTP/1.1 200 OK
Cache-Control: no-cache, must-revalidate
Content-Length: 414
Content-Type: application/json
access-control-allow-origin: *
openai-model: gpt-35-turbo
apim-request-id: 14547065-d5d0-4093-89aa-9b8dfc13eba6
openai-processing-ms: 675.7901
x-content-type-options: nosniff
x-accel-buffering: no
x-request-id: 70b67d77-9794-48d7-bf50-8eb231b3ac96
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
x-ms-region: East US
Date: Fri, 10 Mar 2023 11:57:30 GMT
Connection: close

{
  "id": "cmpl-6sVfGTgbWkGukV3l6LeDickDdA5mS",
  "object": "text_completion",
  "created": 1678449450,
  "model": "gpt-35-turbo",
  "choices": [
    {
      "text": "\nAs an AI language model, I don't have emotions or feelings, but I'm here to assist you with any questions or tasks you may have. How can I help you today?",
      "index": 0,
      "finish_reason": "stop",
      "logprobs": null
    }
  ],
  "usage": {
    "completion_tokens": 38,
    "prompt_tokens": 21,
    "total_tokens": 59
  }
}

レスポンスも一部異なります。Azure OpenAI Service では Python API と同じくレスポンスが文字列で返ってきます。

Azure OpenAI Service (Chat Completions)

Python API と同様に、REST API でも余計な構文を含めず本家 OpenAI 版とほぼ同じ操作性でリクエストができるようになりました。

具体的なリクエストとレスポンスは以下のとおりです。事前に API キーを取得 して <your-aoai-api-key> と置き換えます。また、<your-aoai-resource-name> は Azure OpenAI Service のリソース名、<your-deployment-name> は ChatGPT モデルのデプロイ名と置き換えます。

リクエスト:

curl https://<your-aoai-resource-name>.openai.azure.com/openai/deployments/<your-deployment-name>/chat/completions?api-version=2023-05-15 \
  -H "Content-Type: application/json" \
  -H "api-key: <your-aoai-api-key>" \
  -d '{
    "messages":[
      {"role": "system", "content": "You are a friendly assistant."},
      {"role": "user", "content": "What''s up?"}],
    "max_tokens": 800,
    "temperature": 0
  }'

レスポンス:

HTTP/1.1 200 OK
Cache-Control: no-cache, must-revalidate
Content-Length: 406
Content-Type: application/json
access-control-allow-origin: *
apim-request-id: 3ff71920-f9bb-4291-9b12-62a106e617f0
openai-model: gpt-35-turbo
x-content-type-options: nosniff
openai-processing-ms: 1211.8348
x-ms-region: East US
x-accel-buffering: no
x-request-id: bfe28c05-fe48-4f8d-a706-86b841b30825
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
Date: Thu, 18 May 2023 08:03:28 GMT
Connection: close

{
  "id": "chatcmpl-7HStbXjKGcxiEXcSwrzHXD2ZB4kBL",
  "object": "chat.completion",
  "created": 1684397007,
  "model": "gpt-35-turbo",
  "choices": [
    {
      "index": 0,
      "finish_reason": "stop",
      "message": {
        "role": "assistant",
        "content": "Hello! As an AI assistant, I don't have emotions, but I'm here to help you with anything you need. How can I assist you today?"
      }
    }
  ],
  "usage": {
    "completion_tokens": 32,
    "prompt_tokens": 22,
    "total_tokens": 54
  }
}

おわりに

Azure OpenAI Service の ChatGPT API 公開をきっかけにして企業等の環境での活用がより一層進みそうです。

以上です。🍵

Microsoft (有志)

Discussion