再現可能な出力 (プレビュー) を使用する方法について説明します

既定では、Azure OpenAI Chat Completion モデルに同じ質問を複数回行うと、それぞれ異なる応答が返される可能性があります。 したがって、その応答は非決定論的であると見なされます。 再現可能な出力は、新しいプレビュー機能であり、より決定論的な出力を生成するために役立つ既定の動作を選択的に変更することができます。

再現可能な出力のサポート

再現可能な出力は、現在、次の場合にのみサポートされています。

サポートされているモデル

API バージョン

再現可能な出力のサポートは、API バージョン 2023-12-01-preview で最初に追加されました

まず、同じ質問に対して 3 つの応答を生成し、他のパラメーターが同じ場合でも、Chat Completion 応答には共通する変動性があることを実証します。

import os
from openai import AzureOpenAI

client = AzureOpenAI(
  azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT"), 
  api_key=os.getenv("AZURE_OPENAI_API_KEY"),  
  api_version="2024-02-01"
)

for i in range(3):
  print(f'Story Version {i + 1}\n---')
    
  response = client.chat.completions.create(
    model="gpt-35-turbo-0125", # Model = should match the deployment name you chose for your 0125-preview model deployment
    #seed=42,
    temperature=0.7,
    max_tokens =50, 
    messages=[
      {"role": "system", "content": "You are a helpful assistant."},
      {"role": "user", "content": "Tell me a story about how the universe began?"}
    ]
  )
  
  print(response.choices[0].message.content)
  print("---\n")
  
  del response

出力

Story Version 1
---
Once upon a time, before there was time, there was nothing but a vast emptiness. In this emptiness, there existed a tiny, infinitely dense point of energy. This point contained all the potential for the universe as we know it. And
---

Story Version 2
---
Once upon a time, long before the existence of time itself, there was nothing but darkness and silence. The universe lay dormant, a vast expanse of emptiness waiting to be awakened. And then, in a moment that defies comprehension, there
---

Story Version 3
---
Once upon a time, before time even existed, there was nothing but darkness and stillness. In this vast emptiness, there was a tiny speck of unimaginable energy and potential. This speck held within it all the elements that would come

各ストーリーには類似した要素や逐語的な繰り返しが一部含まれる場合があるものの、応答が長くなるほど、相違が生じる傾向にあることに注意してください。

次に、前と同じコードを実行しますが、今回は seed=42 というパラメーターの行でコメントを解除します

import os
from openai import AzureOpenAI

client = AzureOpenAI(
  azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT"), 
  api_key=os.getenv("AZURE_OPENAI_API_KEY"),  
  api_version="2024-02-01"
)

for i in range(3):
  print(f'Story Version {i + 1}\n---')
    
  response = client.chat.completions.create(
    model="gpt-35-turbo-0125", # Model = should match the deployment name you chose for your 0125-preview model deployment
    seed=42,
    temperature=0.7,
    max_tokens =50, 
    messages=[
      {"role": "system", "content": "You are a helpful assistant."},
      {"role": "user", "content": "Tell me a story about how the universe began?"}
    ]
  )
  
  print(response.choices[0].message.content)
  print("---\n")
  
  del response

出力

Story Version 1
---
In the beginning, there was nothing but darkness and silence. Then, suddenly, a tiny point of light appeared. This point of light contained all the energy and matter that would eventually form the entire universe. With a massive explosion known as the Big Bang
---

Story Version 2
---
In the beginning, there was nothing but darkness and silence. Then, suddenly, a tiny point of light appeared. This point of light contained all the energy and matter that would eventually form the entire universe. With a massive explosion known as the Big Bang
---

Story Version 3
---
In the beginning, there was nothing but darkness and silence. Then, suddenly, a tiny point of light appeared. This was the moment when the universe was born.

The point of light began to expand rapidly, creating space and time as it grew.
---

この 3 つの要求のそれぞれに同じ seed パラメータの 42 を使用すると、その他すべてのパラメータを同じにしながら、より一貫性のある結果を生成できます。

重要

再現可能な出力では決定論は保証されません。 seed パラメータと system_fingerprint が API 呼び出し全体で同じである場合でも、現時点では応答の変動の程度を観察することは珍しくありません。 max_tokens 値が大きい同一の API 呼び出しでは、通常、seed パラメータが設定されている場合でも、決定論的な応答が少なくなります。

パラメーターの詳細

seed は省略可能なパラメーターです。整数または null 値に設定できます。

この機能はプレビュー段階にあります。 指定した場合、このシステムでは、同じシードとパラメーターを使用してくり返される要求に対して同じ結果を返すように、決定論的にサンプリングすることに最善を尽くします。 決定性は保証されないため、バックエンドでの変更を監視するには、system_fingerprint 応答パラメーターを参照する必要があります。

system_fingerprint は文字列であり、chat completion オブジェクトの一部です。

このフィンガープリントは、モデルが実行されるバックエンド構成を表します。

これをシード要求パラメーターと共に使用すると、決定性に影響を与える可能性のある、バックエンドの変更がいつ行われたのかを把握できます。

system_fingerprint を含む完全な chat completion オブジェクトを表示するには、前の Python コードの既存の print ステートメントの横に print(response.model_dump_json(indent=2)) を追加するか、PowerShell の例の最後に $response | convertto-json -depth 5 を追加します。 この変更により、次の情報が出力の一部として追加されます。

出力

{
  "id": "chatcmpl-8LmLRatZxp8wsx07KGLKQF0b8Zez3",
  "choices": [
    {
      "finish_reason": "length",
      "index": 0,
      "message": {
        "content": "In the beginning, there was nothing but a vast emptiness, a void without form or substance. Then, from this nothingness, a singular event occurred that would change the course of existence forever—The Big Bang.\n\nAround 13.8 billion years ago, an infinitely hot and dense point, no larger than a single atom, began to expand at an inconceivable speed. This was the birth of our universe, a moment where time and space came into being. As this primordial fireball grew, it cooled, and the fundamental forces that govern the cosmos—gravity, electromagnetism, and the strong and weak nuclear forces—began to take shape.\n\nMatter coalesced into the simplest elements, hydrogen and helium, which later formed vast clouds in the expanding universe. These clouds, driven by the force of gravity, began to collapse in on themselves, creating the first stars. The stars were crucibles of nuclear fusion, forging heavier elements like carbon, nitrogen, and oxygen",
        "role": "assistant",
        "function_call": null,
        "tool_calls": null
      },
      "content_filter_results": {
        "hate": {
          "filtered": false,
          "severity": "safe"
        },
        "self_harm": {
          "filtered": false,
          "severity": "safe"
        },
        "sexual": {
          "filtered": false,
          "severity": "safe"
        },
        "violence": {
          "filtered": false,
          "severity": "safe"
        }
      }
    }
  ],
  "created": 1700201417,
  "model": "gpt-4",
  "object": "chat.completion",
  "system_fingerprint": "fp_50a4261de5",
  "usage": {
    "completion_tokens": 200,
    "prompt_tokens": 27,
    "total_tokens": 227
  },
  "prompt_filter_results": [
    {
      "prompt_index": 0,
      "content_filter_results": {
        "hate": {
          "filtered": false,
          "severity": "safe"
        },
        "self_harm": {
          "filtered": false,
          "severity": "safe"
        },
        "sexual": {
          "filtered": false,
          "severity": "safe"
        },
        "violence": {
          "filtered": false,
          "severity": "safe"
        }
      }
    }
  ]
}

その他の注意点

再現可能な出力を使用する場合は、chat completions 呼び出し全体で seed に同じ整数を設定する必要があります。 temperaturemax_tokens などの他のパラメーターも一致させる必要があります。