liveBook · Manning

Prompt

As explained in Starting Data Analytics with Generative AI and Python

An input instruction or contextual payload that directs a generative AI system to produce a response.

Need to find positive Portuguese customer reviews? A prompt is the question and context sent to a generative AI to guide its response. Unlike a search query made mainly of keywords, it can also specify the task, audience, background, and desired form of the answer. Its wording matters: a small change in phrasing can change the response. section 5.5.3 section 1.1

Make the request useful, then check what comes back:

  • State the problem clearly and concisely. Include relevant examples or background, and specify a format when the result must feed another step. section 1.3.6 section 8.4.2
  • Inspect the answer and refine the prompt if necessary. Keep notes on prompts whose outputs you have verified, including errors to avoid. section 1.3.6
  • Check consequential results against records or other reference points. A plausible answer can still be wrong, and a model may agree with a challenge even when its first answer was right. section 1.1 section 6.2.2

From a request to a review workflow

Ask: “Generate a list of 20 keywords indicating positive sentiment to be used for searching customer reviews in Portuguese.” A Python API call can return a list for searching reviews, but inspect its format before processing it. The requested 20 keywords are not the same as a 20-token output limit: a low max_tokens setting can truncate the list. For a different approach, ask for the sentiment of supplied Portuguese reviews, then compare the answers with review scores before relying on a larger run. section 5.5.3 section 6.2.2

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())

The Python listing places the keyword request in a user message to the ChatGPT API and splits the returned text into lines. Treat that split as something to verify, not a guaranteed output format; check current API documentation if the call needs updating. section 5.5.3

More detail is not automatically better. Supply context that changes how the task should be interpreted—for instance, known sarcasm in text being classified—and ask for the explanation or format you need. Test revised prompts rather than trusting a polished answer at first sight. section 8.4.2 section 1.3.6

How Starting Data Analytics with Generative AI and Python covers it

How deep it goes
Develops practical wording, context, output-format, iteration, and validation guidance, without descending into model architecture.
Examples
Uses a Portuguese-review keyword request, a Python ChatGPT API call, and sentiment questions checked against review scores.
What it assumes
Introduces natural-language prompting without requiring model-architecture details; the API example assumes readers can inspect a Python workflow.
What you can do after
Apply it via a ChatGPT API call or direct sentiment request, then inspect formatting and validate results before use.
How it connects
Links prompts to prompt engineering, API calls, keyword search, sentiment analysis, and output validation.

Also explained in

Compare how 50 books explain prompt →

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.