了解如何使用可重现的输出(预览)

在本文中

  1. 可重复的输出支持
  2. 例子
  3. 参数详情
  4. 其他注意事项

默认情况下,如果您多次向 Azure OpenAI 聊天完成模型询问同一问题,您可能会得到不同的响应。因此,响应被认为是不确定的。可重复输出是一项新的预览功能,允许您有选择地更改默认行为以产生更具确定性的输出。

可重复的输出支持

目前仅支持以下内容的可重现输出:

支持机型

API版本

  • 2023-12-01-preview

例子

首先,我们将对同一问题生成三个响应,以展示聊天完成响应常见的可变性,即使其他参数相同:


Python

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="2023-12-01-preview"
)

for i in range(3):
  print(f'Story Version {i + 1}\n---')
    
  response = client.chat.completions.create(
    model="gpt-4-1106-preview", # Model = should match the deployment name you chose for your 1106-preview model deployment
    #seed=42,
    temperature=0.7,
    max_tokens =200, 
    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

PowerShell

$openai = @{
   api_key     = $Env:AZURE_OPENAI_API_KEY
   api_base    = $Env:AZURE_OPENAI_ENDPOINT # like the following https://YOUR_RESOURCE_NAME.openai.azure.com/
   api_version = '2023-12-01-preview' # may change in the future
   name        = 'YOUR-DEPLOYMENT-NAME-HERE' # name you chose for your deployment
}

$headers = @{
  'api-key' = $openai.api_key
}

$messages  = @()
$messages += @{
  role     = 'system'
  content  = 'You are a helpful assistant.'
}
$messages += @{
  role     = 'user'
  content  = 'Tell me a story about how the universe began?'
}

$body         = @{
  #seed       = 42
  temperature = 0.7
  max_tokens  = 200
  messages    = $messages
} | ConvertTo-Json

$url = "$($openai.api_base)/openai/deployments/$($openai.name)/chat/completions?api-version=$($openai.api_version)"

for ($i=0; $i -le 2; $i++) {
  $response = Invoke-RestMethod -Uri $url -Headers $headers -Body $body -Method Post -ContentType 'application/json'
  write-host "Story Version $($i+1)`n---`n$($response.choices[0].message.content)`n---`n"
}

输出

Story Version 1
---
In the beginning, there was nothingness, a vast expanse of empty space, a blank canvas waiting to be painted with the wonders of existence. Then, approximately 13.8 billion years ago, something extraordinary happened, an event that would mark the birth of the universe – the Big Bang.

The Big Bang was not an explosion in the conventional sense but rather an expansion, an incredibly rapid stretching of space that took place everywhere in the universe at once. In just a fraction of a second, the universe grew from smaller than a single atom to an incomprehensibly large expanse.

In these first moments, the universe was unimaginably hot and dense, filled with a seething soup of subatomic particles and radiant energy. As the universe expanded, it began to cool, allowing the first particles to form. Protons and neutrons came together to create the first simple atomic nuclei in a process known as nucleosynthesis.

For hundreds of thousands of years, the universe continued to cool and expand
---

Story Version 2
---
Once upon a time, in the vast expanse of nothingness, there was a moment that would come to define everything. This moment, a tiny fraction of a second that would be forever known as the Big Bang, marked the birth of the universe as we know it.

Before this moment, there was no space, no time, just an infinitesimally small point of pure energy, a singularity where all the laws of physics as we understand them did not apply. Then, suddenly, this singular point began to expand at an incredible rate. In a cosmic symphony of creation, matter, energy, space, and time all burst forth into existence.

The universe was a hot, dense soup of particles, a place of unimaginable heat and pressure. It was in this crucible of creation that the simplest elements were formed. Hydrogen and helium, the building blocks of the cosmos, came into being.

As the universe continued to expand and cool, these primordial elements began to co
---

Story Version 3
---
Once upon a time, in the vast expanse of nothingness, there was a singularity, an infinitely small and infinitely dense point where all the mass and energy of what would become the universe were concentrated. This singularity was like a tightly wound cosmic spring holding within it the potential of everything that would ever exist.

Then, approximately 13.8 billion years ago, something extraordinary happened. This singularity began to expand in an event we now call the Big Bang. In just a fraction of a second, the universe grew exponentially during a period known as cosmic inflation. It was like a symphony's first resounding chord, setting the stage for a cosmic performance that would unfold over billions of years.

As the universe expanded and cooled, the fundamental forces of nature that we know today – gravity, electromagnetism, and the strong and weak nuclear forces – began to take shape. Particles of matter were created and began to clump together under the force of gravity, forming the first atoms
---

通过对seed三个请求中的每一个使用相同的参数 42,我们能够产生更加一致(在本例中是相同)的结果。

参数详情

seed是可选参数,可以设置为整数或空。

此功能处于预览状态。如果指定,我们的系统将尽最大努力进行确定性采样,以便使用相同种子和参数的重复请求应返回相同的结果。无法保证确定性,您应该参考system_fingerprint响应参数来监控后端的变化。

system_fingerprint是一个字符串,是聊天完成对象的一部分。

该指纹代表模型运行时使用的后端配置。

它可以与种子请求参数一起使用,以了解何时进行了可能影响确定性的后端更改。

要使用 查看完整的聊天完成对象system_fingerprint,您可以添加 print(response.model_dump_json(indent=2))到现有 print 语句旁边或$response | convertto-json -depth 5PowerShell 示例末尾的先前 Python 代码。此更改会导致以下附加信息成为输出的一部分:

输出

JSON

{
  "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等。

反馈


即将推出:整个 2024 年,我们将逐步淘汰 GitHub Issues 作为内容反馈机制,并用新的反馈系统取而代之。有关详细信息,请参阅:Provide feedback for Microsoft Learn content - Contributor guide | Microsoft Learn

 

  • 9
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值