liveBook · Manning

Language model

As explained in Starting Data Analytics with Generative AI and Python

A machine-learning model trained on large text collections to understand and generate natural language.

A language model works with text: it can interpret a request and generate a response. A large language model (LLM) is trained on vast amounts of text and uses deep learning to predict likely sequences of words. That makes it useful for tasks such as question answering, summarization, translation, and sentiment analysis. Its fluent answer is still an output to check, not a fact to accept automatically. section 6.2 section 1.2.2

In an analytics workflow, an LLM can help plan an analysis, explain or generate Python code, and put findings into words. Think of it as a capable new teammate: useful for moving work forward, but not someone to whom you hand the data and unquestioningly forward the reply. Code and conclusions need inspection, execution checks, and comparison with the underlying data. section 1.2.2 section 8.0

Reading a customer review

Suppose a review says, “back to the drawing board.” A keyword search may miss its disappointed tone because it contains no explicit negative word. A pretrained language model can consider the expression’s meaning in context when judging sentiment. Check its classification against the review itself, particularly when a business decision depends on it. section 6.2 section 5.0

A practical way to use a language model is to:

  • State the business question and supply the relevant context, rather than expecting the model to know your data or earlier requests. section 1.3.3 section 4.1
  • Ask for an explanation, code, or an initial text interpretation; run generated code and verify consequential outputs. section 1.2.2
  • Choose another tool when the task calls for reliably reading tiny differences in charts or for the consistency and scale of a dedicated text-processing library. section 4.1 section 5.1.3
Listing 1.1 Interacting with ChatGPT through the API
from openai import OpenAI
prompt_text = """Generate a list of 20 keywords indicating positive 
sentiment to be used for searching customer reviews in 
Portuguese."""
client = OpenAI(
    api_key=<<your-API-key-here>>,
)
chat_completion = client.chat.completions.create(
    messages=[
    {"role": "system", "content": "You are a scientific assistant, skilled
    at explaining science to schoolchildren."},
    {"role": "user", "content": "Explain resonance to first-graders?"},
    {"role": "assistant", "content": "Alright, kids, today we're going to
    talk about something really cool called resonance! (...)."},
    # Shortened from the full model response
    {"role": "user", "content": "Where can we use it?"}
  ],


    model="gpt-4-0125-preview",
    temperature=0.7,   
)
print(chat_completion.choices[0].message.content)

This Python API call shows how a model receives a system instruction, conversation history, a selected model, and a temperature setting. Inspect what is actually sent: the separate Portuguese-review keyword prompt is assigned to a variable but never included in the messages, which instead ask about resonance. section 1.3.3

An API call does not automatically remember previous requests; include needed history in the messages you send. Temperature can make responses more varied but raises hallucination risk when increased. A downloadable model can also run locally with appropriate hardware and libraries, though a simple single-prompt setup has no conversation memory. These choices change the workflow, not the need to validate results. section 1.3.3 section 1.3.5

How Starting Data Analytics with Generative AI and Python covers it

How deep it goes
Defines an LLM and demonstrates access workflows, while leaving model architecture and technical internals aside.
Examples
Uses customer-review idioms, a Python chat API call, and a locally invoked Llama model as concrete cases.
What it assumes
Starts with a plain-language LLM definition; assumes some Python and command-line familiarity for the API and local workflows.
What you can do after
Apply it via a chat API or local model to assist analytics, then check generated code and interpretations against the task.
How it connects
Connects language models to sentiment analysis, text summarization, code generation, APIs, and dedicated text-mining libraries.

Also explained in

Compare how 67 books explain language model →

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.