OpenAI API ドキュメント 日本語訳|#1 GET STARTED 前編
OpenAI API ドキュメントの日本語訳をこちらでまとめます。文字量の多いドキュメントなので、セクションごとに記事を分割しています。
今回は「GET STARTED 」のセクションからIntroduction と Quickstart を抜粋した前編です。
基本 DeepLで翻訳して、気になるところだけ書き換えています(ほぼ気になるところがないのが、DeepLのすごいところ)。原文との突き合わせができるようにはじめに原文を入れてますので、間違いなど見つけられましたら、ぜひご指摘ください。ご指摘箇所は随時反映させていただきます。
原文のリンクが有効になってますので、それぞれ必要な場合は原文リンクの方を参照ください。
Introduction|はじめに
Overview|概要
The OpenAI API can be applied to virtually any task that involves understanding or generating natural language or code. We offer a spectrum of models with different levels of power suitable for different tasks, as well as the ability to fine-tune your own custom models. These models can be used for everything from content generation to semantic search and classification.
Key concepts|キーコンセプト
We recommend completing our quickstart tutorial to get acquainted with key concepts through a hands-on, interactive example.
Prompts and completions|プロンプトとコンプリート
The completions endpoint is at the center of our API. It provides a simple interface to our models that is extremely flexible and powerful. You input some text as a prompt, and the model will generate a text completion that attempts to match whatever context or pattern you gave it. For example, if you give the API the prompt, “Write a tagline for an ice cream shop”, it will return a completion like “We serve up smiles with every scoop!”
Designing your prompt is essentially how you “program” the model, usually by providing some instructions or a few examples. This is different from most other NLP services which are designed for a single task, such as sentiment classification or named entity recognition. Instead, the completions endpoint can be used for virtually any task including content or code generation, summarization, expansion, conversation, creative writing, style transfer, and more.
Tokens|トークン
Our models understand and process text by breaking it down into tokens. Tokens can be words or just chunks of characters. For example, the word “hamburger” gets broken up into the tokens “ham”, “bur” and “ger”, while a short and common word like “pear” is a single token. Many tokens start with a whitespace, for example “ hello” and “ bye”.
The number of tokens processed in a given API request depends on the length of both your inputs and outputs. As a rough rule of thumb, 1 token is approximately 4 characters or 0.75 words for English text. One limitation to keep in mind is that your text prompt and generated completion combined must be no more than the model's maximum context length (for most models this is 2048 tokens, or about 1500 words). Check out our tokenizer tool to learn more about how text translates to tokens.
Models|モデル
The API is powered by a set of models with different capabilities and price points. Our base GPT-3 models are called Davinci, Curie, Babbage and Ada. Our Codex series is a descendant of GPT-3 that’s been trained on both natural language and code. To learn more, visit our models documentation.
Next steps|ネクストステップ
Keep our usage policies in mind as you start building your application.
Explore our examples library for inspiration.
Jump into one of our guides to start building.
Guides|ガイド
Text completion
Learn how to generate or edit text using our models
Code completion *Limited beta
Learn how to generate, edit, or explain code
Image generation *Beta
Learn how to generate or edit images
Fine-tuning
Learn how to train a model for your use case
Embeddings
Learn how to search, classify, and compare text
Quickstart|クイックスタート
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.
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:
Content generation
Summarization
Classification, categorization, and sentiment analysis
Data extraction
Translation
Many more!
Introduction|はじめに
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.
Prompt Write a tagline for an ice cream shop.
↓
Completion We serve up smiles with every scoop!
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.
1. Start with an instruction|インストラクションから始める
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.
Suggest one name for a horse.
Not bad! Now, try making your instruction more specific.
Suggest one name for a black horse.
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.
2. Add some examples|事例を追加する
Crafting good instructions is important for getting good results, but sometimes they aren’t enough. Let’s try making your instruction even more complex.
Suggest three names for a horse that is a superhero.
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.
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: Horse
Names:
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.
3. Adjust your settings|設定を調整する
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.
Try re-submitting the same prompt a few times with temperature set to 1.
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: Horse
Names:
See what happened? When temperature is above 0, submitting the same prompt results in different completions each time.
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.
DEEP DIVE - Understanding tokens and probabilities|さらに深掘り - トークンと確率を理解する
Our models process text by breaking it down into smaller units called tokens. Tokens can be words, chunks of words, or single characters. Edit the text below to see how it gets tokenized.
Common words like “cat” are a single token, while less common words are often broken down into multiple tokens. For example, “Butterscotch” translates to four tokens: “But”, “ters”, “cot”, and “ch”. Many tokens start with a whitespace, for example “ hello” and “ bye”.
Given some text, the model determines which token is most likely to come next. For example, the text “Horses are my favorite” is most likely to be followed with the token “ animal”.
This is where temperature comes into play. If you submit this prompt 4 times with temperature set to 0, the model will always return “ animal” next because it has the highest probability. If you increase the temperature, it will take more risks and consider tokens with lower probabilities.
It’s usually best to set a low temperature for tasks where the desired output is well-defined. Higher temperature may be useful for tasks where variety or creativity are desired, or if you'd like to generate a few variations for your end users or human experts to choose from.
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.
4. Build your application|アプリケーションの構築
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 - NODE.JS|セットアップ
If you don’t have Node.js installed, install it from here. Then download the code by cloning this repository.
git clone https://github.com/openai/openai-quickstart-node.git
If you prefer not to use git, you can alternatively download the code using this zip file.
Setup - PYTHON (FLASK)|セットアップ
If you don’t have Python installed, install it from here. Then download the code by cloning this repository.
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.
Add your API key|APIキーの追加
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.
Run the app - NODE.JS|アプリの実行
Run the following commands in the project directory to install the dependencies and run the app.
npm install
npm run dev
Open http://localhost:3000 in your browser and you should see the pet name generator!
Run the app - PYTHON (FLASK)|アプリの実行
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.
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!
Understand the code - NODE.JS|コードを理解する
Open up generate.js in the openai-quickstart-node/pages/api 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.
function generatePrompt(animal) {
const capitalizedAnimal = animal[0].toUpperCase() + animal.slice(1).toLowerCase();
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: ${capitalizedAnimal}
Names:`;
}
On line 9 in generate.js, 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.
const completion = await openai.createCompletion({
model: "text-davinci-003",
prompt: generatePrompt(req.body.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!
Understand the code - PYTHON (FLASK)|コードを理解する
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.
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.
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!
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.
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.
DEEP DIVE - Models and pricing|さらに深掘り - モデルと価格
We offer a spectrum of models with different capabilities and price points. In this tutorial, we used text-davinci-003, our most capable natural language model. We recommend using this model while experimenting since it will yield the best results. Once you’ve got things working, you can see if the other models can produce the same results with lower latency and costs.
The total number of tokens processed in a single request (both prompt and completion) can’t exceed the model's maximum context length. For most models, this is 2,048 tokens or about 1,500 words. As a rough rule of thumb, 1 token is approximately 4 characters or 0.75 words for English text.
Pricing is pay-as-you-go per 1,000 tokens, with $18 in free credit that can be used during your first 3 months. Learn more.
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.
Next steps|ネクストステップ
To get inspired and learn more about designing prompts for different tasks:
Read our completion guide.
Explore our library of example prompts.
Start experimenting in the Playground.
Keep our usage policies in mind as you start building.
今回は「GET STARTED 」のセクションからIntroduction と Quickstart を抜粋した前編をご紹介しました。
ここまで読んでいただきありがとうございます。
間違いなど見つけられましたら、ぜひご指摘ください。ご指摘箇所は随時反映させていただきます。