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) is an interface through which a program sends requests to another system and receives responses. It lets an analytics application use a generative AI model as part of a larger workflow, rather than relying on a web interface for each question. section 1.3.3

To use a model API, obtain an API key, install the appropriate library, and follow the provider’s documentation. An SDK or library can provide prebuilt functions for common tasks; check its compatibility and support status before using it. section 1.3.3

Generating review-search keywords

An application can ask a model for Portuguese keywords indicating positive sentiment, then search customer reviews for those terms. The response may need post-processing: even if one keyword list has the expected format, later responses may differ. Inspect the output and refine the prompt when necessary. section 5.5.3

Listing 5.5 Generating keywords via ChatGPT API
from openai import OpenAI

def generate_keywords(temperature=0.5, max_tokens=150):
    client = OpenAI(
        api_key="your_api_key", # Make sure to use your actual API key here
    )

    prompt = """Generate a list of 20 keywords indicating positive 
    sentiment to be used for searching customer reviews in Portuguese."""

    try:
        response = client.chat.completions.create(
            messages=[
                {
                    "role": "user",
                    "content": prompt,
                }
            ],
            model="gpt-4-0125-preview", # Replace with the model you have 
            access to
            temperature=temperature,
            max_tokens=max_tokens,
        )
        return(response.choices[0].message.content.split("\n"))

    except Exception as e:
        print(f"An error occurred: {str(e)}")

# Example usage
print("Generated Keywords:")
print(generate_keywords())

See a Python function send a keyword-generation prompt, set generation parameters, and split the response into lines. section 5.5.3

Classifying review sentiment

A sentiment workflow sends each review to ChatGPT and searches the response for sentiment labels. In a 500-review comparison, this approach identified positive reviews with 0.74 sensitivity and 0.93 specificity, using review scores as the reference. Its simple label matching can miss nuances such as mixed feelings, so inspect individual responses as well as summary metrics. section 6.2.3

An API connection does not make model output automatically reliable. Generated API code may use an outdated interface, so check the current documentation. API access may also require a pricing plan or sufficient account funds. A model does not necessarily have direct access to databases or other APIs without a separate integration. section 5.5.3 section 6.2.3 section 1.2.3

How Starting Data Analytics with Generative AI and Python covers it

How deep it goes
Demonstrates API setup, requests, response processing, and evaluation, without detailing interface internals.
Examples
Uses Portuguese review-keyword generation and sentiment classification of 500 customer reviews.
What you can do after
Apply it via the OpenAI Python library to request model responses and process them for analytics.
How it connects
Connects APIs with SDKs, generative AI, keyword search, and sentiment analysis.

Also explained in

Compare how 45 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.