HaystackHaystack 3.1 · Python 3.10+
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
19 small wins to finish your pathNext lesson

A RAG pipeline: retrieve, prompt, generate, answer

A RAG pipeline connects the retriever, the prompt builder, a generator and AnswerBuilder, so an answer comes back with the documents it was based on.

Examplethe pipeline, after the store, retriever and prompt builder from earlier lessons
from haystack.components.builders import AnswerBuilder

from shop_chat import ShopChat

rag = Pipeline()
rag.add_component("retriever", retriever)
rag.add_component("prompt", prompt_builder)
rag.add_component("llm", ShopChat())
rag.add_component("answer", AnswerBuilder())
rag.connect("retriever.documents", "prompt.documents")
rag.connect("prompt.prompt", "llm.messages")
rag.connect("llm.replies", "answer.replies")
rag.connect("retriever.documents", "answer.documents")
Example
def ask(question):
    result = rag.run({"retriever": {"query": question}, "prompt": {"question": question}, "answer": {"query": question}})
    return result["answer"]["answers"][0]


answer = ask("How long do refunds take?")
print(answer.data)
print([d.meta["topic"] for d in answer.documents])

The question goes to three components: the retriever to search, the prompt builder to fill the template, and AnswerBuilder to record it. The retriever's documents go to both the prompt and the answer, so the answer carries its sources. answer.data is the reply text.

Example
for question in ["Can I return a damaged item?", "Do you sell gift cards?"]:
    print(question, "->", ask(question).data)

For the second question the retriever found nothing, so the prompt had no documents and the stand-in refused. A real model given the same empty prompt might invent an answer instead, which is why the prompt says "using only these documents" and why lesson 14 measures retrieval.

Two questions to ask of every answer

  • Did the retriever find the right documents? Check answer.documents.
  • Did the generator use them? Compare answer.data with those documents.
Try it yourself
  • Add the audience filter from lesson 8 so staff documents never reach the prompt.
  • Print the prompt with include_outputs_from={"prompt"}.
  • Set the retriever's top_k=1 and ask about refunds again.

You understood something today that you didn't yesterday.