|
| 1 | +--- |
| 2 | +title: "Llm Structured Ouputs" |
| 3 | +date: 2025-07-11T18:54:20+08:00 |
| 4 | +# bookComments: false |
| 5 | +# bookSearchExclude: false |
| 6 | +--- |
| 7 | + |
| 8 | +# LLM 结构化输出 |
| 9 | + |
| 10 | +Structured Outputs和JSON mode是模型结构化输出的两种方式,Structured Outputs是JSON mode的演进版本,能够保证模型产出的 JSON完全符合提供的JSON Schema(字段名、是否必填、类型、取值范围、枚举等都对得上),Open AI官方更建议使用Structured Outputs |
| 11 | + |
| 12 | +## Structured Outputs |
| 13 | + |
| 14 | +Structured Outputs方式实现结构化输出有objects和手动模式两种方法,objects通过pydantic定义basemodel,手动模式需要自己手动构建json_schema |
| 15 | + |
| 16 | +### SDK Objects |
| 17 | + |
| 18 | +```python |
| 19 | +from pydantic import BaseModel |
| 20 | +from openai import OpenAI |
| 21 | + |
| 22 | +client = OpenAI() |
| 23 | + |
| 24 | +class Step(BaseModel): |
| 25 | + explanation: str |
| 26 | + output: str |
| 27 | + |
| 28 | +class MathReasoning(BaseModel): |
| 29 | + steps: list[Step] |
| 30 | + final_answer: str |
| 31 | + |
| 32 | +completion = client.chat.completions.parse( |
| 33 | + model="gpt-4o-2024-08-06", |
| 34 | + messages=[ |
| 35 | + {"role": "system", "content": "You are a helpful math tutor. Guide the user through the solution step by step."}, |
| 36 | + {"role": "user", "content": "how can I solve 8x + 7 = -23"} |
| 37 | + ], |
| 38 | + response_format=MathReasoning, |
| 39 | +) |
| 40 | + |
| 41 | +math_reasoning = completion.choices[0].message.parsed |
| 42 | +``` |
| 43 | + |
| 44 | +### 手动模式 |
| 45 | + |
| 46 | +```python |
| 47 | +response = client.chat.completions.create/parse( |
| 48 | + model="gpt-4o-2024-08-06", |
| 49 | + messages=[ |
| 50 | + {"role": "system", "content": "You are a helpful math tutor. Guide the user through the solution step by step."}, |
| 51 | + {"role": "user", "content": "how can I solve 8x + 7 = -23"} |
| 52 | + ], |
| 53 | + response_format={ |
| 54 | + "type": "json_schema", |
| 55 | + "json_schema": { |
| 56 | + "name": "math_response", |
| 57 | + "schema": { |
| 58 | + "type": "object", |
| 59 | + "properties": { |
| 60 | + "steps": { |
| 61 | + "type": "array", |
| 62 | + "items": { |
| 63 | + "type": "object", |
| 64 | + "properties": { |
| 65 | + "explanation": {"type": "string"}, |
| 66 | + "output": {"type": "string"} |
| 67 | + }, |
| 68 | + "required": ["explanation", "output"], |
| 69 | + "additionalProperties": False |
| 70 | + } |
| 71 | + }, |
| 72 | + "final_answer": {"type": "string"} |
| 73 | + }, |
| 74 | + "required": ["steps", "final_answer"], |
| 75 | + "additionalProperties": False |
| 76 | + }, |
| 77 | + "strict": True |
| 78 | + } |
| 79 | + } |
| 80 | +) |
| 81 | +## 注意是content,不是跟sdk objects一样的parsed,这里打印parsed会返回None,即使content里是遵循schema的json |
| 82 | +print(response.choices[0].message.content) |
| 83 | +``` |
| 84 | + |
| 85 | +## JSON mode |
| 86 | + |
| 87 | +将response_format中的type字段设置为`{ "type": "json_object" }`可启用json_object |
| 88 | + |
| 89 | +## 两种结构化输出方式的对比 |
| 90 | + |
| 91 | +| | Structured Outputs | JSON Mode | |
| 92 | +| :--------------------- | :----------------------------------------------------------- | ------------------------------------------------------------ | |
| 93 | +| **Outputs valid JSON** | Yes | Yes | |
| 94 | +| **Adheres to schema** | Yes (see [supported schemas](https://platform.openai.com/docs/guides/structured-outputs?api-mode=chat&format=without-parse#supported-schemas)) | No | |
| 95 | +| **Compatible models** | `gpt-4o-mini`, `gpt-4o-2024-08-06`, and later | `gpt-3.5-turbo`, `gpt-4-*` and `gpt-4o-*` models | |
| 96 | +| **Enabling** | `response_format: { type: "json_schema", json_schema: {"strict": true, "schema": ...} }` | `response_format: { type: "json_object" }` | |
| 97 | +| 约束表达 | prompt中的自然语言描述 | JSON Schema表达:`required`、`type`、`enum`、`minItems`、正则 `pattern`…… | |
| 98 | + |
| 99 | +**Adheres to schema**的具体意思是: |
| 100 | + |
| 101 | +* JSON mode 只保证输出JSON的结构(花括号、逗号、引号)正确。 |
| 102 | + |
| 103 | +* Structured Outputs除了保证输出是合法 JSON,还强制要求模型产出的 JSON 必须完全符合你提供的 JSON Schema(字段名、是否必填、类型、取值范围、枚举等都要对得上)。输出在返回给你之前会被自动按 JSON Schema 校验,不合格就报错/返回 `None`。 |
0 commit comments