>
在2024年8月,Openai宣布了其API的强大新功能 - 结构化输出。顾名思义,使用此功能,您可以确保LLM仅以指定的格式生成响应。此功能将使需要精确数据格式的应用程序变得更加容易。
在本教程中,您将学习如何从OpenAI结构化输出开始,了解其新的语法并探索其关键应用程序。
>
确定性响应,换句话说,以一致格式的响应对于许多任务,例如数据输入,信息检索,问答,多步工作流等等至关重要。您可能已经体验了LLMS如何以截然不同的格式生成输出,即使提示是相同的。例如,考虑由GPT-4O驱动的此简单的分类函数:
# List of hotel reviews reviews = [ "The room was clean and the staff was friendly.", "The location was terrible and the service was slow.", "The food was amazing but the room was too small.", ] # Classify sentiment for each review and print the results for review in reviews: sentiment = classify_sentiment(review) print(f"Review: {review}\nSentiment: {sentiment}\n")
>输出:
Review: The room was clean and the staff was friendly. Sentiment: Positive Review: The location was terrible and the service was slow. Sentiment: Negative Review: The food was amazing but the room was too small. Sentiment: The sentiment of the review is neutral.
>我们可以通过一些及时的工程来解决此问题,但这是一个耗时的迭代过程。即使有了完美的提示,我们也不能100%确定响应将在以后的请求中符合我们的格式。当然,除非我们使用结构化的输出:
>输出:
def classify_sentiment_with_structured_outputs(review): """Sentiment classifier with Structured Outputs""" ... # Classify sentiment for each review with Structured Outputs for review in reviews: sentiment = classify_sentiment_with_structured_outputs(review) print(f"Review: {review}\nSentiment: {sentiment}\n")
使用新函数,classify_sentiment_with_structured_outputs,响应都以相同的格式。
Review: The room was clean and the staff was friendly. Sentiment: {"sentiment":"positive"} Review: The location was terrible and the service was slow. Sentiment: {"sentiment":"negative"} Review: The food was amazing but the room was too small. Sentiment: {"sentiment":"neutral"}
>以刚性格式强迫语言模型的能力非常重要,可以为您节省无数小时的及时工程或依赖其他开源工具。
在本节中,我们将使用情感分析仪函数的示例分解结构化输出。
设置您的环境在开始之前,请确保您有以下内容:
>
3。验证安装:创建一个简单的python脚本以验证安装: >运行脚本以确保正确设置所有内容。您应该在终端中看到模型的响应。
使用pydantic
>: sentimentResponse是一个pydantic模型,它定义了输出的预期结构。
如果您注意到,而不是使用client.chat.completions.create,我们使用的是client.beta.chat.completions.parse方法。 .parse()是专门为结构化输出编写的聊天完成API中的一种新方法。
在这里,结果是一个消息对象:
如您所见,我们有一个sentermentresponse类的实例。这意味着我们可以使用.sentiment属性以字符串而不是字典访问情感: >
>
在此示例中
示例文本完全不可读,并且在关键信息之间缺少空间。让我们看看该模型是否成功。我们将使用JSON库来使响应很好:
简而言之,通过嵌套pydantic模型,您可以定义处理层次数据并为复杂输出执行特定结构的复杂模式。 >新语言模型的广泛特征之一是函数调用(也称为工具调用)。此功能使您可以将语言模型连接到用户定义的功能,从而有效地(模型)访问外部世界。
>重要的是,使用结构化输出,使用OpenAI模型使用函数调用变得更加容易。过去,您将传递给OpenAI模型的功能将需要编写复杂的JSON模式,并用类型提示概述每个功能参数。这是一个示例: >即使get_current_weather函数具有两个参数,其JSON模式也变得巨大且容易出错。
这是如何将此工具作为请求的一部分使用: >由于上面的查询是“东京的天气是什么?”,我们在返回消息对象的tool_calls中看到了一个电话。
>由我们通过提供的参数调用该函数: 如果您希望该模型生成该功能的参数并同时调用它,则您正在寻找AI代理。 >
使用OpenAI结构化输出 在使用结构化输出时,请记住许多最佳实践和建议。在本节中,我们将概述其中的一些。
>使用适当的数据类型(str,int,float,bool,list,dict)准确表示您的数据。
是,是的,结构性输出可以简化函数,以简化函数 虽然功能强大,结构化的输出可能会限制AI的灵活性,并且需要仔细的模式设计才能平衡结构,并在输出中及时详细介绍详细信息。# List of hotel reviews
reviews = [
"The room was clean and the staff was friendly.",
"The location was terrible and the service was slow.",
"The food was amazing but the room was too small.",
]
# Classify sentiment for each review and print the results
for review in reviews:
sentiment = classify_sentiment(review)
print(f"Review: {review}\nSentiment: {sentiment}\n")
Review: The room was clean and the staff was friendly.
Sentiment: Positive
Review: The location was terrible and the service was slow.
Sentiment: Negative
Review: The food was amazing but the room was too small.
Sentiment: The sentiment of the review is neutral.
>要使用结构化输出,您需要使用Pydantic模型来定义预期的输出结构。 Pydantic是Python的数据验证和设置管理库,它允许您使用Python型注释来定义数据模型。然后可以使用这些模型来强制执行OpenAI模型生成的输出的结构。
这是一个示例pydantic模型,用于指定我们的评论情感分类器的格式:def classify_sentiment_with_structured_outputs(review):
"""Sentiment classifier with Structured Outputs"""
...
# Classify sentiment for each review with Structured Outputs
for review in reviews:
sentiment = classify_sentiment_with_structured_outputs(review)
print(f"Review: {review}\nSentiment: {sentiment}\n")
在OpenAI请求中强制执行我们的Pydantic模式,我们要做的就是将其传递给聊天完成API的响应_format参数。粗略地,这是它的样子:>
然后,我们编写了一个使用.parse()助手方法的新功能:Review: The room was clean and the staff was friendly.
Sentiment: {"sentiment":"positive"}
Review: The location was terrible and the service was slow.
Sentiment: {"sentiment":"negative"}
Review: The food was amazing but the room was too small.
Sentiment: {"sentiment":"neutral"}
$ pip install -U openai
# List of hotel reviews
reviews = [
"The room was clean and the staff was friendly.",
"The location was terrible and the service was slow.",
"The food was amazing but the room was too small.",
]
# Classify sentiment for each review and print the results
for review in reviews:
sentiment = classify_sentiment(review)
print(f"Review: {review}\nSentiment: {sentiment}\n")
嵌套pydantic模型,用于定义复杂模式
在某些情况下,您可能需要定义涉及嵌套数据的更复杂的输出结构。 Pydantic允许您相互嵌套模型,使您能够创建可以处理各种用例的复杂模式。在处理层次数据时,或者需要为复杂输出执行特定结构时,这特别有用。
首先,我们为地址和用户信息定义了pydantic模型:Review: The room was clean and the staff was friendly.
Sentiment: Positive
Review: The location was terrible and the service was slow.
Sentiment: Negative
Review: The food was amazing but the room was too small.
Sentiment: The sentiment of the review is neutral.
> UserInfo是一种pydantic模型,其中包含地址对象列表,以及用户名称,电子邮件和电话号码的字段。
接下来,我们使用这些嵌套的pydantic模型来在OpenAI API调用中强制执行输出结构:
def classify_sentiment_with_structured_outputs(review):
"""Sentiment classifier with Structured Outputs"""
...
# Classify sentiment for each review with Structured Outputs
for review in reviews:
sentiment = classify_sentiment_with_structured_outputs(review)
print(f"Review: {review}\nSentiment: {sentiment}\n")
Review: The room was clean and the staff was friendly.
Sentiment: {"sentiment":"positive"}
Review: The location was terrible and the service was slow.
Sentiment: {"sentiment":"negative"}
Review: The food was amazing but the room was too small.
Sentiment: {"sentiment":"neutral"}
>检索实时数据(例如,天气,股价,运动得分)
执行计算或数据分析
# List of hotel reviews
reviews = [
"The room was clean and the staff was friendly.",
"The location was terrible and the service was slow.",
"The food was amazing but the room was too small.",
]
# Classify sentiment for each review and print the results
for review in reviews:
sentiment = classify_sentiment(review)
print(f"Review: {review}\nSentiment: {sentiment}\n")
Review: The room was clean and the staff was friendly.
Sentiment: Positive
Review: The location was terrible and the service was slow.
Sentiment: Negative
Review: The food was amazing but the room was too small.
Sentiment: The sentiment of the review is neutral.
def classify_sentiment_with_structured_outputs(review):
"""Sentiment classifier with Structured Outputs"""
...
# Classify sentiment for each review with Structured Outputs
for review in reviews:
sentiment = classify_sentiment_with_structured_outputs(review)
print(f"Review: {review}\nSentiment: {sentiment}\n")
Review: The room was clean and the staff was friendly.
Sentiment: {"sentiment":"positive"}
Review: The location was terrible and the service was slow.
Sentiment: {"sentiment":"negative"}
Review: The food was amazing but the room was too small.
Sentiment: {"sentiment":"neutral"}
$ pip install -U openai
$ export OPENAI_API_KEY='your-api-key'
>保持模式简单明了,以获得最准确的结果。拒绝模型。使用新的.parse()方法时,消息对象具有新的.refusal属性,以表示拒绝:
结论
在本教程中,我们学会了如何使用新的OpenAI API功能开始:结构化输出。我们已经看到该特征是如何迫使语言模型以我们指定的格式产生输出。我们已经学会了如何将其与函数调用结合使用,并探索了一些最佳实践来充分利用该功能。>使用OpenAI API课程
证明您可以有效,负责任地使用AI。获得认证,雇用
以上是开始使用OpenAI结构化输出的详细内容。更多信息请关注PHP中文网其他相关文章!