原生的来了!OpenAI 在 API 中引入 JSON 结构化输出功能
OpenAI 在其 API 中引入了结构化输出功能,这意味着模型的输出可以可靠地遵循开发人员提供的 JSON 模式。
对复杂 JSON 模式进行评估时,具有结构化输出的新模型 gpt-4o-2024-08-06 得分为 100%。相比之下,gpt-4-0613 得分不到 40%。
这一功能包括两种形式:
- 函数调用:通过在函数定义中设置
strict: true
可以使用工具的结构化输出。此功能适用于支持工具的所有型号大模型,包括所有型号 gpt-4-0613 和 gpt-3.5-turbo-0613 及更高版本。启用结构化输出后,模型输出将与提供的工具定义匹配。 -
response_format
参数新选项:开发人员现在可以使用新参数 JSON 模式json_schema
。此功能适用于最新的 GPT-4o 模型:gpt-4o-2024-08-06
、gpt-4o-mini-2024-07-18
。当response_format
设定strict: true
,模型输出将与提供的模式匹配。
函数调用通过在函数定义中设置结构化输出,使模型输出与提供的工具定义相匹配,适用于所有支持工具的模型。参数 response_format
允许开发人员通过提供 JSON 模式来约束模型的响应格式,适用于最新的 GPT-4o 模型。此外,新的结构化输出功能遵循 OpenAI 的安全政策,允许模型拒绝不安全的请求,并通过新的字符串值 refusal
在 API 响应中允许开发人员以编程方式检测模型的拒绝。
同时 OpenAI 还提供了原生 SDK 支持结构化输出,包括 Python 和 Node SDK,简化了开发过程。结构化输出还支持从非结构化数据中提取结构化数据,如会议记录中的待办事项和截止日期。为了实现这一功能,OpenAI 采用了基于上下文无关语法 (CFG) 的受限解码方法,而不是传统的有限状态机 (FSM) 或正则表达式,以处理更复杂的嵌套或递归数据结构。具体原理可以查看官方博客深入了解:https://openai.com/index/introducing-structured-outputs-in-the-api
结构化输出目前已在 API 中正式推出,支持所有支持函数调用的模型,包括 GPT-4o 和 GPT-4o-mini 系列,以及之后的所有模型。此功能还与视觉输入兼容,并且可以在 chat.completion API、助手 API 和批处理 API 上使用。结构化输出的引入有助于开发人员构建更可靠的 AI 应用程序,并且可以节省输入输出费用。
简单看一下示例:
1、Function Calling:
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
}
}
}
]
}
格式化输出:
{
"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
参数方式:
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
}
}
}
}
格式化输出:
{
"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"
}
最后再来看一下当前世面上的一些格式化输出框架:

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
李开复旗下 AI 公司「零一万物」完成数亿美元融资
根据36氪的独家报道,,李开复创办的AI大模型独角兽公司「零一万物」已经完成新一轮融资,金额达数亿美元。 知情人士表示,此轮融资参与方包括某国际战投、东南亚财团等多家机构。 「零一万物」成立于2023年5月,由创新工场董事长、前微软全球副总裁李开复创立。核心团队成员来自谷歌、微软、IBM、百度等公司。消息称近期来自谷歌、微软等多位负责技术、产品管线的AI高阶人才,已经加盟了零一万物。 如今的“大模型六小虎”(智谱AI、零一万物、百川智能、MiniMax、月之暗面、阶跃星辰),正以惊人的速度,跨过200亿元的估值大关。 2024年8月5日,据彭博社报道,月之暗面刚交割一轮超过3亿美元的融资,投后估值高达33亿美元。 前不久,王小川创立的百川智能,正在以200亿元的估值,寻求B轮融资。 延伸阅读 百川智能完成 50 亿元融资,国内第三家估值 200 亿元大模型独角兽 月之暗面新一轮估值达 30 亿美金,继阿里后腾讯也入局
-
下一篇
智谱开源清影模型 CogVideoX
7 月 26 日,智谱发布 AI 生视频「清影」并上线智谱清言APP,30秒将任意文图生成视频。现在,智谱宣布正式开源清影模型 CogVideoX。 CogVideoX开源模型包含多个不同尺寸大小的模型,此次开源的是 CogVideoX-2B,它在FP-16精度下的推理仅需18GB显存,微调则只需要40GB显存,这意味着单张4090显卡即可进行推理,而单张A6000显卡即可完成微调。 CogVideoX-2B的提示词上限为226个token,视频长度为6秒,帧率为8帧/秒,视频分辨率为720*480。“我们为视频质量的提升预留了广阔的空间,期待开发者们在提示词优化、视频长度、帧率、分辨率、场景微调以及围绕视频的各类功能开发上贡献开源力量。” 模型下载:https://huggingface.co/THUDM/CogVideoX-2b 性能 为了评估文本到视频生成的质量,智谱方面使用了VBench中的多个指标,如人类动作、场景、动态程度等。还使用了两个额外的视频评估工具:Devil 中的 Dynamic Quality 和 Chrono-Magic 中的 GPT4o-MT Score,这...
相关文章
文章评论
共有0条评论来说两句吧...