AI知识库

53AI知识库

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


千呼万唤终于来了,OpenAI的API终于能够百分之百输出JSON了
发布日期:2024-08-08 06:23:58 浏览次数: 1758


今天OpenAI的API新增加了一个功能,支持结构化输出,通俗的来说API的输出支持稳定的JSON模式。

为什么强调稳定呢?其实在去年的DevDay,OpenAI就介绍了JSON models,尽管它提高了模型输出是JSON的可靠性,但它不能百分之百确保输出是JSON。而今天提出的结构化输出终于确保API输出是JSON模式了。

其实这个功能非常非常有用,对于调用API的应用程序来说,从非结构化输入生成结构化输出是最核心的用例,想当年我们在调用OpenAI接口的时候,通过prompt让接口输出JSON,可有的时候行,有的时候又不行,根本找不到规律,现在这个问题终于解决了!

OpenAI经过测试,gpt-4o-2024-08-06支持结构化输出,其输出JSON的稳定性是100%,而gpt-4-0613得分不到40%,如下图:

说了这么多,如何使用API的结构化输出呢?有两种模式。

1:Function calling

在函数定义中设置 strict: true,即可通过tools进行结构化输出,如下图:

其输出大概如下:

2:response_format 参数

上述Function calling的方式个人觉得非常复杂,所以也可以在API中使用新增的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"}

虽然API request看上去很复杂,比如Python官方包做了封装,实现上述功能的代码如下:

from pydantic import BaseModelfrom 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)

此外OpenAI特别强调了结构化输出功能是安全的,必须符合现有的安全策略,所以特别在API中新增加了一个输出字段refusal,用于描述是不符合JSON还是不安全!

这篇文章描述了如何使用结构化输出功能,其实OpenAI也介绍了如何实现该功能,这个有机会再讲,对于理解模型训练很有帮助!


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

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

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

联系我们

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

微信扫码

与创始人交个朋友

回到顶部

 
扫码咨询