liveBook · Manning

Consistency

As explained in Starting Data Analytics with Generative AI and Python

The property of data values and representations conforming to the same rules and conventions across records or sources.

Consistency means applying the same rules and conventions across records, sources, formats, systems, or repeated model outputs. It asks whether comparable data receives comparable treatment, so analysis does not change merely because its origin or generation run changed. section 2.2 section 5.1.3

Check consistency at several points in an analytical workflow:

  • Compare values and representations across sources, formats, and systems before drawing conclusions. section 2.2
  • Treat generative-AI output as variable: lists may change between runs or arrive in an unexpected format. section 5.5.3
  • Inspect returned data manually, refine the prompt, and use dedicated libraries when repeatable rules, equitable treatment, speed, or scale matter. section 5.1.3 section 5.5.3

A keyword list that changes shape

A prompt requests 20 Portuguese keywords expressing positive sentiment in customer reviews. One run may produce a usable list, while another may vary its terms or formatting. A Python workflow can split the response into lines, but the result still requires inspection, prompt refinement, and checks for truncation. The max_tokens setting limits response tokens rather than the number of keywords, so a setting below roughly 100 or 120 can produce an incomplete list. 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())

Inspect the Python API workflow that generates keyword lists and exposes the points where formatting, model variation, and token limits can disrupt consistency. section 5.5.3

How Starting Data Analytics with Generative AI and Python covers it

How deep it goes
Defines cross-system consistency and examines generated-output variation, formatting, inspection, prompt refinement, and token-limit failures.
Examples
Uses cross-source data checks, social-media moderation, and Portuguese customer-review keyword generation with a Python API workflow.
What it assumes
Builds from basic data-quality concerns and a working understanding of prompts, generated text, and API parameters.
What you can do after
Diagnose inconsistent generated lists by inspecting formatting, refining prompts, adjusting token limits, and comparing results with repeatable processing.
How it connects
Connects data quality, generative AI, text mining, sentiment analysis, content moderation, and dedicated libraries.

Also explained in

Compare how 20 books explain consistency →

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.