Text Generation

Learn how to generate text from a prompt.

Overview

OpenAI provides simple APIs to use a large language model to generate text from a prompt, as you might using ChatGPT. These models have been trained on vast quantities of data to understand multimedia inputs and natural language instructions.

Quickstart

To generate text, you can use the chat completions endpoint in the REST API. You can either use the REST API from the HTTP client of your choice, or use one of OpenAI's official SDKs.

Generate Prose

Create a human-like response to a prompt

import OpenAI from "openai";
const openai = new OpenAI();

const completion = await openai.chat.completions.create({
    model: "gpt-4o",
    messages: [
        { role: "developer", content: "You are a helpful assistant." },
        {
            role: "user",
            content: "Write a haiku about recursion in programming.",
        },
    ],
});

console.log(completion.choices[0].message);

Analyze an Image

Describe the contents of an image

import OpenAI from "openai";
const openai = new OpenAI();

const completion = await openai.chat.completions.create({
    model: "gpt-4o",
    messages: [
        {
            role: "user",
            content: [
                { type: "text", text: "What's in this image?" },
                {
                    type: "image_url",
                    image_url: {
                        "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
                    },
                }
            ],
        },
    ],
});

console.log(completion.choices[0].message);

Generate JSON Data

Generate JSON data based on a JSON Schema

import OpenAI from "openai";
const openai = new OpenAI();

const completion = await openai.chat.completions.create({
    model: "gpt-4o-2024-08-06",
    messages: [
        { role: "developer", content: "You extract email addresses into JSON data." },
        {
            role: "user",
            content: "Feeling stuck? Send a message to help@mycompany.com.",
        },
    ],
    response_format: {
        type: "json_schema",
        json_schema: {
            name: "email_schema",
            schema: {
                type: "object",
                properties: {
                    email: {
                        description: "The email address that appears in the input",
                        type: "string"
                    }
                },
                additionalProperties: false
            }
        }
    }
});

console.log(completion.choices[0].message.content);

Choosing a Model

Large Model

GPT-4o offers high intelligence and strong performance, with higher cost per token.

Small Model

GPT-4o-mini is faster and less expensive, with good intelligence for focused tasks.

Reasoning Model

o1 models excel at complex reasoning and multi-step planning tasks.

Building Prompts

Prompt engineering is the process of crafting prompts to get the right output from a model. The chat completions API uses different message roles to influence how the model interprets input.

Developer Messages

Messages with the developer role provide instructions that are prioritized ahead of user messages.

Example of a developer message

const response = await openai.chat.completions.create({
  model: "gpt-4o",
  messages: [
    {
      "role": "developer",
      "content": [
        {
          "type": "text",
          "text": `
            You are a helpful assistant that answers programming 
            questions in the style of a southern belle from the 
            southeast United States.
          `
        }
      ]
    },
    {
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": "Are semicolons optional in JavaScript?"
        }
      ]
    }
  ]
});

Conversations

Implement multi-turn conversations by providing additional messages in your requests.

Example of a conversation

const response = await openai.chat.completions.create({
  model: "gpt-4o",
  messages: [
    {
      "role": "user",
      "content": [{ "type": "text", "text": "knock knock." }]
    },
    {
      "role": "assistant",
      "content": [{ "type": "text", "text": "Who's there?" }]
    },
    {
      "role": "user",
      "content": [{ "type": "text", "text": "Orange." }]
    }
  ]
});

Optimization

Accuracy

Ensure accurate responses through prompt engineering, RAG, and model fine-tuning.

Cost

Reduce costs by optimizing token usage and using appropriate models.

Latency

Decrease response time through prompt engineering and code optimization.

Next Steps