Openai API 文档 - 创建第一个应用

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.

现在我们学会了如何使用提示词和配置,我们可以创建我们的宠物名称生成器了。我们已经写过一些代码了,下面的教程将带着我们一起下载代码,跑起应用!

Setup 安装

If you don’t have Python installed, install it from here. Then download the code by cloning this repository.

如果你还没有安装python,  点击这里安装. 然后从这个地址下载代码

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

If you prefer not to use git, you can alternatively download the code using this zip file.

如果你不喜欢使用git,你可以选择直接下载。

Add your API key 添加API key

To get the app working, you’ll need an API key. You can get one by signing up for an account and returning to this page.

我们需要api key 使你的应用跑起来。你可以你的账号里获取一个。

Run the app 启动应用

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
flask run

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

打开 http://localhost:5000 你就可以看到你的宠物名称生成器了!

Understand the code 了解代码

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。

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!

就是这样,你应该已经了解到了怎么让你的宠物起名器使用open api了。

Closing 最后

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.

这些概念和技术将一直帮助你创建你的应用。当然,这只是一个简单的例子。我们的completions可以灵活的完成任何语言类型的任务,包括生成内容,总结,搜索,打标签,语义分析等等等等。

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个单词)

For more advanced tasks, you might find yourself wishing you could provide more examples or context than you can fit in a single prompt. The fine-tuning API is a great option for more advanced tasks like this. Fine-tuning allows you to provide hundreds or even thousands of examples to customize a model for your specific use case.

对于高级的任务,你会发现你需要提供更多的例子和上下文。fine-tuning API 对于高级任务是一个很好的选择。Fine-tuning允许你提供成百上千个例子来针对你的使用场景个性化你的模型。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值