Kick off your book project in 2 hours! Live workshop on Zoom. You’ll leave with a real book project, progress on your first chapter, and a clear plan to keep going. Tuesday, June 16, 2026. Learn more…

Leanpub Header

Skip to main content

Practical TypeScript Artificial Intelligence Programming

Practical AI Programming with TypeScript — From Classic Machine Learning to Large Language Models and Knowledge Representation

This book is 100% completeLast updated on 2026-06-14

From KNN to LLMs: build real AI applications in TypeScript — no GPU required. Implement machine learning from scratch, train neural networks with TensorFlow.js, integrate Google Gemini and OpenAI APIs, run local models with Ollama, build a chess engine with alpha-beta search, and query the world's knowledge graphs with SPARQL — all from the command line.

Minimum price

$9.00

$18.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
WEB
About

About

About the Book

This book is a practical, hands-on introduction to artificial intelligence for TypeScript and JavaScript programmers. It covers the full spectrum of modern AI — from classical machine learning algorithms implemented from scratch to deep learning with TensorFlow.js, large language model integration via cloud APIs and local models, symbolic AI techniques, and knowledge representation with graph databases and the Semantic Web. All examples run as command-line programs on Node.js with no browser or UI framework required.

The book is organized into five parts that can be read in any order. Part 1 covers the foundations of machine learning with TypeScript, including K-nearest neighbors, regression, clustering, exploratory data analysis, and anomaly detection — all built with nothing more than Node.js built-in modules. Part 2 dives into deep learning with TensorFlow.js, covering neural network fundamentals, natural language processing, image generation, reinforcement learning, and recommendation systems. Each chapter balances conceptual explanations with working TypeScript code.

Part 3 focuses on large language models, arguably the most transformative development in AI. You will learn the transformer architecture, then build practical applications using the Google Gemini and OpenAI APIs, run open-weights models locally with Ollama, and construct a production-quality AI command-line REPL with search grounding and persistent caching. Parts 4 and 5 round out the book with symbolic AI (frame-based knowledge representation, constraint satisfaction, and a complete chess engine with alpha-beta search) and knowledge representation (SPARQL queries against Wikidata, SQLite knowledge bases, RDF, and linked data).

Share this book

Author

About the Author

Mark Watson

Mark Watson is a consultant specializing in LLMs, deep learning, machine learning, knowledge graphs, and general artificial intelligence software development. He uses Common Lisp, Clojure, Python, Java, Haskell, and Ruby for development.

He is the author of 20+ published books on Artificial Intelligence, Deep Learning, Java, Ruby, Machine Learning, Common LISP, Clojure, JavaScript, Semantic Web, NLP, C++, Linux, and Scheme. He has 55 US Patents.

Mark's consulting customer list includes: Google, Capital One, Olive AI, CompassLabs, Disney, Sitescout.com, Embed.ly, and Webmind Corporation.

Mark wrote ten traditional published books for McGraw Hill, Springer Verlag, J Wiley, and Morgan Kaufman publishers before adopting the LeanPub self-publishing platform.

Leanpub Podcast

Episode 253

An Interview with Mark Watson

Contents

Table of Contents

Cover Material, Copyright, and License

Preface

  1. About the Author
  2. Using the Example Code
  3. NPM Security Concerns
  4. Acknowledgements

Setting Up a TypeScript Development Environment

  1. Installing Node.js
  2. Installing TypeScript and tsx
  3. Creating a New Project
  4. Running Existing Projects in directory source-code
  5. Running TypeScript Files
  6. Environment Variables
  7. Code Formatting (Optional)

A TypeScript Tutorial for Command-Line AI Programs

  1. Type Basics
  2. Interfaces and Type Aliases
  3. Functions
  4. Async/Await and Promises
  5. Classes
  6. Modules and Imports
  7. Error Handling
  8. Working with Files
  9. Enums and Literal Types
  10. Map, Set, and Iterators
  11. Practical Patterns for AI Code
  12. TypeScript Tutorial Wrap-up

Part 1 - Machine Learning

“Classic” Machine Learning

  1. Example Material
  2. Loading CSV Data
  3. Classification Using K-Nearest Neighbors
  4. Classic Machine Learning Wrap-up

Regression and Clustering

  1. Regression: Predicting Housing Prices
  2. Clustering: Discovering Groups in Data
  3. Regression and Clustering Wrap-up

Exploratory Data Analysis and Feature Engineering

  1. Exploratory Data Analysis
  2. Feature Engineering
  3. EDA and Feature Engineering Wrap-up

Anomaly Detection

  1. What Is a Gaussian Distribution?
  2. How the Detector Works
  3. The Wisconsin Breast Cancer Dataset
  4. Project Structure
  5. Walking Through the Code
  6. Running the Example
  7. Using the API in Your Own Code
  8. Understanding the Evaluation Metrics
  9. Wrap Up

Part 2 - Deep Learning

The Basics of Deep Learning

  1. Using TensorFlow.js for Building a Cancer Prediction Model

Natural Language Processing Using Deep Learning

  1. Hugging Face and the Transformers.js Library
  2. Comparing Sentences for Similarity Using Transformer Models
  3. Deep Learning Natural Language Processing Wrap-up

Part IV - Overviews of Image Generation, Reinforcement Learning, and Recommendation Systems

Overview of Image Generation

  1. Image Generation Using the Hugging Face Inference API
  2. Image Generation Using Local Ollama Models
  3. Recommended Reading for Image Generation

Overview of Reinforcement Learning (Optional Material)

  1. Overview
  2. An Introduction to Markov Decision Process
  3. A Concrete Example: Q-Learning
  4. Reinforcement Learning Wrap-up

Overview of Recommendation Systems (Optional Material)

  1. TensorFlow Recommenders
  2. Project Structure
  3. Item-Based Collaborative Filtering
  4. Embedding Matrix Factorization
  5. Running the Examples
  6. Comparing the Two Approaches
  7. Using the API in Your Own Code
  8. Recommendation Systems Wrap-up

Part 3 - Large Language Models

Introduction to Transformers and Large Language Models

  1. The Transformer Architecture
  2. Tokenization
  3. From Transformers to Large Language Models
  4. Key Capabilities of Modern LLMs
  5. Practical Considerations

LLMs with Public APIs

  1. Setup and Authentication
  2. Text Generation
  3. Thinking Models
  4. Multi-Turn Conversations
  5. Multimodal Input: Analyzing Images
  6. Structured Output
  7. Tool Use (Function Calling)
  8. Practical Considerations
  9. Summary

LLMs with Local Models

  1. Installing Ollama
  2. Downloading and Running Models
  3. Using Ollama from TypeScript
  4. Reasoning with Local Models
  5. Conversation Memory with Ollama
  6. Describe Content of Images
  7. Adding Web Search Tools
  8. OpenAI-Compatible API
  9. Alternative Tools for Running Local Models
  10. Hardware Considerations
  11. Summary

An AI Command-Line Tool with Search Grounding and Persistent Cache

  1. How It Works
  2. Prerequisites
  3. Project Structure
  4. Keyword Extraction
  5. The Cache Engine
  6. The Main REPL Application
  7. Running the Tool
  8. Example Session
  9. REPL Command Reference
  10. Key Takeaways

Part 4 - Symbolic AI and Knowledge Representation

Classic Graph Search

  1. Graphs and Search Representation
  2. Implementing Graph Search in TypeScript
  3. Example Run
  4. Wrap-up

Chess Game with Alpha-Beta Search

  1. How a Chess Engine Works
  2. Project Structure
  3. Walking Through the Code
  4. Running the Example
  5. Wrap Up

Part 5 - Knowledge Representation

Getting Setup To Use Graph and Relational Databases

  1. Querying Wikidata with SPARQL and TypeScript
  2. The SQLite Relational Database for Knowledge Representation

Optional Material: A Deeper Dive Into Semantic Web and Linked Data

  1. Overview and Theory

Open Knowledge Format (OKF) Bundle Explorer

  1. Inspiration and the Specification
  2. The Knowledge Bundle Structure
  3. Defining the OKF Data Model
  4. Implementing the Knowledge Bundle
  5. Building the Consumption Agent
  6. Tying It All Together
  7. Running the Explorer and Sample Output
  8. Summary and Future Improvements

Also by the Author

Also by the Author

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub