首页 >科技周边 >人工智能 >OpenAI函数调用教程:生成结构化输出

OpenAI函数调用教程:生成结构化输出

Lisa Kudrow
Lisa Kudrow原创
2025-03-10 12:02:12671浏览

使用openai而无需函数

>在本节中,我们将使用GPT-3.5-Turbo模型生成响应,而无需呼叫,以查看我们是否获得一致的输出。

>在安装OpenAI Python API之前,您必须获得一个API键并将其设置在本地系统上。通过Python教程中的OpenAI API遵循GPT-3.5和GPT-4,以了解如何获取API键并进行设置。该教程还包括在DataCamp的DataCamp的AI启用数据笔记本中设置环境变量的示例。

以获取进一步的帮助,请查看Datalab上的OpenAI函数拨打工作簿中的代码。

>使用以下方式将OpenAi Python API升级到V1

之后,使用API​​键启动OpenAI客户端。
pip install --upgrade openai -q
>

>
import os
from openai import OpenAI

client = OpenAI(
  api_key=os.environ['OPENAI_API_KEY'],
)
注:OpenAI不再向新用户提供免费的积分,因此您必须购买它们才能使用API​​。

我们将编写一个随机的学生描述。您可以提出自己的文字,或者使用chatgpt为您生成一个。>

>在下一部分中,我们将编写一个提示,以从文本中提取学生信息并将输出返回为JSON对象。我们将在学生描述中提取名称,专业,学校,成绩和俱乐部。

>
student_1_description = "David Nguyen is a sophomore majoring in computer science at Stanford University. He is Asian American and has a 3.8 GPA. David is known for his programming skills and is an active member of the university's Robotics Club. He hopes to pursue a career in artificial intelligence after graduating."

>将提示添加到OpenAI API聊天完成模块中以生成响应。>

# A simple prompt to extract information from "student_description" in a JSON format.
prompt1 = f'''
Please extract the following information from the given text and return it as a JSON object:

name
major
school
grades
club

This is the body of text to extract the information from:
{student_1_description}
'''
响应非常好。让我们将其转换为JSON,以更好地理解它。

>

# Generating response back from gpt-3.5-turbo
openai_response = client.chat.completions.create(
    model = 'gpt-3.5-turbo',
    messages = [{'role': 'user', 'content': prompt_1}]
)

openai_response.choices[0].message.content
我们将使用“ JSON”库将文本转换为JSON对象。

最终结果非常完美。那么,为什么我们需要调用函数?
'{\n  "name": "David Nguyen",\n  "major": "computer science",\n  "school": "Stanford University",\n  "grades": "3.8 GPA",\n  "club": "Robotics Club"\n}'
>

>让我们尝试相同的提示,但使用其他学生描述。
import json

# Loading the response as a JSON object
json_response = json.loads(openai_response.choices[0].message.content)
json_response

>我们将在提示中更改学生描述文本。

{'name': 'David Nguyen',
 'major': 'computer science',
 'school': 'Stanford University',
 'grades': '3.8 GPA',
 'club': 'Robotics Club'}

,并使用第二个提示来运行聊天完成功能。

student_2_description="Ravi Patel is a sophomore majoring in computer science at the University of Michigan. He is South Asian Indian American and has a 3.7 GPA. Ravi is an active member of the university's Chess Club and the South Asian Student Association. He hopes to pursue a career in software engineering after graduating."
如您所见,这是不一致的。它没有返回一个俱乐部,而是返回了拉维(Ravi)加入的俱乐部名单。这也与第一个学生不同。

>

prompt2 = f'''
Please extract the following information from the given text and return it as a JSON object:

name
major
school
grades
club

This is the body of text to extract the information from:
{student_2_description}
'''
openai函数调用示例

为了解决此问题,我们现在将使用最近引入的功能呼叫的功能。创建一个自定义功能以在字典列表中添加必要的信息是至关重要的,以便OpenAI API了解其功能。

  • >名称:写您最近创建的python函数名称。
  • 描述:函数的功能。
  • >
  • 参数:在“属性”中,我们将写入参数,类型和描述的名称。它将帮助Openai API确定我们正在寻找的世界。
>

note:确保您遵循正确的模式。通过阅读官方文档来了解有关函数调用的更多信息。>

接下来,我们将使用添加到“函数”参数中的自定义函数为两个学生描述生成响应。之后,我们将将文本响应转换为JSON对象并打印它。
pip install --upgrade openai -q
>

如我们所见,我们获得了统一的输出。我们甚至在数字而不是字符串中获得成绩。一致的输出对于创建没有错误的AI应用程序至关重要。

>
import os
from openai import OpenAI

client = OpenAI(
  api_key=os.environ['OPENAI_API_KEY'],
)

多个自定义功能

student_1_description = "David Nguyen is a sophomore majoring in computer science at Stanford University. He is Asian American and has a 3.8 GPA. David is known for his programming skills and is an active member of the university's Robotics Club. He hopes to pursue a career in artificial intelligence after graduating."
>您可以在聊天完成功能中添加多个自定义功能。在本节中,我们将看到OpenAI API的神奇功能,以及它如何自动选择正确的函数并返回正确的参数。

。 在字典的python列表中,我们将添加另一个称为“ extract_school_info”的功能,该功能将帮助我们从文本中提取大学信息。 为了实现这一目标,您必须添加另一个具有名称,描述和参数的函数的字典。

>我们将使用Chatgpt生成“ Stanford University”描述来测试我们的功能。

>创建学生和学校描述列表,并通过OpenAI聊天完成功能将其传递以生成响应。确保您提供了更新的自定义功能。

>
# A simple prompt to extract information from "student_description" in a JSON format.
prompt1 = f'''
Please extract the following information from the given text and return it as a JSON object:

name
major
school
grades
club

This is the body of text to extract the information from:
{student_1_description}
'''

> GPT-3.5-Turbo模型已自动为不同的描述类型选择了正确的功能。我们为学生和学校提供了完美的JSON输出。

>
# Generating response back from gpt-3.5-turbo
openai_response = client.chat.completions.create(
    model = 'gpt-3.5-turbo',
    messages = [{'role': 'user', 'content': prompt_1}]
)

openai_response.choices[0].message.content

我们甚至可以使用“ extract_school_info”函数生成休息的名称。

'{\n  "name": "David Nguyen",\n  "major": "computer science",\n  "school": "Stanford University",\n  "grades": "3.8 GPA",\n  "club": "Robotics Club"\n}'

import json

# Loading the response as a JSON object
json_response = json.loads(openai_response.choices[0].message.content)
json_response

>函数调用的应用

在本节中,我们将构建一个稳定的文本摘要,该摘要将以某种方式汇总学校和学生信息。 OpenAI函数调用教程:生成结构化输出首先,我们将创建两个python函数,即extract_student_info和extract_school_info,从函数调用中获取参数并返回汇总的字符串。

pip install --upgrade openai -q
  1. 创建Python列表,该列表由学生描述,随机提示和学校描述组成。添加随机提示以验证自动函数调用机械师。
  2. 我们将使用“描述”列表中的每个文本生成响应。
  3. >
  4. 如果使用了函数调用,我们将获得函数的名称,并基于它,将相关参数应用于函数。否则,返回正常响应。
  5. >
  6. 打印所有三个样本的输出。
import os
from openai import OpenAI

client = OpenAI(
  api_key=os.environ['OPENAI_API_KEY'],
)
  • 示例#1 :GPT模型选择了“ extract_student_info”,我们得到了有关学生的简短摘要。
  • >
  • >样本#2 :GPT模型尚未选择任何功能,并将提示视为常规问题,结果,我们得到了亚伯拉罕·林肯的传记。
  • >样本#3 :GPT模型选择了“ extract_school_info”,我们得到了有关斯坦福大学的简短摘要。
结论
student_1_description = "David Nguyen is a sophomore majoring in computer science at Stanford University. He is Asian American and has a 3.8 GPA. David is known for his programming skills and is an active member of the university's Robotics Club. He hopes to pursue a career in artificial intelligence after graduating."
在本教程中,我们了解了Openai的功能调用。我们还学习了如何使用它来生成一致的输出,创建多个功能并构建可靠的文本摘要。

>如果您想了解有关OpenAi API的更多信息,请考虑使用OpenAI API课程,并在Python备忘单中使用OpenAi API来创建您的第一个AI驱动项目。

>>

定义函数模式中的嵌套JSON结构。通过指定参数属性中的层次关系,您可以确保该模型生成适当嵌套和结构化的JSON输出,以满足复杂的数据要求。> 是否可以与外部API或数据库一起使用OpenAI函数来使用?

>

>

>如果模型的功能调用与任何定义的函数不匹配,会发生什么?

>

如果模型的函数调用与已定义的函数或所提供的架构不匹配,则该函数调用未触发,并且该模型将输入视为基于标准文本的提示,返回基于文本的基于典型的基于文本的响应。这确保了处理各种输入类型的灵活性。

赚取顶级AI认证证明您可以有效,负责任地使用AI。获得认证,被录用

以上是OpenAI函数调用教程:生成结构化输出的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn