Skip to content

OpenAI API Cheat Sheet

This quick reference summarizes the most common API methods and the key parameters you need when working with OpenAI models. It complements the more detailed official OpenAI API documentation.

1. Text Completion

Use openai.Completion.create to generate or continue text based on a prompt.

  • model – ID of the model to use (e.g., text-davinci-003).
  • prompt – The text to complete.
  • max_tokens – Maximum number of tokens in the generated response.
  • temperature – Controls randomness; higher values yield more creative results.
  • top_p – Alternative to temperature using nucleus sampling.
  • n – How many completions to generate.
  • stop – Up to four sequences where the API should stop generating.
  • logprobs – Include log probabilities on generated tokens.
  • user – Unique identifier for tracking the end user.

2. Chat Completion

Use openai.ChatCompletion.create for conversational interactions.

  • model – Typically gpt-3.5-turbo or gpt-4.
  • messages – List of {"role": "user|assistant|system", "content": "..."} messages.
  • max_tokens – Limit on response length.
  • temperature – Response creativity.
  • top_p – Nucleus sampling value.
  • n – Number of chat completions to generate.
  • stream – If True, returns responses incrementally.
  • stop – Sequences that cut off the response.
  • presence_penalty – Penalize new tokens based on whether they appear in the text so far.
  • frequency_penalty – Penalize new tokens based on their frequency so far.
  • logit_bias – Adjust the likelihood of specified tokens.
  • user – Unique identifier for the end user.

3. Embeddings

Use openai.Embedding.create to obtain vector representations of text.

  • model – Embedding model name (e.g., text-embedding-ada-002).
  • input – String or array of text strings to embed.
  • user – Optional unique identifier for end user.

4. Audio Transcription

Use openai.Audio.transcribe for speech-to-text.

  • model – E.g., whisper-1.
  • file – Audio file object in supported format (mp3, mp4, wav, etc.).
  • prompt – Optional text to guide transcription.
  • response_formatjson, text, srt, or vtt.
  • temperature – Controls randomness in transcription.

5. Images

Use openai.Image.create to generate images from a prompt.

  • prompt – Text description of the desired image.
  • n – Number of images to generate.
  • size – Image dimensions (256x256, 512x512, or 1024x1024).
  • response_formaturl (default) or b64_json for base64-encoded content.
  • user – Unique identifier for the end user.

6. Moderation

Use openai.Moderation.create to check text against OpenAI's content policy.

  • input – Text to be moderated.
  • model – Defaults to text-moderation-latest.

7. Managing API Keys

Set your API key in the environment before making requests:

export OPENAI_API_KEY="sk-..."

You can also use openai.api_key within your Python code.

8. Helpful Tips

  • Handle API errors with try/except blocks to gracefully manage rate limits or authentication issues.
  • Keep track of usage by inspecting response headers or using the usage dashboard.
  • Securely store your API key. Avoid hard-coding keys in shared repositories.