Instant download [EARLY RELEASE] Quick Start Guide to Large Language Models: Strategies and Best Practices for using ChatGPT and Other LLMs Sinan Ozdemir pdf all chapter
Instant download [EARLY RELEASE] Quick Start Guide to Large Language Models: Strategies and Best Practices for using ChatGPT and Other LLMs Sinan Ozdemir pdf all chapter
com
https://ebookmass.com/product/early-release-quick-start-
guide-to-large-language-models-strategies-and-best-
practices-for-using-chatgpt-and-other-llms-sinan-ozdemir/
OR CLICK HERE
DOWLOAD NOW
https://ebookmass.com/product/quick-start-guide-to-ffmpeg-v-subhash/
ebookmass.com
https://ebookmass.com/product/dragon-heat-destined-lovers-collide-in-
a-mythical-paradise-dragon-island-book-1-jodi-kendrick/
ebookmass.com
The Age of Post-Rationality: Limits of economic reasoning
in the 21st century 1st Edition Val Colic-Peisker
https://ebookmass.com/product/the-age-of-post-rationality-limits-of-
economic-reasoning-in-the-21st-century-1st-edition-val-colic-peisker/
ebookmass.com
https://ebookmass.com/product/organizing-occupy-wall-street-marisa-
holmes/
ebookmass.com
https://ebookmass.com/product/infoselves-demetra-garbasevschi/
ebookmass.com
https://ebookmass.com/product/death-society-and-human-experience/
ebookmass.com
https://ebookmass.com/product/european-welfare-state-constitutions-
after-the-financial-crisis-ulrich-becker-editor/
ebookmass.com
Everyday Enterprise Architecture: Sense-making, Strategy,
Structures, and Solutions 1st Edition Tom Graves
https://ebookmass.com/product/everyday-enterprise-architecture-sense-
making-strategy-structures-and-solutions-1st-edition-tom-graves-2/
ebookmass.com
Quick Start Guide to Large Language
Models
Strategies and Best Practices for using ChatGPT and Other LLMs
Sinan Ozdemir
Addison-Wesley
Contents at a Glance
Preface
Part I: Introduction to Large Language Models
1. Overview of Large Language Models
2. Launching an Application with Proprietary Models
3. Prompt Engineering with GPT3
4. Fine-Tuning GPT3 with Custom Examples
Part II: Getting the most out of LLMs
5. Advanced Prompt Engineering Techniques
6. Building a Recommendation Engine
7. Combining Transformers
8. Fine-Tuning Open-Source LLMs
9. Deploying Custom LLMs to the Cloud
Table of Contents
Preface
Part I: Introduction to Large Language Models
Chapter 1: Overview of Large Language Models
What Are Large Language Models (LLMs)?
Popular Modern LLMs
Domain-Specific LLMs
Applications of LLMs
Chapter 2: Launching an Application with Proprietary Models
Introduction
The Task
Solution Overview
The Components
Putting It All Together
The Cost of Closed-Source
Summary
Chapter 3: Prompt Engineering with GPT3
Introduction
Prompt Engineering
Working with Prompts Across Models
Building a Q/A bot with ChatGPT
Summary
Chapter 4: Fine-Tuning GPT3 with Custom Examples
Overview of Transfer Learning & Fine-tuning
Overview of GPT3 Fine-tuning API
Using Fine-tuned GPT3 Models to Get Better Results
Part II: Getting the most out of LLMs
Chapter 5: Advanced Prompt Engineering Techniques
Input/Output Validation
Chain of Thought Prompting
Prompt Chaining Workflows
Preventing against Prompt Injection Attacks
Building a bot that can execute code on our behalf
Chapter 6: Building a Recommendation Engine
Overview of Siamese BERT Architectures
Fine-Tuning BERT for Classifying + Tagging Items
Fine-Tuning Siamese BERT for Recommendations
Chapter 7: Combining Transformers
Overview of Vision Transformer
Building an Image Captioning System with GPT-J
Chapter 8: Fine-Tuning Open-Source LLMs
Overview of T5
Building Translation/Summarization Pipelines with T5
Chapter 9: Deploying Custom LLMs to the Cloud
Overview of Cloud Deployment
Best Practices for Cloud Deployment
Preface
This practical guide to the use of LLMs in NLP provides an overview of the
key concepts and techniques used in LLMs and explains how these models
work and how they can be used for various NLP tasks. The book also covers
advanced topics, such as fine-tuning, alignment, and information retrieval
while providing practical tips and tricks for training and optimizing LLMs for
specific NLP tasks.
This work addresses a wide range of topics in the field of Large Language
Models, including the basics of LLMs, launching an application with
proprietary models, fine-tuning GPT3 with custom examples, prompt
engineering, building a recommendation engine, combining Transformers,
and deploying custom LLMs to the cloud. It offers an in-depth look at the
various concepts, techniques, and tools used in the field of Large Language
Models.
Topics covered:
Combining Transformers
Ever since an advanced artificial intelligence (AI) deep learning model called
the Transformer was introduced by a team at Google Brain in 2017, it has
become the standard for tackling various natural language processing (NLP)
tasks in academia and industry. It is likely that you have interacted with a
Transformer model today without even realizing it, as Google uses BERT to
enhance its search engine by better understanding users’ search queries. The
GPT family of models from OpenAI have also received attention for their
ability to generate human-like text and images.
Figure 1.1 A brief history of Modern NLP highlights using deep learning to
tackle language modeling, advancements in large scale semantic token
embeddings (Word2vec), sequence to sequence models with attention
(something we will see in more depth later in this chapter), and finally the
Transformer in 2017.
Using the Copilot LLM to get an output from Facebook’s BART LLM
def classify_text(email):
"""
Use Facebook's BART model to classify an email into "spam"
Args:
email (str): The email to classify
Returns:
str: The classification of the email
"""
# COPILOT START. EVERYTHING BEFORE THIS COMMENT WAS INPUT T
classifier = pipeline(
'zero-shot-classification', model='facebook/bart-large-
labels = ['spam', 'not spam']
hypothesis_template = 'This email is {}.'
results = classifier(
email, labels, hypothesis_template=hypothesis_template)
return results['labels'][0]
# COPILOT END
In this listing, I use Copilot to take in only a Python function definition and
some comments I wrote and wrote all of the code to make the function do
what I wrote. No cherry-picking here, just a fully working python function
that I can call like this:
It appears we are surrounded by LLMs, but just what are they doing under the
hood? Let’s find out!
Large language models (LLMs) are AI models that are usually (but not
necessarily) derived from the Transformer architecture and are designed to
understand and generate human language, code, and much more. These
models are trained on vast amounts of text data, allowing them to capture the
complexities and nuances of human language. LLMs can perform a wide
range of language tasks, from simple text classification to text generation,
with high accuracy, fluency, and style.
In the healthcare industry, LLMs are being used for electronic medical record
(EMR) processing, clinical trial matching, and drug discovery. In finance,
LLMs are being utilized for fraud detection, sentiment analysis of financial
news, and even trading strategies. LLMs are also used for customer service
automation via chatbots and virtual assistants. With their versatility and
highly performant natures, Transformer-based LLMs are becoming an
increasingly valuable asset in a variety of industries and applications.
Note
Since the advent of the Transformer in 2017, the ecosystem around using and
deploying Transformers has only exploded. The aptly named “Transformers”
library and its supporting packages have made it accessible for practitioners
to use, train, and share models, greatly accelerating its adoption and being
used by thousands of organizations and counting. Popular LLM repositories
like Hugging Face have popped up, providing access to powerful open-source
models to the masses. In short, using and productionizing a Transformer has
never been easier.
My goal is to guide you on how to use, train, and optimize all kinds of LLMs
for practical applications while giving you just enough insight into the inner
workings of the model to know how to make optimal decisions about model
choice, data format, fine-tuning parameters, and so much more.
Definition of LLMs
To back up only slightly, we should talk first about the specific NLP task that
LLMs and Transformers are being used to solve and provides the foundation
layer for their ability to solve a multitude of tasks. Language modeling is a
subfield of NLP that involves the creation of statistical/deep learning models
for predicting the likelihood of a sequence of tokens in a specified
vocabulary (a limited and known set of tokens). There are generally two
kinds of language modeling tasks out there: autoencoding tasks and
autoregressive tasks Figure 1.2)
Note
To summarize, Large Language Models (LLMs) are language models that are
either autoregressive , autoencoding, or a combination of the two. Modern
LLMs are usually based on the Transformer architecture which is what we
will use but they can be based on another architecture. The defining feature of
LLMs is their large size and large training datasets which enables them to
perform complex language tasks, such as text generation and classification,
with high accuracy and with little to no fine-tuning.
Table 1.1 shows the disk size, memory usage, number of parameters, and
approximate size of the pre-training data for several popular large language
models (LLMs). Note that these sizes are approximate and may vary
depending on the specific implementation and hardware used.
But size is everything. Let’s look at some of the key characteristics of LLMs
and then dive into how LLMs learn to read and write.
An encoder which is tasked with taking in raw text, splitting them up into its
core components (more on this later), converting them into vectors (similar to
the Word2vec process), and using attention to understand the context of the
text
Figure 1.4 shows the breakdown of the key characteristics of LLMs based on
these three buckets.
No matter how the LLM is constructed and what parts of the Transformer it
is using, they all care about context (Figure 1.5). The goal is to understand
each token as it relates to the other tokens in the input text. Beginning with
the popularity of Word2vec around 2013, NLP practitioners and researchers
were always curious about the best ways of combining semantic meaning
(basically word definitions) and context (with the surrounding tokens) to
create the most meaningful token embeddings possible. The Transformer
relies on the attention calculation to make this combination a reality.
Figure 1.5 LLMs are great at understanding context. The word “Python” can
have different meanings depending on the context. We could be talking about
a snake, or a pretty cool coding language.
Choosing what kind of Transformer derivation you want isn’t enough. Just
choosing the encoder doesn’t mean your Transformer is magically good at
understanding text. Let’s take a look at how these LLMs actually learn to
read and write.
Pre-training
Every LLM on the market has been pre-trained on a large corpus of text data
and on specific language modeling related tasks. During pre-training, the
LLM tries to learn and understand general language and relationships
between words. Every LLM is trained on different corpora and on different
tasks.
BERT, for example, was originally pre-trained on two publicly available text
corpora (Figure 1.6):
• 800M words.
The Masked Language Modeling (MLM) task (AKA the autoencoding task)
—this helps BERT recognize token interactions within a single sentence.
The Next Sentence Prediction Task—this helps BERT understand how tokens
interact with each other between sentences.
Figure 1.6 BERT was originally pre-trained on English Wikipedia and the
BookCorpus. More modern LLMs are trained on datasets thousands of times
larger.
We will not revisit the idea of pre-training often in this book because it’s not
exactly the “quick” part of a “quick start guide” but it can be worth knowing
how these models were pre-trained because it’s because of this pre-training
that we can apply something called transfer learning to let us achieve the
state-of-the-art results we want, which is a big deal!
Figure 1.7 BERT was pre-trained on two tasks: the autoencoding language
modeling task (referred to as the “masked language modeling” task) to teach
it individual word embeddings and the “next sentence prediction” task to help
it learn to embed entire sequences of text.
Transfer Learning
The idea behind transfer learning is that the pre-trained model has already
learned a lot of information about the language and relationships between
words, and this information can be used as a starting point to improve
performance on a new task. Transfer learning allows LLMs to be fine-tuned
for specific tasks with much smaller amounts of task-specific data than it
would require if the model were trained from scratch. This greatly reduces
the amount of time and resources required to train LLMs. Figure 1.8 provides
a visual representation of this relationship.
Fine-tuning
Once a LLM has been pre-trained, it can be fine-tuned for specific tasks.
Fine-tuning involves training the LLM on a smaller, task-specific dataset to
adjust its parameters for the specific task at hand. This allows the LLM to
leverage its pre-trained knowledge of the language to improve its accuracy
for the specific task. Fine-tuning has been shown to drastically improve
performance on domain-specific and task-specific tasks and lets LLMs adapt
quickly to a wide variety of NLP applications.
Figure 1.8 The general transfer learning loop involves pre-training a model
on a generic dataset on some generic self-supervised task and then fine-
tuning the model on a task-specific dataset.
Figure 1.9 shows the basic fine-tuning loop that we will use for our models in
later chapters. Whether they are open-sourced or closed-sourced the loop is
more or less the same:
2. We will aggregate some training data (the format and other characteristics
depend on the model we are updating)
If some of that went over your head, not to worry: we will rely on pre-built
tools from Hugging Face’s Transformers package (Figure 1.9) and OpenAI’s
Fine-tuning API to abstract away a lot of this so we can really focus on our
data and our models.
Note
Attention
The name of the original paper that introduced the Transformer was called
“Attention is all you need”. Attention is a mechanism used in deep learning
models (not just Transformers) that assigns different weights to different
parts of the input, allowing the model to prioritize and emphasize the most
important information while performing tasks like translation or
summarization. Essentially, attention allows a model to “focus” on different
parts of the input dynamically, leading to improved performance and more
accurate results. Before the popularization of attention, most neural networks
processed all inputs equally and the models relied on a fixed representation of
the input to make predictions. Modern LLMs that rely on attention can
dynamically focus on different parts of input sequences, allowing them to
weigh the importance of each part in making predictions.
Figure 1.9 The Transformers package from Hugging Face provides a neat
and clean interface for training and fine-tuning LLMs.
Attention is attributed for being the most responsible for helping LLMs learn
(or at least recognize) internal world models and human-identifiable rules. A
Stanford study in 2019 showed that certain attention calculations in BERT
corresponded to linguistic notions of syntax and grammar rules. For example,
they noticed that BERT was able to notice direct objects of verbs,
determiners of nouns, and objects of prepositions with remarkably high
accuracy from only its pre-training. These relationships are presented visually
in Figure 1.10.
There is research that explores what other kinds of “rules” LLMs are able to
learn simply by pre-training and fine-tuning. One example is a series of
experiments led by researchers at Harvard that explored an LLM’s ability to
learn a set of rules to a synthetic task like the game of Othello (Figure 1.11).
They found evidence that an LLM was able to understand the rules of the
game simply by training on historical move data.
Figure 1.10 Research has probed into LLMs to uncover that they seem to be
recognizing grammatical rules even when they were never explicitly told
these rules.
Figure 1.11 LLMs may be able to learn all kinds of things about the world,
whether it be the rules and strategy of a game or the rules of human language.
For any LLM to learn any kind of rule, however, it has to convert what we
perceive as text into something machine readable. This is done through a
process called embedding.
Embeddings
Figure 1.12 An example of how BERT uses three layers of embedding for a
given piece of text. Once the text is tokenized, each token is given an
embedding and then the values are added up, so each token ends up with an
initial embedding before any attention is calculated. We won’t focus too
much on the individual layers of LLM embeddings in this text unless they
serve a more practical purpose but it is good to know about some of these
parts and how they look under the hood!
LLMs learn different embeddings for tokens based on their pre-training and
can further update these embeddings during fine-tuning.
Tokenization
Tokenization, as mentioned previously, involves breaking text down into the
smallest unit of understanding - tokens. These tokens are the pieces of
information that are embedded into semantic meaning and act as inputs to the
attention calculations which leads to ... well, the LLM actually learning and
working. Tokens make up an LLMs static vocabulary and don’t always
represent entire words. Tokens can represent punctuation, individual
characters, or even a sub-word if a word is not known to the LLM. Nearly all
LLMs also have special tokens that have specific meaning to the model. For
example, the BERT model has a few special tokens including the [CLS]
token which BERT automatically injects as the first token of every input and
is meant to represent an encoded semantic meaning for the entire input
sequence.
Readers may be familiar with techniques like stop words removal, stemming,
and truncation which are used in traditional NLP. These techniques are not
used nor are they necessary for LLMs. LLMs are designed to handle the
inherent complexity and variability of human language, including the usage
of stop words like “the” and “an” and variations in word forms like tenses
and misspellings. Altering the input text to an LLM using these techniques
could potentially harm the performance of the model by reducing the
contextual information and altering the original meaning of the text.
Tokenization can also involve several preprocessing steps like casing, which
refers to the capitalization of the tokens. There are two types of casing:
uncased and cased. In uncased tokenization, all the tokens are lowercased and
usually accents from letters are stripped, while in cased tokenization, the
capitalization of the tokens is preserved. The choice of casing can impact the
performance of the model, as capitalization can provide important
information about the meaning of a token. An example of this can be found in
Figure 1.13.
Note
Figure 1.13 The choice of uncased versus cased tokenization depends on the
task. Simple tasks like text classification usually prefer uncased tokenization
while tasks that derive meaning from case like Named Entity Recognition
prefer a cased tokenization.
##an - a special sub-word token that is different from the word “an” and is
used only as a means to split up unknown words
Figure 1.14 Any LLM has to deal with words they’ve never seen before.
How an LLM tokenizes text can matter if we care about the token limit of an
LLM.
Some LLMs limit the number of tokens we can input at any one time so how
an LLM tokenizes text can matter if we are trying to be mindful about this
limit.
Alignment in language models refers to how well the model can respond to
input prompts that match the user’s expectations. Standard language models
predict the next word based on the preceding context, but this can limit their
usefulness for specific instructions or prompts. Researchers are coming up
with scalable and performant ways of aligning language models to a user’s
intent. One such broad method of aligning language models is through the
incorporation of reinforcement learning (RL) into the training loop.
It happened that just then it was full high tide, when the current
changes from its usual course, and in consequence the logs floated
in favor of Hemling.
In days gone by, so the story goes, it happened that a milkmaid did
not produce as much milk and butter from her herd as usual, for
which her master took her severely to task. The girl sought
vindication by charging it upon the Vätts, who, she claimed,
possessed the place and appropriated a share of the product of the
herd. This, the master was not willing to believe, but, to satisfy
himself, went one autumn evening, after the cattle had been brought
home, to the dairy house, where he secreted himself, as he
supposed, under an upturned cheese kettle. He had not sat in his
hiding place long when a Vätt mother with her family—a large one—
came trooping in and began preparation for their meal.
The mother, who was busy at the fireplace, finally inquired if all had
spoons.
“Yes,” replied one of the Vätts. “All except him under the kettle.”
When St. Jaffen, “the Apostle of the North,” was one time riding
through Jämtland from the borders of Norway, his way led along a
beautiful green valley, in the parish of Åre. Becoming weary, he
dismounted and laid himself down for a nap. When he awoke it
occurred to him that such a garden spot must some day be inhabited
by mankind, so, selecting a slab of stone, he cut in its surface the
following prophetic lines:
Some time later he was again hunting, when he lost his way. After a
long and wearisome wandering he reached a Lapp hut, where he
found a woman stirring something in a kettle. When she had
concluded her cooking, she invited the hunter to dine, and gave him
the same knife to eat with that he had thrown at the storm.
The following day he wished to return home, but could not possibly
discover the course he should take, whereupon the Troll woman—for
his hostess was none other—directed him to get into the Lapp sled,
and attach to it a rope, in which he must tie three knots.
“Now, untie one knot at a time,” said she, “and you will soon reach
home.”
The hunter untied one knot, as instructed, and away went the rope,
dragging the sled after it into the air. After a time he untied another
knot, and his speed was increased. Finally he untied the last knot,
increasing the speed to such a rate that when the sled came to a
standstill, as it did, suddenly, not long after, he concluded his
journey, falling into his own yard with such force as to break his leg.
[219]
[Contents]
The Lapp Genesis, or the First of Mankind.
The Lapps, like other people, have their legends, and many of them
the same, or nearly so, as are found among other nations. Others
reflect more particularly the national characteristics of the Lapp folk.
Thus, for instance, there is to be found among them a tradition of a
general deluge, a universal catastrophe, whereof there still remains
a dim reminiscence in the consciences of so many other primitive
people.
Before the Lord destroyed mankind, so says the Lapp legend, there
were people in Samelads (Lappland), but when the Flood came
upon the earth every living creature perished except two, a brother
and sister, whom God conducted to a high mountain—Passevare
—“The Holy Mountain.”
When the waters had subsided and the land was again dry, the
brother and sister separated, going in opposite directions in search
of others, if any might be left. After three years’ fruitless search they
met, and, recognizing each other, they once more went into the
world, to meet again in three years, but, recognizing one another
now, also, they parted a third time. When they met at the end of
these three years neither knew the other, whereafter they lived
together, and from them came the Lapps and Swedes.
More than with anything else, the Lapp legends have to do with
giants and the adventures of mankind with them. The giant is feared
because of his great size and strength and his insatiable appetite for
human flesh. His laziness, clumsiness, and that he is inferior to the
man in intelligence are, however, often the cause of his overthrow.
There was one time a giant who made love to a rich Lapp girl.
Neither she nor her father were much inclined toward the match, but
they did not dare do otherwise than appear to consent and at the
same time thank the Giant for the high honor he would bestow upon
them. The father, nevertheless, determined that the union should not
take place, and consoled himself with the hope that when the time
arrived some means of defeating the Giant’s project would be
presented. Meantime he was obliged to set the day when the Giant
might come and claim his bride. Before the Giant’s arrival the Lapp
took a block of wood, about the size of his daughter, and clothing it in
a gown, a new cap, silver belt, shoes and shoe band, he sat it up in
a corner of the tent, with a close veil, such as is worn by Lapp brides,
over the head.
When the Giant entered the tent he was much [222]pleased to find the
bride, as he supposed, in her best attire awaiting him, and at once
asked his prospective father-in-law to go out with him and select the
reindeer that should go with the bride as her dower. Meanwhile the
daughter was concealed behind an adjacent hill with harnessed
reindeer ready for flight. When the reindeer had been counted out
the Giant proceeded to kill one of them for supper, while the Lapp
slipped off into the woods, and, joining his daughter, they fled with all
speed into the mountains.
The Giant, after dressing the reindeer, went into the tent to visit his
sweetheart.
“Now, my little darling,” said he, “put the kettle over the fire.”
“Oh, the little dear is bashful, I’ll have to do it myself then,” said he.
After the pot had been boiling awhile he again addressed the object
in the corner:
“Now my girl, you may cleave the marrow bone,” but still no
response.
“Come, now, my dear, and prepare the meat.” But the bride was as
bashful as before, and did not stir.
When he had prepared the meal he bade her come and eat, but
without effect. The bride remained motionless in her corner. [223]
“The more for me, then,” thought he, and sat himself to the repast
with a good appetite. When he had eaten, he bade his bride prepare
the bed.
“Ah, my love, are you so bashful? I must then do it myself,” said the
simple Giant.
“Go now and retire.” No, she had not yet overcome her bashfulness,
whereupon the Giant became angry and grasped the object with
great force.
Discovering how the Lapp had deceived him, and that he had only a
block of wood instead of a human of flesh and blood, he was beside
himself with rage, and started in hot pursuit after the Lapp. The latter,
however, had so much the start that the Giant could not overtake
him. At the same time it was snowing, which caused the Giant to
lose his way in the mountains. Finally he began to suffer from the
cold. The moon coming up, he thought it a fire built by the Lapp, and
at once set out on a swift run toward it, but he had already run so far
that he was completely exhausted. He then climbed to the top of a
pine, thinking thereby to get near enough to the fire to warm himself,
but he froze to death instead, and thus ends the story. [224]
[Contents]
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookmass.com