How to call the OpenAI API from Python

· Category: AI & Machine Learning

Short answer

The OpenAI API provides access to large language models for text generation, embeddings, and image generation through simple HTTP requests.

Steps

  1. Install the openai Python library and set your API key as an environment variable.
  2. Choose the appropriate model and endpoint for your task such as chat.completions.create.
  3. Construct prompts with system and user messages for chat models.
  4. Adjust parameters like temperature, max_tokens, and top_p to control output creativity.
  5. Handle rate limits and errors with exponential backoff and retries.

Tips

  • Use streaming responses for long outputs to improve perceived latency.
  • Store API keys in environment variables or secret managers, never in source code.
  • Tokenize prompts locally to estimate costs and stay within context limits.
  • Cache frequent queries to reduce redundant API calls and cost.

Common issues

  • Hitting rate limits during high-volume batch processing.
  • Exceeding token limits causing truncated or failed responses.
  • Inconsistent output format requiring post-processing and validation.
  • Unexpected costs from verbose prompts or high temperature sampling.

Example

import openai

response = openai.chat.completions.create(
    model='gpt-4',
    messages=[{'role': 'user', 'content': 'Explain Python decorators'}]
)
print(response.choices[0].message.content)

This snippet calls the OpenAI chat completions endpoint and prints the generated explanation for a programming concept.