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。


相关文章
|
11月前
|
机器学习/深度学习 人工智能 搜索推荐
LangChain入门指南
LangChain入门指南
2187 0
|
25天前
|
机器学习/深度学习 数据采集 算法
scikit-learn入门指南:从基础到实践
【4月更文挑战第17天】这篇指南介绍了scikit-learn,一个Python数据分析和机器学习的重要库。内容涵盖安装、数据加载与预处理、模型训练(如KNN分类器)、评估、调参优化及高级应用,如降维和聚类。通过实例展示了scikit-learn在分类任务中的使用,强调其在数据科学中的重要性。要深入了解,可参考官方文档和实践案例。
|
3月前
|
机器学习/深度学习 人工智能 自然语言处理
如何使用Sora?OpenAI Sora 介绍及使用教程
2024年2月16日,OpenAI 在其官网上面正式宣布推出文本生成视频的大模型 Sora, Sora能够根据简单的文本描述,生成高达60秒的高质量视频,使得视频创作变得前所未有的简单和高效。
如何使用Sora?OpenAI Sora 介绍及使用教程
|
机器学习/深度学习 存储 算法
python机器学习课程——决策树全网最详解超详细笔记附代码
决策树算法是一种逼近离散函数值的方法。它是一种典型的分类方法,首先对数据进行处理,利用归纳算法生成可读的规则和决策树,然后使用决策对新数据进行分析。本质上决策树是通过一系列规则对数据进行分类的过程。决策树方法最早产生于上世纪60年代,到70年代末。由J Ross Quinlan提出了ID3算法,此算法的目的在于减少树的深度。但是忽略了叶子数目的研究。C4.5算法在ID3算法的基础上进行了改进,对于预测变量的缺值处理、剪枝技术、派生规则等方面作了较大改进,既适合于分类问题,又适合于回归问题。决策树算法构造决策
290 0
|
机器学习/深度学习 数据挖掘 领域建模
机器学习神器Scikit-Learn入门教程
Scikit-learn是一个非常知名的Python机器学习库,它广泛地用于统计分析和机器学习建模等数据科学领域。
|
机器学习/深度学习 自然语言处理 并行计算
Pytorch学习系列教程:入门简介
新的一年还是要肝起来啊,这个公众号节前节后沉寂了快两个月了,许久没更新原创推文,自己感觉不能再这样堕落下去了。所以最近一直在思考写些什么推文:既要符合当下工作所需,倒逼成长;也要于广大读者有益,而非单纯的推销运营。 于是,终于决定“染指”深度学习方向,并打算先更新一波Pytorch学习教程。当然,这会是一个系列。
218 0
Pytorch学习系列教程:入门简介
|
机器学习/深度学习 人工智能 算法
2019 年 11 月最新《TensorFlow 2.0 深度学习算法实战》中文版教材免费开源(附随书代码+pdf)
2019 年 11 月最新《TensorFlow 2.0 深度学习算法实战》中文版教材免费开源(附随书代码+pdf)
344 0
2019 年 11 月最新《TensorFlow 2.0 深度学习算法实战》中文版教材免费开源(附随书代码+pdf)
|
算法框架/工具 TensorFlow 算法
带你读《TensorFlow机器学习实战指南(原书第2版)》之二:TensorFlow进阶
本书由资深数据科学家撰写,从实战角度系统讲解TensorFlow基本概念及各种应用实践。真实的应用场景和数据,丰富的代码实例,详尽的操作步骤,带领读者由浅入深系统掌握TensorFlow机器学习算法及其实现。本书第1章和第2章介绍了关于TensorFlow使用的基础知识,后续章节则针对一些典型算法和典型应用场景进行了实现,并配有较详细的程序说明,可读性非常强。读者如果能对其中代码进行复现,则必定会对TensorFlow的使用了如指掌。
|
机器学习/深度学习 API TensorFlow
|
机器学习/深度学习 TensorFlow 算法框架/工具
【干货】史上最全的Tensorflow学习资源汇总,速藏!
本篇文章将为大家总结Tensorflow纯干货学习资源,分为教程、视频和项目三大板块,非常适合新手学习,建议大家收藏。
6796 0