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。


相关文章
|
21天前
大模型应用开发-LangChain入门教程
大模型应用开发-LangChain入门教程
137 0
|
12天前
|
存储 机器学习/深度学习 人工智能
【LangChain系列】第八篇:文档问答简介及实践
【5月更文挑战第22天】本文探讨了如何使用大型语言模型(LLM)进行文档问答,通过结合LLM与外部数据源提高灵活性。 LangChain库被介绍为简化这一过程的工具,它涵盖了嵌入、向量存储和不同类型的检索问答链,如Stuff、Map-reduce、Refine和Map-rerank。文章通过示例展示了如何使用LLM从CSV文件中提取信息并以Markdown格式展示
67 2
|
15天前
|
机器学习/深度学习 人工智能 自然语言处理
【LangChain系列】第五篇:大语言模型中的提示词,模型及输出简介及实践
【5月更文挑战第19天】LangChain是一个Python库,简化了与大型语言模型(LLM)如GPT-3.5-turbo的交互。通过ChatOpenAI类,开发者可以创建确定性输出的应用。提示词是指导LLM执行任务的关键,ChatPromptTemplate允许创建可重用的提示模板。输出解析器如StructuredOutputParser将模型的响应转化为结构化数据,便于应用处理。LangChain提供可重用性、一致性、可扩展性,并有一系列预建功能。它使得利用LLM构建复杂、直观的应用变得更加容易。
62 0
|
21天前
|
机器学习/深度学习 算法 Python
OpenAI Gym高级教程——解决实际问题与研究探索
OpenAI Gym高级教程——解决实际问题与研究探索
38 1
|
21天前
|
机器学习/深度学习 监控 算法
OpenAI Gym 高级教程——深度强化学习库的高级用法
OpenAI Gym 高级教程——深度强化学习库的高级用法
143 0
|
21天前
|
机器学习/深度学习 存储 算法
OpenAI Gym 中级教程——强化学习实践项目
OpenAI Gym 中级教程——强化学习实践项目
64 1
|
21天前
Midjourney如何使用
Midjourney如何使用
70 0
|
机器学习/深度学习 Web App开发 搜索推荐
GitHub标星23k+,从零开始的深度学习实用教程 | PyTorch官方推荐
背景基础部分,除了有Python指南、笔记本用法,以及Numpy、Pandas这些库的用法,还有线性规划、逻辑规划、随机森林、k-means聚类这些机器学习的基本技术。
GitHub标星23k+,从零开始的深度学习实用教程 | PyTorch官方推荐
|
机器学习/深度学习 人工智能 算法
2019 年 11 月最新《TensorFlow 2.0 深度学习算法实战》中文版教材免费开源(附随书代码+pdf)
2019 年 11 月最新《TensorFlow 2.0 深度学习算法实战》中文版教材免费开源(附随书代码+pdf)
354 0
2019 年 11 月最新《TensorFlow 2.0 深度学习算法实战》中文版教材免费开源(附随书代码+pdf)
|
机器学习/深度学习 人工智能 算法
TF之AutoML框架:AutoML框架的简介、特点、使用方法详细攻略
TF之AutoML框架:AutoML框架的简介、特点、使用方法详细攻略

热门文章

最新文章