分享方式:


了解如何使用可重現的輸出 (預覽)

根據預設,如果您多次向 Azure OpenAI 聊天完成模型詢問相同的問題,可能會得到不同的回應。 因此,回應會被視為不具決定性。 可重現的輸出是新的預覽功能,可讓您選擇性地變更預設行為,以協助產品更具決定性的輸出。

可重現的輸出支援

目前僅支援可重現的輸出,如下所示:

支援的模型

  • gpt-35-turbo (1106)
  • gpt-35-turbo (0125)
  • gpt-4 (1106-預覽)
  • gpt-4 (0125-預覽)
  • gpt-4 (turbo-2024-04-09)
  • gpt-4o (2024-05-13)

如需模型區域可用性的最新相關資訊,請參閱模型頁面

API 版本

API 版本中 2023-12-01-preview 首次新增可重現輸出的支援

範例

首先,我們將針對相同的問題產生三個回應,以示範聊天完成回應常見的變異性 (即使其他參數相同):

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.
---

針對三個要求中的每一個使用相同的 seed 參數 42,同時讓所有其他參數保持相同,我們就能產生更一致的結果。

重要

可重現輸出並不保證決定性。 即使是在種子參數和 system_fingerprint 在 API 呼叫之間相同的情況下,目前在回應中仍會觀察到一定程度變異性的情況並不罕見。 具有較大 max_tokens 值的相同 API 呼叫,通常即使在設定種子參數時,也會產生較不具決定性的回應。

參數詳細資料

seed 是選擇性參數,可設定為整數或 Null。

這項功能處於預覽狀態。 如果指定,我們的系統會盡最大努力以決定性方式取樣,讓具有相同種子和參數的重複要求應該傳回相同的結果。 不保證決定性,且您應參照 system_fingerprint 回應參數來監視後端的變更。

system_fingerprint 是字串,並且屬於聊天完成物件。

此指紋代表模型執行時使用的後端組態。

其可以搭配種子要求參數使用,以了解可能會影響決定性的後端變更是何時進行。

若要使用 system_fingerprint 來檢視完整的聊天完成物件,您可以將 print(response.model_dump_json(indent=2)) 新增至現有 print 陳述式旁的上一個 Python 程式碼,或在 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"
        }
      }
    }
  ]
}

其他考量

當您想要使用可重現的輸出時,必須在聊天完成呼叫之間將 seed 設定為相同的整數。 您也應該比對任何其他參數,例如 temperaturemax_tokens 等。