>在本节中,我们将使用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了解其功能。
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的神奇功能,以及它如何自动选择正确的函数并返回正确的参数。
>我们将使用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
>函数调用的应用
在本节中,我们将构建一个稳定的文本摘要,该摘要将以某种方式汇总学校和学生信息。
首先,我们将创建两个python函数,即extract_student_info和extract_school_info,从函数调用中获取参数并返回汇总的字符串。
pip install --upgrade openai -q
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的功能调用。我们还学习了如何使用它来生成一致的输出,创建多个功能并构建可靠的文本摘要。
>
以上是OpenAI函数调用教程:生成结构化输出的详细内容。更多信息请关注PHP中文网其他相关文章!