AI知识库

53AI知识库

学习大模型的前沿技术与行业应用场景


OpenAI 发布新特性,Chatgpt API 现已支持 JSON 格式化输出
发布日期:2024-08-07 07:14:14 浏览次数: 2723


在今天,OpenAI 的 CEO 山姆·奥特曼宣布推出了一项新的特性:支持 JSON 格式化输出。这一功能的引入将大大提升在实际业务场景中使用模型输出结构化数据的能力。

背景介绍


去年 DevDay 上,OpenAI 推出了 JSON 模式。这是一种对希望通过模型构建可靠应用程序的开发者非常有用的工具。虽然 JSON 模式提高了模型生成有效 JSON 输出的可靠性,但它不能保证响应完全符合特定的 schema。为了解决这个问题,OpenAI 推出了 API 的结构化输出功能,确保模型输出完全匹配开发者提供的 JSON Schema。

从非结构化输入生成结构化数据是当今应用程序中 AI 的核心应用场景之一。开发者利用 OpenAI API 构建强大的助手,这些助手能够通过函数调用获取数据并回答问题,提取结构化数据以进行数据录入,并构建多步骤的代理工作流程,使LLMs能够采取行动。长期以来,开发者通过开源工具、提示工程和反复重试请求来克服LLMs在这一领域的局限性,以确保模型输出符合与其系统互操作所需的格式。结构化输出通过约束 OpenAI 模型匹配开发者提供的模式,并通过训练我们的模型更好地理解复杂的模式,解决了这一问题。

在最近的评估中,新模型 gpt-4o-2024-08-06 凭借结构化输出获得了完美的 100%评分。相比之下,gpt-4-0613 的得分不足 40%。


如何使用结构化输出


1. Function calling:

通过在函数定义中设置 `strict: true`,可以实现通过工具的结构化输出。此功能适用于支持工具的所有模型,包括所有 `gpt-4-0613` 和 `gpt-3.5-turbo-0613` 及后续版本。启用结构化输出后,模型输出将与提供的工具定义相匹配。

示例请求

POST /v1/chat/completions{"model": "gpt-4o-2024-08-06","messages": [{"role": "system","content": "You are a helpful assistant. The current date is August 6, 2024. You help users query for the data they are looking for by calling the query function."},{"role": "user","content": "look up all my orders in may of last year that were fulfilled but not delivered on time"}],"tools": [{"type": "function","function": {"name": "query","description": "Execute a query.","strict": true,"parameters": {"type": "object","properties": {"table_name": {"type": "string","enum": ["orders"]},"columns": {"type": "array","items": {"type": "string","enum": ["id","status","expected_delivery_date","delivered_at","shipped_at","ordered_at","canceled_at"]}},"conditions": {"type": "array","items": {"type": "object","properties": {"column": {"type": "string"},"operator": {"type": "string","enum": ["=", ">", "<", ">=", "<=", "!="]},"value": {"anyOf": [{"type": "string"},{"type": "number"},{"type": "object","properties": {"column_name": {"type": "string"}},"required": ["column_name"],"additionalProperties": false}]}},"required": ["column", "operator", "value"],"additionalProperties": false}},"order_by": {"type": "string","enum": ["asc", "desc"]}},"required": ["table_name", "columns", "conditions", "order_by"],"additionalProperties": false}}}]}

示例输出 JSON



{"table_name": "orders","columns": ["id", "status", "expected_delivery_date", "delivered_at"],"conditions": [{"column": "status","operator": "=","value": "fulfilled"},{"column": "ordered_at","operator": ">=","value": "2023-05-01"},{"column": "ordered_at","operator": "<","value": "2023-06-01"},{"column": "delivered_at","operator": ">","value": {"column_name": "expected_delivery_date"}}],"order_by": "asc"}



2. response_format 参数的新选项

开发者现在可以通过 `json_schema` 为 `response_format` 参数提供一个 JSON Schema。当模型不调用工具,而是以结构化方式响应用户时,这一功能尤为有用。此功能适用于我们最新的 gpt-4o 系列模型:今日发布的 gpt-4o-2024-08-06 和 gpt-4o-mini-2024-07-18。当 `response_format` 设置为 `strict: true` 时,模型输出将匹配提供的 Schema。

示例请求

POST /v1/chat/completions{"model": "gpt-4o-2024-08-06","messages": [{"role": "system","content": "You are a helpful math tutor."},{"role": "user","content": "solve 8x + 31 = 2"}],"response_format": {"type": "json_schema","json_schema": {"name": "math_response","strict": true,"schema": {"type": "object","properties": {"steps": {"type": "array","items": {"type": "object","properties": {"explanation": {"type": "string"},"output": {"type": "string"}},"required": ["explanation", "output"],"additionalProperties": false}},"final_answer": {"type": "string"}},"required": ["steps", "final_answer"],"additionalProperties": false}}}}

示例输出 JSON

{"steps": [{"explanation": "Subtract 31 from both sides to isolate the term with x.","output": "8x + 31 - 31 = 2 - 31"},{"explanation": "This simplifies to 8x = -29.","output": "8x = -29"},{"explanation": "Divide both sides by 8 to solve for x.","output": "x = -29 / 8"}],"final_answer": "x = -29 / 8"}


安全结构化输出


安全性始终是 OpenAI 的首要任务。新推出的结构化输出功能严格遵循现有的安全政策,并允许模型在遇到不安全请求时进行拒绝。为了方便开发者检测模型是否生成了拒绝而非与 schema 匹配的输出,API 响应中新增了一个拒绝字符串值。

当模型的响应不包含拒绝,并且响应没有因任何原因被过早中断(如通过 finish_reason 指示),则模型的响应将可靠地产生与提供的 schema 完全匹配的有效 JSON。这一功能使得开发者可以以编程方式确保模型响应的准确性和安全性。

示例拒绝响应


{"id": "chatcmpl-9nYAG9LPNonX8DAyrkwYfemr3C8HC","object": "chat.completion","created": 1721596428,"model": "gpt-4o-2024-08-06","choices": [{"index": 0,"message": {"role": "assistant","refusal": "I'm sorry, I cannot assist with that request."},"logprobs": null,"finish_reason": "stop"}],"usage": {"prompt_tokens": 81,"completion_tokens": 11,"total_tokens": 92},"system_fingerprint": "fp_3407719c7f"}


原生 SDK 支持


OpenAI 的 Python 和 Node SDK 已更新以原生支持结构化输出。通过将 schema 作为工具或响应格式提供,开发者可以轻松地使用 Pydantic 或 Zod 对象。这使得 SDK 能够自动将数据类型转换为支持的 JSON schema,并将 JSON 响应反序列化为类型化的数据结构。

以下是如何通过函数调用实现结构化输出的示例:


from enum import Enumfrom typing import Union
from pydantic import BaseModel
import openaifrom openai import OpenAI

class Table(str, Enum):orders = "orders"customers = "customers"products = "products"

class Column(str, Enum):id = "id"status = "status"expected_delivery_date = "expected_delivery_date"delivered_at = "delivered_at"shipped_at = "shipped_at"ordered_at = "ordered_at"canceled_at = "canceled_at"

class Operator(str, Enum):eq = "="gt = ">"lt = "<"le = "<="ge = ">="ne = "!="

class OrderBy(str, Enum):asc = "asc"desc = "desc"

class DynamicValue(BaseModel):column_name: str

class Condition(BaseModel):column: stroperator: Operatorvalue: Union[str, int, DynamicValue]

class Query(BaseModel):table_name: Tablecolumns: list[Column]conditions: list[Condition]order_by: OrderBy

client = OpenAI()
completion = client.beta.chat.completions.parse(model="gpt-4o-2024-08-06",messages=[{"role": "system","content": "You are a helpful assistant. The current date is August 6, 2024. You help users query for the data they are looking for by calling the query function.",},{"role": "user","content": "look up all my orders in may of last year that were fulfilled but not delivered on time",},],tools=[openai.pydantic_function_tool(Query),],)
print(completion.choices[0].message.tool_calls[0].function.parsed_arguments)


原生结构化输出支持也可用于 `response_format`。

from pydantic import BaseModel
from openai import OpenAI

class Step(BaseModel):explanation: stroutput: str

class MathResponse(BaseModel):steps: list[Step]final_answer: str

client = OpenAI()
completion = client.beta.chat.completions.parse(model="gpt-4o-2024-08-06",messages=[{"role": "system", "content": "You are a helpful math tutor."},{"role": "user", "content": "solve 8x + 31 = 2"},],response_format=MathResponse,)
message = completion.choices[0].messageif message.parsed:print(message.parsed.steps)print(message.parsed.final_answer)else:print(message.refusal)


一些使用技巧


为模型提供一个单独的“思考链”字段,有助于提升最终回复的质量。

{"model": "gpt-4o-2024-08-06","messages": [{"role": "system","content": "You are a helpful assistant"},{"role": "user","content": "9.11 and 9.9 -- which is bigger?"}],"response_format": {"type": "json_schema","json_schema": {"name": "reasoning_schema","strict": true,"schema": {"type": "object","properties": {"reasoning_steps": {"type": "array","items": {"type": "string"},"description": "The reasoning steps leading to the final conclusion."},"answer": {"type": "string","description": "The final answer, taking into account the reasoning steps."}},"required": ["reasoning_steps", "answer"],"additionalProperties": false}}}}

输出

{"reasoning_steps": ["First step is to compare the numbers 9.11 and 9.9.","Both numbers have the same whole number part, which is 9.","To compare the decimal parts, convert them to the same number of decimal places.","9.11 has two decimal places: it is 9.11.","9.9 has one decimal place: it can be rewritten as 9.90.","Now, compare 9.11 and 9.90 by looking at the decimal parts.","Compare 11 with 90.","90 is greater than 11, so 9.90 is greater than 9.11."],"answer": "9.9 is bigger than 9.11."}


技术原理



OpenAI 采用了一种双管齐下的方法来提高模型输出与 JSON Schema 匹配的可靠性:
模型训练最新的 gpt-4o-2024-08-06 模型经过训练,可以更好地理解复杂的 Schema 并生成与之匹配的输出。尽管模型性能已显著提升,在基准测试中达到了 93%的准确性,但其行为的固有不确定性仍然存在。为了确保开发者构建应用的稳健性,OpenAI 提供了一种确定性的方法来约束模型的输出,从而实现 100%的可靠性。
受限解码
OpenAI 采用了一种称为约束采样或约束解码的技术。默认情况下,模型生成输出时完全不受约束,可能从词汇表中选择任何标记作为下一个输出。这种灵活性可能导致错误,例如,在生成有效 JSON 时随意插入无效字符。
为了避免此类错误,OpenAI 使用动态约束解码的方法,强制模型仅选择符合提供模式的有效标记。在模型生成每个标记后,推理引擎根据前一个标记和模式规则确定下一个有效标记。这一方法通过屏蔽无效标记的可能性,确保生成的输出始终符合提供的模式。
为了实现这一点,OpenAI 将提供的 JSON Schema 转换为上下文无关文法(CFG)。CFG 是一组规则,定义了语言的有效结构。对于每个 JSON Schema,OpenAI 计算出一个代表该模式的语法,并在采样期间高效地访问预处理的组件。
这种方法不仅使生成的输出更准确,还减少了不必要的延迟。首次请求新模式可能会有额外的处理时间,但随后的请求将受益于缓存机制,实现快速响应。
备选方案
除了 CFG 方法,其他方法通常使用有限状态机(FSM)或正则表达式来进行受限解码。然而,这些方法在动态更新有效标记时的能力有限。特别是对于复杂的嵌套或递归数据结构,FSM 通常难以处理。
OpenAI 的 CFG 方法在表达复杂模式时表现出色。例如,支持递归模式的 JSON Schema 在 OpenAI API 上已得到实现,但无法通过 FSM 方法表达。
限制与约束
在使用结构化输出时,开发者需注意以下限制:
JSON Schema 子集结构化输出仅支持 JSON Schema 的一个子集,以确保最佳性能。具体限制可参考 OpenAI 的官方文档.
首次请求延迟新模式的首次请求可能带来额外延迟,因为需要对模式进行处理并缓存以供后续使用。处理时间通常在 10 秒内,但复杂模式可能需要长达一分钟。
拒绝不安全请求当模型拒绝不安全请求时,可能无法遵循模式。此时,返回消息将包含拒绝布尔值以指示这一点。
生成过程限制若生成过程达到最大令牌数或其他停止条件,模型可能无法遵循模式。
模型错误结构化输出不能完全防止所有类型的模型错误。开发者可通过提供系统指令中的示例或将任务拆分为更简单的子任务来减少错误。
并行函数调用不兼容结构化输出与并行函数调用不兼容。生成并行函数调用时,可能不符合提供的模式。建议禁用并行函数调用,使用 parallel_tool_calls: false 设置。
数据保留政策结构化输出与 JSON 模式不符合零数据保留(ZDR)资格。
可用性与成本
结构化输出功能现已在 API 中正式发布。支持函数调用的所有模型均可实现结构化输出。这包括最新的 gpt-4o 和 gpt-4o-mini 模型,以及 gpt-4-0613 和 gpt-3.5-turbo-0613 等支持函数调用的微调模型。此功能可在 Chat Completions API、Assistants API 和 Batch API 上使用,并兼容视觉输入。
与 gpt-4o-2024-05-13 版本相比,gpt-4o-2024-08-06 版本在成本上也更具优势,开发者可以在输入端节省 50%的成本(每百万输入令牌 $2.50),在输出端节省 33%的成本(每百万输出令牌 $10.00)


53AI,企业落地应用大模型首选服务商

产品:大模型应用平台+智能体定制开发+落地咨询服务

承诺:先做场景POC验证,看到效果再签署服务协议。零风险落地应用大模型,已交付160+中大型企业

联系我们

售前咨询
186 6662 7370
预约演示
185 8882 0121

微信扫码

与创始人交个朋友

回到顶部

 
扫码咨询