Text generation and prompting
=============================
Learn how to prompt a model to generate text.
With the OpenAI API, you can use a [large language model](/docs/models) to generate text from a prompt, as you might using [ChatGPT](https://chatgpt.com). Models can generate almost any kind of text response—like code, mathematical equations, structured JSON data, or human-like prose.
Here's a simple example using the [Chat Completions API](/docs/api-reference/chat).
Generate text from a simple prompt
```javascript
import OpenAI from "openai";
const client = new OpenAI();
const completion = await client.chat.completions.create({
model: "gpt-4o",
messages: [
{
role: "user",
content: "Write a one-sentence bedtime story about a unicorn.",
},
],
});
console.log(completion.choices[0].message.content);
```
```python
from openai import OpenAI
client = OpenAI()
completion = client.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "user",
"content": "Write a one-sentence bedtime story about a unicorn."
}
]
)
print(completion.choices[0].message.content)
```
```bash
curl "https://api.openai.com/v1/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-4o",
"messages": [
{
"role": "user",
"content": "Write a one-sentence bedtime story about a unicorn."
}
]
}'
```
An array of content generated by the model is in the `choices` property of the response. In this simple example, we have just one output which looks like this:
```json
[
{
"index": 0,
"message": {
"role": "assistant",
"content": "Under the soft glow of the moon, Luna the unicorn danced through fields of twinkling stardust, leaving trails of dreams for every child asleep.",
"refusal": null
},
"logprobs": null,
"finish_reason": "stop"
}
]
```
In addition to plain text, you can also have the model return structured data in JSON format - this feature is called [**Structured Outputs**](/docs/guides/structured-outputs).
Message roles and instruction following
---------------------------------------
You can provide instructions (prompts) to the model with [differing levels of authority](https://model-spec.openai.com/2025-02-12.html#chain_of_command) using **message roles**.
Generate text with messages using different roles
```javascript
import OpenAI from "openai";
const client = new OpenAI();
const completion = await client.chat.completions.create({
model: "gpt-4o",
messages: [
{
role: "developer",
content: "Talk like a pirate."
},
{
role: "user",
content: "Are semicolons optional in JavaScript?",
},
],
});
console.log(completion.choices[0].message);
```
```python
from openai import OpenAI
client = OpenAI()
completion = client.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "developer",
"content": "Talk like a pirate."
},
{
"role": "user",
"content": "Are semicolons optional in JavaScript?"
}
]
)
print(completion.choices[0].message.content)
```
```bash
curl "https://api.openai.com/v1/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-4o",
"messages": [
{
"role": "developer",
"content": "Talk like a pirate."
},
{
"role": "user",
"content": "Are semicolons optional in JavaScript?"
}
]
}'
```
The [OpenAI model spec](https://model-spec.openai.com/2025-02-12.html#chain_of_command) describes how our models give different levels of priority to messages with different roles.
|developer|user|assistant|
|---|---|---|
|developer messages are instructions provided by the application developer, weighted ahead of user messages.|user messages are instructions provided by an end user, weighted behind developer messages.|Messages generated by the model have the assistant role.|
A multi-turn conversation may consist of several messages of these types, along with other content types provided by both you and the model. Learn more about [managing conversation state here](/docs/guides/conversation-state).
Choosing a model
----------------
A key choice to make when generating content through the API is which model you want to use - the `model` parameter of the code samples above. [You can find a full listing of available models here](/docs/models).
### Which model should I choose?
Here are a few factors to consider when choosing a model for text generation.
* **[Reasoning models](/docs/guides/reasoning)** generate an internal chain of thought to analyze the input prompt, and excel at understanding complex tasks and multi-step planning. They are also generally slower and more expensive to use than GPT models.
* **GPT models** are fast, cost-efficient, and highly intelligent, but benefit from more explicit instructions around how to accomplish tasks.
* **Large and small (mini) models** offer trade-offs for speed, cost, and intelligence. Large models are more effective at understanding prompts and solving problems across domains, while small models are generally faster and cheaper to use. Small models like GPT-4o mini can also be trained to excel at a specific task through [fine tuning](/docs/guides/fine-tuning) and [distillation](/docs/guides/distillation) of results from larger models.
When in doubt, `gpt-4o` offers a solid combination of intelligence, speed, and cost effectiveness.
Prompt engineering
------------------
Creating effective instructions for a model to generate content is a process known as **prompt engineering**. Because the content generated from a model is non-deterministic, it is a combination of art and science to build a prompt that will generate the right kind of content from a model. You can find a more complete exploration of [prompt engineering here](/docs/guides/prompt-engineering), but here are some general guidelines:
* Be detailed in your instructions to the model to eliminate ambiguity in how you want the model to respond.
* Provide examples to the model of the type of inputs you expect, and the type of outputs you would want for that input - this technique is called **few-shot learning**.
* When using a [reasoning model](/docs/guides/reasoning), describe the task to be done in terms of goals and desired outcomes, rather than specific step-by-step instructions of how to accomplish a task.
* Invest in creating [evaluations (evals)](/docs/guides/evals) for your prompts, using test data that looks like the data you expect to see in production. Due to the inherent variable results from different models, using evals to see how your prompts perform is the best way to ensure your prompts work as expected.
Iterating on prompts is often all you need to do to get great results from a model, but you can also explore [fine tuning](/docs/guides/fine-tuning) to customize base models for a particular use case.
Next steps
----------
Now that you known the basics of text inputs and outputs, you might want to check out one of these resources next.
[
Build a prompt in the Playground
Use the Playground to develop and iterate on prompts.
](/playground)[
Generate JSON data with Structured Outputs
Ensure JSON data emitted from a model conforms to a JSON schema.
](/docs/guides/structured-outputs)[
Full API reference
Check out all the options for text generation in the API reference.
](/docs/api-reference/responses)
Was this page useful?