4 Analyzing text data
Text is everywhere, yet historically hard to analyze computationally; recent large language models make core text analytics—classification, information extraction, and clustering—practical with concise code. This chapter shows how to apply LLMs directly to common tasks by crafting clear prompt templates, constraining outputs for reliable downstream processing, and adding simple robustness (e.g., retries). It also outlines a minimal toolkit: OpenAI’s Python client for chat and embeddings, pandas for tabular I/O, and scikit-learn for clustering.
For classification, the chapter builds a sentiment analyzer for reviews that demonstrates a reusable pattern: generate a prompt that states the task and enforces a strict answer schema, call a chat model with retry logic, and aggregate results. It highlights why specifying the output format matters (to avoid unpredictable variations), and how parameters like temperature, max tokens, and logit bias can improve consistency. The same approach generalizes to multi-class email triage or content moderation. For information extraction, the workflow shifts to turning free text (e.g., applications or biographies) into rows with named attributes: the prompt lists target fields, embeds the source text, and requests a lightweight, delimiter-based table with placeholders for missing values. A small post-processor then slices the model’s response between markers and splits on lines and pipes to return clean rows for CSV.
For clustering, documents are first mapped to semantic vectors via an embeddings model and then grouped with K-means to discover topical structure without predefined labels. The code shows a compact embedding function with retries, a thin wrapper around scikit-learn’s KMeans, and a script that reads text, computes embeddings, assigns cluster IDs, and writes results. The chapter encourages experiments with the number of clusters and different models, and closes by noting broader embedding uses beyond clustering—such as semantic retrieval and outlier detection—making embeddings a versatile foundation for text-centric data pipelines.
For each review, we generate a prompt that contains the review, together with instructions describing the classification task. Given the prompt as input, the language model outputs a class label for the review.
For each email, we generate a prompt that contains the email and a description of the extraction task. This description references the attributes to extract, specified by the user. Given the prompt as input, the language model generates an answer text that contains extracted values for the attributes. Via post-processing, we extract those values from the raw answer text.
Clustering emails: we first calculate embedding vectors for all emails. Then, we cluster those vectors in order to assign emails with similar content to the same cluster.
Summary
- You can apply language models directly for analyzing text data.
- Often, you can use a text completion endpoint for text analysis.
- Prompts typically contain text to analyze, along with instructions.
- Instructions describe the task to solve as well as the output format.
- You can use completion for classification, extraction, and question answering.
- Raw model output may need post-processing to change the format.
- Language models can transform a text into embedding vectors.
- Create embedding vectors via the embedding endpoint.
- Comparing embedding vectors is relatively efficient.
- You can use embeddings for clustering, retrieval, and outlier detection.
Data Analysis with LLMs ebook for free