基于 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":"Youareahelpfulassistant.ThecurrentdateisAugust6,2024.Youhelpusersqueryforthedatatheyarelookingforbycallingthequeryfunction."},{"role":"user","content":"lookupallmyordersinmayoflastyearthatwerefulfilledbutnotdeliveredontime"}],"tools":[{"type":"function","function":{"name":"query","description":"Executeaquery.","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":"Youareahelpfulmathtutor."},{"role":"user","content":"solve8x+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/
| 欢迎光临 链载Ai (https://www.lianzai.com/) | Powered by Discuz! X3.5 |