你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

Azure OpenAI 推理模型

Azure OpenAI 推理模型旨在通过提高焦点和功能来解决推理和解决问题的任务。 与之前的迭代相比,这些模型花费更多的时间处理和理解用户的请求,使它们在科学、编码和数学等领域非常强大。

推理模型的主要功能:

  • 复杂代码生成:能够生成算法并处理高级编码任务以支持开发人员。
  • 高级问题解决:非常适合全面的集思广益会话和应对多方面挑战。
  • 复杂的文档比较:非常适合用于分析合同、案例文件或法律文档,以确定细微的差异。
  • 指令跟随和工作流管理:特别适合管理需要较短背景信息的工作流。

先决条件

  • 已部署Azure OpenAI 推理模型。

  • 如果使用 REST 示例:

    • 安装Azure CLI。 有关详细信息,请参阅 install Azure CLI

    • 使用 az login 登录,然后生成一个持有者令牌,并将其存储在 AZURE_OPENAI_AUTH_TOKEN 环境变量中。

      az account get-access-token --resource https://cognitiveservices.azure.com --query accessToken -o tsv
      

使用

这些模型 当前不支持与 使用聊天完成 API 的其他模型相同的参数集。

聊天完成 API

using Azure.Identity;
using OpenAI;
using OpenAI.Chat;
using System.ClientModel.Primitives;

#pragma warning disable OPENAI001 //currently required for token based authentication

BearerTokenPolicy tokenPolicy = new(
    new DefaultAzureCredential(),
    "https://ai.azure.com/.default");

ChatClient client = new(
    model: "o4-mini",
    authenticationPolicy: tokenPolicy,
    options: new OpenAIClientOptions()
    {

        Endpoint = new Uri("https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1")
    }
);

ChatCompletionOptions options = new ChatCompletionOptions
{
    MaxOutputTokenCount = 100000
};

ChatCompletion completion = client.CompleteChat(
         new DeveloperChatMessage("You are a helpful assistant"),
         new UserChatMessage("Tell me about the bitter lesson")
    );

Console.WriteLine($"[ASSISTANT]: {completion.Content[0].Text}");

推理工作

注意

推理模型 reasoning_tokens 包含在 completion_tokens_details 模型响应中。 这些是隐藏令牌,这些令牌不会作为消息响应内容的一部分返回,但模型使用它们来帮助生成请求的最终答案。 reasoning_effort 可以设置为 lowmediumhigh,适用于除 o1-mini 外的所有推理模型。 工作量设置越高,模型处理请求所需的时间越长,通常会导致生成更多的reasoning_tokens

开发人员消息

开发人员消息("role": "developer")在功能上与系统消息相同。

将开发人员消息添加到前面的代码示例如下所示:


using Azure.Identity;
using OpenAI;
using OpenAI.Chat;
using System.ClientModel.Primitives;

#pragma warning disable OPENAI001 //currently required for token based authentication

BearerTokenPolicy tokenPolicy = new(
    new DefaultAzureCredential(),
    "https://ai.azure.com/.default");

ChatClient client = new(
    model: "o4-mini",
    authenticationPolicy: tokenPolicy,
    options: new OpenAIClientOptions()
    {

        Endpoint = new Uri("https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1")
    }
);

ChatCompletionOptions options = new ChatCompletionOptions
{
    ReasoningEffortLevel = ChatReasoningEffortLevel.Low,
    MaxOutputTokenCount = 100000
};

ChatCompletion completion = client.CompleteChat(
         new DeveloperChatMessage("You are a helpful assistant"),
         new UserChatMessage("Tell me about the bitter lesson")
    );

Console.WriteLine($"[ASSISTANT]: {completion.Content[0].Text}");

推理摘要

将最新的推理模型与 响应 API 配合使用时,可以使用推理摘要参数来接收模型的思维推理链摘要。

重要

试图通过推理摘要参数以外的方法来提取原始推理是不被支持的,这可能会违反可接受使用政策,并在被检测到时导致限制或挂起。

using OpenAI;
using OpenAI.Responses;
using System.ClientModel.Primitives;
using Azure.Identity;

#pragma warning disable OPENAI001 //currently required for token based authentication

BearerTokenPolicy tokenPolicy = new(
    new DefaultAzureCredential(),
    "https://ai.azure.com/.default");

OpenAIResponseClient client = new(
    model: "o4-mini",
    authenticationPolicy: tokenPolicy,
    options: new OpenAIClientOptions()
    {
        Endpoint = new Uri("https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1")
    }
);

OpenAIResponse response = await client.CreateResponseAsync(
    userInputText: "What's the optimal strategy to win at poker?",
    new ResponseCreationOptions()
    {
        ReasoningOptions = new ResponseReasoningOptions()
        {
            ReasoningEffortLevel = ResponseReasoningEffortLevel.High,
            ReasoningSummaryVerbosity = ResponseReasoningSummaryVerbosity.Auto,
        },
    });

// Get the reasoning summary from the first OutputItem (ReasoningResponseItem)
Console.WriteLine("=== Reasoning Summary ===");
foreach (var item in response.OutputItems)
{
    if (item is ReasoningResponseItem reasoningItem)
    {
        foreach (var summaryPart in reasoningItem.SummaryParts)
        {
            if (summaryPart is ReasoningSummaryTextPart textPart)
            {
                Console.WriteLine(textPart.Text);
            }
        }
    }
}

Console.WriteLine("\n=== Assistant Response ===");
// Get the assistant's output
Console.WriteLine(response.GetOutputText());

注意

即使启用,也不能保证为每个步骤/请求生成推理摘要。 这是预期行为。

Python lark

GPT-5 系列推理模型能够调用新的 custom_tool,这个叫做 lark_tool。 此工具基于 Python lark,可用于更灵活的模型输出约束。

响应 API

{
  "model": "gpt-5-2025-08-07",
  "input": "please calculate the area of a circle with radius equal to the number of 'r's in strawberry",
  "tools": [
    {
      "type": "custom",
      "name": "lark_tool",
      "format": {
        "type": "grammar",
        "syntax": "lark",
        "definition": "start: QUESTION NEWLINE ANSWER\nQUESTION: /[^\\n?]{1,200}\\?/\nNEWLINE: /\\n/\nANSWER: /[^\\n!]{1,200}!/"
      }
    }
  ],
  "tool_choice": "required"
}

Microsoft Entra ID:

from openai import OpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider

token_provider = get_bearer_token_provider(
    DefaultAzureCredential(), "https://ai.azure.com/.default"
)

client = OpenAI(  
  base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",  
  api_key=token_provider,
)

response = client.responses.create(  
    model="gpt-5",  # replace with your model deployment name  
    tools=[  
        {  
            "type": "custom",
            "name": "lark_tool",
            "format": {
                "type": "grammar",
                "syntax": "lark",
                "definition": "start: QUESTION NEWLINE ANSWER\nQUESTION: /[^\\n?]{1,200}\\?/\nNEWLINE: /\\n/\nANSWER: /[^\\n!]{1,200}!/"
            }
        }  
    ],  
    input=[{"role": "user", "content": "Please calculate the area of a circle with radius equal to the number of 'r's in strawberry"}],  
)  

print(response.model_dump_json(indent=2))  

API 密钥:

import os
from openai import OpenAI

client = OpenAI(  
  base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
  api_key=os.getenv("AZURE_OPENAI_API_KEY")  
)

response = client.responses.create(  
    model="gpt-5",  # replace with your model deployment name  
    tools=[  
        {  
            "type": "custom",
            "name": "lark_tool",
            "format": {
                "type": "grammar",
                "syntax": "lark",
                "definition": "start: QUESTION NEWLINE ANSWER\nQUESTION: /[^\\n?]{1,200}\\?/\nNEWLINE: /\\n/\nANSWER: /[^\\n!]{1,200}!/"
            }
        }  
    ],  
    input=[{"role": "user", "content": "Please calculate the area of a circle with radius equal to the number of 'r's in strawberry"}],  
)  

print(response.model_dump_json(indent=2))  
  

输出

{
  "id": "resp_689a0cf927408190b8875915747667ad01c936c6ffb9d0d3",
  "created_at": 1754926332.0,
  "error": null,
  "incomplete_details": null,
  "instructions": null,
  "metadata": {},
  "model": "gpt-5",
  "object": "response",
  "output": [
    {
      "id": "rs_689a0cfd1c888190a2a67057f471b5cc01c936c6ffb9d0d3",
      "summary": [],
      "type": "reasoning",
      "encrypted_content": null,
      "status": null
    },
    {
      "id": "msg_689a0d00e60c81908964e5e9b2d6eeb501c936c6ffb9d0d3",
      "content": [
        {
          "annotations": [],
          "text": "“strawberry” has 3 r’s, so the radius is 3.\nArea = πr² = π × 3² = 9π ≈ 28.27 square units.",
          "type": "output_text",
          "logprobs": null
        }
      ],
      "role": "assistant",
      "status": "completed",
      "type": "message"
    }
  ],
  "parallel_tool_calls": true,
  "temperature": 1.0,
  "tool_choice": "auto",
  "tools": [
    {
      "name": "lark_tool",
      "parameters": null,
      "strict": null,
      "type": "custom",
      "description": null,
      "format": {
        "type": "grammar",
        "definition": "start: QUESTION NEWLINE ANSWER\nQUESTION: /[^\\n?]{1,200}\\?/\nNEWLINE: /\\n/\nANSWER: /[^\\n!]{1,200}!/",
        "syntax": "lark"
      }
    }
  ],
  "top_p": 1.0,
  "background": false,
  "max_output_tokens": null,
  "max_tool_calls": null,
  "previous_response_id": null,
  "prompt": null,
  "prompt_cache_key": null,
  "reasoning": {
    "effort": "medium",
    "generate_summary": null,
    "summary": null
  },
  "safety_identifier": null,
  "service_tier": "default",
  "status": "completed",
  "text": {
    "format": {
      "type": "text"
    }
  },
  "top_logprobs": null,
  "truncation": "disabled",
  "usage": {
    "input_tokens": 139,
    "input_tokens_details": {
      "cached_tokens": 0
    },
    "output_tokens": 240,
    "output_tokens_details": {
      "reasoning_tokens": 192
    },
    "total_tokens": 379
  },
  "user": null,
  "content_filters": null,
  "store": true
}

聊天完成

{
  "messages": [
    {
      "role": "user",
      "content": "Which one is larger, 42 or 0?"
    }
  ],
  "tools": [
    {
      "type": "custom",
      "name": "custom_tool",
      "custom": {
        "name": "lark_tool",
        "format": {
          "type": "grammar",
          "grammar": {
            "syntax": "lark",
            "definition": "start: QUESTION NEWLINE ANSWER\nQUESTION: /[^\\n?]{1,200}\\?/\nNEWLINE: /\\n/\nANSWER: /[^\\n!]{1,200}!/"
          }
        }
      }
    }
  ],
  "tool_choice": "required",
  "model": "gpt-5-2025-08-07"
}

可用 性

区域可用性

模型 地区 受限访问
gpt-chat-latest 全局标准
美国东部 2
瑞典中部
美国中南部
波兰中部
不需要访问请求。
gpt-5.5 模型可用性 不需要访问请求。 需要根据配额层提出申请。 第 5 层和第 6 层订阅默认具有配额。
gpt-5.4-mini 模型可用性 不需要访问请求。
gpt-5.4-nano 模型可用性 不需要访问请求。
gpt-5.4-pro 模型可用性 请求访问: 受限访问模型应用程序。 如果已有权访问受限访问模型,则无需请求。
gpt-5.4 模型可用性 请求访问: 受限访问模型应用程序。 如果已有权访问受限访问模型,则无需请求。
gpt-5.3-codex 模型可用性 请求访问: 受限访问模型应用程序。 如果已有权访问受限访问模型,则无需请求。
gpt-5.2-codex 模型可用性 请求访问: 受限访问模型应用程序。 如果已有权访问受限访问模型,则无需请求。
gpt-5.2 模型可用性 请求访问: 受限访问模型应用程序。 如果已有权访问受限访问模型,则无需请求。
gpt-5.1-codex-max 模型可用性 此模型不再限制访问。
gpt-5.1 模型可用性 此模型不再限制访问。
gpt-5.1-chat 模型可用性 不需要访问请求。
gpt-5.1-codex 模型可用性 此模型不再限制访问。
gpt-5.1-codex-mini 模型可用性 不需要访问请求。
gpt-5-pro 模型可用性 此模型不再限制访问。
gpt-5-codex 模型可用性 此模型不再限制访问。
gpt-5 模型可用性 此模型不再限制访问。
gpt-5-mini 模型可用性 不需要访问请求。
gpt-5-nano 模型可用性 不需要访问请求。
o3-pro 模型可用性 请求访问: 受限访问模型应用程序。 如果已有权访问受限访问模型,则无需请求。
codex-mini 模型可用性 不需要访问请求。
o4-mini 模型可用性 使用此模型的核心功能不需要访问请求。

请求访问: o4 微型推理摘要功能
o3 模型可用性 请求访问: 受限访问模型应用程序
o3-mini 模型可用性 此模型不再限制访问。
o1 模型可用性 此模型不再限制访问。

API 和功能支持

特征 gpt-5.52026-04-24 gpt-5.4-nano2026-03-17 gpt-5.4-mini2026-03-17 gpt-5.4-pro gpt-5.42026-03-05 gpt-5.3-codex2026-02-24 gpt-5.2-codex2026-01-14 gpt-5.22025-12-11 gpt-5.1-codex-max2025-12-04 gpt-5.12025-11-13 gpt-5.1-chat2025-11-13 gpt-5.1-codex2025-11-13 gpt-5.1-codex-mini2025-11-13 gpt-5-pro2025-10-06 gpt-5-codex2025-09-011 gpt-52025-08-07 gpt-5-mini2025-08-07 gpt-5-nano2025-08-07
开发人员消息
结构化输出
上下文窗口 1,050,000

输入:
922,000
输出:
128,000
400,000

输入:272,000
输出:128,000
400,000

输入:272,000
输出:128,000
1,050,000

输入:
922,000
输出:
128,000
1,050,000

输入:
922,000
输出:
128,000
400,000

输入:272,000
输出:128,000
400,000

输入:272,000
输出:128,000
400,000

输入:272,000
输出:128,000
400,000

输入:272,000
输出:128,000
400,000

输入:272,000
输出:128,000
128,000

输入:111,616
输出:16,384
400,000

输入:272,000
输出:128,000
400,000

输入:272,000
输出:128,000
400,000

输入:272,000
输出:128,000
400,000

输入:272000
输出:128,000
400,000

输入:272000
输出:128,000
400,000

输入:272,000
输出:128,000
400,000

输入:272,000
输出:128,000
推理工作7 6 4 5
图像输入
聊天完成 API - - - - - - - -
响应 API
功能/工具
并行工具调用1 - -
max_completion_tokens 2 - - - - - - - -
系统消息 3
推理摘要
流媒体 -

1 并行工具调用在 reasoning_effort 设置为 minimal 时不被支持

2 推理模型仅在使用聊天完成 API 时使用 max_completion_tokens 参数。 与响应 API 一起使用 max_output_tokens

3 最新的推理模型支持系统消息,以便更轻松地迁移。 不应在同一 API 请求中使用开发人员消息和系统消息。

4gpt-5.1reasoning_effort 默认为 none. 从以前的推理模型升级到gpt-5.1时,请记住,如果希望推理努力(reasoning_effort)进行,则可能需要更新代码以明确传递推理努力级别。

5gpt-5-pro 仅支持 reasoning_efforthigh,即使未显式传递给模型,这也是默认值。

6gpt-5.1-codex-max 添加了对新 reasoning_effort 级别的支持,这是推理工作可以设置为的最高级别 xhigh

7gpt-5.2、、gpt-5.1gpt-5.1-codexgpt-5.1-codex-max支持gpt-5.1-codex-mini'None'作为参数的值reasoning_effort。 如果要使用这些模型生成响应而不进行推理,请设置 reasoning_effort='None'。 此设置可以提高速度。

新的 GPT-5 推理功能

功能 描述
reasoning_effort xhigh 仅在与 gpt-5.1-codex-max 一起使用时支持。
minimal 仅支持原始 GPT-5 推理模型。 minimal 不支持 gpt-5.1 或更高的 *

选项none、、minimallowmediumhighxhigh
verbosity 提供更细致地控制模型输出简洁程度的新参数。

选项:low、、mediumhigh.
preamble GPT-5 系列推理模型在执行函数/工具调用之前,能够花费额外的时间“思考”。

在进行此规划时,模型可以通过一个名为 preamble 的新对象,在模型响应中提供有关规划步骤的洞察。

虽然您可以通过使用instructions参数并传递类似于“每次函数调用之前必须广泛规划”这样的内容来鼓励模型,但不能保证模型响应中会生成引导语。 始终向用户输出计划,然后再调用任何函数”
允许的工具 可以在下面 tool_choice 指定多个工具,而不是只指定一个工具。
自定义工具类型 启用原始文本(非 json)输出
lark_tool 允许你使用 Python lark 的一些功能来更灵活地约束模型响应

* gpt-5-codex 也不支持 reasoning_effortminimal

有关详细信息,我们还建议阅读 OpenAI 的 GPT-5 提示指南 及其 GPT-5 功能指南

注意

  • 为避免超时,建议使用o3-pro
  • o3-pro 当前不支持映像生成。

不支持

推理模型当前不支持以下各项:

  • temperature、、top_ppresence_penaltyfrequency_penaltylogprobstop_logprobs、、 logit_biasmax_tokens

Markdown 输出

默认情况下, o3-mini 模型 o1 不会尝试生成包含 markdown 格式的输出。 当希望模型输出 markdown 代码块中包含的代码时,此行为是不受欢迎的常见用例。 当模型生成不带 markdown 格式的输出时,在交互体验中会丢失语法高亮和可复制代码块等功能。 若要重写此新的默认行为,并鼓励在模型响应中包含 Markdown,请将字符串 Formatting re-enabled 添加到开发人员消息的开头。

添加到 Formatting re-enabled 开发人员消息的开头不能保证模型在其响应中包含 markdown 格式,只会增加可能性。 我们从内部测试中发现,单独使用Formatting re-enabled搭配o1模型的效果相比搭配o3-mini模型要差。

为了提高 Formatting re-enabled 的性能,您可以在开发人员消息的开头进行进一步的补充,这样通常会得到所需的输出结果。 可以尝试在您的开发人员消息开头添加更具描述性的初始指令,而不是仅仅添加Formatting re-enabled,以下是一些示例:

  • Formatting re-enabled - please enclose code blocks with appropriate markdown tags.
  • Formatting re-enabled - code output should be wrapped in markdown.

根据预期输出,可能需要进一步自定义初始开发人员消息,以针对特定用例。