🆕 Haystack 2.30 is here! Pass a plain string to any ChatGenerator

Web QA with Mistral


Colab by Tuana Celik - ( LI & Twitter)

Quick guide to building Question Answering on the web with a Mistral AI model and Haystack. We use mistral-small-latest via the Mistral AI API and the MistralChatGenerator from the official Mistral integration for Haystack.

  1. Use the MistralChatGenerator to query the model on its own
  2. Add the generator to a full RAG Pipeline (on the web)

Screenshot 2023-12-13 at 17.46.33.png

Install dependencies

!uv pip install haystack-ai trafilatura sentence_transformers mistral-haystack
Resolved packages and installed haystack-ai trafilatura sentence_transformers mistral-haystack

Prompt the Model - Standalone

We are using the Mistral AI API with mistral-small-latest.

import os
from getpass import getpass

os.environ["MISTRAL_API_KEY"] = getpass("Enter Mistral API key: ")
from haystack_integrations.components.generators.mistral import MistralChatGenerator

generator = MistralChatGenerator(model="mistral-small-latest")
from haystack.dataclasses import ChatMessage

messages = [
    ChatMessage.from_system("\\nYou are a helpful, respectful and honest assistant"),
    ChatMessage.from_user("What's Natural Language Processing?")
]

result = generator.run(messages)
print(result["replies"][0].text)
**Natural Language Processing (NLP)** is a field of **artificial intelligence (AI)** that focuses on the interaction between computers and human (natural) languages. Its goal is to enable machines to **understand, interpret, generate, and respond to human language** in a way that is both meaningful and useful.

Use the Model in a full RAG pipeline (on the web)

Here, we will be using the same generator component as the above, in a full RAG pipeline. You can change this pipeline to use your own data source (such as a vector database, Notion, documentation) instead of the LinkContentFetcher we are using here.

from haystack.components.fetchers.link_content import LinkContentFetcher
from haystack.components.converters import HTMLToDocument
from haystack.components.preprocessors import DocumentSplitter
from haystack.components.rankers import SentenceTransformersSimilarityRanker
from haystack.components.builders.chat_prompt_builder import ChatPromptBuilder
from haystack.dataclasses import ChatMessage
from haystack import Pipeline

fetcher = LinkContentFetcher()
converter = HTMLToDocument()
document_splitter = DocumentSplitter(split_by="word", split_length=50)
similarity_ranker = SentenceTransformersSimilarityRanker(top_k=3)

prompt_template = """
According to these documents:

{% for doc in documents %}
  {{ doc.content }}
{% endfor %}

Answer the given question: {{question}}
Answer:
"""

prompt_template = [ChatMessage.from_user(prompt_template)]
prompt_builder = ChatPromptBuilder(template=prompt_template, required_variables="*")

pipeline = Pipeline()
pipeline.add_component("fetcher", fetcher)
pipeline.add_component("converter", converter)
pipeline.add_component("splitter", document_splitter)
pipeline.add_component("ranker", similarity_ranker)
pipeline.add_component("prompt_builder", prompt_builder)
pipeline.add_component("llm", generator)

pipeline.connect("fetcher.streams", "converter.sources")
pipeline.connect("converter.documents", "splitter.documents")
pipeline.connect("splitter.documents", "ranker.documents")
pipeline.connect("ranker.documents", "prompt_builder.documents")
pipeline.connect("prompt_builder.prompt", "llm")
<haystack.core.pipeline.pipeline.Pipeline object>
Components
  - fetcher: LinkContentFetcher
  - converter: HTMLToDocument
  - splitter: DocumentSplitter
  - ranker: TransformersSimilarityRanker
  - prompt_builder: ChatPromptBuilder
  - llm: MistralChatGenerator
question = "What do graphs have to do with Haystack?"
result = pipeline.run({"prompt_builder": {"question": question},
                   "ranker": {"query": question},
                   "fetcher": {"urls": ["https://haystack.deepset.ai/blog/introducing-haystack-2-beta-and-advent"]},
                    "llm":{}})

print(result['llm']['replies'][0].text)
Graphs play a central role in **Haystack 2.0** due to its new **graph-based pipeline architecture**, which replaces the linear, directed acyclic graph (DAG) structure of Haystack 1.x. Here's how graphs relate to Haystack:

1. **Flexible Pipeline Structure**: Haystack 2.0 introduces cyclic and branching pipelines, allowing components to loop back, retry, or dynamically reconfigure.

2. **Explicit Component Roles**: Unlike Haystack 1.x, Haystack 2.0 uses graphs to clearly define each component's purpose and interactions, making pipelines more transparent and modular.

3. **Overcoming 1.x Limitations**: Haystack 2.0's graph-based approach removes fixed start/end constraints, allowing dynamic inputs, loops, and reusable subgraphs.