Contents....5
About the Author....14
About the Technical Reviewer....15
Acknowledgments....16
Introduction....17
Chapter 1: Evolution and Significance of Large Language Models....19
The Evolutionary Steps of Large Language Models....20
Markov, Shannon, and the Language Models....21
Chomsky and the Language Models....23
Rule-Based Language Models....24
The First Chatbot: ELIZA....24
Statistical Language Processing....26
N-grams....26
Bag-of-Words (BOW)....27
TF-IDF (Term Frequency-Inverse Document Frequency)....28
Vector Space Models and State Space Models....30
Neural Language Models – The Rise of LLMs....31
Recurrent Neural Networks....32
Long Short-Term Memory....34
Gated Recurrent Units....36
Sequence-to-Sequence Language Models....36
Attention-Based Language Models....38
Attention Mechanism....38
The Transformer Architecture....39
Large Language Models (LLMs)....40
The Era of Multimodal Learning....40
Multimodal Learning Challenges....41
Fusion....42
Alignment....43
Translation....43
Co-learning....43
How Multimodal Learning Operates....44
Applications of Multimodal Deep Learning....44
Image Captioning....45
Image Retrieval....45
Text-to-Image Generation....45
Emotion Recognition....45
Understanding the NLP Basics....46
What Exactly Is Natural Language Processing?....46
How Does NLP Function?....47
Elements of NLP....47
Syntax....48
Semantics....48
Pragmatics....48
Discourse....48
NLP Tasks....49
Text Preprocessing and Feature Engineering....50
Tokenization....50
Parsing....51
Lemmatization....52
Word Segmentation....52
Word Sense Disambiguation....52
Sentence Boundary Detection....53
Morphological Segmentation....53
Stemming....53
Named Entity Recognition (NER)....54
How Entities Are Recognized....54
NLP and Feature Engineering....55
Python and the Natural Language Toolkit (NLTK)....56
Tokenization....56
Stop Word Removal....56
Stemming....57
Lemmatization....57
N-grams for NLP Feature Engineering....58
Part-of-Speech (POS) Tagging....58
Named Entity Recognition (NER)....59
TF-IDF....59
Word Embeddings and Semantic Understanding....61
Word Embedding....61
Benefits....62
Limitations....63
Semantic Understanding....63
The Importance of Semantic Analysis in NLP....64
Semantic Analysis Within a Semantic System....65
Sentiment Analysis and Text Classification with Python....65
Sentiment Analysis....65
Text Classification....67
Advantages of Text Classification....67
Varieties of Text Classification....68
Mechanics of Text Classification....68
Challenges in Text Classification....68
Applications of Text Classification....68
Exploring Approaches in Natural Language Processing (NLP)....70
Supervised NLP....70
Unsupervised NLP....70
Natural Language Understanding....70
Natural Language Generation....71
Statistical NLP, Machine Learning, and Deep Learning....71
NLP Challenges....71
Delineating NLP Basics from LLM Capabilities....72
Contrasting Traditional NLP Techniques with LLMs....73
Summary....75
Chapter 2: What Are Large Language Models?....76
LM’s Development Stages....76
How Do Large Language Models Work?....79
Overall Architecture of Large Language Models....80
In-Depth Architecture of the LLMs....81
Tokenization....82
Attention....82
Attention Mechanisms in LLMs....83
Positional Encoding....84
Activation Functions....85
ReLU....85
GELU....85
GLU Variants....85
Layer Normalization....86
LayerNorm....86
RMSNorm....86
Pre-norm and Post-norm....87
DeepNorm....87
Distributed Training of Large Language Models (LLMs)....87
Data Preprocessing....88
Architectures....89
Encoder-Decoder....90
Causal Decoder....90
Prefix Decoder....90
Pre-training Objectives....90
Model Adaptation....91
Pre-training....91
Alignment Verification and Utilization....92
Prompting/Utilization....93
Training of LLMs....94
Benefits and Challenges of LLMs in Various Domains....95
General Purpose....95
Medical Applications....96
Healthcare Communication and Management....97
Enhanced Natural Language Processing....97
Education....98
Content Creation and Augmentation....99
Language Translation and Localization....99
Research and Data Analysis....99
Finance....99
Creative Arts....100
Ethical and Responsible Use....100
Legal and Compliance Assistance....100
Financial Analysis and Forecasting....101
Disaster Response and Management....101
Personalized Marketing and Customer Insights....101
Gaming and Interactive Entertainment....101
Accessibility Enhancements....102
Environmental Monitoring and Sustainability....102
LLMs and Engineering Applications....102
Chatbots....103
LLM Agents....104
LLM Limitations....104
Bias....105
Hallucinations....106
Vulnerability to Various Types of Cyber Attacks....107
Beyond the Hype of the LLMs – Why Are They So Popular?....107
Common Benefits – The Real Reason Why LLMs Are So Popular....108
Large Language Models for Business....112
Creation of Digital Content....112
Enhancing Search Engine Optimization (SEO)....112
Content Moderation....113
Emotion/Sentiment Analysis....113
Client Services....114
Language Translation....114
Virtual Teamwork....115
Recruitment and HR Support....115
Sales Enhancement....116
Fraud Identification....116
Summary....117
Chapter 3: Python for LLMs....118
Python at a Glance....118
Python Syntax and Semantics....119
Syntax Design Principles....120
Zen of Python....120
Python Identifiers....121
Python Indentation....122
Python Multiline Statements....123
Quotations in Python....123
Comments in Python....124
Utilizing Blank Lines in Python Code....125
Combining Multiple Statements in a Single Line....125
How to Install Python and Your First Python Program....125
Installing Python on Windows....126
Install Python on macOS....129
Installing Python on Linux – Ubuntu/Debian and Fedora....130
Your First Python Program....131
Variables and Data Types, Numbers, Strings, and Casting....131
Naming a Variable....132
Data Types....133
Numbers in Python....133
Integers....133
Floats....134
Complex....134
Strings....134
String Delimiters and Characteristics....135
Handling Special Characters in Strings....136
RAW Strings....137
Triple-Quoted Strings in Python....137
Booleans and Operators....138
Booleans....138
Converting Integers and Floats into Booleans....139
Boolean Operators....139
Python Operators....140
Arithmetic Operators....141
Comparison Operators....142
Logical Operators....143
Bitwise Operators....144
Assignment Operators....145
Identity Operators....147
Membership Operators....148
Ternary Operator....148
Conditionals and Loops....149
Conditionals....149
Grouping Statements....150
Nested Blocks....151
Else and Elif Clauses....152
One-Line if Statements....153
Python Loops (For and While)....153
While Loop in Python....154
Else Statement with while Loop....155
Creating an Infinite Loop with Python while Loop....155
For Loops in Python....156
Else Statement with for Loop....157
Nested Loops in Python....157
Loop Control Statements....159
Python Data Structures: Lists, Sets, Tuples, Dictionaries....159
What Is a Data Structure?....160
Python’s Built-In Data Structures....160
Custom Data Structures in Python....160
Built-In Data Structures....161
Lists....161
Creating Lists....161
Adding Elements....161
Deleting Elements....162
Accessing Elements....163
Additional List Operations....164
Dictionaries in Python....165
Creating a Dictionary....165
Modifying and Adding Key-Value Pairs....165
Removing Key-Value Pairs....166
Accessing Elements....167
Other Functions....168
Tuples in Python....169
Creating a Tuple....169
Accessing Elements....170
Appending Elements....170
Other Functions....171
Sets in Python....172
Creating a Set....172
Adding Elements....172
Operations on Sets....173
Regular Functions and Lambda Functions....175
What Is a Function in Python?....175
The return Statement....176
Return or Print in a Function....177
Methods vs. Functions....177
How to Call a Function in Python....177
Function Arguments in Python....178
Positional Arguments....178
Keyword Arguments....179
Default Arguments....179
Variable-Length Arguments (*args and **kwargs)....179
Anonymous Functions in Python....180
Summary....181
Chapter 4: Python and Other Programming Approaches....182
Object-Oriented Programming in Python....182
Why Do We Use Object-Oriented Programming in Python?....183
Everything Is an Object in Python....184
Attributes and Methods....184
Your First Python Object....185
Creating and Using a “Book” Object....185
Object-Oriented Programming (OOP) in Python Is Founded on Four Fundamental Concepts....186
Abstraction....186
Inheritance....187
Polymorphism....188
Encapsulation....190
Modules and File Handling....191
Python Modules....191
Understanding Python Modules....191
Creating a Python Module....191
Importing Modules in Python....192
Python Import Using “from” Statement....193
Importing Specific Attributes from a Python Module....193
Importing All Names....194
Locating Python Modules....194
Renaming Python Modules....195
Python Built-In Modules....195
Python File Handling....197
Python File Opening....197
Working in Read Mode....199
Creating a File Using the “write()” Function....202
Working in Append Mode....202
The Powerful Features of Python 3.11....203
TypedDicts....203
TypedDict or Just a Dict?....203
Required[ ] and NotRequired[ ]....204
Self Type....205
With Self Type....206
Improved Exceptions....206
Better Error Messages....206
Exception Notes....207
Another Way to Add Exception Notes: Define It As an Attribute to a Custom-Defined Exception Class....207
Exception Groups....207
TOML Support....208
Improved Type Variables....210
Arbitrary Literal String Type....210
Variadic Generics....211
Negative Zero Formatting....211
Understanding the Role of Python 3.11 in AI and NLP – Why Python?....212
Why Python for AI?....212
Why Python for NLP?....213
Summary....214
Chapter 5: Basic Overview of the Components of the LLM Architectures....215
Embedding Layers....216
Stage 1: Nodes....218
Stage 2: Returning to the Words....219
Stage 3: Implementing the Softmax Layer....220
Feedforward Layers....221
What Is a Feedforward Neural Network?....221
Feedforward Phase....222
Backpropagation Phase....222
LLMs and Feedforward Layers....222
Recurrent Layers....224
Here’s a Closer Look at How Recurrent Layers Function Within LLMs....224
Sequential Data Processing....224
Hidden States....224
Backpropagation Through Time....224
Challenges and Solutions....225
Attention Mechanisms....225
Self-attention (Intra-attention)....226
Multi-head Attention....226
Cross-Attention (Encoder-Decoder Attention)....227
Masked Attention....227
Sparse Attention....227
Global/Local Attention....227
Understanding Tokens and Token Distributions and Predicting the Next Token....228
Understanding Tokenization in the Context of Large Language Models....228
The Advantages of Tokenization for LLMs....229
Limitations and Challenges....229
Challenges in Current Tokenization Techniques....229
Case Sensitivity in Tokenization....230
Numeric Data Handling....230
Inconsistencies with Trailing Whitespace....230
Model-Specific Tokenization Practices....230
Grasping Contextual Nuances....230
Navigating Ambiguity....231
Interpreting Idioms....231
Handling Special Symbols and Characters....231
Tokenization Strategies in Large Language Models....231
What Is Token Distribution?....232
Predicting the Next Token2....234
Zero-Shot and Few-Shot Learning....238
Few-Shot Learning....238
The Significance of Few-Shot Learning....238
Real-World Applications of Few-Shot Learning....238
Zero-Shot Learning....239
Significance and Use Cases of Zero-Shot Learning....240
Navigating Limited Data Learning: Few-Shot, One-Shot, and Zero-Shot Learning Explained....241
Examples....242
Few-Shot Learning....242
One-Shot Learning....242
Zero-Shot Learning....243
LLM Hallucinations....243
Classification of Hallucinations in Large Language Models (LLMs)....244
Factuality Hallucinations....244
Faithfulness Hallucinations....244
Implications of AI Hallucination....245
Mitigating the Risks of AI Hallucinations: Strategies for Prevention....246
Ensure High-Quality Training Data....247
Clarify the Model’s Purpose and Constraints....247
Implement Data Templates....247
Restrict Possible Outcomes....247
Continuous Testing and Refinement....247
Incorporate Human Oversight....248
When Hallucinations Might Be Good?....248
Future Implications....249
Examples of LLM Architectures....250
GPT-4....251
GPT-4 Limitations....252
Key Takeaways....255
BERT....256
Introduction to BERT....256
The Bidirectional Nature of BERT....256
Training Stages of BERT: Pre-training and Fine-Tuning....257
Phase 1: Pre-training with Unlabeled Data....257
Phase 2: Fine-Tuning for Specific Tasks....257
How BERT Functions....257
BERT’s Architectural Innovations....258
From Training to Application....259
Uses of BERT in Language Processing....259
T5....261
Cohere....262
PaLM 2....263
How PaLM 2 Operates....264
Initial Data Acquisition and Preparation....264
Leveraging Transformer Architecture....264
Extensive Pre-training....264
Task-Specific Fine-Tuning....265
The Novel Pathways Architecture....265
Independent Pathway Functioning....265
Adaptive Computational Allocation....265
Pathway Interaction and Collaboration....265
Selective Pathway Engagement....265
Generating Outputs....266
Jurassic-2....266
Claude v1....267
Data and Training Approach....267
Model Design....267
Claude v1’s Limitations....268
Falcon 40B....268
Model Design....268
Data for Training....269
Training Process....269
Multi-query Attention Mechanism....269
Instruct Versions for Enhanced Performance....270
Accessibility for Users....270
LLaMA....270
LaMDA....271
Guanaco-65B....273
Orca....274
StableLM....274
Palmyra....275
GPT4ALL....276
Summary....277
Chapter 6: Applications of LLMs in Python....279
Text Generation and Creative Writing....279
The Mechanism Behind Text Generation....279
The Significance of Text Generation....280
Key Use Cases of Text Generation....280
What Is Creative Writing....281
Utilizing LLMs for Creative Writing Endeavors....281
1. Conceptualization and Brainstorming....281
2. Composition and Refinement....281
3. Dialogue Crafting and Characterization....282
4. World Building and Scene Setting....282
5. Poetry and Experimental Literature....282
Blog Post Generator on a Topic and Length Provided by the User Based on OpenAI....282
Language Translation and Multilingual LLMs....284
Advantages of Utilizing LLMs for Translation....284
How LLMs Translate Languages?....285
Challenges Associated with LLMs in Translation....285
The Potential Impacts of LLMs on the Translation and Localization Industry....286
Enhanced Efficiency....286
Elevated Quality....286
Pioneering Opportunities....286
Translation App Based on the Google T5 Model....286
Text Summarization and Document Understanding....289
Article Summarization Application Using User-Provided URL....290
Question-Answering Systems: Knowledge at Your Fingertips....295
Enhancing Question-Answering Capabilities Through Large Language Models (LLMs)....295
Utilizing Large Language Models for Advanced Document Analysis....295
The Journey from Data to Response: A Comprehensive Overview....296
Document Parsing and Preparation....296
Text Embedding and Indexing....296
Query Processing and Context Retrieval....296
Answer Generation....296
Practical Applications and Use Cases of Generative Question Answering....297
Enhanced Customer Support Through Automated Responses....297
Efficient Search in Reports and Unstructured Documents....297
Knowledge Management for Large Organizations....297
Question Answering Chatbot over Documents with Sources....298
Crawling the Articles Provided by the User....299
Initiating the Chain Setup....302
Full Code of the App....303
Chatbots and Virtual Assistants....306
What Is the Concept Behind Chatbots?....306
Practical Applications of LLM-Trained Chatbots....306
Guide to Building a Chatbot with LLMs....307
Model Selection....308
Data Preprocessing and Cleansing....308
Fine-Tuning the Model....308
Integration and Deployment....308
Best Practices and Considerations....308
Customer Support Question Answering Chatbot....309
Step 1: Document Segmentation and Embedding Calculation....310
Step 2: Formulate a Prompt for GPT-3 Utilizing Recommended Techniques....311
Step 3: Employ the GPT-3 Model with a Temperature of 0 for Text Generation....311
Basic Prompting – The Common Thing Between All Applications Presented....315
Understanding Prompting....315
Fundamental Prompting Techniques....315
Prompt Template Examples....316
Summary....317
Chapter 7: Harnessing Python 3.11 and Python Libraries for LLM Development....318
LangChain....318
LangChain Features....319
What Are the Integrations of LangChain?....320
How to Build Applications in LangChain?....320
Use Cases of LangChain....321
Example of a LangChain App – Article Summarizer....322
Hugging Face....324
History of Hugging Face....325
Key Components of Hugging Face....325
Transformers Library....326
Hugging Face Hub....326
Model Hub....327
Tokenizers....328
Datasets Library....328
OpenAI API....331
Features of the OpenAI API....332
Pre-trained Models....332
Customization Through Fine-Tuning....332
User-Friendly API Interface....333
Scalable Infrastructure....333
Industry Applications of the OpenAI API....333
Simple Example of a Connection to the OpenAI API....335
Cohere....337
Cohere Models....338
Command....339
Embed....339
Rerank....339
Example App for Sentiment Analysis....339
Pinecone....342
How Vector Databases Operate....342
What Exactly Is a Vector Database?....343
Pinecone’s Features....343
Practical Applications....344
Lamini.ai....347
Lamini’s Operational Mechanics....347
Lamini’s Features, Functionalities, and Advantages....347
Applications and Use Cases for Lamini....348
Data Collection, Cleaning, and Preparation of Python Libraries....352
Gathering and Preparing Data for Large Language Models....352
Data Acquisition....353
What Is Data Preprocessing?....353
Preparing Datasets for Training....354
Managing Unwanted Data....354
Handling Document Length....358
Text Produced by Machines....359
Removing Duplicate Content....359
Data Decontamination....360
Addressing Toxicity and Bias....362
Protecting Personally Identifiable Information (PII)....365
Managing Missing Data....365
Enhancing Datasets Through Augmentation....368
Data Normalization....368
Data Parsing....371
Tokenization....373
Stemming and Lemmatization....374
Feature Engineering for Large Language Models....377
Word Embeddings....377
Contextual Embeddings....377
Subword Embeddings....378
Best Practices for Data Processing....379
Implementing Strong Data Cleansing Protocols....380
Proactive Bias Management....380
Implementing Continuous Quality Control and Feedback Mechanisms....380
Fostering Interdisciplinary Collaboration....380
Prioritizing Educational Growth and Skill Development....381
Delving into Key Libraries....381
Summary....383
Index....384
df-Capture.PNG....-1
df-Capture - Copy.PNG....1
Gain a solid foundation for Natural Language Processing (NLP) and Large Language Models (LLMs), emphasizing their significance in today's computational world. This book is an introductory guide to NLP and LLMs with Python programming.
The book starts with the basics of NLP and LLMs. It covers essential NLP concepts, such as text preprocessing, feature engineering, and sentiment analysis using Python. The book offers insights into Python programming, covering syntax, data types, conditionals, loops, functions, and object-oriented programming. Next, it delves deeper into LLMs, unraveling their complex components.
You'll learn about LLM elements, including embedding layers, feedforward layers, recurrent layers, and attention mechanisms. You'll also explore important topics like tokens, token distributions, zero-shot learning, LLM hallucinations, and insights into popular LLM architectures such as GPT-4, BERT, T5, PALM, and others. Additionally, it covers Python libraries like Hugging Face, OpenAI API, and Cohere. The final chapter bridges theory with practical application, offering step-by-step examples of coded applications for tasks like text generation, summarization, language translation, question-answering systems, and chatbots.
In the end, this book will equip you with the knowledge and tools to navigate the dynamic landscape of NLP and LLMs.
Data analysts, AI and Machine Learning Experts, Python developers, and Software Development Professionals interested in learning the foundations of NLP, LLMs, and the processes of building modern LLM applications for various tasks.