微信扫码
添加专属顾问
我要投稿
下面将介绍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-04-30
MCP入门指南:大模型时代的USB接口
2025-04-30
通俗易懂的梳理MCP的工作流程(以高德地图MCP为例)
2025-04-30
一文说明 Function Calling、MCP、A2A 的区别!
2025-04-30
MCP很好,但它不是万灵药|一文读懂 MCP
2025-04-30
旅行规划太难做?5 分钟构建智能Agent,集成地图 MCP Server
2025-04-29
10万元跑满血版DeepSeek,这家公司掀了一体机市场的桌子|甲子光年
2025-04-29
谷歌大神首次揭秘Gemini预训练秘密:52页PPT干货,推理成本成最重要因素
2025-04-29
一文说清:什么是算法备案、大模型备案、大模型登记 2.0
2024-08-13
2024-06-13
2024-08-21
2024-09-23
2024-07-31
2024-05-28
2024-08-04
2024-04-26
2024-07-09
2024-09-17
2025-04-29
2025-04-29
2025-04-29
2025-04-28
2025-04-28
2025-04-28
2025-04-28
2025-04-28