liveBook · Manning

Contract

As explained in Starting Data Analytics with Generative AI and Python

A defined software interface through which programs request data or operations from another system and receive responses.

An application programming interface (API) lets a program send a request to another service and receive a response. Think of it as a service counter: your Python code submits a prompt to a generative AI model, then uses the returned text in an analysis. Unlike a web chat, API access can fit into a repeatable workflow or a custom analytics application. section 1.3.3

A practical model-API workflow has a few checkpoints:

  • Set up the provider’s library and API key, then follow its current documentation. SDKs can reduce routine coding, but check their compatibility and support. section 1.3.3
  • Send a prompt and any relevant model parameters; collect the response in Python rather than assuming it will arrive in exactly the format you want. section 5.5.3
  • Inspect results and handle failures. A model may generate code for an outdated API interface, and even a successful request may return text that needs careful parsing. section 5.5.3

From a customer question to review analysis

Suppose a team wants to find positive Portuguese customer reviews. An application could request candidate search keywords through an API, then run its own code to find matching reviews. That is feasible, but the returned keyword list needs inspection before it drives a search: consistent formatting is not guaranteed. section 5.5.3

Listing 6.1 Sentiment analysis with ChatGPT's intrinsic language capabilities
from openai import OpenAI
# Replace 'your_openai_api_key' with your actual OpenAI API key
client = OpenAI(
    api_key= "your-api-key",
)
def get_sentiment(review):
    response = client.chat.completions.create(
        messages=[
            {
                "role": "user",
                "content": f"The sentiment of this review is: {review}",
            }
        ],
        model="gpt-4-0125-preview",
    )
    completion = response.choices[0].message.content
    if "positive" in completion:
        return "positive"
    elif "neutral" in completion:
        return "neutral"
    elif "negative" in completion:
        return "negative"
    else:
        return "unknown"
# Analyze the reviews and store the output (manually adapted)
sentiments = []
for review in reviews:
    sentiments.append(get_sentiment(review))
df["GPT4"] = sentiments

The Python listing sends each review to ChatGPT and extracts a sentiment label from the response. Notice that its simple word-matching rules can miss nuanced or mixed answers. section 6.2.3

Scale does not remove the need to validate. In a 500-review comparison using review scores as the reference, the API-based method reached 0.74 sensitivity and 0.93 specificity for positive reviews; basic keyword search reached 0.45 and 0.90. APIs also do not give a generative AI automatic access to your databases or other data sources—connecting those systems requires additional integration. section 6.2.3 section 1.2.3

Table 6.2 Quality of sentiment analysis with ChatGPT 4 API
Sentiment analysis ( n=500)
Number of reviews classified as positive
Sensitivity
Specificity
Reference review score
320 (64%)
true positives
1
1
Basic keyword search
163 (33%)
0.45
0.9
ChatGPT 4 API
250 (50%)
0.74
0.93

Compare the API method with keyword search against the same review-score reference, rather than judging it by plausible-looking individual responses. section 6.2.3

How Starting Data Analytics with Generative AI and Python covers it

How deep it goes
Demonstrates Python requests, response handling, and evaluation; does not explain API internals.
Examples
Uses Portuguese review-keyword generation and API-based classification of 500 customer reviews.
What it assumes
Assumes basic Python workflow familiarity while spelling out the need for an API key and provider documentation.
What you can do after
Apply it via the OpenAI Python library to request model output, inspect responses, and compare sentiment results with a reference.
How it connects
Links APIs to SDKs, prompt engineering, sentiment analysis, and database integration.

Also explained in

Compare how 44 books explain contract →

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.