liveBook · Manning

Language model

As explained in Learn Generative AI with PyTorch

A large language model trained beforehand on broad textual data and reused for downstream tasks.

A language model learns patterns in sequences of text. A pretrained large language model (LLM) has already learned from a large textual dataset, so it can be reused for tasks such as text generation, sentiment analysis, question answering, and named entity recognition. Using one avoids the resources needed to train a large model yourself. section 1.3.3 section 16.0

The distinction is between building a language model and applying a pretrained one. A smaller LSTM language model can be trained for word-level text generation; a GPT-style Transformer can also be assembled from decoder blocks. Pretrained LLMs instead provide capabilities that an application can call without repeating that training. Transformers use attention to capture relationships across a sequence and, unlike sequential RNNs, support parallel training on large datasets. section 8.5.2 section 1.3 section 16.0

Text must first become tokens and numerical representations. In a GPT-style model, token indexes pass through token and position embeddings, then decoder blocks with causal self-attention and feed-forward layers. A final head produces a logit for each vocabulary token; softmax turns those logits into probabilities for generating text. One small PyTorch implementation uses three decoder blocks and a 10,600-token vocabulary. It illustrates the mechanism, not the scale of a pretrained LLM. section 8.2 section 12.3.3

Answering a recent question

A GPT-4 query about the Best Actor award at the 2024 Academy Awards returned a response saying it could not provide the information. The problem is practical: training data does not give a model reliable access to recent events or live facts. A LangChain agent can instead choose a tool, using Wikipedia for recent facts or Wolfram Alpha for real-time information and computations. section 16.2.1 section 1.3.3

completion = client.chat.completions.create(
 model="gpt-4",
 messages=[
   {"role": "system", "content": 
    '''You are a helpful assistant, knowledgeable about recent facts.'''},
   {"role": "user", "content": 
    '''Who won the Best Actor Award in 2024 Academy Awards?'''}
 ]
)
print(completion.choices[0].message.content)

The GPT-4 completion call shows the system instruction and recent-event question used to test the model before adding external tools. section 16.2.1

Use the model for its learned language capabilities, but connect it to an appropriate external source when an answer depends on current information. section 1.3.3 section 16.2.1

How Learn Generative AI with PyTorch covers it

How deep it goes
Explains a small GPT-style model’s token-to-logit path and demonstrates pretrained-model use, without training a pretrained LLM at scale.
Examples
Uses a small PyTorch GPT model and a GPT-4 question about the 2024 Academy Awards.
What it assumes
Assumes foundational machine learning and neural-network knowledge while introducing tokenization and numerical text representations.
What you can do after
Apply it via LangChain to combine an LLM with Wikipedia and Wolfram Alpha for factual or real-time queries.
How it connects
Connects language models to tokenization, embeddings, Transformers, self-attention, prompting, and tool-using agents.

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 Learn Generative AI with PyTorch. The section links point to the parts of the book it draws on.