Cover Material, Copyright, and License
Preface
- Who This Book Is For
- How To Read This Book
- Open Source Example Programs and Manuscript Files
- Acknowledgments
Scala 3 Conventions and Tricks Used in This Book
- Running an Example with scala-cli
- Indentation Instead of Braces
- Top-Level Definitions and @main
- Case Classes and copy
- Enums as Data Types
- Pattern Matching
- Options and Either
- For-Comprehensions
- Collections
- String Interpolation
- Extension Methods, Givens, and Context Parameters
- Operator Overloading
- Early Exit with boundary and break
- inline and @tailrec
- A Note on DSLs
- Where to Go Next
Search Algorithms
- Search as a Formal Problem
- Representing Graphs
- Depth-First and Breadth-First Graph Search
- Maze Generation and Solving
- Game Tree Search: Minimax for Tic-Tac-Toe
- Running the Search Demos
A Chess Engine and AI Bot
- A Short History and the Size of the Problem
- Architecture of a Chess Engine
- Search Engine: Negamax with Alpha-Beta Pruning
- Positional Evaluation
- Running the Chess CLI and Perft Tests
Backpropagation Neural Networks from Scratch
- A Short History: Why We Need Hidden Layers
- Neural Network Representation
- Feedforward Pass
- Backpropagation and Weight Updates
- Demo: Learning the XOR Gate
Genetic Algorithms
- Evolutionary Computation and When to Reach for It
- Chromosomes and the Genetic Representation
- The Genetic Algorithm Engine
- A Simple Example: Maximizing a Complex Function
- Running the Optimization
Anomaly Detection
- Why Anomaly Detection, Not Classification
- How Gaussian Anomaly Detection Works
- Preprocessing the Data
- Implementing Anomaly Detection
- Running the Anomaly Detector
Natural Language Processing
- The Classic NLP Pipeline
- Tokenization
- Part-of-Speech Tagging
- Named Entity Extraction
- Running the NLP Demo
Semantic Web and SPARQL
- From a Web of Documents to a Web of Data
- The Apache Jena Integration
- Local Ontological Reasoning and RDFS
- Local and Remote Queries
- Running the Semantic Web Demo
Knowledge Graph Navigator (KGN)
- The Grounding Problem and Entity Linking
- How KGN Works
- Extracting and Resolving Entities
- Fetching Entity Details with SPARQL Templates
- Discovering Semantic Relationships
- Running the KGN Shell
Integrating Google Gemini
- How Large Language Models Work
- Project Setup and Dependencies
- Implementing the Gemini Client
- Running the Demos
Integrating OpenAI
- The Chat Completions Format
- The OpenAI REST Client
- Beyond Chat: Embeddings
- Running the OpenAI Demo
Local LLMs with Ollama
- Why Run a Model Locally
- The Ollama REST Client
- Running the Ollama Demo
An Interactive Text Adventure Game with Ollama
- From Generate to Chat
- The Story Prompt as Data
- The Data Model: Roles and Messages
- The Ollama Chat Client
- The Game Loop
- A Connection Test Entry Point
- Building and Running
- Interpreting the Results
- Wrap Up
- Optional Practice Problems
Autonomous Agents with AgentScope
- From Text Generator to Agent
- Project Configuration
- Creating a Conversational Agent
- Equipping Agents with Custom Tools
- Running the Agent Demo
Building a Neural-Symbolic Knowledge Graph Engine in Scala
- What we are going to build
- Background: three ideas you need first
- How the pieces connect
- The build file
- The data model: terms and triples
- Unification
- The triplestore
- The reader
- The query engine
- The neural layer
- Supporting infrastructure
- The embedded DSL: recreating reader macros in Scala
- Running the engine
- Testing
- Interpreting the results
- Wrap Up
- Optional Practice Problems
Probability: Bayes, Base Rates, and Tests
- Two Ways to Read a Probability
- Bayes Theorem
- Bayes in Seven Lines
- The Medical Test That Fools Doctors
- Frequentist Checks: z, Chi-Squared, Wilson
- Correlation in Nine Lines
- The Test Suite
- Running the Examples
- Interpreting the Output
- Wrap Up
- Optional Practice Problems
Symbolic Math: Differentiate and Integrate
- Two Ways to Do Calculus on a Computer
- Terms and Polynomials as Data
- Differentiation: The Power Rule and Linearity
- Integration: The Reverse Power Rule and the Fundamental Theorem
- The Demo
- The Test Suite
- Interpreting the Output
- What This Core Does Not Do
- Wrap Up
- Optional Practice Problems
Agentic RAG with Local Docs
- The RAG Pipeline
- Lexical and Dense Retrieval
- Chunking Long Documents
- Ranking with TF-IDF and BM25
- Rewriting and Fanning Out Queries
- Building the Grounded Prompt
- Answering with Local Ollama
- Loading the Docs and Running the Demo
- Offline Checks
- Extending the Pipeline
Brave Web Search for Agents and RAG
- Getting a Key and the Request Shape
- Reading the Reply
- The Client
- Parsing the Reply
- Running a Search
- Wiring It to RAG and Tools
- Demo and Checks
- Extending the Client
LLM Tool Use Without a Framework
- What Tool Use Actually Is
- A Tool Registry in Thirty Lines
- Three Safe Builtins
- The CALL and FINAL Loop
- The Demo
- Offline Tests
- Wrap Up
- Optional Practice Problems
A SQLite Cache for LLM Calls
- Why Cache Model Calls
- Four Places to Cache
- Exact Keys Versus Term Matching
- What Belongs in a Cache Key
- Opening the Store
- Writing Rows Safely
- Looking Up by Terms
- Managing the Data Store
- Demo and Its Output
- Cache First, Model Second
- Safety, Staleness, and Cost
- Offline Checks
- Extending the Cache
More Hosted Models: Anthropic, Mistral, Groq, Moonshot, Perplexity, Hugging Face
- One Record for Six Vendors
- The Odd Two: Anthropic and Hugging Face
- One Send for All
- Demo and Its Output
- Parsers You Can Test Offline
Ollama Vision: Read Text from Images
- How a Vision Model Sees
- What Ollama Expects
- The Client
- Request and Response in Full
- The Demo
- Running the Demo
- Interpreting the Output
- Offline Tests
- Practical Limits
- Wrap Up
- Optional Practice Problems
A Tiny Transformer from Scratch
- Why Attention Replaced Recurrence
- A Tiny Corpus and Character Tokens
- The Autograd Engine
- The Transformer Block
- Loss and a Training Step
- Greedy Generation
- The Demo and Its Output
- Tests That Pin Learning
- Wrap Up
- Optional Practice Problems
Interactive Web Widgets with Laminar
- Why Laminar Fits This Book
- Widget One: Bayes Sliders
- Widget Two: Doc Search
- What the Reader Sees
- Build and Open