
下面将介绍OpenAI,是如何实现的,然后介绍怎么调用(如果无法使用OpenAI接口,可以使用如下方法生成结构化输出:一款约束大模型结构化输出的开源工具)
实现原理
-
基于 JSON Schema 的约束:通过使用开发者提供的 JSON Schema,API 能够限制模型输出的格式,确保输出数据的结构化和准确性。
-
上下文无关文法(Context-Free Grammar, CFG):OpenAI 将 JSON Schema 转换成上下文无关文法。CFG 是一组定义语言的规则,能够表示比有限状态机(FSM)更广泛的语言类别,特别是对于嵌套或递归的数据结构。
-
动态约束解码:在模型生成每个 token 之后,推理引擎会根据之前生成的 tokens 和 CFG 中的规则来确定接下来哪些 tokens 是有效的。这个过程称为动态约束解码。
-
转换为缓存的数据结构:为了在解码过程中高效地应用约束,OpenAI 将预处理后的 JSON Schema 转换为易于访问的缓存数据结构。这样,在模型采样的每一步,都可以快速地确定并应用有效的 token 约束。 -
降低无效 token 的概率:在每个 token 生成之后,通过使用有效的 token 列表来掩蔽下一个采样步骤,从而有效地将无效 tokens 的概率降低到 0。 -
处理新 Schema 的延迟:当使用一个新的 JSON Schema 时,第一个 API 请求可能会有额外的延迟,因为需要先处理并缓存这个 Schema。这个处理过程通常需要不到 10 秒,但更复杂的 Schema 可能需要更长时间。 -
避免并行函数调用:Structured Outputs 与并行函数调用不兼容。如果生成了并行函数调用,它可能不会匹配提供的 Schema。可以通过设置 parallel_tool_calls: false来禁用并行函数调用。 -
安全性和可靠性:尽管模型的行为本质上是非确定性的,但通过这种工程化的方法,OpenAI 能够实现 100% 的可靠性,确保模型的输出严格符合 JSON Schema。 -
限制和限制条件:Structured Outputs 有一些限制,例如只支持 JSON Schema 的一部分,并且如果模型拒绝不安全的请求或生成达到最大 token 限制,模型可能无法遵循 Schema。
-
终结符集合:这是语言的基本构建块,相当于句子中的单词或字符。 -
非终结符集合:这些符号用于表示终结符的组合,它们可以被一系列终结符或非终结符替换。 -
起始符号:这是定义语言的起点,通常是 CFG 中的一个特殊非终结符。 -
产生式规则集合:每个规则指定了如何将一个非终结符替换为终结符序列或非终结符序列。规则的一般形式是:A→α,其中 A 是非终结符,α 是终结符和/或非终结符的序列。 -
句子生成:通过反复应用产生式规则,从一个起始符号开始,逐步替换为终结符,直到生成一个完整的句子(句子序列)。
如何调用
1、函数调用:通过在函数定义中设置来获得结构化输出 。此功能适用于支持工具的所有模型,包括所有模型 和 更高版本。启用结构化输出后,模型输出将与提供的工具定义匹配。tools strict: true gpt-4-0613 gpt-3.5-turbo-0613
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/


