-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy path_large_language_models.qmd
More file actions
242 lines (167 loc) · 7.19 KB
/
Copy path_large_language_models.qmd
File metadata and controls
242 lines (167 loc) · 7.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
## Large Language Models (The Transformer Architecture)
This presentation was prepared by Harish Mohan.
## Introduction
A Large Language Model (LLM) is a type of machine learning model trained on large amounts of text data to understand and generate human language.
These models can:
- Predict the next word in a sequence
- Answer questions
- Summarize text
- Generate code
Modern systems such as ChatGPT are built on LLMs, which rely on a neural network architecture known as the **Transformer**.
## Why Transformers Matter
Earlier language models struggled with:
- Capturing long-range dependencies in text
- Scaling to very large datasets
- Efficient parallel computation
The Transformer architecture addresses all three issues and has become the foundation of nearly all modern LLMs.
## From Language Models to Transformers
Earlier language models were limited in how they handled sequences.
Recurrent approaches (RNNs, LSTMs, GRUs):
- Process text one token at a time
- Maintain a running internal state
- Struggle with long-range dependencies
- Are difficult to parallelize
These limitations motivated a different approach.
## Attention
Transformers use a mechanism called **Attention** to function.
- Look at all words in a sequence at once
- Assign importance to each word
- Focus on the most relevant information
Specific kind of attention: **Self Attention**
- Each word interacts directly with every other word
- Capture relationships regardless of distance
- Resolve ambiguity using context
- Build richer representations of text
## The Transformer Architecture
Transformers are built entirely from attention and simple neural networks.
Core structure:
- **Encoder:** processes the input sequence
- **Decoder:** generates the output sequence
- Input → embeddings + positional information
- Repeated layers of:
- Self-attention
- Feed-forward networks
There is no recurrence.
## How Attention is Computed
\[
\text{Attention}(Q,K,V) =
\text{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V
\]
Each word produces:
- A **query** (what it is looking for)
- A **key** (what it represents)
- A **value** (information it contributes)
The model uses similarity between queries and keys to combine information.
## Input Representation
Before entering the model, tokens are converted into vectors called **embeddings**.
Since Transformers do not inherently encode order, **positional encoding** is added to these embeddings.
Together, this allows the model to:
- Represent word meaning (embeddings)
- Capture word order (positional encoding)
At the output stage:
- A linear layer maps representations to vocabulary size
- A softmax converts them into probabilities
## Scaled Dot-Product Attention
\[
\text{Attention}(Q,K,V) =
\text{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V
\]
- Queries represent what is being searched for
- Keys represent available information
- Values carry the information forward
## Multi-Head Attention
Instead of computing attention once, the model computes it multiple times in parallel.
Each head learns different types of relationships, such as:
- Grammar
- Meaning
- Long-distance dependencies
These are combined into a single representation.
## Feed-Forward Networks
\[
\text{FFN}(x) = \max(0, xW_1 + b_1)W_2 + b_2
\]
This component:
- Processes each position independently
- Adds non-linearity
- Refines learned representations
## Why Transformers Work Well
Transformers provide:
- Parallel computation across tokens
- Efficient modeling of long sequences
- Direct interaction between all words
These properties allow them to scale effectively to large datasets.
## Demo: Setup
This section demonstrates how to interact with a Transformer-based model, particularly the OpenAI API, in practice.
- First, we will install the OpenAI Python package using pip
- Then, we will import the OpenAI client to make API calls.
```
from openai import OpenAI
```
- Now, we create a client object that allows us to interact with the API
- Note: This requires an API key, which should be stored as an environment variable (e.g., OPENAI_API_KEY) so that OpenAI() can access it automatically.
```
client = OpenAI()
```
- To interact with a language model, we provide a text input (called a prompt)
- We can store this prompt in a variable
```
prompt = "INSERT PROMPT HERE"
```
## Making an API Call
- We now send the prompt to the model using the .responses.create() function
- This function requires a model name, and the input prompt
```
response = client.responses.create(
model="gpt-5.4",
input=prompt
)
```
- The model’s response is stored in the variable response
- To extract the generated text, we access the output field
```
output_text = response.output[0].content[0].text
print(output_text)
```
- Essentially, internally, the model processes the input prompt through the layers we mentioned earlier
- Different models (like gpt-3.5, gpt-4, gpt-5.4) have different architectures and capabilities, which can affect the quality and relevance of the generated output.
## System Prompts
- System prompts are special instructions that guide the model’s behavior
- They can be used to set the tone, style, or constraints for the generated response
- Each message has a role (e.g., "system", "user") and content
- The system role defines how the model should behave, the user role contains the actual prompt, and the assistant role contains the model’s response
```
response = client.responses.create(
model="gpt-5.4",
input=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "SAMPLE PROMPT"}
]
)
print(response.output[0].content[0].text)
```
- By using system prompts, we can influence the style and content of the model’s output, making it more aligned with our needs.
- They allow us to control the tone, level of detail, and style of the response.
```
response = client.responses.create(
model="gpt-5.4",
input=[
{"role": "system", "content": "Explain everything like I am 10 years old."},
{"role": "user", "content": "What is a transformer model?"}
]
)
print(response.output[0].content[0].text)
```
## Key Takeaways
- Large Language Models (LLMs) are built on the Transformer architecture
- Core components such as embeddings, positional encoding, and attention layers work together to produce meaningful language representations
- In practice, these models are commonly accessed through APIs, where:
- A prompt is provided as input
- The model processes it using the Transformer architecture
- A generated response is returned
- Techniques such as prompt design and system prompts allow users to control the behavior and quality of the output
- Overall, Transformers provide both the theoretical foundation and the practical tools that make modern AI systems usable in real-world applications
## Further Reading/Works Cited
- Attention Is All You Need (https://arxiv.org/abs/1706.03762) (Vaswani et al., 2017)
- Neural Networks and Deep Learning (http://neuralnetworksanddeeplearning.com/) (Michael Nielsen)
- The Illustrated Transformer (https://jalammar.github.io/illustrated-transformer/) (Jay Alammar)
- OpenAI API Documentation (https://platform.openai.com/docs)