सरल भाषा में
Haystack deepset का एक open source Python framework है, जो लार्ज लैंग्वेज मॉडल्स से चलने वाले प्रोडक्शन-रेडी ऐप्स बनाने के लिए है। इसका मूल विचार pipeline है: Haystack में बनाया गया हर LLM ऐप retrievers, generators, rankers, prompt builders और routers जैसे components का एक स्पष्ट, जाँचा जा सकने वाला graph होता है, जो typed connections से जुड़े होते हैं। कुछ भी अंधेरे में नहीं चलता; आप हर हिस्से को देख, टेस्ट और बदल सकते हैं।

यहाँ एक उदाहरण है। कार की असेंबली लाइन सोचिए। हर स्टेशन का एक ही काम होता है — चेसिस वेल्ड करना, इंजन लगाना, बॉडी पेंट करना — और स्टेशन एक conveyor belt से जुड़े रहते हैं ताकि एक का output अगले का input बन जाए। Haystack pipelines भी इसी तरह काम करते हैं। एक retriever स्टेशन आपके knowledge base से प्रासंगिक documents निकालता है, एक prompt-builder स्टेशन उन्हें template में ढालता है, और एक generator स्टेशन नतीजा LLM को भेजकर जवाब लौटाता है। आप किसी भी स्टेशन को जाँच सकते हैं, उसे किसी दूसरे supplier से बिना रुके बदल सकते हैं, या लाइन में कहीं भी नया quality-check स्टेशन जोड़ सकते हैं।
Haystack को deepset ने बनाया है, जो बर्लिन स्थित एक AI कंपनी है और enterprise NLP तथा RAG सिस्टम पर केंद्रित है। वर्तमान major version Haystack 2.x है, जो 2024 में जारी एक पूरी तरह नया डिज़ाइन था। इसने पहले के document-centric API की जगह explicit directed-graph pipeline model अपनाया, जो अब framework की पहचान है। इसे इंस्टॉल करने के लिए pip install haystack-ai चलाएँ।
यह क्यों मायने रखता है
ज़्यादातर LLM frameworks Python में calls को chain करके ऐप जल्दी बनाने देते हैं — लेकिन वे इन calls के अंदर क्या हो रहा है, यह छिपा देते हैं। डेमो के लिए यह अदृश्यता ठीक है, पर production सिस्टम में यह तकलीफ़देह है। जवाब बिगड़ने पर कौन सा step टूटा? अगर retriever algorithm को किसी नए algorithm से बदलना हो, तो असंबंधित code छुए बिना यह कैसे होगा? और अगर पूरी pipeline को serialize करके दूसरे environment में भेजना और बिल्कुल वैसे ही reload करना हो, तो क्या यह संभव भी है?
Haystack का जवाब है कि pipeline को ऐप का पहले दर्जे का हिस्सा बनाया जाए। हर component — चाहे built-in InMemoryEmbeddingRetriever हो या आपकी लिखी custom class — के घोषित, typed input और output sockets होते हैं। code चलने से पहले framework जाँचता है कि components के बीच connections type-compatible हैं। पूरी pipeline को YAML या JSON में serialize करके disk पर सेव किया जा सकता है, version control में diff किया जा सकता है, और बिना किसी code बदलाव के एक नए Python process में reload किया जा सकता है।
यह तीन production स्थितियों में सबसे ज़्यादा मायने रखता है: RAG systems, जहाँ retriever strategies की तुलना करनी हो और यह मापना हो कि कौन सी सच में जवाब की गुणवत्ता बढ़ाती है; multi-step pipelines, जहाँ queries को शर्तों के आधार पर route करना, steps को parallel चलाना और हर चरण पर outputs verify करने हों; और teams, जहाँ अलग-अलग engineers अलग-अलग pipeline components संभालते हैं और एक-दूसरे के काम में दखल से बचने के लिए साफ़ सीमाएँ चाहिए। Haystack का explicit model उन अदृश्य साइड इफ़ेक्ट्स को दिखने वाले, टेस्ट किए जा सकने वाले contracts में बदल देता है।
यह कैसे काम करता है
Haystack हर ऐप को components का एक directed multigraph मानता है। Pipeline object में nodes (components) और edges (उनके output sockets और input sockets के बीच connections) होते हैं। runtime पर framework topological order में graph पर चलता है और nodes के बीच typed data पास करता है। अगर कोई connection type-incompatible है, तो Haystack wiring के समय ही error देता है — यह नहीं कि जब कोई user देख रहा हो, तब runtime पर।
Components और sockets
component कोई भी Python class है जिसे @component से decorate किया गया हो। उसमें run() method होना ज़रूरी है, जो named inputs लेता है और named outputs का dictionary लौटाता है। @component.output_types decorator उन outputs के नाम और types घोषित करता है, ताकि framework connections को static रूप से validate कर सके। हर built-in — SentenceTransformersTextEmbedder, InMemoryEmbeddingRetriever, ChatPromptBuilder, OpenAIChatGenerator, BM25Retriever, और दर्जनों अन्य — ठीक इसी contract का पालन करता है। आपका लिखा कोई भी custom component भी यही करता है।
from haystack import component
@component
class WordCounter:
"""Counts words in a string — a trivial custom component example."""
@component.output_types(word_count=int)
def run(self, text: str) -> dict:
return {"word_count": len(text.split())}Pipeline बनाना और चलाना
आप Pipeline बनाते हैं, components को नाम से जोड़ते हैं, फिर connect() call करके output sockets को input sockets से wire करते हैं। string notation "component_name.socket_name" है। अंत में pipeline.run() graph चलाता है और सभी component outputs का dictionary लौटाता है। async variant, AsyncPipeline, अलग-अलग branches को अपने आप parallel में चलाता है — इसके लिए अतिरिक्त code की ज़रूरत नहीं।
from haystack import Pipeline
from haystack.components.embedders import SentenceTransformersTextEmbedder
from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
from haystack.components.builders import ChatPromptBuilder
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.dataclasses import ChatMessage
from haystack.document_stores.in_memory import InMemoryDocumentStore
# Build the document store and add documents first (omitted for brevity)
document_store = InMemoryDocumentStore()
# Assemble the pipeline
rag = Pipeline()
rag.add_component("embedder", SentenceTransformersTextEmbedder())
rag.add_component("retriever", InMemoryEmbeddingRetriever(document_store=document_store))
rag.add_component("prompt", ChatPromptBuilder(template=[
ChatMessage.from_user(
"Answer using only these documents:\n{% for d in documents %}{{ d.content }}{% endfor %}\n\nQuestion: {{ query }}"
)
]))
rag.add_component("llm", OpenAIChatGenerator(model="gpt-5.5"))
# Wire the graph
rag.connect("embedder.embedding", "retriever.query_embedding")
rag.connect("retriever.documents", "prompt.documents")
rag.connect("prompt.prompt", "llm.messages")
# Run it
result = rag.run({"embedder": {"text": "What is the refund window?"}, "prompt": {"query": "What is the refund window?"}})
print(result["llm"]["replies"][0].text)Document stores और indexing pipeline
Retrieve करने से पहले ingest करना ज़रूरी है। Haystack indexing pipelines (जो raw files पढ़ती हैं, उन्हें chunks में बाँटती हैं, embed करती हैं और document store में लिखती हैं) को querying pipelines (जो user का सवाल embed करती हैं और प्रासंगिक chunks लाती हैं) से अलग रखता है। document store दोनों के बीच साझा state है। Built-in stores में local development के लिए InMemoryDocumentStore है, और production-scale backends के लिए integrations हैं: OpenSearch, Elasticsearch, Weaviate, Qdrant, Pinecone, pgvector और अन्य — हर एक haystack-ai integrations registry से installable package के रूप में उपलब्ध है।
Haystack बनाम LangChain: कब कौन सा बेहतर है
Haystack और LangChain दो सबसे व्यापक रूप से इस्तेमाल होने वाले Python LLM frameworks हैं, और दोनों की सोच वास्तव में अलग है। नए प्रोजेक्ट के लिए चुनते समय इस trade-off को समझने से समय बचता है।
- स्पष्ट pipeline graph
- Typed, जाँचे गए connections
- बिल्ट-इन YAML/JSON serialization
- Evaluation components शामिल
- कोई भी node अलग से बदलें
- शुरुआती wiring की लागत ज़्यादा
- Chain और agent abstractions
- विशाल integration catalog
- LCEL से तेज़ prototyping
- LangSmith से evaluation
- लचीला, पर अस्पष्ट data flow
- पहले 30 मिनट आसान
Haystack का explicit pipeline model ज़्यादा शुरुआती wiring माँगता है — हर component का नाम देना और हर socket को जोड़ना पड़ता है। यह झंझट तब फ़ायदा देता है जब आपको retriever algorithm बदलना हो, re-ranker का A/B टेस्ट करना हो, या pipeline YAML किसी गैर-engineer को configure करने के लिए देनी हो। serialized YAML आपके ऐप का पूरा, इंसान के पढ़ने लायक विवरण है: कौन सा model, कौन सा retriever, कौन सा prompt template, कौन सा document store। इसलिए Haystack enterprise teams के लिए, जिन्हें auditability चाहिए, और RAG-heavy workloads के लिए मज़बूत विकल्प है, जहाँ व्यवस्थित evaluation मायने रखता है।
LangChain की integrations की विस्तृत श्रृंखला और LangChain Expression Language (LCEL) की सहजता पहले सत्र में कुछ चालू करना तेज़ बना देती है। जिन टीमों को जटिल, multi-step agentic reasoning चाहिए — जहाँ agent runtime पर तय करता है कि आगे क्या करना है, न कि एक निश्चित graph का पालन करता है — उन्हें अक्सर LangChain के agent primitives ज़्यादा सुविधाजनक लगते हैं। कोई भी framework हर स्थिति में बेहतर नहीं है; दोनों समस्या के अलग-अलग हिस्सों के लिए अनुकूलित हैं।
| मापदंड | Haystack चुनें | LangChain चुनें |
|---|---|---|
| मुख्य उपयोग | RAG, document search, संरचित pipelines | Agents, chains, विविध tool orchestration |
| टीम का आकार | कई engineers, components की स्पष्ट ज़िम्मेदारी | अकेले या छोटी टीम, जो तेज़ी से आगे बढ़ रही हो |
| Auditability | pipelines को serialize, diff और version करना ज़रूरी | प्राथमिकता कम |
| Evaluation | pipeline के भीतर built-in RAG metrics चाहिए | LangSmith या बाहरी evaluation इस्तेमाल |
| Data flow | स्पष्ट, typed, जाँचा हुआ | अस्पष्ट, लचीला, duck-typed |
Agents, tools और async pipelines
Haystack 2.x ने अपनी पारंपरिक pipelines के साथ एक पूरा agent system जोड़ा। Agent component LLM को tool-calling loop में लपेटता है: वह model को कॉल करता है, जाँचता है कि model कोई tool चलवाना चाहता है या नहीं, उस tool को चलाता है, नतीजा वापस देता है, और तब तक loop करता है जब तक model अंतिम जवाब न दे दे। यह वही ReAct-style loop है जो आपको दूसरे agent frameworks में मिलता है, पर इसे एक first-class Haystack component के रूप में व्यक्त किया गया है, जिसे किसी बड़ी pipeline के भीतर embed किया जा सकता है।
Tools Agent को तीन प्रकारों में से किसी के रूप में दिए जाते हैं: ComponentTool एक single Haystack component (retriever, API caller, calculator) को लपेटता है, ताकि model उसे नाम से कॉल कर सके; PipelineTool पूरी Haystack pipeline को एक कॉल किए जाने योग्य tool के रूप में लपेटता है, जिससे आप जटिल multi-step tools बना सकते हैं, बिना उनके internals को model के सामने उजागर किए; और MCPTool / MCPToolset किसी भी Model Context Protocol server से जुड़ते हैं और बढ़ते MCP ecosystem से बाहरी tools लोड करते हैं।
from haystack.components.agents import Agent
from haystack.components.tools import PipelineTool
from haystack.components.generators.chat import OpenAIChatGenerator
# Wrap an existing retrieval pipeline as one callable tool
retrieval_tool = PipelineTool(
name="search_knowledge_base",
description="Searches company documents and returns relevant passages.",
pipeline=rag, # the pipeline we built earlier
input_mapping={"query": "embedder.text"},
output_mapping={"llm.replies": "results"},
)
agent = Agent(
chat_generator=OpenAIChatGenerator(model="gpt-5.5"),
tools=[retrieval_tool],
)
response = agent.run(messages=[ChatMessage.from_user("What is our refund policy?")])
print(response["messages"][-1].text)जिन pipelines में स्वतंत्र components एक-दूसरे के outputs पर निर्भर नहीं होते, वहाँ AsyncPipeline (Pipeline की drop-in replacement) अपने आप पहचानता है कि कौन सी branches एक साथ चल सकती हैं और उन्हें parallel में चलाता है। उदाहरण के लिए, एक indexing pipeline जो text embedding और image embedding को parallel में चलाती है, दोनों को एक साथ पूरा करती है, क्रमिक रूप से नहीं — और component code में कोई बदलाव नहीं करना पड़ता, केवल इस्तेमाल होने वाली pipeline class बदलनी पड़ती है।
आगे गहराई में
एक बार बुनियादी pipeline चल जाए, तो production की चिंताएँ iteration पर हावी होने लगती हैं। कुछ पैटर्न जिन्हें अनुभवी Haystack टीमें अपनाती हैं:
Hybrid retrieval और re-ranking
सिर्फ़ vector search उन queries में चूक जाता है जो सटीक शब्दों पर टिकी हों — product codes, नाम, version numbers। Hybrid retrieval BM25 keyword retriever को embedding retriever के साथ जोड़ता है, फिर document joiner से नतीजों को मिलाता है। एक re-ranker (SentenceTransformersDiversityRanker या cross-encoder ranker) को आगे वाले component के रूप में जोड़ने से मिली हुई list को query के प्रति प्रासंगिकता के आधार पर फिर से score किया जाता है, इससे पहले कि वह prompt तक पहुँचे। व्यवहार में, hybrid + re-rank वास्तविक दुनिया के question-answering benchmarks पर किसी भी अकेली pure strategy से लगातार बेहतर प्रदर्शन करता है।
Conditional routing
हर query को एक ही pipeline path की ज़रूरत नहीं होती। Haystack MetadataRouter, ConditionalRouter और FileTypeRouter जैसे router components देता है, जो component outputs जाँचते हैं और data को अलग-अलग branches की ओर भेजते हैं। एक router तथ्यात्मक सवाल RAG branch को, गणना वाले सवाल code interpreter branch को, और विषय से बाहर के queries rejection branch को भेज सकता है — यह सब एक pipeline object के भीतर। इससे वह if/else का उलझा जाल खत्म हो जाता है जो अन्यथा application code में बिखरा रहता।
Hayhooks: pipelines को HTTP endpoints के रूप में deploy करना
Hayhooks एक आधिकारिक open source companion package है, जो किसी भी Haystack pipeline को HTTP server में लपेटता है। इसे pipeline YAML file की ओर इंगित करें, और बहुत कम configuration में आपको auto-generated OpenAI-compatible /chat/completions endpoints और streaming support मिल जाता है। बिना custom FastAPI wrapper लिखे production में Haystack pipelines serve करने का यह अनुशंसित तरीका है।
| उन्नत तकनीक | Haystack component | यह किस समस्या को हल करता है |
|---|---|---|
| Hybrid retrieval | InMemoryBM25Retriever + InMemoryEmbeddingRetriever + DocumentJoiner | सटीक शब्द और semantic चूक |
| Re-ranking | SentenceTransformersDiversityRanker, cross-encoders | खराब क्रम वाले retrieval नतीजे |
| Conditional routing | ConditionalRouter, MetadataRouter | अलग query प्रकारों को अलग रास्ते चाहिए |
| समानांतर execution | AsyncPipeline (drop-in) | स्वतंत्र steps एक-दूसरे को रोकते हैं |
| Pipeline-as-tool | PipelineTool | Agent को एक multi-step प्रक्रिया कॉल करनी है |
| HTTP deployment | Hayhooks package | custom API code के बिना pipelines serve करना |
सबसे गहरा निवेश व्यवस्थित evaluation में है। Haystack के built-in evaluators आपको pipeline के हिस्से के रूप में context relevance (क्या retriever को सही passages मिले?) और faithfulness (क्या जवाब जो मिला उस पर आधारित है?) मापने देते हैं। pipeline output से evaluator जोड़ना और हर run के scores log करना वह तरीका है जिससे टीमें "लगता है काम कर रहा है" से "हमारे पास एक संख्या है, वह सुधरी है, और हम इसे साबित कर सकते हैं" तक पहुँचती हैं। LLMOps observability tooling के साथ मिलाकर, यही प्रोटोटाइप और ऐसे सिस्टम के बीच का फ़र्क है जो समय के साथ बेहतर होता जाता है।
अक्सर पूछे जाने वाले प्रश्न
RAG ऐप्स बनाने के लिए Haystack और LangChain में क्या फ़र्क है?
दोनों RAG ऐप्स बनाते हैं, पर Haystack एक explicit, typed pipeline graph इस्तेमाल करता है जहाँ हर component और connection code चलने से पहले घोषित और validate होता है। LangChain chains और agents इस्तेमाल करता है, जिनमें data flow ज़्यादा implicit होता है। auditability, serialization और built-in RAG evaluation में Haystack आगे है; integrations की विविधता और जटिल agent tasks के तेज़ prototype में LangChain आगे है।
क्या Haystack मुफ़्त और open source है?
हाँ। Haystack Apache 2.0 license के तहत open source है और deepset द्वारा maintain किया जाता है। इसे इंस्टॉल करने के लिए pip install haystack-ai चलाएँ। deepset managed pipeline hosting के लिए deepset Cloud नाम का एक commercial platform भी देता है, लेकिन core framework पूरी तरह मुफ़्त है।
Haystack component क्या है और custom component कैसे लिखें?
component कोई भी Python class है जिसे @component से decorate किया गया हो और जिसमें run() method लागू हो। @component.output_types decorator outputs के नाम और types घोषित करता है, ताकि framework connections validate कर सके। इस contract का पालन करने वाली कोई भी class built-in components के साथ pipeline में जोड़ी जा सकती है — किसी subclassing या registration की ज़रूरत नहीं।
क्या Haystack pipelines को serialize और version किया जा सकता है?
हाँ — YAML serialization एक मूल feature है। pipeline.dumps() call करें और हर component, उसके parameters और हर connection दर्शाने वाली YAML string मिलेगी। Pipeline.loads(yaml_str) call करके इसे एक नए process में बिल्कुल वैसा ही दोबारा बनाया जा सकता है। pipeline YAMLs को git में commit करने से prompt template के बदलाव या model upgrades को भी उसी तरह diff और review किया जा सकता है जैसे code को।
क्या Haystack agents और tool calling सपोर्ट करता है?
हाँ। Haystack 2.x में Agent component है, जो LLM को tool-calling loop में लपेटता है। Tools एकल components (ComponentTool), पूरी pipelines (PipelineTool), या बाहरी MCP servers (MCPToolset) हो सकते हैं। Agents स्वयं components हैं, इसलिए उन्हें बड़ी pipelines के भीतर embed किया जा सकता है।
Haystack कौन से vector databases सपोर्ट करता है?
Haystack development और testing के लिए InMemoryDocumentStore के साथ आता है। Production integrations, जो अलग से installable packages के रूप में उपलब्ध हैं, OpenSearch, Elasticsearch, Weaviate, Qdrant, Pinecone, pgvector और अन्य को कवर करते हैं। हर integration एक document store और मेल खाते retriever components देता है, ताकि pipeline logic बदले बिना backend बदला जा सके।
