Introduction #
Overview #
The GPT 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 quick start tutorial to get acquainted with key concepts through a hands-on, interactive example.
Quick start tutorial #
Learn by building a quick sample application!
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 token’s “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.
Quick start #
GPT AI 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 quick start 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.
1Start 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.
Not bad! Now, try making your instructions more specific.
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.
2Add 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.
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 of examples.
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.
3Adjust 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.
Temperature
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 #
For your pet’s name generator, you probably want to be able to generate a lot of name ideas. A moderate temperature of 0.6 should work well.
4Build your application #
NODE.JSPYTHON (FLASK)
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 Node.js installed, install it from here. Then download the code by cloning this repository.
If you prefer not to use git, you can alternatively download the code using this zip file.
Understand the code #
Open generate.js in the GPT-quick start-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.
- 1 function generatePrompt(animal) {
- 2 const capitalizedAnimal = animal[0].toUpperCase() + animal.slice(1).toLowerCase();
- 3 return `Suggest three names for an animal that is a superhero.
- 4 Animal: Cat
- 5 Names: Captain Sharp claw, Agent Fluffball, The Incredible Feline
- 6 Animal: Dog
- 7 Names: Ruff the Protector, Wonder Canine, Sir Barks-a-Lot
- 8 Animal: ${capitalizedAnimal}
- 9 Names:`;
- 10 }
11 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.
- 1 const completion = await openai.createCompletion({
- 2 model: “text-davinci-003”,
- 3 prompt: generatePrompt(req.body.animal),
- 4 temperature: 0.6,
- 5 });
And that’s it! You should now have a full understanding of how your (superhero) pet name generator uses the GPT 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 #
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.

