liveBook · Manning

Prompt engineering

As explained in Starting Data Analytics with Generative AI and Python

Prompt engineering is the deliberate design and refinement of instructions supplied to a generative AI system.

Prompt engineering is the deliberate shaping of instructions to a generative AI assistant. For an analytical question, give it the context you would give a colleague: what problem you are solving, what the data means, and who will use the answer. Unlike a search query, a prompt can also specify how the answer should be presented. A useful first prompt is a starting point, not a guarantee of a correct result. section 8.4.2 section 1.2.1

Refine a prompt when the response misses your purpose:

  • Add relevant background. If a text’s author uses sarcasm, say so before requesting sentiment analysis. section 8.4.2
  • Specify the audience and form. Ask for examples, formal language, a word limit, or an explanation suitable for a particular reader. section 8.4.2 section 8.4.3
  • Follow up on a particular gap or suspected error. Explain what seems wrong and ask for a correction or the reasoning behind the choice. section 1.2.1 section 5.3.4

Change the audience, then check the answer

An explanation of linear regression for a pet-food business can be followed with “Now explain it to a 5-year-old.” The assistant changes its analogy to toy cars, but the response may still be too long for that audience. Audience instructions help; reading the result remains necessary. section 8.4.3

A terse follow-up can work when it names the concern. In a Python word-frequency workflow for Portuguese reviews, “Are you sure it is for Portuguese?” prompted revised NLTK tokenization code. Inspect the changed setting and the resulting analysis rather than treating the assistant’s apology as proof. Repeated, vague challenges such as “Are you sure?” can instead elicit an unjustified reversal. section 5.3.4

import pandas as pd
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from collections import Counter

# Make sure to download Portuguese stopwords
nltk.download('stopwords')
nltk.download('punkt')

# Load your DataFrame
df = pd.read_csv('olist_order_reviews_dataset.csv')

# Preprocessing function
def preprocess_text(text):
    # Tokenize, convert to lowercase, and remove punctuation
    tokens = word_tokenize(text.lower(), language='portuguese')
    words = [word for word in tokens if word.isalpha()]

    # Remove Portuguese stopwords
    stop_words = set(stopwords.words('portuguese'))
    filtered_words = [word for word in words if word not in stop_words]

    return filtered_words

# Apply preprocessing to the 'review_comment_message' column
df['processed_review'] = 
df['review_comment_message'].apply(preprocess_text)

# Calculate word frequencies
word_freq = Counter()
for _, row in df.iterrows():
    word_freq.update(row['processed_review'])

# Print the most common words and their frequencies
print(word_freq.most_common(10))

The revised Python listing makes the follow-up concrete: inspect where the tokenization call specifies Portuguese alongside the Portuguese stopword setting. section 5.3.4

Prompt wording can improve relevance, but it is not a substitute for checking generated code or findings. It can also be used to evade a model’s content restrictions. Treat prompts as a way to direct a conversation, not as a reliability or safety control on their own. section 1.2.1 section 8.1

How Starting Data Analytics with Generative AI and Python covers it

How deep it goes
Defines ordinary prompt direction through context, audience, format, and follow-up; distinguishes scientific response-testing as a niche meaning without developing it.
Examples
Illustrates audience changes with linear regression and targeted error questioning with Portuguese review tokenization.
What it assumes
Assumes an analytical question and access to a generative AI assistant; introduces everyday prompt direction without requiring specialized training.
What you can do after
Practice/perform it by adding context and output requirements, then questioning specific weaknesses and inspecting revised answers or code.
How it connects
Connects generative AI to data analytics, sentiment analysis, text mining, and model sycophancy.

Also explained in

Compare how 38 books explain prompt engineering →

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.