老新闻了,gpt-4o-2024-08-06
支持结构化输出。事实上不止,据官方文档介绍 gpt-4o-mini
也是支持的(亲测)。
以下是检测代码,在官方给的代码基础上增加了自定义base_url
,里面的模型可以换成 gpt-4o-mini
。运行前请安装依赖 pip install -U openai
,并替换 api_key
和 base_url
!
from openai import OpenAI
from pydantic import BaseModel
client = OpenAI(api_key='sk-xxx', base_url='https://api.xxx.com/v1')
class Step(BaseModel):
explanation: str
output: str
class MathReasoning(BaseModel):
steps: list[Step]
final_answer: str
completion = client.beta.chat.completions.parse(
model="gpt-4o-2024-08-06",
messages=[
{"role": "system",
"content": "You are a helpful math tutor. Guide the user through the solution step by step."},
{"role": "user", "content": "how can I solve 8x + 7 = -23"}
],
response_format=MathReasoning,
)
print(completion.choices[0].message.parsed)
如果你的中转保真,则 gpt-4o-2024-08-06
和 gpt-4o-mini
模型调用不会报错(其他模型会报错),并且能展示解题步骤。不过你也看见了,可能会发生使用 gpt-4o-mini
冒充 gpt-4o-2024-08-06
的情况。
快去试试吧!
5分拿走。