RAG 比你想象的更简单
RAG Is Simpler Than You Think

原始链接: https://www.lighthousenewsletter.com/p/rag-is-simpler-than-you-think

许多团队在构建 RAG(检索增强生成)技术栈时,往往默认选择复杂的向量数据库和嵌入技术,从而导致过度设计。与其盲目堆砌工具,不如根据数据时效性、语料库规模和查询模式等具体需求来构建系统。 作者建议采用分层方案: 1. **从 BM25(全文搜索)开始:** 它速度快、擅长精确匹配、无需分块,且没有重新索引的成本。 2. **通过查询重写进行增强:** 使用大模型(LLM)来优化用户输入。相比原始的语义搜索,这种方式处理行话和同义词的效果更好,且成本更低、更易于迭代。 3. **使用混合搜索:** 如果需要语义深度,可以将 BM25 与基于嵌入的重排序(Reranking)相结合。 4. **扩展策略:** * **即时处理(On-the-fly):** 适用于高频变动的数据(延迟:200–500 毫秒)。 * **冷热分层:** 适用于符合帕累托分布的访问模式。 * **完全预嵌入:** 仅适用于数据稳定且规模巨大的场景(每日 1 万次查询以上)。 **总结:** 大多数系统(60%)只需简单的全文搜索加查询重写即可满足需求。除非真正处于极大规模运行环境,否则请避开“5% 方案”(完全预嵌入)。在增加复杂性之前,务必先验证其必要性。

这场 Hacker News 上的讨论围绕“RAG 比你想象的更简单”这一论点展开,引发了关于检索增强生成(RAG)系统是否存在过度工程化的争论。 **讨论的核心要点包括:** * **全文检索(FTS)与向量嵌入:** 许多开发者认为全文检索被严重低估了。与基于向量的语义搜索相比,它通常更快、更具可移植性,且更易于调试。向量数据库的批评者指出,语义相似性的作用往往被夸大了,且伴随着复杂的运维难度。 * **“代理化”(Agentic)的转变:** 一种主流观点认为,搜索的最终目标是利用大语言模型(LLM)自动重写查询、执行迭代式的全文检索并分析结果。这种方法比纯概率性的嵌入搜索更具可解释性和可控性。 * **复杂性与简洁性:** 参与者警惕所谓“灵丹妙药”式的解决方案。成功的 RAG 并不取决于特定的技术(向量还是关键词),而在于数据规范、合理的切片处理以及确定性的编排。 * **对“AI 垃圾内容”的质疑:** 多位用户批评原文具有明显的“AI 生成”风格——引用了生硬、短促的句子和堆砌术语的标题。这表明社区正对缺乏深度技术内涵的 AI 生成内容感到疲劳。
相关文章

原文

Nowadays, most people seem to over-engineer their RAG stack. They jump straight to embeddings, vector databases, and reranking pipelines. Meanwhile, their users just want to find the doc that says “How to reset my password.”

In engineering, there’s always the right tool for the right problem. In AI Retrieval Systems it’s not different.

Before we dive into recipes, let’s establish when you should use each approach. The key factors are:

1. Data Freshness Requirements - Real-time updates (news, social media) favor approaches with easy re-indexing. Daily or weekly updates work well with hybrid approaches. A stable corpus (monthly or quarterly updates) makes pre-embedding sensible.

2. Corpus Characteristics - High churn (more than 10% changes daily) means you should avoid full pre-embedding. Stable documents work fine with pre-embedding. Long-tail distribution (90% never accessed) means on-the-fly wins.

3. Query Patterns - Keyword-heavy queries should start with full-text search. Semantic or conversational queries benefit from embeddings. Mixed patterns need hybrid approaches.

4. Scale & Performance - Less than 1000 queries per day means simple approaches are sufficient. 1K to 10K queries per day requires selective optimization. More than 10K queries per day justifies full optimization.

5. Team Capabilities - No ML expertise means stay with full-text plus query rewriting. Some ML experience makes hybrid search manageable. Having an ML team available makes advanced approaches viable.

Now, let’s look at the recipe book. Start at the top. Move down only when you have data proving you need to.

Good old BM25. Elasticsearch. Postgres full-text search. The stuff that existed before “embedding” became a verb.

You’re just starting out. Your users write keyword-style queries (”pandas merge dataframe”). Exact matches matter (”invoice #12345”). You want zero ML complexity. Your corpus has proprietary terminology (more on this later).

Zero API costs. Fast (under 10ms). Easy to debug (you can see exactly why a document matched). Surprisingly effective (handles many use cases). No chunking strategy needed – works with full documents. No evaluation complexity – easy to test and validate. No model deprecation risk (BM25 doesn’t change).

Misses synonyms (”car” vs “automobile”). Fails on semantic queries (”How do I...?”). Can’t understand intent beyond keywords.

In my experience, this handles a significant portion of use cases. Don’t skip this step. You might be surprised how far you can get.

When you jump straight to embeddings, you immediately face questions like: What chunk size? (512 tokens? 1024?) What overlap? (50 tokens? 100?) Semantic chunking or fixed-size? How do I evaluate if my chunking is good?

With full-text search, you skip all of this. Your documents are your documents. Search just works.

Use an LLM to transform messy user queries into clean keyword searches.

Most “semantic search” problems are actually query formulation problems.

Users ask questions conversationally. Vocabulary mismatch (users say “fix bugs”, docs say “debugging”). You have internal jargon (your framework called “Atlas”). You want flexibility to iterate quickly on query strategies.

~$0.001 per query (using GPT-4o-mini for query rewriting)

An LLM can remove stopwords (”how do I” becomes nothing). It can add synonyms (”car” becomes “car automobile vehicle”). It can translate domain terms (”speed up code” becomes “optimize performance”). It can decompose complex queries (”read CSV and plot” becomes [”read CSV”, “plot data”]). It can learn from your glossary (via system prompt).

With embeddings, if results aren’t good, you need to adjust chunking strategy, re-embed entire corpus, run regression tests on your eval set, and hope it improved.

With query rewriting, if results aren’t good, you adjust the system prompt. That’s it. Test immediately.

Even better, you can create a loop:

The agent can iterate, learn, and adapt – all without re-embedding anything.

Say your company has a Python framework called “Atlas.” If you use general-purpose embeddings:

General embedding model (trained on internet):
“Atlas” = [vectors pointing toward: Greek mythology, maps, geography]
Your actual Atlas docs = [vectors about data processing]
Similarity score: 0.15 (terrible!)

The model has no idea your “Atlas” exists. It falls back to what it learned in training. But with query rewriting:

For proprietary terms, exact keyword matching beats semantic understanding.

Use BM25 to get candidates (top 50-100), then rerank with embeddings (top 10).

BM25 is fast and great at keyword matching. Embeddings are good at semantic understanding. Together, they cover each other’s weaknesses.

Users ask semantic questions (”find alternatives to X”). BM25 plus query rewriting alone isn’t cutting it (you have data proving this). You can tolerate 100-500ms latency. Your corpus is relatively stable (not changing every minute).

Let’s do the math with current pricing (OpenAI text-embedding-3-small at $0.02 per 1M tokens):

  • Embedding 50 docs per query (avg 500 tokens each) means 50 docs × 500 tokens = 25,000 tokens

  • Cost: 25,000 × $0.00002 = ~$0.0005 per query. At 1,000 queries per day × 30 days = ~$15 per month.

Actually pretty reasonable. But there’s a catch: latency.

Embedding 50 documents on-the-fly adds 200-500ms per query. For user-facing search, that’s noticeable. This is where the real trade-off lives – not cost, but speed.

When you introduce embeddings, you need to decide how to chunk your documents (fixed-size? semantic? by section?). You need to determine what chunk size and overlap to use. You need to handle chunks that span important context.

This adds complexity that pure full-text search avoids.

If your data changes frequently, why pay to re-embed everything?

High document churn (more than 10% of docs updated daily). Real-time content (news, social media, live updates). You’re experimenting with embedding models (no re-indexing needed). Data freshness is critical (documents must be up-to-date). Small K for reranking (20-50 docs).

On-the-fly / online (1000 queries/day, 50 docs/query):
- Embedding cost: ~$15/month (ongoing)
- Storage: $0 (just store text)
- Latency: 200-500ms per query
- Freshness: Perfect (always current)
- Model switching: Easy (just change the API call)

Here’s something people don’t talk about enough: embedding models get deprecated.

OpenAI deprecated text-embedding-ada-002 in favor of text-embedding-3. If you pre-embedded 10 million documents with the old model, you now need to re-embed all 10 million documents with the new model, update your vector database, run regression tests on your evaluation set, validate that quality didn’t degrade, handle the cutover period, and deal with any API changes.

You literally just change one line of code. Done.

Latency. You’re embedding documents on every query. This is only viable if you’re okay with 200-500ms latency, K is small (reranking 20-50 docs, not 500), and your use case favors freshness over speed.

Pre-embed frequently accessed documents (”hot tier”), embed rarely-accessed documents on-the-fly (”cold tier”).

Access patterns follow Pareto distribution. 20% of docs get 80% of traffic.

Clear access patterns (some docs are accessed way more than others). Medium-to-large corpus (more than 100K documents). Mix of stable and changing content. Need good latency for common queries. Want to minimize re-embedding on model updates.

Fast for 80% of queries (hit pre-embedded cache). Fresh for rarely-accessed docs. Only re-embed hot tier when switching models (20% of corpus). Adapts to changing access patterns. Best latency/cost/flexibility trade-off.

When your embedding model gets deprecated:

Full pre-embedding: Re-embed 1M docs × $0.01 = $10,000 + downtime
Hot/cold tiers: Re-embed 200K docs × $0.01 = $2,000 + minimal downtime
On-the-fly: Change one line of code = $0 + zero downtime

Embed everything upfront. Store in vector database. Search with ANN (approximate nearest neighbors).

Very high query volume (more than 10K queries per day). Need under 50ms latency. Very stable corpus (under 5% churn per month). Access pattern is broad (no long tail). You have ML team to manage infrastructure.

Pre-embedding (1M docs):
- One-time embedding: 1M docs × 500 tokens × $0.00002 = $10
- Storage: 1M × 1536 dims × 4 bytes = 6GB (~$10-30/month)
- Search latency: under 50ms (blazing fast!)
- Freshness: Only as fresh as last re-index

Documents change frequently (more than 10% per week). You’re experimenting with embedding models. Low query volume (under 1K queries per day). You haven’t tried simpler approaches first.

This is where full pre-embedding hurts the most. When you need to switch models, you face downtime (your search is degraded while re-embedding), compute cost (re-embedding millions of documents), testing burden (full regression test suite on new embeddings), chunking reevaluation (maybe new model works better with different chunk sizes?), and risk (what if the new model is worse for your domain?).

This is overkill for most systems. I’ve seen teams spend months optimizing their vector database setup when query rewriting would have solved 90% of their problems.

But if you’re Pinterest, Shopify, or handling massive scale with a stable corpus, this is where you end up.

Here’s where things get spicy. We’ve been discussing single-intent queries: “How do I merge dataframes?”

But real users ask stuff like: “How do I read a CSV file, clean missing data, and plot the results?”

That’s three separate intents. Searching for this as one query is like trying to find a restaurant that serves pizza, sushi, and tacos. Good luck.

Modern agentic RAG systems (Perplexity, ChatGPT search) handle this elegantly:

Break down the query.

Route each sub-query optimally

Combine results into coherent answer

Each sub-query is focused and precise, leading to better retrieval. Parallel execution means lower latency (max, not sum). Adaptive routing results in lower cost (only complex queries pay for LLM). Structured output provides better UX.

Without decomposition

With decomposition

15x cheaper, better quality.

This is where agentic retrieval really shines. The agent can intelligently decide which sub-queries need expensive processing (embeddings) and which can be handled with cheap methods (simple preprocessing + BM25).

Okay, you’ve read this far. You just want to know: “What should I build?”

Start here: Do you have search at all? If not, build BM25 first. Seriously. Stop reading and build it. If you do have search, continue.

Measure your baseline. Run your current search for 2-4 weeks and collect user feedback. Are users happy with the results? If yes, stop. You’re done. Go ship features. If no, continue.

What’s the main complaint?

If users say “Can’t find docs that clearly exist,” try query rewriting first. At $0.001 per query with zero re-indexing, it’s worth testing. Run an A/B test for 2 weeks. If you see good improvement, keep it and you’re done. If it’s not enough, continue.

If users say “Results are okay but not great,” A/B test hybrid search (sparse plus embedding rerank). Is the added latency worth it? If yes, decide on implementation. If your data changes frequently, use on-the-fly embedding. If you have clear hot docs, use hot/cold tiers. If you have a stable corpus and high scale, use full pre-embedding. If the latency isn’t worth it, optimize query rewriting further instead.

If users say “Need better semantic understanding,” use hybrid search and choose your approach based on your situation. High churn (more than 10% per day) means on-the-fly. Medium scale with clear patterns means hot/cold tiers. Massive scale with stable data means full pre-embedding.

Key decision factors:

Full-text with query rewriting offers perfect data freshness with low setup complexity and query latency under 50ms. Model switching is trivial, no chunking is needed, and it works for most use cases.

On-the-fly embedding provides perfect data freshness with low setup complexity but higher query latency of 200-500ms. Model switching is trivial, chunking is needed, and it’s best for high churn scenarios.

Hot/cold tiers provide mixed data freshness with medium setup complexity and query latency of 50-100ms. Model switching is easy, chunking is needed, and it offers balanced performance for varied needs.

Full pre-embedding has stale data until reindex with high setup complexity but query latency under 50ms. Model switching is painful, chunking is needed, and it’s designed for massive scale operations.

The 80/20 rule: 60% of systems should stop at full-text plus query rewriting. 25% need hybrid with on-the-fly or hot/cold. 10% need full pre-embedding. 5% need custom solutions.

Bottomline: Don’t be the person who builds the 5% solution for a 60% problem.

联系我们 contact @ memedata.com