Skip to main content
PromptQuorum
主页/本地LLM进阶/Haystack RAG框架2026:deepset出品,Python搭建生产级检索与问答系统
RAG & Document Chat

Haystack RAG框架2026:deepset出品,Python搭建生产级检索与问答系统

·12分钟阅读·Hans Kuepper 作者 · PromptQuorum创始人,多模型AI调度工具 · PromptQuorum

Haystack是德国公司deepset开发的免费开源(Apache 2.0)Python框架,通过显式、带类型的Component与Pipeline构件搭建生产级搜索与RAG管道——它不是一个托管产品。

Haystack比"RAG"这个词还要老。deepset在2019年将其打造为抽取式问答框架,那时距离检索增强生成成为让LLM基于自有文档回答问题的标准方法还有好几年。这种脱胎于搜索引擎的设计思路至今仍在:每个处理步骤都是一个带类型的Component,被显式连接进一个Pipeline——不是一串隐式调用。

Haystack RAG框架2026:deepset出品,Python搭建生产级检索与问答系统

关键要点

  • Apache 2.0许可证,可免费自托管——pip install haystack-ai,源码在github.com/deepset-ai/haystack
  • 由总部位于德国的deepset自2019年起开发,最初是抽取式问答与搜索框架——早于检索增强生成成为常用术语
  • 两个构件:Component(单个处理步骤:retriever、embedder、generator、converter)与Pipeline(通过显式.add_component()和.connect()调用连接的Component图)
  • Document Store是可替换的后端抽象——Haystack自带用于测试的内存存储,并集成Elasticsearch、Weaviate、Pinecone等向量数据库
  • 不绑定模型提供商:可与OpenAI、Anthropic、Mistral、Cohere、Hugging Face、Google、Azure OpenAI、AWS Bedrock集成
  • deepset还销售Haystack Enterprise Platform和Haystack Enterprise Starter——在同一开源核心之上叠加可视化管道构建器和托管部署的商业层

📍 简单一句话

Haystack是deepset开发的开源(Apache 2.0)Python框架,通过显式、带类型的Component与Pipeline抽象搭建生产级搜索与RAG管道,当前版本为3.1。

💬 简单来说

大多数LLM库靠链式调用函数,Haystack则要求把命名Component——retriever、prompt builder、generator——通过显式的.connect()调用接入Pipeline对象,数据流因此清晰可见、可测试,而不是藏在一条链里。

📌: 开源框架与deepset的商业产品是两回事:除非某节明确写出"Enterprise Platform",本评测只讨论免费、自托管的Apache 2.0代码。

Haystack是什么?

Haystack是一个开源Python框架(Apache 2.0,github.com/deepset-ai/haystack),用于构建搜索、问答和检索增强生成(RAG)应用。由总部位于德国的deepset维护,通过pip install haystack-ai安装。

  • 2019年以抽取式问答框架起步——在文档中定位精确的答案片段——早于生成式LLM让RAG成为主流模式
  • 围绕Haystack 2.0大幅重写为当前的Component/Pipeline架构,目前版本为3.1(2026年8月24日发布)
  • 提供文档转换(PDF、HTML、DOCX)、文本切分、embedding、retrieval(基于关键词的BM25与向量/语义)、生成与评估等Component
  • Document Store抽象将管道逻辑与存储后端解耦——无需重写管道即可将内存存储替换为Elasticsearch、Weaviate或Pinecone
  • 开箱自带内存文档存储,用于本地开发和测试,无需外部数据库即可上手
  • 同一管道内同时支持关键词retrieval(BM25)和向量/语义retrieval,还支持结合两者的混合retrieval

Haystack的Pipeline/Component架构如何运作?

Component是单个处理步骤(retriever、embedder、generator等),Pipeline是通过显式.add_component()和.connect()调用连接起来的Component有向图,步骤之间的数据流在代码中清晰可见,而不是藏在链对象里。

  • Pipeline可序列化为YAML——用Python构建的pipeline可以保存、纳入版本管理,并在不重新运行构建代码的情况下重新加载
  • 分支与合并是图模型的原生能力——一个retriever的输出可以同时喂给两个不同的generator,或两个retriever可以同时喂给一个ranker,无需额外的胶水代码
  • 由于连接是显式的,出错的pipeline(类型不匹配、缺少连接)会在pipeline构建阶段就报出清晰的错误,而不是在运行时深藏在调用栈中才失败

Haystack与LangChain、LlamaIndex有何不同?

Haystack、LangChain和LlamaIndex都是code-first的Python框架,开源核心中都没有可视化构建器——区别在于各自的核心抽象。Haystack围绕由带类型Component组成的显式Pipeline图组织一切;LlamaIndex围绕在你的数据上构建的Index组织,Retriever和QueryEngine对象位于其上;LangChain围绕chain组织,其LangGraph扩展则围绕面向agent的state graph组织。

如何搭建第一个Haystack管道?

一个最小的RAG管道需要三个相连的Component:一个retriever、一个prompt builder和一个generator,把它们接入Pipeline对象并用查询运行。

  1. 1
    安装包:pip install haystack-ai。
  2. 2
    导入所需模块:from haystack import Pipeline, Document; from haystack.components.generators.chat import OpenAIChatGenerator; from haystack.components.retrievers import InMemoryBM25Retriever; from haystack.document_stores.in_memory import InMemoryDocumentStore; from haystack.components.builders import ChatPromptBuilder; from haystack.utils import Secret。
  3. 3
    创建document store并写入文档:document_store = InMemoryDocumentStore(),然后document_store.write_documents([Document(content="...")​, ...])。
  4. 4
    创建各Component:retriever = InMemoryBM25Retriever(document_store=document_store)、prompt_builder = ChatPromptBuilder(template=prompt_template, required_variables="*")、llm = OpenAIChatGenerator(api_key=Secret.from_env_var("OPENAI_API_KEY"), model="gpt-4o-mini")。
  5. 5
    组装管道:rag_pipeline = Pipeline(),然后rag_pipeline.add_component("retriever", retriever)、rag_pipeline.add_component("prompt_builder", prompt_builder)、rag_pipeline.add_component("llm", llm)。
  6. 6
    连接各步骤:rag_pipeline.connect("retriever", "prompt_builder.documents") 和 rag_pipeline.connect("prompt_builder", "llm")。
  7. 7
    运行:results = rag_pipeline.run({"retriever": {"query": question}, "prompt_builder": {"question": question}})。
python
from haystack import Pipeline, Document
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.components.retrievers import InMemoryBM25Retriever
from haystack.document_stores.in_memory import InMemoryDocumentStore
from haystack.components.builders import ChatPromptBuilder
from haystack.utils import Secret

document_store = InMemoryDocumentStore()
document_store.write_documents([
    Document(content="My name is Jean and I live in Paris."),
    Document(content="My name is Mark and I live in Berlin."),
    Document(content="My name is Giorgio and I live in Rome."),
])

retriever = InMemoryBM25Retriever(document_store=document_store)
prompt_builder = ChatPromptBuilder(template=prompt_template, required_variables="*")
llm = OpenAIChatGenerator(api_key=Secret.from_env_var("OPENAI_API_KEY"), model="gpt-4o-mini")

rag_pipeline = Pipeline()
rag_pipeline.add_component("retriever", retriever)
rag_pipeline.add_component("prompt_builder", prompt_builder)
rag_pipeline.add_component("llm", llm)
rag_pipeline.connect("retriever", "prompt_builder.documents")
rag_pipeline.connect("prompt_builder", "llm")

results = rag_pipeline.run({"retriever": {"query": question}, "prompt_builder": {"question": question}})

试用Haystack需要OpenAI API密钥吗?

上面的示例使用OpenAIChatGenerator,但Haystack还提供Hugging Face、Anthropic、Mistral、Cohere、Google、Azure OpenAI、AWS Bedrock的generator Component,以及本地模型集成——不会被绑定在某一个模型提供商上。

入门需要向量数据库吗?

不需要。InMemoryDocumentStore无需任何外部服务,足以在本地搭建和测试管道。只有迁移到生产规模时才需要换成Elasticsearch、Weaviate或Pinecone。

Haystack适合哪些人?

Haystack适合需要对生产级搜索或RAG管道保持显式控制、并且熟悉Python的团队——在开源核心层面,它不是无代码或可视化工具。

Haystack对比其他方案

以下三个框架都是开源、以Python为主并积极维护——选择取决于哪种核心抽象更符合你团队思考管道的方式。

工具
核心抽象
许可证
最适合
Haystack带类型Component组成的PipelineApache 2.0生产级搜索/可衡量的RAG
LlamaIndexIndex + Retriever + QueryEngineMIT快速数据摄取/索引
LangChainChain / LangGraph state graphMIT通用LLM应用与agent胶水代码

评估Haystack时的常见误区

这些误区源于把Haystack当成托管SaaS产品,或当成另一个框架抽象的直接替代品。

常见问题

Haystack是什么?

Haystack是总部位于德国的deepset开发的开源Python框架,用于搜索、问答和RAG管道。许可证为Apache 2.0,通过pip install haystack-ai安装。

谁开发了Haystack?

总部位于德国的deepset。deepset于2019年启动Haystack,当时是抽取式问答框架,早于检索增强生成成为常用术语。

Haystack采用什么许可证发布?

Apache License 2.0。源代码位于github.com/deepset-ai/haystack,可自由使用、修改和自托管。

Haystack目前的版本是多少?

Haystack 3.1,于2026年8月24日发布。该框架围绕Haystack 2.0进行了大幅重写,形成了当前的Component/Pipeline架构。

Haystack中Component和Pipeline有什么区别?

Component是单个处理步骤——retriever、embedder、prompt builder或generator。Pipeline是通过显式.add_component()和.connect()调用连接起来的Component图;运行pipeline会按依赖顺序遍历整个图。

Haystack与LlamaIndex有何不同?

Haystack围绕接入Pipeline图的显式、带类型Component组织管道。LlamaIndex围绕在你的数据上构建Index、再通过Retriever和QueryEngine查询来组织。开始索引数据通常LlamaIndex更快;Haystack的显式图为生产管道提供了更高的可见性和控制力。

Haystack与LangChain有何不同?

两者都是code-first的Python框架,开源核心中都没有可视化构建器。LangChain围绕chain组织,并通过LangGraph提供面向agent的state graph。Haystack围绕由带类型Component组成的Pipeline组织,将retrieval和搜索当作一等、可衡量的关切,而不是通用chain中的一环。

Haystack需要特定的向量数据库吗?

不需要。Haystack为开发环境自带内存Document Store,并通过同一Component接口集成多种向量数据库和搜索后端,包括Elasticsearch、Weaviate和Pinecone——切换后端无需重写管道。

deepset的Haystack Enterprise Platform是什么?

这是deepset在开源Haystack框架之上构建的付费商业产品,增加了可视化管道构建器以及托管或自托管的部署选项。它与本评测所讨论的免费、自托管、Apache 2.0开源框架是分开的。

资料来源

← 返回 本地LLM进阶