100% found this document useful (1 vote)
87 views55 pages

Download Programming Large Language Models With Azure Open Ai: Conversational Programming and Prompt Engineering With Llms (Developer Reference) 1st Edition Esposito ebook All Chapters PDF

Conversational

Uploaded by

periokiele4f
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
100% found this document useful (1 vote)
87 views55 pages

Download Programming Large Language Models With Azure Open Ai: Conversational Programming and Prompt Engineering With Llms (Developer Reference) 1st Edition Esposito ebook All Chapters PDF

Conversational

Uploaded by

periokiele4f
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 55

Download the Full Version of textbook for Fast Typing at textbookfull.

com

Programming Large Language Models With Azure Open


Ai: Conversational Programming and Prompt
Engineering With Llms (Developer Reference) 1st
Edition Esposito
https://textbookfull.com/product/programming-large-language-
models-with-azure-open-ai-conversational-programming-and-
prompt-engineering-with-llms-developer-reference-1st-
edition-esposito/

OR CLICK BUTTON

DOWNLOAD NOW

Download More textbook Instantly Today - Get Yours Now at textbookfull.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

Transforming Conversational AI: Exploring the Power of


Large Language Models in Interactive Conversational Agents
1st Edition Michael Mctear
https://textbookfull.com/product/transforming-conversational-ai-
exploring-the-power-of-large-language-models-in-interactive-
conversational-agents-1st-edition-michael-mctear/
textboxfull.com

Clean Architecture With net Developer Reference 1st


Edition Esposito Dino

https://textbookfull.com/product/clean-architecture-with-net-
developer-reference-1st-edition-esposito-dino/

textboxfull.com

Beginning Game AI with Unity: Programming Artificial


Intelligence with C# Sebastiano M. Cossu

https://textbookfull.com/product/beginning-game-ai-with-unity-
programming-artificial-intelligence-with-c-sebastiano-m-cossu/

textboxfull.com

Learn AI-Assisted Python Programming with GitHub Copilot


and ChatGPT 1st Edition Leo Porter

https://textbookfull.com/product/learn-ai-assisted-python-programming-
with-github-copilot-and-chatgpt-1st-edition-leo-porter/

textboxfull.com
Programming with MicroPython Embedded Programming with
Microcontrollers and Python 1st Edition Nicholas H.
Tollervey
https://textbookfull.com/product/programming-with-micropython-
embedded-programming-with-microcontrollers-and-python-1st-edition-
nicholas-h-tollervey/
textboxfull.com

Programming with MicroPython embedded programming with


Microcontrollers and Python First Edition Nicholas H.
Tollervey
https://textbookfull.com/product/programming-with-micropython-
embedded-programming-with-microcontrollers-and-python-first-edition-
nicholas-h-tollervey/
textboxfull.com

Programming Microsoft ASP NET MVC Dino Esposito

https://textbookfull.com/product/programming-microsoft-asp-net-mvc-
dino-esposito/

textboxfull.com

Open Heritage Data: An introduction to research,


publishing and programming with open data in the heritage
sector Henriette Roued-Cunliffe
https://textbookfull.com/product/open-heritage-data-an-introduction-
to-research-publishing-and-programming-with-open-data-in-the-heritage-
sector-henriette-roued-cunliffe/
textboxfull.com

Hands On Start to Wolfram Mathematica And Programming with


the Wolfram Language Cliff Hastings

https://textbookfull.com/product/hands-on-start-to-wolfram-
mathematica-and-programming-with-the-wolfram-language-cliff-hastings/

textboxfull.com
Programming Large Language
Models with Azure Open AI:
Conversational programming and
prompt engineering with LLMs

Francesco Esposito
Programming Large Language Models with Azure Open AI:
Conversational programming and prompt engineering with
LLMs
Published with the authorization of Microsoft Corporation by: Pearson
Education, Inc.

Copyright © 2024 by Francesco Esposito.


All rights reserved. This publication is protected by copyright, and
permission must be obtained from the publisher prior to any prohibited
reproduction, storage in a retrieval system, or transmission in any form or by
any means, electronic, mechanical, photocopying, recording, or likewise. For
information regarding permissions, request forms, and the appropriate
contacts within the Pearson Education Global Rights & Permissions
Department, please visit www.pearson.com/permissions.
No patent liability is assumed with respect to the use of the information
contained herein. Although every precaution has been taken in the
preparation of this book, the publisher and author assume no responsibility
for errors or omissions. Nor is any liability assumed for damages resulting
from the use of the information contained herein.
ISBN-13: 978-0-13-828037-6
ISBN-10: 0-13-828037-1
Library of Congress Control Number: 2024931423
$PrintCode

Trademarks
Microsoft and the trademarks listed at http://www.microsoft.com on the
“Trademarks” webpage are trademarks of the Microsoft group of companies.
All other marks are property of their respective owners.

Warning and Disclaimer


Every effort has been made to make this book as complete and as accurate as
possible, but no warranty or fitness is implied. The information provided is
on an “as is” basis. The author, the publisher, and Microsoft Corporation
shall have neither liability nor responsibility to any person or entity with
respect to any loss or damages arising from the information contained in this
book or from the use of the programs accompanying it.

Special Sales
For information about buying this title in bulk quantities, or for special sales
opportunities (which may include electronic versions; custom cover designs;
and content particular to your business, training goals, marketing focus, or
branding interests), please contact our corporate sales department at
corpsales@pearsoned.com or (800) 382-3419.
For government sales inquiries, please contact
governmentsales@pearsoned.com.
For questions about sales outside the U.S., please contact
intlcs@pearson.com.

Editor-in-Chief
Brett Bartow

Executive Editor
Loretta Yates

Associate Editor
Shourav Bose

Development Editor
Kate Shoup

Managing Editor
Sandra Schroeder

Senior Project Editor


Tracey Croom

Copy Editor
Dan Foster

Indexer
Timothy Wright

Proofreader
Donna E. Mulder

Technical Editor
Dino Esposito

Editorial Assistant
Cindy Teeters

Cover Designer
Twist Creative, Seattle

Compositor
codeMantra

Graphics
codeMantra

Figure Credits
Figure 4.1: LangChain, Inc
Figures 7.1, 7.2, 7.4: Snowflake, Inc
Figure 8.2: SmartBear Software
Figure 8.3: Postman, Inc
Dedication

A I.
Perché non dedicarti un libro sarebbe stato un sacrilegio.
Contents at a Glance

Introduction

CHAPTER 1 The genesis and an analysis of large language models


CHAPTER 2 Core prompt learning techniques
CHAPTER 3 Engineering advanced learning prompts
CHAPTER 4 Mastering language frameworks
CHAPTER 5 Security, privacy, and accuracy concerns
CHAPTER 6 Building a personal assistant
CHAPTER 7 Chat with your data
CHAPTER 8 Conversational UI

Appendix: Inner functioning of LLMs

Index
Contents

Acknowledgments
Introduction

Chapter 1 The genesis and an analysis of large language models


LLMs at a glance
History of LLMs
Functioning basics
Business use cases
Facts of conversational programming
The emerging power of natural language
LLM topology
Future perspective
Summary

Chapter 2 Core prompt learning techniques


What is prompt engineering?
Prompts at a glance
Alternative ways to alter output
Setting up for code execution
Basic techniques
Zero-shot scenarios
Few-shot scenarios
Chain-of-thought scenarios
Fundamental use cases
Chatbots
Translating
LLM limitations
Summary

Chapter 3 Engineering advanced learning prompts


What’s beyond prompt engineering?
Combining pieces
Fine-tuning
Function calling
Homemade-style
OpenAI-style
Talking to (separated) data
Connecting data to LLMs
Embeddings
Vector store
Retrieval augmented generation
Summary

Chapter 4 Mastering language frameworks


The need for an orchestrator
Cross-framework concepts
Points to consider
LangChain
Models, prompt templates, and chains
Agents
Data connection
Microsoft Semantic Kernel
Plug-ins
Data and planners
Microsoft Guidance
Configuration
Main features
Summary

Chapter 5 Security, privacy, and accuracy concerns


Overview
Responsible AI
Red teaming
Abuse and content filtering
Hallucination and performances
Bias and fairness
Security and privacy
Security
Privacy
Evaluation and content filtering
Evaluation
Content filtering
Summary

Chapter 6 Building a personal assistant


Overview of the chatbot web application
Scope
Tech stack
The project
Setting up the LLM
Setting up the project
Integrating the LLM
Possible extensions
Summary

Chapter 7 Chat with your data


Overview
Scope
Tech stack
What is Streamlit?
A brief introduction to Streamlit
Main UI features
Pros and cons in production
The project
Setting up the project and base UI
Data preparation
LLM integration
Progressing further
Retrieval augmented generation versus fine-tuning
Possible extensions
Summary

Chapter 8 Conversational UI
Overview
Scope
Tech stack
The project
Minimal API setup
OpenAPI
LLM integration
Possible extensions
Summary

Appendix: Inner functioning of LLMs

Index
Acknowledgments

In the spring of 2023, when I told my dad how cool Azure OpenAI was
becoming, his reply was kind of a shock: “Why don’t you write a book about
it?” He said it so naturally that it hit me as if he really thought I could do it.
In fact, he added, “Are you up for it?” Then there was no need to say more.
Loretta Yates at Microsoft Press enthusiastically accepted my proposal, and
the story of this book began in June 2023.
AI has been a hot topic for the better part of a decade, but the emergence
of new-generation large language models (LLMs) has propelled it into the
mainstream. The increasing number of people using them translates to more
ideas, more opportunities, and new developments. And this makes all the
difference.
Hence, the book you hold in your hands can’t be the ultimate and
definitive guide to AI and LLMs because the speed at which AI and LLMs
evolve is impressive and because—by design—every book is an act of
approximation, a snapshot of knowledge taken at a specific moment in time.
Approximation inevitably leads to some form of dissatisfaction, and
dissatisfaction leads us to take on new challenges. In this regard, I wish for
myself decades of dissatisfaction. And a few more years of being on the stage
presenting books written for a prestigious publisher—it does wonders for my
ego.
First, I feel somewhat indebted to all my first dates since May because
they had to endure monologues lasting at least 30 minutes on LLMs and
some weird new approach to transformers.
True thanks are a private matter, but publicly I want to thank Martina first,
who cowrote the appendix with me and always knows what to say to make
me better. My gratitude to her is keeping a promise she knows. Thank you,
Martina, for being an extraordinary human being.
To Gianfranco, who taught me the importance of discussing and
expressing, even loudly, when something doesn’t please us, and taught me to
always ask, because the worst thing that can happen is hearing a no. Every
time I engage in a discussion, I will think of you.
I also want to thank Matteo, Luciano, Gabriele, Filippo, Daniele,
Riccardo, Marco, Jacopo, Simone, Francesco, and Alessia, who worked with
me and supported me during my (hopefully not too frequent) crises. I also
have warm thoughts for Alessandro, Antonino, Sara, Andrea, and Cristian
who tolerated me whenever we weren’t like 25-year-old youngsters because I
had to study and work on this book.
To Mom and Michela, who put up with me before the book and probably
will continue after. To my grandmas. To Giorgio, Gaetano, Vito, and Roberto
for helping me to grow every day. To Elio, who taught me how to dress and
see myself in more colors.
As for my dad, Dino, he never stops teaching me new things—for
example, how to get paid for doing things you would just love to do, like
being the technical editor of this book. Thank you, both as a father and as an
editor. You bring to my mind a song you well know: “Figlio, figlio, figlio.”
Beyond Loretta, if this book came to life, it was also because of the hard
work of Shourav, Kate, and Dan. Thank you for your patience and for
trusting me so much.
This book is my best until the next one!
Introduction

This is my third book on artificial intelligence (AI), and the first I wrote on
my own, without the collaboration of a coauthor. The sequence in which my
three books have been published reflects my own learning path, motivated by
a genuine thirst to understand AI for far more than mere business
considerations. The first book, published in 2020, introduced the
mathematical concepts behind machine learning (ML) that make it possible to
classify data and make timely predictions. The second book, which focused
on the Microsoft ML.NET framework, was about concrete applications—in
other words, how to make fancy algorithms work effectively on amounts of
data hiding their complexity behind the charts and tables of a familiar web
front end.
Then came ChatGPT.
The technology behind astonishing applications like ChatGPT is called a
large language model (LLM), and LLMs are the subject of this third book.
LLMs add a crucial capability to AI: the ability to generate content in
addition to classifying and predicting. LLMs represent a paradigm shift,
raising the bar of communication between humans and computers and
opening the floodgates to new applications that for decades we could only
dream of.
And for decades, we did dream of these applications. Literature and
movies presented various supercomputers capable of crunching any sort of
data to produce human-intelligible results. An extremely popular example
was HAL 9000—the computer that governed the spaceship Discovery in the
movie 2001: A Space Odyssey (1968). Another famous one was JARVIS
(Just A Rather Very Intelligent System), the computer that served Tony
Stark’s home assistant in Iron Man and other movies in the Marvel Comics
universe.
Often, all that the human characters in such books and movies do is
simply “load data into the machine,” whether in the form of paper
documents, digital files, or media content. Next, the machine autonomously
figures out the content, learns from it, and communicates back to humans
using natural language. But of course, those supercomputers were conceived
by authors; they were only science fiction. Today, with LLMs, it is possible
to devise and build concrete applications that not only make human–
computer interaction smooth and natural, but also turn the old dream of
simply “loading data into the machine” into a dazzling reality.
This book shows you how to build software applications using the same
type of engine that fuels ChatGPT to autonomously communicate with users
and orchestrate business tasks driven by plain textual prompts. No more, no
less—and as easy and striking as it sounds!

Who should read this book


Software architects, lead developers, and individuals with a background in
programming—particularly those familiar with languages like Python and
possibly C# (for ASP.NET Core)—will find the content in this book
accessible and valuable. In the vast realm of software professionals who
might find the book useful, I’d call out those who have an interest in ML,
especially in the context of LLMs. I’d also list cloud and IT professionals
with an interest in using cloud services (specifically Microsoft Azure) or in
sophisticated, real-world applications of human-like language in software.
While this book focuses primarily on the services available on the Microsoft
Azure platform, the concepts covered are easily applicable to analogous
platforms. At the end of the day, using an LLM involves little more than
calling a bunch of API endpoints, and, by design, APIs are completely
independent of the underlying platform.
In summary, this book caters to a diverse audience, including
programmers, ML enthusiasts, cloud-computing professionals, and those
interested in natural language processing, with a specific emphasis on
leveraging Azure services to program LLMs.
Assumptions

To fully grasp the value of a programming book on LLMs, there are a couple
of prerequisites, including proficiency in foundational programming concepts
and a familiarity with ML fundamentals. Beyond these, a working knowledge
of relevant programming languages and frameworks, such as Python and
possibly ASP.NET Core, is helpful, as is an appreciation for the significance
of classic natural language processing in the context of business domains.
Overall, a blend of programming expertise, ML awareness, and linguistic
understanding is recommended for a comprehensive grasp of the book’s
content.

This book might not be for you if…

This book might not be for you if you’re just seeking a reference book to find
out in detail how to use a particular pattern or framework. Although the book
discusses advanced aspects of popular frameworks (for example, LangChain
and Semantic Kernel) and APIs (such as OpenAI and Azure OpenAI), it does
not qualify as a programming reference on any of these. The focus of the
book is on using LLMs to build useful applications in the business domains
where LLMs really fit well.

Organization of this book

This book explores the practical application of existing LLMs in developing


versatile business domain applications. In essence, an LLM is an ML model
trained on extensive text data, enabling it to comprehend and generate
human-like language. To convey knowledge about these models, this book
focuses on three key aspects:
The first three chapters delve into scenarios for which an LLM is
effective and introduce essential tools for crafting sophisticated
solutions. These chapters provide insights into conversational
programming and prompting as a new, advanced, yet structured,
approach to coding.
The next two chapters emphasize patterns, frameworks, and techniques
for unlocking the potential of conversational programming. This
involves using natural language in code to define workflows, with the
LLM-based application orchestrating existing APIs.
The final three chapters present concrete, end-to-end demo examples
featuring Python and ASP.NET Core. These demos showcase
progressively advanced interactions between logic, data, and existing
business processes. In the first demo, you learn how to take text from an
email and craft a fitting draft for a reply. In the second demo, you apply
a retrieval augmented generation (RAG) pattern to formulate responses
to questions based on document content. Finally, in the third demo, you
learn how to build a hotel booking application with a chatbot that uses a
conversational interface to ascertain the user’s needs (dates, room
preferences, budget) and seamlessly places (or denies) reservations
according to the underlying system’s state, without using fixed user
interface elements or formatted data input controls.

Downloads: notebooks and samples


Python and Polyglot notebooks containing the code featured in the initial part
of the book, as well as the complete codebases for the examples tackled in the
latter part of the book, can be accessed on GitHub at:
https://github.com/Youbiquitous/programming-llm

Errata, updates, & book support


We’ve made every effort to ensure the accuracy of this book and its
companion content. You can access updates to this book—in the form of a
list of submitted errata and their related corrections—at:
MicrosoftPressStore.com/LLMAzureAI/errata
If you discover an error that is not already listed, please submit it to us at
the same page.
For additional book support and information, please visit
MicrosoftPressStore.com/Support.
Please note that product support for Microsoft software and hardware is
not offered through the previous addresses. For help with Microsoft software
or hardware, go to http://support.microsoft.com.

Stay in touch
Let’s keep the conversation going! We’re on X / Twitter:
http://twitter.com/MicrosoftPress.
Chapter 1

The genesis and an analysis of large


language models

Luring someone into reading a book is never a small feat. If it’s a novel, you
must convince them that it’s a beautiful story, and if it’s a technical book,
you must assure them that they’ll learn something. In this case, we’ll try to
learn something.
Over the past two years, generative AI has become a prominent buzzword.
It refers to a field of artificial intelligence (AI) focused on creating systems
that can generate new, original content autonomously. Large language
models (LLMs) like GPT-3 and GPT-4 are notable examples of generative
AI, capable of producing human-like text based on given input.
The rapid adoption of LLMs is leading to a paradigm shift in
programming. This chapter discusses this shift, the reasons for it, and its
prospects. Its prospects include conversational programming, in which you
explain with words—rather than with code—what you want to achieve. This
type of programming will likely become very prevalent in the future.
No promises, though. As you’ll soon see, explaining with words what you
want to achieve is often as difficult as writing code.
This chapter covers topics that didn’t find a place elsewhere in this book.
It’s not necessary to read every section or follow a strict order. Take and read
what you find necessary or interesting. I expect you will come back to read
certain parts of this chapter after you finish the last one.
LLMs at a glance

To navigate the realm of LLMs as a developer or manager, it’s essential to


comprehend the origins of generative AI and to discern its distinctions from
predictive AI. This chapter has one key goal: to provide insights into the
training and business relevance of LLMs, reserving the intricate mathematical
details for the appendix.
Our journey will span from the historical roots of AI to the fundamentals
of LLMs, including their training, inference, and the emergence of
multimodal models. Delving into the business landscape, we’ll also spotlight
current popular use cases of generative AI and textual models.
This introduction doesn’t aim to cover every detail. Rather, it intends to
equip you with sufficient information to address and cover any potential gaps
in knowledge, while working toward demystifying the intricacies surrounding
the evolution and implementation of LLMs.

History of LLMs
The evolution of LLMs intersects with both the history of conventional AI
(often referred to as predictive AI) and the domain of natural language
processing (NLP). NLP encompasses natural language understanding (NLU),
which attempts to reduce human speech into a structured ontology, and
natural language generation (NLG), which aims to produce text that is
understandable by humans.
LLMs are a subtype of generative AI focused on producing text based on
some kind of input, usually in the form of written text (referred to as a
prompt) but now expanding to multimodal inputs, including images, video,
and audio. At a glance, most LLMs can be seen as a very advanced form of
autocomplete, as they generate the next word. Although they specifically
generate text, LLMs do so in a manner that simulates human reasoning,
enabling them to perform a variety of intricate tasks. These tasks include
sentiment analysis, summarization, translation, entity and intent recognition,
structured information extraction, document generation, and so on.
LLMs represent a natural extension of the age-old human aspiration to
construct automatons (ancestors to contemporary robots) and imbue them
with a degree of reasoning and language. They can be seen as a brain for such
automatons, able to respond to an external input.

AI beginnings
Modern software—and AI as a vibrant part of it—represents the culmination
of an embryonic vision that has traversed the minds of great thinkers since
the 17th century. Various mathematicians, philosophers, and scientists, in
diverse ways and at varying levels of abstraction, envisioned a universal
language capable of mechanizing the acquisition and sharing of knowledge.
Gottfried Leibniz (1646–1716), in particular, contemplated the idea that at
least a portion of human reasoning could be mechanized.
The modern conceptualization of intelligent machinery took shape in the
mid-20th century, courtesy of renowned mathematicians Alan Turing and
Alonzo Church. Turing’s exploration of “intelligent machinery” in 1947,
coupled with his groundbreaking 1950 paper, “Computing Machinery and
Intelligence,” laid the cornerstone for the Turing test—a pivotal concept in
AI. This test challenged machines to exhibit human behavior
(indistinguishable by a human judge), ushering in the era of AI as a scientific
discipline.

Note
Considering recent advancements, a reevaluation of the original
Turing test may be warranted to incorporate a more precise
definition of human and rational behavior.

NLP
NLP is an interdisciplinary field within AI that aims to bridge the interaction
between computers and human language. While historically rooted in
linguistic approaches, distinguishing itself from the contemporary sense of
AI, NLP has perennially been a branch of AI in a broader sense. In fact, the
overarching goal has consistently been to artificially replicate an expression
of human intelligence—specifically, language.
The primary goal of NLP is to enable machines to understand, interpret,
and generate human-like language in a way that is both meaningful and
contextually relevant. This interdisciplinary field draws from linguistics,
computer science, and cognitive psychology to develop algorithms and
models that facilitate seamless interaction between humans and machines
through natural language.
The history of NLP spans several decades, evolving from rule-based
systems in the early stages to contemporary deep-learning approaches,
marking significant strides in the understanding and processing of human
language by computers.
Originating in the 1950s, early efforts, such as the Georgetown-IBM
experiment in 1954, aimed at machine translation from Russian to English,
laying the foundation for NLP. However, these initial endeavors were
primarily linguistic in nature. Subsequent decades witnessed the influence of
Chomskyan linguistics, shaping the field’s focus on syntactic and
grammatical structures.
The 1980s brought a shift toward statistical methods, like n-grams, using
co-occurrence frequencies of words to make predictions. An example was
IBM’s Candide system for speech recognition. However, rule-based
approaches struggled with the complexity of natural language. The 1990s saw
a resurgence of statistical approaches and the advent of machine learning
(ML) techniques such as hidden Markov models (HMMs) and statistical
language models. The introduction of the Penn Treebank, a 7-million word
dataset of part-of-speech tagged text, and statistical machine translation
systems marked significant milestones during this period.
In the 2000s, the rise of data-driven approaches and the availability of
extensive textual data on the internet rejuvenated the field. Probabilistic
models, including maximum-entropy models and conditional random fields,
gained prominence. Begun in the 1980s but finalized years later, the
development of WordNet, a semantical-lexical database of English (with its
groups of synonyms, or synonym set, and their relations), contributed to a
deeper understanding of word semantics.
The landscape transformed in the 2010s with the emergence of deep
learning made possible by a new generation of graphics processing units
(GPUs) and increased computing power. Neural network architectures—
particularly transformers like Bidirectional Encoder Representations from
Transformers (BERT) and Generative Pretrained Transformer (GPT)—
revolutionized NLP by capturing intricate language patterns and contextual
information. The focus shifted to data-driven and pretrained language
models, allowing for fine-tuning of specific tasks.

Predictive AI versus generative AI


Predictive AI and generative AI represent two distinct paradigms, each
deeply entwined with advancements in neural networks and deep-learning
architectures.
Predictive AI, often associated with supervised learning, traces its roots
back to classical ML approaches that emerged in the mid-20th century. Early
models, such as perceptrons, paved the way for the resurgence of neural
networks in the 1980s. However, it wasn’t until the advent of deep learning in
the 21st century—with the development of deep neural networks,
convolutional neural networks (CNNs) for image recognition, and recurrent
neural networks (RNNs) for sequential data—that predictive AI witnessed a
transformative resurgence. The introduction of long short-term memory
(LSTM) units enabled more effective modeling of sequential dependencies in
data.
Generative AI, on the other hand, has seen remarkable progress, propelled
by advancements in unsupervised learning and sophisticated neural network
architectures (the same used for predictive AI). The concept of generative
models dates to the 1990s, but the breakthrough came with the introduction
of generative adversarial networks (GANs) in 2014, showcasing the power of
adversarial training. GANs, which feature a generator for creating data and a
discriminator to distinguish between real and generated data, play a pivotal
role. The discriminator, discerning the authenticity of the generated data
during the training, contributes to the refinement of the generator, fostering
continuous enhancement in generating more realistic data, spanning from
lifelike images to coherent text.
Table 1-1 provides a recap of the main types of learning processes.

TABLE 1-1 Main types of learning processes

Type Definition Training Use Cases


Supervised Trained on Adjusts Classification,
labeled data parameters to regression
where each input minimize the
has a prediction error
corresponding
label
Self- Unsupervised Learns to fill in NLP, computer
supervised learning where the blank (predict vision
the model parts of input data
generates its own from other parts)
labels
Semi- Combines Uses labeled data Scenarios with
supervised labeled and for supervised limited labeled
unlabeled data tasks, unlabeled data—for
for training data for example, image
generalizations classification
Unsupervised Trained on data Identifies inherent Clustering,
without explicit structures or dimensionality
supervision relationships in reduction,
the data generative
modeling

The historical trajectory of predictive and generative AI underscores the


symbiotic relationship with neural networks and deep learning. Predictive AI
leverages deep-learning architectures like CNNs for image processing and
RNNs/LSTMs for sequential data, achieving state-of-the-art results in tasks
ranging from image recognition to natural language understanding.
Generative AI, fueled by the capabilities of GANs and large-scale language
models, showcases the creative potential of neural networks in generating
novel content.

LLMs
An LLM, exemplified by OpenAI’s GPT series, is a generative AI system
built on advanced deep-learning architectures like the transformer (more on
this in the appendix).
These models operate on the principle of unsupervised and self-supervised
learning, training on vast text corpora to comprehend and generate coherent
and contextually relevant text. They output sequences of text (that can be in
the form of proper text but also can be protein structures, code, SVG, JSON,
XML, and so on), demonstrating a remarkable ability to continue and expand
on given prompts in a manner that emulates human language.
The architecture of these models, particularly the transformer architecture,
enables them to capture long-range dependencies and intricate patterns in
data. The concept of word embeddings, a crucial precursor, represents words
as continuous vectors (Mikolov et al. in 2013 through Word2Vec),
contributing to the model’s understanding of semantic relationships between
words. Word embeddings is the first “layer” of an LLM.
The generative nature of the latest models enables them to be versatile in
output, allowing for tasks such as text completion, summarization, and
creative text generation. Users can prompt the model with various queries or
partial sentences, and the model autonomously generates coherent and
contextually relevant completions, demonstrating its ability to understand and
mimic human-like language patterns.
The journey began with the introduction of word embeddings in 2013,
notably with Mikolov et al.’s Word2Vec model, revolutionizing semantic
representation. RNNs and LSTM architectures followed, addressing
challenges in sequence processing and long-range dependencies. The
transformative shift arrived with the introduction of the transformer
architecture in 2017, allowing for parallel processing and significantly
improving training times.
In 2018, Google researchers Devlin et al. introduced BERT. BERT
adopted a bidirectional context prediction approach. During pretraining,
BERT is exposed to a masked language modeling task in which a random
subset of words in a sentence is masked and the model predicts those masked
words based on both left and right context. This bidirectional training allows
BERT to capture more nuanced contextual relationships between words. This
makes it particularly effective in tasks requiring a deep understanding of
context, such as question answering and sentiment analysis.
During the same period, OpenAI’s GPT series marked a paradigm shift in
NLP, starting with GPT in 2018 and progressing through GPT-2 in 2019, to
GPT-3 in 2020, and GPT-3.5-turbo, GPT-4, and GPT-4-turbo-visio (with
multimodal inputs) in 2023. As autoregressive models, these predict the next
token (which is an atomic element of natural language as it is elaborated by
machines) or word in a sequence based on the preceding context. GPT’s
autoregressive approach, predicting one token at a time, allows it to generate
coherent and contextually relevant text, showcasing versatility and language
understanding. The size of this model is huge, however. For example, GPT-3
has a massive scale of 175 billion parameters. (Detailed information about
GPT-3.5-turbo and GPT-4 are not available at the time of this writing.) The
fact is, these models can scale and generalize, thus reducing the need for task-
specific fine-tuning.

Functioning basics
The core principle guiding the functionality of most LLMs is autoregressive
language modeling, wherein the model takes input text and systematically
predicts the subsequent token or word (more on the difference between these
two terms shortly) in the sequence. This token-by-token prediction process is
crucial for generating coherent and contextually relevant text. However, as
emphasized by Yann LeCun, this approach can accumulate errors; if the N-th
token is incorrect, the model may persist in assuming its correctness,
potentially leading to inaccuracies in the generated text.
Until 2020, fine-tuning was the predominant method for tailoring models
to specific tasks. Recent advancements, however—particularly exemplified
by larger models like GPT-3—have introduced prompt engineering. This
allows these models to achieve task-specific outcomes without conventional
fine-tuning, relying instead on precise instructions provided as prompts.
Models such as those found in the GPT series are intricately crafted to
assimilate comprehensive knowledge about the syntax, semantics, and
underlying ontology inherent in human language corpora. While proficient at
capturing valuable linguistic information, it is imperative to acknowledge that
these models may also inherit inaccuracies and biases present in their training
corpora.

Different training approaches


An LLM can be trained with different goals, each requiring a different
approach. The three prominent methods are as follows:
Causal language modeling (CLM) This autoregressive method is used
in models like OpenAI’s GPT series. CLM trains the model to predict
the next token in a sequence based on preceding tokens. Although
effective for tasks like text generation and summarization, CLM models
possess a unidirectional context, only considering past context during
predictions. We will focus on this kind of model, as it is the most used
architecture at the moment.
Masked language modeling (MLM) This method is employed in
models like BERT, where a percentage of tokens in the input sequence
are randomly masked and the model predicts the original tokens based
on the surrounding context. This bidirectional approach is advantageous
for tasks such as text classification, sentiment analysis, and named entity
recognition. It is not suitable for pure text-generation tasks because in
those cases the model should rely only on the past, or “left part,” of the
input, without looking at the “right part,” or the future.
Sequence-to-sequence (Seq2Seq) These models, which feature an
encoder-decoder architecture, are used in tasks like machine translation
and summarization. The encoder processes the input sequence,
generating a latent representation used by the decoder to produce the
output sequence. This approach excels in handling complex tasks
involving input-output transformations, which are commonly used for
tasks where the input and output have a clear alignment during training,
such as translation tasks.
The key disparities lie in their objectives, architectures, and suitability for
specific tasks. CLM focuses on predicting the next token and excels in text
generation, MLM specializes in (bidirectional) context understanding, and
Seq2Seq is adept at generating coherent output text in the form of sequences.
And while CLM models are suitable for autoregressive tasks, MLM models
understand and embed the context, and Seq2Seq models handle input-output
transformations. Models may also be pretrained on auxiliary tasks, like next
sentence prediction (NSP), which tests their understanding of data
distribution.

The transformer model


The transformer architecture forms the foundation for modern LLMs.
Vaswani et al. presented the transformer model in a paper, “Attention Is All
You Need,” released in December 2017. Since then, NLP has been
completely revolutionized. Unlike previous models, which rely on sequential
processing, transformers employ an attention mechanism that allows for
parallelization and captures long-range dependencies.
The original model consists of an encoder and decoder, both articulated in
multiple self-attention processing layers. Self-attention processing means that
each word is determined by examining and considering its contextual
information.
In the encoder, input sequences are embedded and processed in parallel
through the layers, thus capturing intricate relationships between words. The
decoder generates output sequences, using the encoder’s contextual
information. Throughout the training process, the decoder learns to predict
the next word by analyzing the preceding words.
The transformer incorporates multiple layers of decoders to enhance its
capacity for language generation. The transformer’s design includes a context
window, which determines the length of the sequence the model considers
during inference and training. Larger context windows offer a broader scope
but incur higher computational costs, while smaller windows risk missing
crucial long-range dependencies. The real “brain” that allows transformers to
understand context and excel in tasks like translation and summarization is
the self-attention mechanism. There’s nothing like conscience or neuronal
learning in today’s LLM.
The self-attention mechanism allows the LLM to selectively focus on
different parts of the input sequence instead of treating the entire input in the
same way. Because of this, it needs fewer parameters to model long-term
dependencies and can capture relationships between words placed far away
from each other in the sequence. It’s simply a matter of guessing the next
words on a statistical basis, although it really seems smart and human.
While the original transformer architecture was a Seq2Seq model,
converting entire sequences from a source to a target format, nowadays the
current approach for text generation is an autoregressive approach.
Deviating from the original architecture, some models, including GPTs,
don’t include an explicit encoder part, relying only on the decoder. In this
architecture, the input is fed directly to the decoder. The decoder has more
self-attention heads and has been trained with a massive amount of data in an
unsupervised manner, just predicting the next word of existing texts.
Different models, like BERT, include only the encoder part that produces the
so-called embeddings.

Tokens and tokenization


Tokens, the elemental components in advanced language models like GPTs,
are central to the intricate process of language understanding and generation.
Unlike traditional linguistic units like words or characters, a token
encapsulates the essence of a single word, character, or subword unit. This
finer granularity is paramount for capturing the subtleties and intricacies
inherent in language.
The process of tokenization is a key facet. It involves breaking down texts
into smaller, manageable units, or tokens, which are then subjected to the
model’s analysis. The choice of tokens over words is deliberate, allowing for
a more nuanced representation of language.
OpenAI and Azure OpenAI employ a subword tokenization technique
Random documents with unrelated
content Scribd suggests to you:
regulates and guides their actions, makes them submit to the
judgment of the most capable, moves them to live and act together,
teaches them to form communities for the weal or woe of the
individual, to share joy and sorrow, good fortune and misfortune,
safety and danger, plenty and scarcity,—in other words, to form an
alliance based on reciprocity—which teaches them to employ
powers and means not theirs by inheritance, and, finally, presses
into their hands weapons with which Nature did not supply them.
Passions of all kinds, it is true, often gain a victory over their
circumspection; but these very passions are proof of the liveliness of
their sensations, or, what comes to the same thing, of their mental
activity. They are as susceptible as children, as irritable as weak-
minded men, and thus very sensitive to every kind of treatment they
may receive; to love and dislike, to encouraging praise and chilling
blame, to pleasant flattery and wounding ridicule, to caresses and
chastisement. Nevertheless they are not so easily managed, still less
so easily trained to anything, as a dog or any other clever domestic
animal, for they are self-willed in a high degree, and almost as
conceited as human beings. They learn without difficulty, but only
when they wish to, and by no means always when they ought to, for
their self-conceit rebels against any submission which they do not
see to be to their own advantage. They are quite aware that they are
liable to be punished, and may loudly express their disapprobation of
the expected chastisement beforehand, yet still refuse to do what is
required of them; while, on the other hand, they will execute it
willingly and with the liveliest expressions of understanding, when
the task happens to suit their humour. Whoever ventures to doubt
their self-esteem has only to watch their way of treating other
animals. Unless terrified by their strength and dangerousness, they
invariably regard other animals as playthings, whether they tease
them and play tricks upon them, or fondle them and load them with
caresses.
Some examples, for which I myself can vouch, or which I know to be
thoroughly authentic, may strengthen the assertions I have just
made.
As I was travelling in Bogosland, on my first ride into the mountains I
fell in with a large band of the Hamadryas baboons, referred to by
Sheikh Kemal el Din Demiri in his narrative. Drying their streaming
hair in the sunshine, they sat picturesquely grouped on the highest
points of a cliff, and, on being greeted with rifle bullets, they beat an
organized retreat and fled. Continuing my journey through the
narrow, winding, rocky valley of the Mensa, I came upon them some
time later, this time in the valley itself, just as they were preparing to
ascend the rocky wall of the other side to seek safety from such
annoying disturbances. A considerable number had already crossed
the valley; the majority were in the act of crossing. Our dogs,
beautiful, slender greyhounds, accustomed to fight successfully with
hyænas and other beasts of prey, rushed towards the baboons,
which, from a distance, looked more like beasts of prey than
monkeys, and drove them hastily up the precipices to right and left.
But only the females took to flight; the males, on the other hand,
turned to face the dogs, growled, beat the ground fiercely with their
hands, opened their mouths wide and showed their glittering teeth,
and looked at their adversaries so furiously and maliciously that the
hounds, usually bold and battle-hardened, shrank back discomfited,
and almost timidly sought safety beside us. Before we had
succeeded in stirring them up to show fight, the position of the
monkeys had changed considerably, and when the dogs charged a
second time nearly all the herd were in safety. But one little monkey
about half a year old had been left behind. It shrieked loudly as the
dogs rushed towards it, but succeeded in gaining the top of a rock
before they had arrived. Our dogs placed themselves cleverly, so as
to cut off its retreat, and we hoped that we might be able to catch it.
But that was not to be. Proudly and with dignity, without hurrying in
the least, or paying any heed to us, an old male stepped down from
the security of the rocks towards the hard-pressed little one, walked
towards the dogs without betraying the slightest fear, held them in
check with glances, gestures, and quite intelligible sounds, slowly
climbed the rock, picked up the baby-monkey, and retreated with it,
before we could reach the spot, and without the visibly disconcerted
dogs making the slightest attempt to prevent him. While the patriarch
of the troop performed this brave and self-sacrificing deed, the other
members, densely crowded on the cliff, uttered sounds which I had
never before heard from baboons. Old and young, males and
females, roared, screeched, snarled, and bellowed all together, so
that one would have thought they were struggling with leopards or
other dangerous beasts. I learned later that this was the monkeys’
battle-cry: it was intended obviously to intimidate us and the dogs,
possibly also to encourage the brave old giant, who was running into
such evident danger before their eyes.[72]
Fig. 45.—Old Baboon Rescuing Young One.

A few days later I learned by experience that these self-reliant


animals are a match even for men. On our return from the
Bogosland, we fell in with a large herd, possibly the same one, and
we opened fire upon them from the valley with seven double rifles.
Our shots had an indescribable effect. The same battle-cry which I
had heard before rang out again, and, as if at the command of a
general, they prepared for resistance. While the screaming females
with the young ones fled in all haste over the crest of the rock
beyond range of our guns, the adult males, casting furious glances,
beating the ground with their hands, and barking rather than roaring,
sprang upon projecting stones and ledges, looked down on the
valley for a few moments, continually growling, snarling, or
screaming, and then began to roll stones down upon us with so
much vigour and adroitness that we immediately saw that our lives
were in danger and took to flight. If it had not been possible for us to
clamber up the opposite wall of the narrow valley, and so to escape
the monkeys’ fire, we should have been utterly routed. The clever
animals not only conducted their defence on a definite plan, but they
acted in co-operation, striving for a common end, and exerting all
their united strength to attain it. One of our number saw one monkey
drag his stone up a tree that he might hurl it down with more effect; I
myself saw two combining their strength to set a heavy stone a-
rolling.
No animals but the higher apes adopt such means of defence, and
no other male animal runs into danger to rescue a helpless young
one of his species. Such traits must not be ignored, and cannot be
misinterpreted, for they speak for themselves better and more loudly
than all the sophistical analysis which refuses to admit that animals
have intelligence and the power of spontaneous action.
That the dog-like monkeys recognize and distinguish between cause
and effect can be certified by every unprejudiced observer. They
open doors and windows, drawers, cupboards, and boxes, untie
knots, and overcome other obstacles when they have once seen
how to set about it; but they also invent means to attain similar ends.
A female baboon, which I brought up in my family, got hold of a kitten
with the intention of making a pet of it and mothering it, but was
scratched by the terrified bundling. The monkey carefully examined
the kitten’s paws, pressed the claws forward, looked at them from
above, from beneath, and from the side, and then bit them off to
secure herself against further scratches. My brother and I used to
startle the same baboon by pouring a little heap of powder on the
ground in front of her, and setting it alight by means of a piece of
burning tinder. The sudden blazing up of the powder gave our
baboon such a fright every time that she screamed loudly and
sprang back as far as her tether would allow. After this trick had
been played upon her several times in succession, she protected
herself from further annoyance by beating the glowing tinder with her
hand till the spark was extinguished, and then eating up the powder.
In another case she conjured up fear and horror for herself. Like all
monkeys without exception, she regarded creeping things, and
above all snakes, with a boundless horror which was most amusing.
We often teased her by putting a snake, live, dead, or stuffed, into a
broad tin box, which was handed to her closed. After a time she
knew the box and its contents perfectly, but her curiosity always
mastered her, and she opened it every time, to run away screaming
directly afterwards.
Not content with recognizing causes really present, this monkey,
when she suffered any annoyance, sought for probable ones.
Something or someone must bear the blame of her discomfort. Thus
her anger was directed against the first person who came in sight. If
she was chastised, she was not angry with her master and keeper,
but with anyone else who was present during her punishment; such
a one must have been the cause of the harsh treatment she received
from her usually kind master. She had thus exactly the same
suspicions as small-minded human beings are apt to have in like
circumstances.
Notwithstanding her own extreme sensitiveness to any punishment,
even if only threatened, and also to quizzing and teasing, the baboon
in question could never refrain from tormenting, annoying, and even
ill-treating other animals. Our crabbed old badger-dog was lying
comfortably in the sun enjoying his mid-day nap. The baboon saw
this, slipped quietly up to him, looked with a sly twinkle of her little
eyes into the dog’s face to make sure that he was really asleep, then
suddenly seized the sleeper’s tail and brought him back with a
violent pull from dreamland to reality. The dog angrily rushed at the
disturber of his peace to avenge the insult. But the monkey escaped
the threatened punishment with a single leap over the advancing
dog, and in the next instant she had seized the tail and repeated the
outrage, obviously enjoying the powerlessness of her furious
opponent, until the latter, almost beside himself with anger and
excitement, unable even to bark, but gasping and foaming, tucked
his tail between his legs and fled, leaving the enemy in possession of
the field. If the baboon could have laughed, the parallel between her
behaviour and that of a mischievous boy would have been complete.
As it was, the scorn and ridicule with which the vanquished dog was
overwhelmed were intelligible enough. The baboon herself took
teasing very ill, would even become furious if laughed at by an
unprivileged person, and never omitted to take her revenge on the
first opportunity, even if that should not occur for weeks. But then
she was a monkey, and felt herself such, therefore regarded a dog
as a creature of a lower order, her insolence towards which was as
pardonable as that of every other creature towards herself was
reprehensible and worthy of punishment.
Of this self-esteem, or rather over-esteem, the dog-like monkeys
give daily proofs to every careful observer. The baboon in question,
like all monkeys, was exceedingly fond of pets, and in particular of a
long-tailed monkey which shared her cage, and could be trusted
even out of the cage with it, because it was always by the baboon’s
side as if under a charm. It slept in her arms, and obeyed her
slavishly. The baboon expected such obedience and took it as a
matter of course; but she demanded the most absolute subjection at
meal-times. While the good-natured and obedient long-tailed
monkey unresistingly allowed its foster-mother to pick out all the
titbits, the latter only left for the little one what was absolutely
necessary, and if it did succeed in storing something in its pouches,
simply opened these again and appropriated the contents to her own
use.
Unbounded as is the arrogance and self-esteem of the dog-like
monkeys, they are thoroughly well aware when they have done
wrong, that is, have done something deserving of punishment.
Schomburgk gives a most instructive example of this. In the
Zoological division of the Botanic Gardens at Adelaide an old sacred
macaque lived in a cage with two younger members of the same
species, over whom, as a matter of course, he ruled despotically.
One day, irritated by something or other, he attacked his keeper and
wounded him dangerously by biting through an artery on the wrist.
For this Schomburgk condemned him to death, and commissioned
another keeper to carry out the sentence by shooting him. The
monkeys were quite accustomed to fire-arms, which were often used
in the gardens for killing injurious animals, and though they knew
their effect they were not disquieted in the least when these were
brought into their immediate neighbourhood. The day after the
misconduct of the old tyrant the two young monkeys remained
quietly at the food-trough on the appearance of the keeper intrusted
with the execution of their comrade, but the criminal himself fled with
the utmost haste into his sleeping cage, and no amount of coaxing
could entice him out of it. An attempt was made to lure him forth by
setting down food; but he did what he had never done before, saw
his two subjects eat up the dainty fare and did not venture to take
part in the meal. Not till the suspected keeper had retired did he
venture to creep forth, seize a few crumbs, and retire in fear and
trembling to his hiding-place again. At length he was persuaded to
come out a second time, and the door of his retreat was closed.
When he saw the keeper with his weapon approaching, he knew that
he was lost. Frantically he threw himself on the door of his sleeping
cage to open it if possible, and not succeeding he rushed through
the whole cage examining every corner and space in the hope of
finding a means of escape; at last, seeing that there was no
possibility of flight, he threw himself despairingly on the ground and
surrendered himself, his whole body trembling and shuddering, to
the fate which overtook him a moment later.
Fig. 46.—Macaque or Bonnet-monkey (Macacus sinicus) and Snake.

It must be admitted that no mammal of any other order, not even the
dog, who has associated with us, and been educated, and, strictly
speaking, formed by us for thousands of years, acts in the manner
described, or exhibits such a high degree of intelligence. And yet a
wide gulf lies between the dog-like monkeys and the anthropoid
apes, of which I have said that they rise even above the average of
monkeyhood.
By the anthropoid apes we understand those which in their structure
most resemble man, but are externally distinguished from him by the
very prominent canine teeth, the relatively long arms and short legs,
the structure of the hand, the ischial callosities present in some
species, and the hairy covering of the body. They inhabit the tropical
countries of Asia and Africa (the former being richer in species), and
they are divided into three families, of which one is confined to
Africa. Each of these families embraces only a few species, but
probably we do not know nearly all of them as yet.
The structure of the anthropoid apes points to an arboreal life; they
are most excellent climbers, though by no means slaves to the tree
any more than the langurs, long-tailed monkeys, and macaques.
Their movements, however, both among the branches and on the
ground, are quite different from those of all other monkeys. In
climbing up a tree, particularly a smooth trunk without branches, they
take the same position as a man would do, but, thanks to their long
arms and short legs, they make much more rapid progress than the
most expert human climber; and when they have reached the
branches they put every gymnast to shame by the variety and
security of their movements. With outstretched arms they seize one
branch, with the feet they clasp a parallel one, about half their height
lower down, and, using the upper branch as a rail, they walk along
the lower one so quickly, though without the least sign of effort, that
a man walking underneath must exert himself vigorously to keep
pace with them. On reaching the end of the branch, they seize any
available bough or twig of the next tree and proceed on their way in
the same manner, with undiminished speed, yet without hurry. In
ascending they seize hold of any branch strong enough to bear their
weight, and swing themselves upwards with equal ease whether
they are holding the branch with both hands or only with one; in
descending, they let themselves hang with both arms and search
about for a new foothold. Sometimes they amuse themselves by
swinging freely for some minutes; sometimes, clasping a branch with
arms and feet, they walk, for a change, on its lower surface; in short,
they assume every imaginable position, and execute every possible
movement. Quite unrivalled masters of climbing are the long-armed
apes or gibbons, anthropoid apes with arms so disproportionately
long that, when outstretched, they measure thrice as much as their
upright bodies. With incomparable speed and security they climb up
a tree or bamboo-stem, set it, or a suitable branch swinging, and on
its rebound spring over spaces of from eight to twelve yards, so
lightly and swiftly, that they seem to fly like a shot arrow or an
alighting bird. They are also able to alter the direction of a leap while
actually springing, or to cut it suddenly short by seizing a branch and
clinging to it—swinging, rocking, and finally climbing up by it, either
to rest for a little, or to begin the old game anew. Sometimes they
spring through the air in this manner three, four, or five times in
succession, so that one almost forgets that they are subject to the
law of gravity. Their walking is as awkward as their climbing is
excellent. Other anthropoid apes are able to traverse a considerable
distance in an upright position—that is, on their feet alone, without
special difficulty, though when in haste they always fall on all-fours,
resting on the inturned knuckles of the fingers and the outer edges of
the feet, and throwing the body laboriously and clumsily forward
between the extended arms. But the long-armed apes move in an
upright position only in cases of extreme necessity, and then they
hop rather than walk. When the distance to be covered is a short one
they raise themselves to their full height, and preserving their
balance by extending their arms, now more, now less, spread out the
great toes as far as possible, and patter pitiably along with short,
quick steps. Their power of movement must therefore be
characterized as one-sided, for their superiority over the other
anthropoid apes in climbing does not counterbalance their
helplessness on the ground.
Fig. 47.—The Hoolock (Hylobates leuciscus), one of the Gibbons.

The voice-power of the anthropoid apes is very noteworthy. We find


that the most active and agile species have the loudest voices, while
those of the more widely developed, though less nimble, anthropoid
apes are capable of greater variety of expression. I do not say too
much when I assert that I have never heard the voice of any
mammal—man, of course, always excepted—which was more full-
toned and sonorous than that of a long-armed ape which I observed
in captivity. I was first astonished, then delighted, with these deep
notes, uttered with full strength, and by no means disagreeable,
because perfectly clear and well-rounded. In one species the ringing
call, which I should describe as a song rather than a cry, begins on
the key-note E, ascends and descends in semitones through the
chromatic scale for a full octave, sometimes ending with a shrill cry,
which seems to be uttered with the animal’s whole strength. The
key-note remains audible throughout, and serves as a grace-note to
each of the succeeding ones, which, in ascending the scale, follow
each other more and more slowly, in descending more and more
quickly, at last with extreme rapidity, but always with perfect
regularity. The notes of some species of the group are said to be
less clear, but all are so loud that, in the open air, one can hear them
distinctly at a distance of an English mile. The same correlation
between agility of motion and voice-power can be observed in other
anthropoid apes. The slow-moving, awkward-looking orang-utan
utters, as far as I know, only a strong, deep throat sound; the lively,
active, sprightly chimpanzee, with only a few notes, understands so
well how to give them variety of emphasis and intelligible expression
that one is tempted to concede to him the power of speech. He does
not indeed speak with words, but with sounds, and even syllables, of
the constancy of whose meaning the observer who has much
acquaintance with the chimpanzee can have no doubt. Other
anthropoid apes of the same family are probably not far behind him
in this respect.
Anyone who wishes to learn to what a height the mental qualities of
a monkey may reach must select the chimpanzee or one of his
nearest relatives for observation, and must associate closely with it
for a lengthened period, as I have done. He will then discover with
wonder and amazement, perhaps with slight horror, how much the
gulf between man and beast can be diminished. The other
anthropoid apes, too, are highly gifted creatures; they, too, surpass
all other monkeys in this respect; but the talents of the long-armed
gibbons or the orang-utans do not attain to the same universally
intelligible expression—I may say, the same impressiveness, as
those of the chimpanzees and their relatives. They—the pongos, the
gorilla, the tschiego, and the chimpanzee—cannot be treated as
animals, but must be associated with as men, if their mental powers
are to be known and appreciated. Their intelligence is not far behind
that of a rude, undisciplined, uneducated human being. They are,
and remain animals, but they behave so humanly that one can
almost lose sight of the beast.
For years in succession I have kept chimpanzees, have observed
them closely and, as far as possible, without prejudice, have
associated intimately with them, taken them into my family, brought
them up as playmates for my children, let them eat at my table,
taught and trained them, waited upon them in sickness, and not
forsaken them in the hour of death. I have therefore a right to believe
that I know them as well as anyone, and that I am justified in
pronouncing an authoritative opinion. For these reasons I select the
chimpanzee, in order to show to what height the mental power of an
animal may rise.
The chimpanzee is not only one of the cleverest of all creatures, he
is a being capable of deliberation and judgment. Everything he does
is done consciously and deliberately. He imitates, but he does so
with intelligence and on due consideration; he allows himself to be
taught, and learns. He knows himself and his surroundings, and he
can appreciate his position. In association with man he yields
submission to superior intelligence; in his relations with animals he
exhibits a self-conceit similar to our own. What is merely hinted at
among other apes is quite pronounced in him. He regards himself as
better, as standing higher than other animals, even other monkeys;
he rates even human beings exactly according to their standing; thus
he treats children quite differently from grown-up people; the latter
he respects, the former he looks upon as comrades and equals. He
shows an interest in animals with which he can form no friendship or
other tie, and also in objects which have no connection with his
natural wants; for he is not merely inquisitive, he is greedy of
knowledge; an object which has attracted his attention increases in
value in his eyes when he has found out its use. He can draw
conclusions, can reason from one to another, and apply the results
of experience to new circumstances, is cunning, even wily, has
flashes of wit, and indulges in practical jokes, exhibits humours and
moods, is entertained in one company and bored in another, enters
into the spirit of some jokes and scorns others, is self-willed but not
stubborn, good-natured but not wanting in independence. He
expresses his emotions like a human being. When in a gay mood he
smirks with satisfaction, when depressed his face is drawn into
wrinkles which speak for themselves, and he gives utterance to his
grief by plaintive sounds. In sickness he behaves like one in despair,
distorts his face, screams, throws himself on his back, beats with his
hands and feet, and tears his hair. To a friendly voice he responds
with sounds expressive of pleasure, to chiding with cries of distress.
He is active and busy from morning till late in the evening, seeks
constant occupation, and when he comes to an end of his usual
employments he invents new ones, even if it should only be slapping
his feet with his hands, or knocking against hollow boards, and thus
producing sounds which give him evident pleasure. In a room he
occupies himself with carefully examining everything that attracts his
attention, opens drawers and rummages among their contents,
opens the stove door to look at the fire and shuts it again, holds a
key properly, stands before the mirror and amuses himself with the
reflection of his own gestures and grimaces, uses brush and duster
as he has been taught, puts on blankets and clothing, and so on.
Fig. 48.—Chimpanzee (Troglodytes niger).

His acuteness of observation is strikingly proved by his almost


unfailingly correct judgment of persons. Not only does he recognize
and distinguish his friends from other people, but well-meaning from
evil-intentioned persons so thoroughly that the keeper of a
chimpanzee was convinced that anyone with whom his protégé
refused to make friends was really a good-for-nothing or a scoundrel.
A thorough but accomplished hypocrite who deceived me and others
was all along a horror to our chimpanzee, just as if he had seen
through the red-headed rascal from the first. Every chimpanzee who
has been much in human society likes best to be a member of a
family circle. There he behaves as though he felt himself among
equals. He carefully observes the manners and customs of the
house, notices immediately whether he is being watched or not, and
does in the former case what he ought to, in the latter what pleases
him. In contrast to other monkeys, he learns very easily and with real
eagerness whatever is taught to him, as, for instance, to sit upright at
table, to eat with knife, fork, and spoon, to drink from a glass or cup,
to stir the sugar in his tea, to touch glasses with his neighbour, to use
his napkin, and so on; with equal case he becomes accustomed to
clothing, beds, and blankets; without great difficulty he gains after a
time an understanding of human speech which far surpasses that of
a well-trained dog, for he follows not merely the emphasis but the
meaning of words, and executes commissions or obeys commands
with equal correctness. Exceedingly appreciative of every caress
and flattery, and even of praise, he is equally sensitive to unfriendly
treatment or blame; he is also capable of deep gratitude, and he
expresses it by shaking hands or kissing without being asked to do
so. He evinces a special fondness for children. Being neither spiteful
nor vicious, he treats children with great friendliness as long as they
do not tease him, and behaves to helpless infants with really
touching tenderness, though towards others of his own species,
monkeys of a different species, and animals generally, he is often
rough and harsh. I lay special stress on this characteristic, which I
have observed in every chimpanzee I have brought up, because it
seems to prove that the chimpanzee recognizes and respects the
human even in the youngest child.
The behaviour of a sick and suffering anthropoid ape is most
touching. Begging piteously, almost humanly, he looks into his
keeper’s face, receives every attempt to help him with warm thanks,
and soon looks upon the physician as a benefactor, holds out his
arm to him, or stretches out his tongue as soon as he is told, and
even does so of his own accord after a few visits from the physician.
He swallows medicine readily, submits even to a surgical operation,
and, in a word, behaves very like a human patient in similar
circumstances. As his end approaches he becomes more gentle, the
animal in him is lost sight of, and the nobler traits of his character
stand out prominently.
The chimpanzee which I kept longest, and with the help of an
intelligent, animal-loving keeper educated most carefully, was taken
ill with inflammation of the lungs, accompanied by suppuration of the
lymphatic glands of the neck. Surgical treatment of the glands was
found necessary. Two surgeons, friends of mine who were on good
terms with the chimpanzee, undertook to open the tumour on the
neck, the more readily that the monkey believed that to be the cause
of his suffering, and continually guided the surgeon’s hand towards
it. But how was the necessary operation in such a dangerous spot to
be performed without imperilling the monkey’s life? Anæsthetics
were out of the question because of the lung disease, and the
attempt to have the chimpanzee held down by several strong men
had to be abandoned because of his intense excitement, and the
strenuous resistance he offered. But where force failed persuasion
succeeded. When the monkey was quieted and reassured by the
coaxing and endearments of his keeper, he allowed a further
examination of the swelling, and even submitted, without twitching
an eyelid or uttering a complaint, to the use of the knife, and other
painful treatment, including the emptying of the opened tumour.
When this was done the distressingly laboured breathing became
instantly less oppressive, an unmistakable expression of relief
passed over the sufferer’s face, and he gratefully held out his hand
to both physicians, and embraced his keeper, without having been
asked to do either.
Unfortunately, the removal of the one trouble did not succeed in
saving the animal’s life. The neck wound healed, but the
inflammation of the lungs increased and killed him. He died fully
conscious, gently and peacefully, not as an animal, but as a man
dies.
These are features of the character and conduct of anthropoid apes
which can neither be misunderstood nor cavilled at. When one
considers that they can be observed in all anthropoids not yet full-
grown but beyond the stage of childhood, one must undoubtedly
grant those animals a very high place. For the opinion expressed by
some one incapable observer, and thoughtlessly repeated by
hundreds, that the monkey loses mental power with increasing age,
that he retrogrades and becomes stupid, is completely false, and is
disproved by every ape which is observed carefully, and without
prejudice, from youth to age.[73] Even if we knew nothing more about
full-grown anthropoids than that they erect shelters resembling huts
rather than nests, in which to pass a single night, and that they drum
on hollow trees for amusement, it would be enough to lead us to the
same conclusion as we have arrived at by observation of the young
members of this group; that is, that they must be regarded as by far
the most gifted and highly developed of animals, and as our nearest
relatives.
And the ape question? I might say that I have just answered it in
what I have said; but I have no hesitation in expressing a more
definite opinion.
Everyone must admit that man is not the representative of a new
order of being, but is simply a member of the animal kingdom, and
every unprejudiced person will also describe apes as the creatures
most resembling man. If we compare them with one another, and
then with man, the conviction is forced upon us, however we may
strive against it, that there is a greater difference between the
marmosets and the anthropoid apes than between the latter group
and man. Zoologically, therefore, one cannot even relegate the apes
and man to different orders of the highest class of animals. This has
indeed been done, and is still done, man being classed as two-
handed and monkeys as four-handed animals, but this leaves the
most important aid to the classification of a mammal, the dentition,
out of the question. For the dentition of man and monkeys is so
essentially similar that it points imperatively to the necessity of
placing the two types together. Nor is the distinction between two-
handed and four-handed tenable, for although as regards the
structure of hands and feet man and monkeys are certainly different,
the difference does not imply any opposition; and the monkeys are
just as much two-handed as we are. If we keep to the basis of
classification adhered to without exception elsewhere, we are forced
to place both in one order. I have given to it the name Hochtiere.
But though the characteristics which belong to all the higher animals
as members of one order correspond thus accurately, a closer
comparison reveals differences between man and apes which
absolutely forbid a fusion of the two groups such as has been
attempted in modern times. The symmetry of form, the comparative
shortness of the arms, the breadth and mobility of the hands, the
length and strength of the legs, as well as the flatness of the feet, the
naked skin, and the less-developed canine teeth are external marks
of man which must not be under-estimated, for they are important
enough to justify putting him and the apes in different families,
perhaps even different sub-orders. If, in addition, we take man’s
endowments into due consideration, compare his movements, his
articulate speech, his mental capacities with the corresponding gifts
in the ape, the need for insisting on the boundaries between the two
is confirmed.
Blind disciples of the Doctrine of Descent, as Darwin founded it and
others developed it, do indeed cross these boundaries without
hesitation, but they cannot possibly be regarded as giving a carefully
thought-out and authoritative judgment on the actual state of the
case. Satisfactory, not to say probable, as this doctrine is, it has not
yet risen above the level of an ingenious hypothesis; and
incontrovertible evidence for the correctness of this hypothesis has
not yet been produced. Variation within the limits of species and
breed can be proved, can even be brought about; but transformation
of one species into another cannot be established in any case. As
long as this is so we are justified in regarding man and apes as
creatures of different nature, and disputing the descent of one from
the other. No attempt to discover or establish a common ancestor, no
undertaking to draw up a pedigree for man, alters this in the
slightest; for true natural science is not satisfied with interpretative
theories, it demands proofs; it does not want to believe, but to know.
[74]

So we may without scruple give the apes the place in the scale of
being which unprejudiced investigation points out. We may look
upon them as the animals most resembling ourselves, and as our
nearest relatives in the zoological sense; anything more than this we
must deny. Much that is characteristic of man is to be found in the
apes also; but a wide gulf still remains between them and true
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.

Let us accompany you on the journey of exploring knowledge and


personal growth!

textbookfull.com

You might also like