How to pass documents from retrieval to DynamicChatPromptBuilder? #6989
-
I am using Haystack v2.0 and I am trying to design the RAG with chat functionality. I came up with the pipeline below. I have feeling that this is not a proper usage, but I could not find in documentation clear explanation on how to do it, could you help me with this? import os
from haystack.components.generators import OpenAIGenerator
from haystack import Pipeline
from haystack.components.builders.answer_builder import AnswerBuilder
from haystack.components.builders.prompt_builder import PromptBuilder
from haystack_integrations.components.retrievers.pgvector import PgvectorEmbeddingRetriever
from haystack.components.embedders import OpenAITextEmbedder
from haystack.dataclasses import ChatMessage
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.components.builders import DynamicChatPromptBuilder
def get_query_pipeline(document_store, k=10):
text_embedder = OpenAITextEmbedder()
retriever = PgvectorEmbeddingRetriever(document_store=document_store, top_k=k)
prompt_node = DynamicChatPromptBuilder()
# llm = OpenAIGenerator()
llm = OpenAIChatGenerator(model="gpt-3.5-turbo")
answer_builder = AnswerBuilder()
query_pipeline = Pipeline()
query_pipeline.add_component("text_embedder", text_embedder)
query_pipeline.add_component("retriever", retriever)
query_pipeline.add_component("prompt_builder", prompt_node)
query_pipeline.add_component("llm", llm)
query_pipeline.add_component("answer_builder", answer_builder)
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
query_pipeline.connect("retriever", "prompt_builder.template_variables.documents")
query_pipeline.connect("prompt_builder.prompt", "llm.messages")
query_pipeline.connect("llm.replies", "answer_builder.replies")
query_pipeline.connect("llm.meta", "answer_builder.meta")
query_pipeline.connect("retriever", "answer_builder.documents")
return query_pipeline
def answer_question(query_pipeline, question):
prompt_template = """
Given these documents, answer the question.\nDocuments:
{% for doc in documents %}
{{ doc.content }}
{% endfor %}
\nQuestion: {{query}}
\nAnswer:
"""
messages = [ChatMessage.from_system("You are a helpful bot bot to answer questions."),
ChatMessage.from_user("{{ query }}"),
ChatMessage.from_system(prompt_template)
]
result = query_pipeline.run({
"text_embedder": {"text": question},
# "retriever": {"filters": {"field": "meta.year", "operator": "›", "value": 2015},},
"prompt_builder": {"template_variables": {"query": question},
"prompt_source": messages},
"answer_builder": {"query": question}
},
)
return result |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 4 replies
-
Hello, @denyslazarenko! It seems that you have to declare the LMK if this helps... |
Beta Was this translation helpful? Give feedback.
-
Hi @anakin87 , Thank you very much for your quick reply and yes that helps a lot :) Maybe one point which would be helpful to provide in the documentation is that only last message will be treated as template:
def _process_chat_messages(self, prompt_source: List[ChatMessage], template_variables: Dict[str, Any]):
"""
Processes a list of :class:`ChatMessage` instances to generate a chat prompt.
It takes the last user message in the list, treats it as a template, and renders it with the provided
template variables. The resulting message replaces the last user message in the list, forming a complete,
templated chat prompt. One additional question which I have: in
|
Beta Was this translation helpful? Give feedback.
-
(FYI @dfokina: a suggestion to improve docs ☝️) @denyslazarenko I think you are right... |
Beta Was this translation helpful? Give feedback.
-
Sorry for reopening the issue but the output of the retriever is a list of documents while the input for the runtime variables is a dictionary. How am I supposed to connect them? |
Beta Was this translation helpful? Give feedback.
-
@Filocava99 We've updated the docs and added an example for using DynamicChatPromptBuilder with runtime variables: https://docs.haystack.deepset.ai/v2.0/docs/dynamicchatpromptbuilder#in-a-pipeline-with-runtime-variables |
Beta Was this translation helpful? Give feedback.
Hello, @denyslazarenko!
It seems that you have to declare the
runtime_variables
(such as Documents) when initializing theDynamicChatPromptBuilder
,as you can read in the docs.
LMK if this helps...