O'Reilly《Hands-On Large Language Models》官方代码库,包含12章核心内容与Bonus章节,涵盖Tokens、Transformer、RAG、微调等主题。提供300+插图与可运行的Jupyter Notebook,支持Colab与本地环境。
项目概述#
Hands-On Large Language Models 是 O'Reilly 出版书籍的官方代码仓库,由 Jay Alammar(Cohere 工程研究员)和 Maarten Grootendorst(IKNL 高级临床数据科学家)合著。项目采用视觉化教育方法,包含近 275-300 个自定义插图,所有示例均提供可运行的 Jupyter Notebook 代码。
核心章节结构(12章)#
- Chapter 1: Introduction to Language Models - 语言模型入门
- Chapter 2: Tokens and Embeddings - 分词与嵌入
- Chapter 3: Looking Inside Transformer LLMs - Transformer 内部机制
- Chapter 4: Text Classification - 文本分类
- Chapter 5: Text Clustering and Topic Modeling - 文本聚类与主题建模
- Chapter 6: Prompt Engineering - 提示工程
- Chapter 7: Advanced Text Generation Techniques - 高级文本生成技术
- Chapter 8: Semantic Search and RAG - 语义搜索与检索增强生成
- Chapter 9: Multimodal Large Language Models - 多模态大语言模型
- Chapter 10: Creating Text Embedding Models - 创建文本嵌入模型
- Chapter 11: Fine-tuning Representation Models - 表示模型微调
- Chapter 12: Fine-tuning Generation Models - 生成模型微调
Bonus 章节内容#
- Mamba 架构: 选择性状态空间模型可视化解析
- Quantization (量化): 模型压缩与加速技术
- Mixture of Experts (MoE): 稀疏专家模型架构
- Reasoning LLMs: 推理增强型语言模型(含 DeepSeek-R1)
- Stable Diffusion: 图像生成原理图解
安装与运行#
方法 1: Google Colab(推荐)#
- 访问仓库 README 中的 Table of Contents
- 点击各章节的 "Open in Colab" 徽章
- 自动获得免费 T4 GPU (16GB VRAM)
- 无需本地环境配置
方法 2: 本地 Conda 环境#
# 克隆仓库
git clone https://github.com/HandsOnLLM/Hands-On-Large-Language-Models.git
cd Hands-On-Large-Language-Models
# 创建 conda 环境
conda create -n thellmbook python=3.10
conda activate thellmbook
# 安装依赖
conda env create -f environment.yml
# 或使用 pip
pip install -r requirements.txt
# 安装 GPU 版 PyTorch
pip3 install --upgrade torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
系统要求#
- Python 3.10.*
- Microsoft Visual C++ 14.0+ (Windows)
- 推荐 NVIDIA GPU + CUDA 驱动
环境验证#
import torch
import sys
import os
# 检查 GPU
print(f"CUDA available: {torch.cuda.is_available()}")
# 检查环境
print(f"Current Env: {os.environ.get('CONDA_DEFAULT_ENV')}")
仓库结构#
Hands-On-Large-Language-Models/
├── .setup/ # 环境配置指南
│ ├── conda/ # Conda 详细安装指南
│ └── images/ # 配置截图
├── bonus/ # 额外主题章节
├── chapter01-12/ # 12 个核心章节 Notebook
├── images/ # 书籍插图资源
├── environment.yml # Conda 环境定义
├── requirements.txt # 完整依赖(锁定版本)
└── requirements_min.txt # 最小依赖
技术覆盖范围#
| 技术层级 | 关键主题 |
|---|---|
| 基础原理 | Tokenization, Embeddings, Self-Attention, Feedforward Networks |
| 表示学习 | BERTopic, Sentiment Analysis, Zero-shot Classification |
| 生成交互 | Prompt Templating, Temperature, Top-k/Top-p Sampling |
| 检索增强 | Vector Search, FAISS, RAG Pipeline, Context Window |
| 进阶架构 | CLIP, Multimodal Embeddings, Contrastive Learning |
| 模型定制 | LoRA, QLoRA, PEFT, Supervised Fine-Tuning |
业界评价#
- Andrew Ng (DeepLearning.AI 创始人): "valuable resource for anyone looking to understand the main techniques behind how Large Language Models are built"
- Nils Reimers (Cohere ML Director, sentence-transformers 作者): "Its highly-visual coverage of generative, representational, and retrieval applications of language models empowers readers to quickly understand, use, and refine LLMs"
出版信息#
- 出版社: O'Reilly Media
- 出版年份: 2024
- ISBN: 978-1098150969