OpenAI函数调用教程:生成结构化输出
使用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'], )
我们将编写一个随机的学生描述。您可以提出自己的文字,或者使用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} '''
>
# 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
最终结果非常完美。那么,为什么我们需要调用函数?
'{\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."
>
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 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."
。 在字典的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
>函数调用的应用
在本节中,我们将构建一个稳定的文本摘要,该摘要将以某种方式汇总学校和学生信息。
首先,我们将创建两个python函数,即extract_student_info和extract_school_info,从函数调用中获取参数并返回汇总的字符串。
pip install --upgrade openai -q
- 创建Python列表,该列表由学生描述,随机提示和学校描述组成。添加随机提示以验证自动函数调用机械师。
- 我们将使用“描述”列表中的每个文本生成响应。 >
- 如果使用了函数调用,我们将获得函数的名称,并基于它,将相关参数应用于函数。否则,返回正常响应。 >
- 打印所有三个样本的输出。
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 API的更多信息,请考虑使用OpenAI API课程,并在Python备忘单中使用OpenAi API来创建您的第一个AI驱动项目。
>>
是否可以与外部API或数据库一起使用OpenAI函数来使用?
>
>>如果模型的功能调用与任何定义的函数不匹配,会发生什么?
>
如果模型的函数调用与已定义的函数或所提供的架构不匹配,则该函数调用未触发,并且该模型将输入视为基于标准文本的提示,返回基于文本的基于典型的基于文本的响应。这确保了处理各种输入类型的灵活性。
赚取顶级AI认证证明您可以有效,负责任地使用AI。获得认证,被录用以上是OpenAI函数调用教程:生成结构化输出的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

本文回顾了AI最高的艺术生成器,讨论了他们的功能,对创意项目的适用性和价值。它重点介绍了Midjourney是专业人士的最佳价值,并建议使用Dall-E 2进行高质量的可定制艺术。

Meta的Llama 3.2:多模式和移动AI的飞跃 Meta最近公布了Llama 3.2,这是AI的重大进步,具有强大的视觉功能和针对移动设备优化的轻量级文本模型。 以成功为基础

本文比较了诸如Chatgpt,Gemini和Claude之类的顶级AI聊天机器人,重点介绍了其独特功能,自定义选项以及自然语言处理和可靠性的性能。

嘿,编码忍者!您当天计划哪些与编码有关的任务?在您进一步研究此博客之前,我希望您考虑所有与编码相关的困境,这是将其列出的。 完毕? - 让&#8217

文章讨论了Grammarly,Jasper,Copy.ai,Writesonic和Rytr等AI最高的写作助手,重点介绍了其独特的内容创建功能。它认为Jasper在SEO优化方面表现出色,而AI工具有助于保持音调的组成

Shopify首席执行官TobiLütke最近的备忘录大胆地宣布AI对每位员工的基本期望是公司内部的重大文化转变。 这不是短暂的趋势。这是整合到P中的新操作范式

本周的AI景观:进步,道德考虑和监管辩论的旋风。 OpenAI,Google,Meta和Microsoft等主要参与者已经释放了一系列更新,从开创性的新车型到LE的关键转变

本文评论了Google Cloud,Amazon Polly,Microsoft Azure,IBM Watson和Discript等高级AI语音生成器,重点介绍其功能,语音质量和满足不同需求的适用性。
