微信扫码
添加专属顾问
我要投稿
下面将介绍OpenAI,是如何实现的,然后介绍怎么调用(如果无法使用OpenAI接口,可以使用如下方法生成结构化输出:一款约束大模型结构化输出的开源工具)
实现原理
基于 JSON Schema 的约束:通过使用开发者提供的 JSON Schema,API 能够限制模型输出的格式,确保输出数据的结构化和准确性。
上下文无关文法(Context-Free Grammar, CFG):OpenAI 将 JSON Schema 转换成上下文无关文法。CFG 是一组定义语言的规则,能够表示比有限状态机(FSM)更广泛的语言类别,特别是对于嵌套或递归的数据结构。
动态约束解码:在模型生成每个 token 之后,推理引擎会根据之前生成的 tokens 和 CFG 中的规则来确定接下来哪些 tokens 是有效的。这个过程称为动态约束解码。
parallel_tool_calls: false 来禁用并行函数调用。如何调用
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}}}]}2、新参数 :开发人员现在可以通过提供 JSON Schema,这是参数的新选项 。当模型不是调用工具,而是以结构化的方式响应用户时,这很有用。此功能适用于最新的 GPT-4o 型号,模型输出将与提供的架构匹配。response_format json_schema response_format gpt-4o-2024-08-06 gpt-4o-mini-2024-07-18 response_format strict: true
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}}}}详细介绍可查看:
https://openai.com/index/introducing-structured-outputs-in-the-api/
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费POC验证,效果达标后再合作。零风险落地应用大模型,已交付160+中大型企业
2026-02-04
刚刚,Xcode 史诗级更新:原生集成 Claude Agent SDK,苹果开发直接起飞!
2026-02-04
国产 Cowork 它来了!MCP、Skills和Expert Agents都支持,全部免费体验!
2026-02-04
混元研究博客上线姚顺雨团队最新成果:从 Context 探索语言模型的范式转变
2026-02-04
通俗讲解大模型短期记忆 vs 长期记忆
2026-02-04
谁动了我的电脑?谁应该抱怨?
2026-02-03
从 CLI 到桌面:Codex 把 coding agent 变成“任务指挥台”
2026-02-03
谷歌重大更新:国内手动开启 Gemini AI 侧边栏与 Auto Browse 自动浏览全攻略
2026-02-03
OpenAI 发布新的 Codex 桌面版,我现在不骂 Codex 难用了
2026-01-24
2026-01-10
2025-11-19
2025-11-13
2026-01-26
2026-01-01
2025-12-09
2025-11-12
2026-01-09
2025-12-21
2026-02-04
2026-02-03
2026-02-03
2026-02-02
2026-02-02
2026-02-02
2026-01-31
2026-01-30