OpenAI 快速入门(附代码)

简介: penAI 训练了最先进的语言模型,这些模型非常擅长理解和生成文本。我们的 API 提供了访问这些模型的途径,并可用于解决几乎所有需要处理自然语言的任务。

OpenAI has trained cutting-edge language models that are very good at understanding and generating text. Our API provides access to these models and can be used to solve virtually any task that involves processing language.

OpenAI 训练了最先进的语言模型,这些模型非常擅长理解和生成文本。我们的 API 提供了访问这些模型的途径,并可用于解决几乎所有需要处理自然语言的任务。

In this quickstart tutorial, you’ll build a simple sample application. Along the way, you’ll learn key concepts and techniques that are fundamental to using the API for any task, including:

在这个快速入门教程中,你将构建一个简单的样例应用。在这个过程中,你将学习到一些关键的概念和技巧,这些对于使用 API 解决任何任务都是基本的。包括:

  • Content generation
  • Summarization
  • Classification, categorization, and sentiment analysis
  • Data extraction
  • Translation
  • Many more!
  • 内容生成
  • 摘要生成
  • 分类、归类和情感分析
  • 数据提取
  • 翻译
  • 以及许多其他的应用!

介绍


The completions endpoint is the core of our API and provides a simple interface that’s extremely flexible and powerful. You input some text as a prompt, and the API will return a text completion that attempts to match whatever instructions or context you gave it.

「完成端点」是我们 API 的核心,提供了一个简单、灵活、功能强大的接口。你可以输入一些文本作为提示,API 将返回一个文本完成结果,尝试匹配你输入的提示中给出的任何指令或上下文。

image-20230220231246847

You can think of this as a very advanced autocomplete — the model processes your text prompt and tries to predict what’s most likely to come next.

你可以将它看作是一个非常先进的自动完成功能——模型处理你的文本提示,尝试预测接下来最可能出现的内容。

让我们从一条指示开始


Imagine you want to create a pet name generator. Coming up with names from scratch is hard!

假设你想创建一个宠物名字生成器。从零开始想出名字可真不容易!

First, you’ll need a prompt that makes it clear what you want. Let’s start with an instruction. Submit this prompt to generate your first completion.

首先,你需要一个清晰表述你需求的提示。让我们从一条指示开始。提交这个提示来生成你的第一个完成结果。


Not bad! Now, try making your instruction more specific.

不错!现在,试着让你的指示更加具体一些。

image-20230220231757559

As you can see, adding a simple adjective to our prompt changes the resulting completion. Designing your prompt is essentially how you “program” the model.

你可以看到,将一个简单的形容词添加到我们的提示中可以改变完成结果。设计你的提示本质上就是如何“编程”这个模型。

添加一些例子


Crafting good instructions is important for getting good results, but sometimes they aren’t enough. Let’s try making your instruction even more complex.

撰写良好的指示对于获得好的结果非常重要,但有时候光是指示还不够。让我们试着让你的指示更加复杂一些。

image-20230220232024846

This completion isn't quite what we want. These names are pretty generic, and it seems like the model didn't pick up on the horse part of our instruction. Let’s see if we can get it to come up with some more relevant suggestions.

这个完成结果并不是我们想要的。这些名字相当普通,似乎模型没有注意到我们指定了马。让我们看看是否可以得到一些更相关的建议。

In many cases, it’s helpful to both show and tell the model what you want. Adding examples to your prompt can help communicate patterns or nuances. Try submitting this prompt which includes a couple examples.

在许多情况下,同时展示和说明你想要什么是有帮助的。在你的提示中添加一些例子可以帮助传达模式或细微差别。试着提交这个提示,其中包括一些例子。

image-20230220232356509

Nice! Adding examples of the output we’d expect for a given input helped the model provide the types of names we were looking for.

不错!添加关于给定输入所期望输出的例子有助于模型提供我们正在寻找的类型的名字。

调整你的设置


Prompt design isn’t the only tool you have at your disposal. You can also control completions by adjusting your settings. One of the most important settings is called temperature.

提示设计并不是你拥有的唯一工具。你还可以通过调整设置来控制完成结果。最重要的设置之一称为「温度」

You may have noticed that if you submitted the same prompt multiple times in the examples above, the model would always return identical or very similar completions. This is because your temperature was set to 0.

你可能已经注意到,在上面的例子中,如果多次提交相同的提示,模型总是返回相同或非常相似的完成结果。这是因为你的温度被设置为0

Try re-submitting the same prompt a few times with temperature set to 1.

尝试使用温度为1重新提交相同的提示几次。

image-20230220232923716

image-20230220232952852

See what happened? When temperature is above 0, submitting the same prompt results in different completions each time.

看到发生了什么了吗?当温度大于0时,重复提交相同的提示每次会得到不同的完成结果。

Remember that the model predicts which text is most likely to follow the text preceding it. Temperature is a value between 0 and 1 that essentially lets you control how confident the model should be when making these predictions. Lowering temperature means it will take fewer risks, and completions will be more accurate and deterministic. Increasing temperature will result in more diverse completions.

请记住,模型预测哪些文本最有可能跟在其前面。温度是介于0和1之间的值,可以控制模型在进行这些预测时的置信度。降低温度意味着模型会冒更少的风险,完成结果将更准确和确定性更高。增加温度将产生更多样化的完成结果。

For your pet name generator, you probably want to be able to generate a lot of name ideas. A moderate temperature of 0.6 should work well.

根据您的宠物名字生成器需求,您可能希望能够生成许多名字的想法。适度的温度值0.6应该能够胜任。

构建应用程序


Now that you’ve found a good prompt and settings, you’re ready to build your pet name generator! We’ve written some code to get you started — follow the instructions below to download the code and run the app.

现在您已经找到了一个好的提示和设置,可以开始构建您的宠物名字生成器了!我们已经编写了一些代码,让您可以开始。按照以下说明下载代码并运行应用程序。

下载代码

官网提供了 Node.js 和 Python,因为电脑上 Python 环境已经配置好,所以,使用 Python 进行演示。

git clone https://github.com/openai/openai-quickstart-python.git


配置 API Key


Navigate into the project directory and make a copy of the example environment variables file.

进入项目目录并复制示例环境变量文件。

cd openai-quickstart-python
cp .env.example .env

Copy your secret API key and set it as the OPENAI_API_KEY in your newly created .env file. If you haven't created a secret key yet, you can do so below.

复制您的密钥,并将其设置为新创建的 .env 文件中的 OPENAI_API_KEY。如果您还没有创建密钥,可以在下面这样做。

image-20230220233856813

运行程序


Run the following commands in the project directory to install the dependencies and run the app. When running the commands, you may need to type python3/pip3 instead of python/pip depending on your setup.

在项目目录中运行以下命令以安装依赖项并运行应用程序。运行命令时,根据您的设置,您可能需要键入python3 / pip3而不是python / pip。

python -m venv venv
. venv/bin/activate
pip install -r requirements.txt

提示下图说明安装依赖成功

image-20230220234844702

flask run

提示下图说明服务启动成功


image-20230220235020565


Open http://localhost:5000 in your browser and you should see the pet name generator!

在你的浏览器中打开 http://localhost:5000 ,你应该能够看到宠物名字生成器!

根据提示地址访问:http://127.0.0.1:5000

image-20230220235321941

输入「小仓鼠」,效果如下:


image-20230221000347996


理解代码


Open up app.py in the openai-quickstart-python folder. At the bottom, you’ll see the function that generates the prompt that we were using above. Since users will be entering the type of animal their pet is, it dynamically swaps out the part of the prompt that specifies the animal.

打开 openai-quickstart-python 文件夹中的 app.py。在底部,你会看到生成我们之前使用的提示的函数。由于用户将输入宠物的类型,因此它会动态地更改指定动物的提示部分。

def generate_prompt(animal):
    return """Suggest three names for an animal that is a superhero.
Animal: Cat
Names: Captain Sharpclaw, Agent Fluffball, The Incredible Feline
Animal: Dog
Names: Ruff the Protector, Wonder Canine, Sir Barks-a-Lot
Animal: {}
Names:""".format(animal.capitalize())

On line 14 in app.py, you’ll see the code that sends the actual API request. As mentioned above, it uses the completions endpoint with a temperature of 0.6.

在 app.py 的第14行,你会看到发送 API 请求的代码。如上所述,它使用了温度为 0.6 的 completion 端点。

response = openai.Completion.create(
  model="text-davinci-003",
  prompt=generate_prompt(animal),
  temperature=0.6
)

And that’s it! You should now have a full understanding of how your (superhero) pet name generator uses the OpenAI API!

这就是全部内容了!你现在应该完全了解你的(超级英雄)宠物名字生成器是如何使用OpenAI API的!

结束语


These concepts and techniques will go a long way in helping you build your own application. That said, this simple example demonstrates just a sliver of what’s possible! The completions endpoint is flexible enough to solve virtually any language processing task, including content generation, summarization, semantic search, topic tagging, sentiment analysis, and so much more.

这些概念和技术将有助于您构建自己的应用程序。话虽如此,这个简单的示例仅展示了可能性的一小部分!完成端点足够灵活,可以解决几乎任何语言处理任务,包括内容生成、摘要、语义搜索、主题标记、情感分析等等。

One limitation to keep in mind is that, for most models, a single API request can only process up to 2,048 tokens (roughly 1,500 words) between your prompt and completion.

需要注意的是,大多数模型的一个限制是,在一个API请求中,您只能处理完整的2048个tokens(大约1500个单词)的数据,包括prompt和completion。


相关文章
|
传感器 编解码 算法
Google Earth Engine(GEE)——GCOM-C / SGLI L3 叶绿素-a 浓度 (V3)数据集(5000米分辨率)
Google Earth Engine(GEE)——GCOM-C / SGLI L3 叶绿素-a 浓度 (V3)数据集(5000米分辨率)
356 0
Google Earth Engine(GEE)——GCOM-C / SGLI L3 叶绿素-a 浓度 (V3)数据集(5000米分辨率)
|
机器学习/深度学习 自然语言处理 达摩院
长文本口语语义理解技术系列①:段落分割实践
长文本口语语义理解技术系列①:段落分割实践
1411 0
长文本口语语义理解技术系列①:段落分割实践
|
编译器 C# Android开发
震惊!Uno Platform 与 C# 最新特性的完美融合,你不可不知的跨平台开发秘籍!
Uno Platform 是一个强大的跨平台应用开发框架,支持 Windows、macOS、iOS、Android 和 WebAssembly,采用 C# 和 XAML 进行编程。C# 作为其核心语言,持续推出新特性,如可空引用类型、异步流、记录类型和顶级语句等,极大地提升了开发效率。要在 Uno Platform 中使用最新 C# 特性,需确保开发环境支持相应版本,并正确配置编译器选项。通过示例展示了如何在 Uno Platform 中应用可空引用类型、异步流、记录类型及顶级语句等功能,帮助开发者更好地构建高效、优质的跨平台应用。
543 59
|
物联网 Linux C#
一键掌控未来!用 Uno Platform 打造跨平台 IoT 应用,轻松连接你的智能设备,让生活更智能!
微软的开源跨平台框架 Uno Platform 支持使用 C# 和 XAML 一次性编写代码并部署至多个平台,如 Windows、macOS、Linux、WebAssembly 及 iOS/Android,这使其成为 IoT 设备开发的理想选择。本文通过创建控制网络 LED 灯的应用,详细介绍了 Uno Platform 的环境搭建及 MQTT 客户端配置过程,实现了 LED 状态订阅与控制指令发送功能。该案例展示了 Uno Platform 在 IoT 领域的潜力及其跨平台优势,未来可扩展至更多设备类型,构建智能家居系统。
664 58
|
Android开发 iOS开发 开发者
App备案-iOS云管理式证书 Distribution Managed 公钥及证书SHA-1指纹的获取方法
App备案-iOS云管理式证书 Distribution Managed 公钥及证书SHA-1指纹的获取方法
1127 0
|
安全 虚拟化 Docker
在win10中使用ModelScope官方镜像
为在办公环境笔记本win10上测试ModelScope的开源模型 ,记录踩坑过程
2539 0
在win10中使用ModelScope官方镜像
TypeError: Cannot set properties of undefined (setting ‘resdata‘),res定义数据出现的问题,定义的方法用this换成that
TypeError: Cannot set properties of undefined (setting ‘resdata‘),res定义数据出现的问题,定义的方法用this换成that
|
网络安全 网络虚拟化 网络架构
什么是共享IP,如何判断IP是否被共享
**摘要:** 共享IP指多个用户或设备共用同一地址,常见于家庭网络、公共Wi-Fi及代理/VPN服务。判断IP是否共享可通过检查网络连接数、使用在线检测工具、观察网络活动、咨询ISP或检查路由器设置。共享IP不必然带来安全问题,但在特定场景下需关注。
|
机器学习/深度学习 算法 算法框架/工具
OpenAI Gym 中级教程——深入强化学习算法
OpenAI Gym 中级教程——深入强化学习算法
639 6
|
机器学习/深度学习 存储 算法
OpenAI Gym 中级教程——强化学习实践项目
OpenAI Gym 中级教程——强化学习实践项目
304 1