Modern NLP applications often require multi-step reasoning, interaction with external tools, and the ability to dynamically adapt to user queries. Haystack Agents, an innovative feature of the <a target="_blank" href="https://github.com/deepset-ai/haystack”>Haystack NLP Framework by <a target="_blank" href="https://www.deepset.ai/”>deepexemplifies this new wave of advanced NLP capabilities.
Haystack agents are designed to handle scenarios that require:
- Complex multi-step reasoning.
- Integration of external tools or API.
- Augmented recovery workflows that go beyond simply answering questions.
This article delves into the Haystack Agents framework, exploring its features, architecture, and real-world applications. To provide practical information, we will create a QA agent that uses tools such as a search engine and a calculator.
Why choose Haystack agents?
Unlike general purpose frameworks like LangChainHaystack agents are deeply integrated into the Haystack ecosystem, making them highly effective for specialized tasks such as document retrieval, custom tool integration, and multi-step reasoning. These agents excel at searching large data sets using advanced retrievers, expanding functionality by incorporating APIs for tasks such as calculations or database queries, and addressing complex queries that require logical deductions. Being open source and modular, Haystack Agents integrate seamlessly with popular machine learning frameworks and libraries such as elastic searchHugging Face models and pre-trained transformers.
Haystack Agent Architecture
Haystack agents are structured using a tool-based architecture. Here, the tools function as individual modules designed for specific tasks, such as document search, calculations, or API interactions. The agent dynamically determines which tools to use, the sequence of their use, and how to combine their results to generate a coherent response. The architecture includes key components such as tools, which execute specific action prompts that guide the agent's decision-making process. These retrievers make it easy to search for documents within large data sets, and nodes and pipelines manage data processing and workflow orchestration in Haystack.
Use case: Creating a QA agent with search tools and calculator
For this tutorial, our QA agent will do the following:
- Get answers to objective questions from a document repository.
- Perform mathematical calculations using a calculator tool.
- Dynamically combine results when necessary.
Step 1: Install prerequisites
Before you dive into deployment, make sure your environment is configured:
1. Install Python 3.8 or higher.
2. Install Haystack with all dependencies:
# bash
pip install farm-haystack(all)
3. Start Elasticsearch, the backbone of our document store:
# bash
docker run -d -p 9200:9200 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.17.1
Step 2: Initialize the Document Vault and Retriever
The document store is the central repository for storing and querying documents, while the retriever finds documents relevant to a given query.
# python
from haystack.utils import launch_es
from haystack.nodes import EmbeddingRetriever
from haystack.pipelines import DocumentSearchPipeline
from haystack.document_stores import ElasticsearchDocumentStore
# Launch Elasticsearch
launch_es()
# Initialize Document Store
document_store = ElasticsearchDocumentStore()
# Add documents to the store
docs = (
{"content": "Albert Einstein was a theoretical physicist who developed the theory of relativity."},
{"content": "The capital of France is Paris."},
{"content": "The square root of 16 is 4."}
)
document_store.write_documents(docs)
# Initialize Retriever
retriever = EmbeddingRetriever(
document_store=document_store,
embedding_model="sentence-transformers/all-MiniLM-L6-v2",
use_gpu=True
)
# Update embeddings
document_store.update_embeddings(retriever)
Step 3: define tools
Tools are the basic components of Haystack Agents. Each tool has a specific purpose, such as searching for documents or performing calculations.
# python
from haystack.agents.base import Tool
# Search Tool
search_pipeline = DocumentSearchPipeline(retriever)
search_tool = Tool(
name="Search",
pipeline_or_node=search_pipeline,
description="Use this tool for answering factual questions using a document store."
)
# Calculator Tool
def calculate(expression: str) -> str:
try:
result = eval(expression)
return str(result)
except Exception as e:
return f"Error in calculation: {e}"
calculator_tool = Tool(
name="Calculator",
pipeline_or_node=calculate,
description="Use this tool to perform mathematical calculations."
)
Step 4: Initialize the agent
Agents in Haystack are configured with tools and a notice template that defines how they interact with the tools.
# python
from haystack.agents import Agent
# Initialize Agent
agent = Agent(
tools=(search_tool, calculator_tool),
prompt_template="Answer questions using the provided tools. Combine results if needed."
)
Step 5: Consult the agent
Interact with the agent by asking questions in natural language.
# python
# Factual Question
response = agent.run("Who developed the theory of relativity?")
print("Agent Response:", response)
# Mathematical Calculation
response = agent.run("What is the result of 8 * (2 + 3)?")
print("Agent Response:", response)
# Combined Query
response = agent.run("What is the square root of 16, and who developed it?")
print("Agent Response:", response)
Advanced Features of Haystack Agents
- Custom tools: Integrate domain-specific APIs or tools to extend functionality (e.g. weather APIs, stock market data).
- Tuned models: Replace the default embedding model with one optimized for specialized tasks.
- Chained pipes: Use multiple pipelines to process complex queries involving multiple data sources.
In conclusion, Haystack Agents offers a powerful, flexible, and modular framework for building advanced NLP applications that require multi-step dynamic reasoning and tool usage. With their seamless integration into the Haystack ecosystem, these agents excel at tasks such as document retrieval, custom API integration, and logic processing, making them ideal for solving complex real-world problems. They are particularly suitable for applications such as customer service bots, which combine document search with external APIs for real-time ticket resolution, educational tools that retrieve information and perform calculations to answer user queries, and intelligence solutions. business that aggregate data from multiple sources and generate insights.
Sources
Also, don't forget to follow us on <a target="_blank" href="https://x.com/intent/follow?screen_name=marktechpost” target=”_blank” rel=”noreferrer noopener”>twitter and join our Telegram channel and LinkedIn Grabove. Don't forget to join our SubReddit over 65,000 ml.
<a target="_blank" href="https://nebius.com/blog/posts/studio-embeddings-vision-and-language-models?utm_medium=newsletter&utm_source=marktechpost&utm_campaign=embedding-post-ai-studio” target=”_blank” rel=”noreferrer noopener”> (Recommended Reading) Nebius ai Studio Expands with Vision Models, New Language Models, Embeddings, and LoRA (Promoted)
Sana Hassan, a consulting intern at Marktechpost and a dual degree student at IIT Madras, is passionate about applying technology and artificial intelligence to address real-world challenges. With a strong interest in solving practical problems, he brings a new perspective to the intersection of ai and real-life solutions.