微信扫码
添加专属顾问
我要投稿
下面将介绍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+中大型企业
2025-06-14
挑战 Transformer 架构的谷歌最新扩散模型 Gemini Diffusion 系统提示词
2025-06-14
“AI原生”时代:企业数智化转型的分水岭
2025-06-14
多模态RAG的三类图文问答实现方式,你知道多少种?
2025-06-14
新一代文本表征Qwen3-Embedding与排序模型Qwen3-Reranker 部署和替换
2025-06-14
“华强北”围攻AI耳机,未来智能如何突围?
2025-06-14
AI时代如何为企业和个人赋能
2025-06-14
没吃透 Function Calling?难怪你不理解 AI Agent 为何非来不可!
2025-06-14
浅尝一下微软的AutoGen框架
2025-05-29
2025-03-20
2025-03-21
2025-04-11
2025-03-20
2025-03-19
2025-03-20
2025-03-19
2025-03-19
2025-03-19
2025-06-14
2025-06-14
2025-06-14
2025-06-14
2025-06-13
2025-06-13
2025-06-13
2025-06-13