• In the manner of St Thomas’s Summa contra Gentiles, we will start from shared rational foundations to build a “thinking” logical automaton.
  • I will try to demystify how LLMs (Large Language Models) work by having you relive the intellectual adventure that leads from matter to AI.
    • First we will build a computer.
    • Then we will see how an artificial neural network works.
    • Finally we will see how an LLM works.

I - The computing automaton

Fundamental logic

Logic is the science of the principles of valid reasoning. It is fundamental to philosophy, mathematics and computer science, and it rests on the principle of non-contradiction. The NAND gate is a universal gate: every other logic gate can be built by combining it.

Imagining the automaton

In theoretical computer science, a Turing machine is an abstract model of how mechanical computing devices, such as a computer, work.

NANDGAME: https://www.nandgame.com/

Building the automaton

The discovery of P-N junctions makes it possible to build tiny silicon transistors. These transistors can be assembled into integrated circuits, from which a computer can be built.

Minecraft CPU: https://youtu.be/TxatLwlj0lU?si=aYUfTmiCIc7kXt56&t=34

Controlling the automaton

We now have to control our automaton and make it compute. We create languages with ever higher and ever more elegant levels of abstraction. We gain a new kind of relationship to language: execution, on top of reading and writing.

Speeding up the automaton

After a while, transistor miniaturisation and clock frequency hit physical limits. Other ways of increasing computing power are needed: duplicate and parallelise, spreading the workload over many cores.

II - Grasping an uncertain world

Neural networks and machine vision (ANNs)

Our machine is deterministic, fast and precise, but it does not like uncertainty, ambiguity or approximation. So we look at how the human brain manages to grasp the uncertain world around us with its networks of neurons, and take inspiration from nature to create artificial neural networks. (Human vision is said to be 576 megapixels, and the human brain holds 86 billion neurons. The near-instant processing of visual information is fascinating.)

3B1B perceptron recap: https://youtu.be/IHZwWFHWa-w?si=LE5qWstbH01bqKZO&t=29

Large Language Models (LLMs)

One theory of the emergence of human faculties is the diversion of part of our visual computing power towards meta-cognition and abstract concepts. LLMs are neural networks trained on very large corpora of text. They can generate text from a prompt.

NanoLLM visualisation: https://bbycroft.net/llm

Here is an example of GPT-4 used to create a Catholic chatbot which, enriched with the Catechism of the Catholic Church, answers users’ questions.

Pre-training corpus

Give the model a corpus of text large and varied enough for it to learn the structure of language. Redundant data and biases must be avoided, markup cleaned, and so on.

Domain corpus

  • A corpus for adapting to a particular domain and its jargon: medical, legal, financial, and so on.

Tokenisation

Converting a text into a sequence of tokens (words, characters, sub-words, and so on) that the model will use.

Choosing the type of LLM

Different architectures suit different tasks:

  • “Auto-encoding”, RoBERTa: fill-in-the-blank, sentiment analysis, NER, and so on
  • “Auto-regressive”, GPT: text generation
  • “Sequence-to-sequence”, BART: translation, summarisation, and so on

Fine-tuning

The model’s weights are changed to fit a particular task by giving it examples of the task and the expected answers. The model can be split into parts, fine-tuning one part and freezing the weights of the rest (PEFT). INSTRUCT mode answers the instructions the user gives.

Benchmarks and evaluation metrics

The similarity between the generated text and the expected human text is measured with matching metrics such as BLEU or ROUGE. Other tests are more general, such as MMLU.

Reinforcement from human feedback (RLHF)

An LLM must follow the three H’s: Helpful, Honest and Harmless. Several answers are generated, a human is asked to rank them, and the model is fine-tuned against that feedback. An LLM that is too “helpful” can be manipulated by a user into malicious activities.

Deployment

Compressing, optimising and deploying the model on a server so that it can answer users.

Orchestration library

Lets the LLM talk to the other components of the system, query data, and so on.

Retrieval-Augmented Generation (RAG)

Adds further data sources to enrich the model’s answers, through database queries, search-engine lookups, semantic search, API calls, and so on. Vector similarity search (embedding vectors) finds texts similar to the query, which can be folded into the model’s answer.

Bias in vectors: https://wikipedia2vec.github.io/demo/

Program-aided language (PAL) models & ReAct

The model is given the ability to generate computer code that carries out complex tasks. A complex calculation, for instance, is beyond an LLM, whereas it can write a simple program that solves it. The model is given only a general task and plans the sub-tasks and agents needed to get there.

Prompt engineering

Depending on the model, we have a limited space in which to instruct it: the context window. That window is where we put:

UI/UX

The user interface is essential for humans to be able to use the model. It is usually close to an instant-messaging interface with conversation bubbles. It also includes user management, message history, and so on.

III - Case study

Demonstration of GPT-4 used to create a Catholic chatbot which, enriched with the Catechism of the Catholic Church, answers users’ questions.