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.
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")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.
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.datawith those documents.
- 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=1and ask about refunds again.
You understood something today that you didn't yesterday.