2024-08-07 09:22 上海
昨晚Openai发了点新的东西,总的来说就是能在API调用的时候更好地控制结构化输出了。
昨晚Openai发了点新的东西,总的来说就是能在API调用的时候更好地控制结构化输出了。下面内容基本是官网的沉浸式翻译,大家可以催开发马上更新一版了。
01- 背景
Last year at DevDay, we introduced JSON mode—a useful building block for developers looking to build reliable applications with our models. While JSON mode improves model reliability for generating valid JSON outputs, it does not guarantee that the model’s response will conform to a particular schema. Today we’re introducing Structured Outputs in the API, a new feature designed to ensure model-generated outputs will exactly match JSON Schemas provided by developers.
在去年的 DevDay 上,我们推出了 JSON 模式--对于希望使用我们的模型构建可靠应用程序的开发人员来说,这是一个非常有用的构建模块。虽然 JSON 模式提高了模型生成有效 JSON 输出的可靠性,但它并不能保证模型的响应符合特定模式。今天,我们在 API 中引入了结构化输出,这是一项新功能,旨在确保模型生成的输出与开发人员提供的 JSON 模式完全匹配。
Generating structured data from unstructured inputs is one of the core use cases for AI in today’s applications. Developers use the OpenAI API to build powerful assistants that have the ability to fetch data and answer questions via function calling(opens in a new window), extract structured data for data entry, and build multi-step agentic workflows that allow LLMs to take actions. Developers have long been working around the limitations of LLMs in this area via open source tooling, prompting, and retrying requests repeatedly to ensure that model outputs match the formats needed to interoperate with their systems. Structured Outputs solves this problem by constraining OpenAI models to match developer-supplied schemas and by training our models to better understand complicated schemas.
从非结构化输入生成结构化数据是当今应用中人工智能的核心用例之一。开发人员使用 OpenAI API 构建功能强大的助手,这些助手能够通过 函数调用(在新窗口中打开)获取数据和回答问题 ,提取结构化数据 用于数据录入,并构建多步骤代理工作流,使LLMs 能够采取行动。长期以来,开发人员一直在通过开源工具、提示和反复重试请求来解决LLMs 在这方面的局限性,以确保模型输出符合与系统互操作所需的格式。结构化输出解决了这一问题,它限制 OpenAI 模型与开发人员提供的模式相匹配,并训练我们的模型更好地理解复杂的模式。
On our evals of complex JSON schema following, our new model gpt-4o-2024-08-06 with Structured Outputs scores a perfect 100%. In comparison, gpt-4-0613 scores less than 40%.
在我们对复杂 JSON 模式的跟踪评估中, 带有结构化输出的 新模型 gpt-4o-2024-08-06 获得了 100%的满分。相比之下, gpt-4-0613 的 得分不到 40%。
02 - 使用
我们将在应用程序接口中引入两种形式的结构化输出:
1. Function calling: Structured Outputs via tools is available by setting strict: true within your function definition. This feature works with all models that support tools, including all models gpt-4-0613 and gpt-3.5-turbo-0613 and later. When Structured Outputs are enabled, model outputs will match the supplied tool definition.
1. 函数调用:在函数定义中 设置 strict: true ,即可 通过 工具进行结构化输出 。此功能适用于所有支持工具的模型,包括所有 gpt-4-0613 和 gpt-3.5-turbo-0613 及更高版本的模型。启用结构化输出后,模型输出将与提供的工具定义相匹配。
2. A new option for the response_format parameter: developers can now supply a JSON Schema via json_schema, a new option for the response_format parameter. This is useful when the model is not calling a tool, but rather, responding to the user in a structured way. This feature works with our newest GPT-4o models: gpt-4o-2024-08-06, released today, and gpt-4o-mini-2024-07-18. When a response_format is supplied with strict: true, model outputs will match the supplied schema.
2.响应格式 参数 的新选项 : 开发人员现在可以通过 响应格式 参数 的新选项 json_schema提供 JSON 模式 。这在模型不调用工具,而是以结构化方式响应用户时非常有用。该功能适用于我们最新的 GPT-4o 模型: 今天发布的 gpt-4o-2024-08-06 和 gpt-4o-mini-2024-07-18。当 提供的 response_format 带有 strict: true 时 ,模型输出将与提供的模式相匹配。
03 - 安全的结构化输出
Safety is a top priority for OpenAI—the new Structured Outputs functionality will abide by our existing safety policies and will still allow the model to refuse an unsafe request. To make development simpler, there is a new refusal string value on API responses which allows developers to programmatically detect if the model has generated a refusal instead of output matching the schema. When the response does not include a refusal and the model’s response has not been prematurely interrupted (as indicated by finish_reason), then the model’s response will reliably produce valid JSON matching the supplied schema.
安全是 OpenAI 的重中之重--新的结构化输出功能将遵守我们现有的安全策略,并仍然允许模型拒绝不安全的请求。为了简化开发, API 响应中 新增了 拒绝 字符串值,开发人员可以通过编程检测模型是否生成了拒绝,而不是与模式匹配的输出。当响应不包含拒绝且模型的响应未被过早中断(如 finish_reason 所示 )时,模型的响应将可靠地生成与所提供模式匹配的有效 JSON。
04 - 本地 SDK 支持
也支持了
05 - 使用示例
将最终答案与支持性推理或补充评注分开
从非结构化数据中提取结构化数据