rag-pipeline

rag-pipeline is a skill for Claude Code, Codex from bojieli/ai-agent-book. It costs 125 tokens per session (2,922 once invoked), scanned A, original, Apache-2.0.

A guide to RAG, a method where a system retrieves relevant passages from an external knowledge base before an AI model writes an answer. It covers splitting documents, searching by meaning or exact words, combining results, and measuring retrieval quality.

In plain words
What is it for?
Use it to build and tune document search, choose dense, sparse, or hybrid retrieval, select an index, add result reranking, and measure recall and ranking quality.
Why use it?
It helps keep answers grounded in information that can be updated without retraining the model. It also helps find whether poor answers come from bad retrieval rather than from the model's writing.

Skill for Claude CodeCodex

Written for no agent in particular: nothing here depends on one.

Good fit Use it to build and tune document search, choose dense, sparse, or hybrid retrieval, select an index, add result reranking, and measure recall and ranking quality.

Compare 6 skills from other repositories ↓
Install with agentmods
npx agentmods add skills/bojieli/ai-agent-book/rag-pipeline
About the project

AI Agent: Design Principles and Engineering Practice is an open-source book that explains how AI agents combine language models, context, and tools, with accompanying experiments and code. It is intended for readers studying the principles and engineering of AI agents, from fundamentals through production use. The catalogue skills support coding-agent work related to the book's subject matter.

bojieli/ai-agent-book · 54,406 stars · on GitHub

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

Any agent
npx skills add bojieli/ai-agent-book --skill rag-pipeline
Clone the repo
git clone --depth 1 https://github.com/bojieli/ai-agent-book

Made for: Claude Code, Codex.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for rag-pipeline

README.md
[![agentmods](https://agentmods.dev/badge/skills/bojieli/ai-agent-book/rag-pipeline/github.svg)](https://agentmods.dev/skills/bojieli/ai-agent-book/rag-pipeline)
Your own site
<a href="https://agentmods.dev/skills/bojieli/ai-agent-book/rag-pipeline"><img src="https://agentmods.dev/badge/skills/bojieli/ai-agent-book/rag-pipeline/github.svg" alt="Measured on agentmods" height="20"></a>

Or the 80×15 button, for a site that already has a row of RSS and ATOM ones. Only the verdict fits; the numbers stay here.

agentmods 80×15 button for rag-pipeline

Your own site · 80×15
<a href="https://agentmods.dev/skills/bojieli/ai-agent-book/rag-pipeline"><img src="https://agentmods.dev/badge/skills/bojieli/ai-agent-book/rag-pipeline.svg" alt="Reviewed on agentmods" width="80" height="20"></a>
Per session 125 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 2,922 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. A grade says what 26 rules found in the file — not that it is safe.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5.1 $0.00125 $0.02922
Opus 5.5 $0.00050 $0.01169
Sonnet 5.5 $0.00025 $0.00584
Haiku 4.5 $0.00013 $0.00292

Measured 22d ago against content hash 69394a37b1bc, method: parsed. Prices are Anthropic first-party input rates as of 2026-10-07, from the pricing page.

Security

Grade A, and why

rag-pipeline scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 22d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

skills/rag-pipeline/SKILL.md · 81 lines

How it starts

The opening of the file, as written. The whole thing — 81 lines — stays where its author put it; the contents beside it link to each section on GitHub.

RAG 检索管道

何时使用

  • 从零搭建知识库检索(分块 → 索引 → 检索 → 注入生成)
  • 选型检索路线:稠密 vs 稀疏 vs 混合;ANN 索引 ANNOY vs HNSW
  • 检索质量不达标时定位调优(分块大小、融合方式、是否加重排序)
  • 建立检索质量的度量与验收标准
  • 判断某类查询为何召回失败(语义相似 vs 精确匹配)
  • 为记忆系统或知识组织层提供底层检索能力

核心原则

  • RAG 核心流程只有三步:检索相关片段 → 注入上下文 → LLM 基于上下文生成。检索器决定答案上限,生成器决定兑现程度;答不好先在检索层定位,别急着调 prompt。

  • RAG 的存在理由:模型训练数据有截止日期,知识库可随时更新——用外部知识库的广度和时效性补模型之短。

  • 系统构成就两块:检索器(从知识库找相关片段)+ 生成器(通常是 LLM,拿片段作上下文生成答案)。调优对象主要是检索器。

  • 分块必不可少,原因有二:嵌入模型有输入长度限制,整篇文档压成一个向量会多主题混杂、语义被稀释;检索的目标是只注入相关部分,块太大会连带引入无关内容、浪费窗口稀释注意力。

  • 稠密懂语义、稀疏懂字面,各有盲区:搜"HTTP-403",稀疏精确命中而稠密可能返回泛泛的"服务器错误";搜"kitty",稠密能找到只写"cat"的文档而稀疏不能。没有单一策略在所有场景可靠——生产级默认混合检索。

  • 分块有固有缺陷:任何切分策略都会切断块与原始上下文的联系("该公司收入增长了 3%"——哪家公司?哪个季度?)。要么在索引期为每块生成上下文前缀再索引(book/chapter3.md「RAG 技巧:上下文感知检索」),要么靠重排序补救。

  • 混合检索三阶段各司其职、层层递进:并行检索(双路各自召回)→ 结果融合(统一候选池)→ 神经重排序(候选池精排)。融合不替代重排,重排也不替代融合。

  • 两路得分不可直接比较:余弦相似度(0-1)与 BM25(0 到几十)量纲分布迥异,融合必须抛开原始得分。

  • 稀疏 vs 稠密的胜负判断(按查询类型选路或混路):

    查询类型 稠密 稀疏 结论
    同义/换说法(kitty vs cat) 胜 负 靠稠密
    精确代码/编号(HTTP-403) 易泛化 胜 靠稀疏
    技术术语、人名 一般 胜 靠稀疏
    多语言/跨语言查询 胜 负 靠稠密
    概念性、描述性问题 胜 一般 靠稠密

    混合检索 + 重排序是覆盖全部类型的生产默认。

实践模式

  • 分块策略选择:
    • 固定大小切分:按固定 token 数(如 512)切分,相邻块重叠 50-100 token 防关键句子被拦腰截断。简单可预测,但完全无视文档结构——段落、代码、表格都可能被截断,仅作基线。
    • 递归/结构感知切分:按章节标题→段落→句子的自然边界递归降级,Markdown/HTML 首选,生产系统最常用的默认选择。
    • 语义切分:计算相邻句子嵌入相似度,在语义"断崖"处下刀,块内主题尽量单一。质量更高,代价是额外嵌入计算。
    • 参数起点:每块 256-1024 token、重叠 10%-20%,再按检索质量实测调优。
    • 块大小的权衡没有免费午餐:太小则单块信息不完整、脱离上下文语义模糊;太大则多主题混杂、嵌入被稀释、命中后带入无关内容。
  • 稠密路线:用 BGE-M3 等上下文感知模型(768 维以上;Word2Vec 类静态词向量无法处理一词多义,BERT 类早期模型 512 token 上限不适合长文本)。余弦相似度比的是向量方向(语义)而非长度,内容相同长度不同的文档能正确判相似。
  • 嵌入空间可做语义运算:"国王" - "男性" + "女性" ≈ "女王"——语义关系以线性可计算的方式编码在向量空间中;同一词在不同语境获得不同向量("苹果公司" vs "两斤苹果"),实现从词汇级到语境级的飞跃。
  • ANN 索引选型:
    • ANNOY(树):构建快、内存低,但不支持增量更新(需完全重建)——适合不常变的静态数据集。
    • HNSW(图):支持增量插入、查询精度极高,但构建较慢、内存较高,长期增量后建议定期重建保精度——适合需实时索引新信息的动态场景。
    • 索引策略与嵌入模型同等重要,直接决定性能、成本和可维护性。
  • 稀疏路线:TF-IDF 的核心直觉是"词在当前文档出现越多、在语料中越少见,越重要"(IDF = ln(N/DF)),但原始词频线性增长且不校文档长度。BM25 的修正:k1 控制词频饱和(重复出现的边际贡献递减),b 控制长度归一化(常用 k1=1.5、b=0.75);IDF 换成 ln((N-DF+0.5)/(DF+0.5)),注意 DF > N/2 时取值为负,实现需设下限。倒排索引是"词→文档"的反向映射,如同书末术语索引页。
  • 分词是稀疏路线的前置质量项:数字(404、3.14、2.0.1)、代码(XK9-2B4-7Q1、API_KEY_123)、技术术语(C++、.NET、Node.js)、混合大小写(JavaScript、PyTorch)、邮箱、十六进制(#FF5733、0x1234)、缩写(API、HTTP)都要能正确切出,并去停用词。
  • 融合与重排:
    • RRF(倒数排名融合):得分 = Σ 1/(k+rank),k 常取 60 平滑头部差距。完全抛开原始得分、只看排名,简单稳健,但丢失得分中的相关性信号。
    • 神经重排序:对融合候选池前约 50 个用跨编码器(查询与文档拼接成一段文字逐词交互)精排,精度远高于双编码器(查询和文档各自编码再算向量相似度)的初筛。比喻:双编码器是猎头快速筛简历,跨编码器是面试官深谈。可用 bge-reranker-v2-m3。
    • 双编码器 vs 跨编码器是速度/精度的结构性取舍:初筛要覆盖海量数据必须快,精排只处理少量候选才舍得慢。
  • 度量与验收(均在带标注答案的测试查询集上计算):
    • recall@k:本书口径实为命中率(前 k 个结果里有任一篇相关即算命中);学术标准口径是相关文档召回比例,跨来源比较时需注意定义差异。这是最贴近 RAG 需求的指标——相关文档进入上下文,LLM 就有机会利用它。
    • MRR:每个查询取第一个相关文档排名的倒数再平均——排第 1 得 1 分,排第 10 只得 0.1 分,回答"找得够不够靠前"。
    • nDCG:综合考虑所有相关文档的排名与相关程度,回答"整个排序列表质量如何"。
    • 检索失败率:正确信息未出现在 top-20 结果中的查询比例。
  • 分阶段验收:流水线每一级(稠密、稀疏、融合、重排)都单独报告指标,才能定位是哪一级丢的召回。

Read the full file on GitHub · 81 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 22d ago First seen · 81 lines · 125 tokens per session scan A 69394a37b1bc

Subscribe to this mod's changes

rag-pipeline is a skill published in the GitHub repository bojieli/ai-agent-book (54,406 stars, last pushed 7d ago), licensed Apache-2.0. It adds 125 tokens to every session and 2,922 once invoked, about $0.0005 per session on Opus 5.5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-09-24.

Related

Other skills, from other repositories

mfs-ingest

Register, update, or re-sync data sources for MFS so they become searchable — postgres / mysql / mongo / snowflake / bigquery, github / jira / linear / notion / hubspot / zendesk, slack / discord / gmail / feishu, s3 / gdrive / web / file. Use whenever the user wants to ADD a new data source to MFS, change an existing…

zilliztech/mfs · 196 tokens

setup-open-index

Install and configure Open Index as the read/write context layer for a domain-specialized agent. Use when setting up a legal, marketing, customer support, sales, infrastructure, or other domain agent with Open Index over MCP.

DrDroidLab/open-index · 48 tokens

memory-rag-poisoning-detection

Detect persistent-memory poisoning and RAG/knowledge-base contamination via dialogue testing. Use when the agent has long-term memory, a user/preference store, or writes to a retrievable knowledge base.

Tencent/AI-Infra-Guard · 48 tokens

gpt-image-2

A skill for generating or editing images with GPT Image 2 across local, host-provided, or advisory setups.

ConardLi/garden-skills · 177 tokens

vector-and-embedding-weaknesses

Hunt vector / embedding weaknesses (OWASP LLM08:2025) — adversarial inputs against the RAG / similarity layer that cause cross-tenant leak, embedding-inversion privacy loss, semantic confusion, and retriever-driven prompt injection.

PurpleAILAB/Decepticon · 59 tokens

kb-retriever

A retrieval and question-answering assistant for a local folder of documents, including Markdown, text, PDFs, and spreadsheets.

ConardLi/garden-skills · 105 tokens