liveBook · Manning

OpenAI API

As explained in Starting Data Analytics with Generative AI and Python

A public programming interface that provides access to OpenAI artificial-intelligence models and services.

The OpenAI API lets a program send requests to OpenAI models and receive responses, giving data-analytics workflows more control than a manual chat. Access begins with an API key and the API documentation. section 1.3.3

A practical Python setup stores OPENAI_API_KEY in the environment, preferably through a project .env file, then creates an OpenAI client. SDKs and libraries such as LangChain can provide reusable functions for common API tasks. section 1.3.3

Listing 1.1 Interacting with ChatGPT through the API
from openai import OpenAI
prompt_text = """Generate a list of 20 keywords indicating positive 
sentiment to be used for searching customer reviews in 
Portuguese."""
client = OpenAI(
    api_key=<<your-API-key-here>>,
)
chat_completion = client.chat.completions.create(
    messages=[
    {"role": "system", "content": "You are a scientific assistant, skilled
    at explaining science to schoolchildren."},
    {"role": "user", "content": "Explain resonance to first-graders?"},
    {"role": "assistant", "content": "Alright, kids, today we're going to
    talk about something really cool called resonance! (...)."},
    # Shortened from the full model response
    {"role": "user", "content": "Where can we use it?"}
  ],


    model="gpt-4-0125-preview",
    temperature=0.7,   
)
print(chat_completion.choices[0].message.content)

A complete listing shows a Python client, a model selection, temperature control, and a multi-turn message exchange before printing the response. section 1.3.3

A small analytics-oriented request

A program can ask for 20 Portuguese keywords expressing positive sentiment for searching customer reviews. The same request pattern can support preprocessing, insight generation, or visualization tasks, but the returned result still requires inspection against the underlying data. section 1.3.3 section 1.3.6

Reliable API integrations should:

  • Define the analytical objective before choosing prompts or API calls. section 1.3.6
  • Use compatible data formats and transform data when necessary to prevent loss or misinterpretation. section 1.3.6
  • Monitor usage and costs, while applying rate limits, caching, or other optimizations. section 1.3.6
  • Handle timeouts and rate-limit errors with error handling and retry mechanisms. section 1.3.6
  • Simplify prompts, experiment with their structure, and preserve verified prompt notes for reuse. section 1.3.6

How Starting Data Analytics with Generative AI and Python covers it

How deep it goes
Demonstrates Python setup and a multiturn request, then emphasizes formats, costs, errors, retries, and prompt refinement rather than advanced integration.
Examples
Uses Portuguese sentiment keywords, a science-explanation conversation, API client code, and environment-based API-key configuration.
What it assumes
Assumes basic Python and a configured API key while teaching setup, prompting, request handling, and integration checks.
What you can do after
Apply it via Python to send prompts, receive model responses, and build controlled analytics interactions.
How it connects
Connects API calls with Python, SDKs, data formats, prompts, error handling, rate limiting, caching, and data analytics.

Also explained in

Compare how 4 books explain OpenAI API →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Starting Data Analytics with Generative AI and Python. The section links point to the parts of the book it draws on.