“把你的文档变成一个可以对话的知识库。”——Haystack,开源世界中的 RAG 工程利器。
在大模型(LLM)快速发展的今天,如何让模型“理解”我们自己的私有数据,成为了每个开发者、企业、团队急需解决的问题。这正是 RAG(Retrieval-Augmented Generation)技术的核心目标。而在开源领域,Haystack 是构建 RAG 应用最成熟、最强大的框架之一。
Haystack 是一个用于构建 LLM 应用、语义搜索系统和问答系统的 Python 框架,由德国 AI 公司 deepset 开发并开源。
它最初专注于 问答系统(QA over documents),而现在已经全面支持:
GitHub: https://github.com/deepset-ai/haystack
官网: https://haystack.deepset.ai
Haystack 的架构设计以 可组合性 + 插件化 为核心,主要模块包括:
┌────────────────────┐
│ Pipelines │
└────────┬───────────┘
│
┌─────────────────┼────────────────┐
▼ ▼ ▼
┌────────────┐ ┌──────────────┐ ┌────────────┐
│ Retriever │ │ Reader / LLM │ │ PromptNode │
└────────────┘ └──────────────┘ └────────────┘
▲ ▲ ▲
│ │ │
┌────────────┐ ┌────────────┐ ┌────────────┐
│ Document │ │ Prompt │ │ Agent / │
│ Store / DB │ │ Templates │ │ ToolChain │
└────────────┘ └────────────┘ └────────────┘
Retriever
: 向量检索模块(FAISS、Weaviate 等)Reader
: 文本阅读器(BERT、LLM 等)PromptNode
: 对接 LLM(支持 OpenAI、Cohere、Llama2 等)Pipeline
: 将各模块串联构建工作流DocumentStore
: 文档数据库(支持 Elasticsearch、FAISS、Milvus、Postgres 等)Haystack 支持 pip 安装,也可以用 Docker 运行:
pip install farm-haystack[all]
或者:
docker run -v $(pwd):/app deepset/haystack-cpu:latest
from haystack.document_stores import InMemoryDocumentStore
from haystack.nodes import EmbeddingRetriever, PromptNode
from haystack.pipelines import Pipeline
from haystack.utils import clean_wiki_text
# 1. 加载文档
docs = [
{"content": "Haystack is a powerful open-source framework."},
{"content": "RAG stands for Retrieval-Augmented Generation."}
]
# 2. 初始化 Document Store
document_store = InMemoryDocumentStore(use_bm25=True)
document_store.write_documents(docs)
# 3. 初始化 Retriever
retriever = EmbeddingRetriever(document_store=document_store, embedding_model="sentence-transformers/all-MiniLM-L6-v2")
document_store.update_embeddings(retriever)
# 4. 使用 PromptNode 构建 LLM 回答器
prompt_node = PromptNode("gpt-3.5-turbo", api_key="你的OpenAI Key")
# 5. 构建 Pipeline
pipe = Pipeline()
pipe.add_node(component=retriever, name="Retriever", inputs=["Query"])
pipe.add_node(component=prompt_node, name="Generator", inputs=["Retriever"])
# 6. 问问题
res = pipe.run(query="What is Haystack?")
print(res["Generator"]["answers"][0])
Haystack 最大的优势之一是可组合的 Pipeline
构建方式,适合构建如下流程:
支持 YAML 编排,也支持 Python 代码动态构建。
pipelines:
- name: my_pipeline
nodes:
- name: Retriever
type: EmbeddingRetriever
inputs: [Query]
- name: Reader
type: FARMReader
inputs: [Retriever]
使用 PromptNode
你可以直接调用:
支持链式提示、模板、参数化提示设计:
prompt_node = PromptNode("gpt-4", api_key="your-key", default_prompt_template="question-answering")
Haystack 支持多种向量数据库作为 DocumentStore
:
示例:
from haystack.document_stores import FAISSDocumentStore
document_store = FAISSDocumentStore(embedding_dim=768)
特性 | Haystack | LangChain |
---|---|---|
架构 | 模块化、流程图风格 | 链式调用、Prompt链 |
可视化 | ✅ 支持 REST UI + Grafana | ❌ 需自建 |
文档支持 | ✅ 文档处理完整(PDF、OCR) | ❌ 依赖第三方 |
LLM 支持 | ✅ 支持主流 LLM | ✅ 支持更多 LLM |
社区 | 强企业级、德国支持 | 社区活跃、生态丰富 |
总结:Haystack 更注重企业级部署和稳定性,LangChain 更灵活更实验性强。
Haystack 是目前构建基于私有数据的问答系统、知识检索系统、RAG 应用中最稳健、最易扩展的开源框架之一。
无论你是做 AI 产品、企业知识库、还是 AI 辅助客服,Haystack 都值得你认真了解和尝试!
官方链接: