DocumentationAPI Reference📓 Tutorials🧑‍🍳 Cookbook🤝 Integrations💜 Discord

PromptBuilder

Use this component in pipelines before a Generator to render a prompt template and fill in variable values.

NamePromptBuilder
Folder Path/builders/
Most Common Position in a PipelineIn a querying pipeline, before a Generator
Inputs“**kwargs”: Any strings that should be used to render the prompt template
Outputs“prompt”: A string that represents the rendered prompt template

Overview

PromptBuilder is initialized with a prompt template and renders it by filling in parameters passed through keyword arguments, kwargs. With kwargs, you can pass a variable number of keyword arguments so that any variable used in the prompt template can be specified with the desired value. Values for all variables appearing in the prompt template need to be provided through the kwargs.

The template that is provided to the PromptBuilder during initialization needs to conform to the Jinja2 template language.

Usage

On its own

Below is an example of using the PromptBuilder to render a prompt template and fill it with target_language and snippet. The PromptBuilder returns a prompt with the string Translate the following context to spanish. Context: I can't speak spanish.; Translation:.

from haystack.components.builders import PromptBuilder

template = "Translate the following context to {{ target_language }}. Context: {{ snippet }}; Translation:"
builder = PromptBuilder(template=template)
builder.run(target_language="spanish", snippet="I can't speak spanish.")

In a Pipeline

Below is an example of a RAG pipeline where we use a PromptBuilder to render a custom prompt template and fill it with the contents of retrieved Documents and a query. The rendered prompt is then sent to a Generator.

from haystack import Pipeline
from haystack.document_stores.in_memory import InMemoryDocumentStore
from haystack.components.retrievers.in_memory import InMemoryBM25Retriever
from haystack.components.generators import OpenAIGenerator
from haystack.components.builders.answer_builder import AnswerBuilder
from haystack.components.builders.prompt_builder import PromptBuilder
prompt_template = """
    Given these documents, answer the question.\nDocuments:
    {% for doc in documents %}
        {{ doc.content }}
    {% endfor %}

    \nQuestion: {{query}}
    \nAnswer:
    """
p = Pipeline()
p.add_component(instance=InMemoryBM25Retriever(document_store=InMemoryDocumentStore()), name="retriever")
p.add_component(instance=PromptBuilder(template=prompt_template), name="prompt_builder")
p.add_component(instance=OpenAIGenerator(api_key=os.environ.get("OPENAI_API_KEY")), name="llm")
p.add_component(instance=AnswerBuilder(), name="answer_builder")
p.connect("retriever", "prompt_builder.documents")
p.connect("prompt_builder", "llm")
p.connect("llm.replies", "answer_builder.replies")
p.connect("llm.metadata", "answer_builder.metadata")
p.connect("retriever", "answer_builder.documents")
rag_pipeline.run(
            {
                "retriever": {"query": question},
                "prompt_builder": {"query": question},
                "answer_builder": {"query": question},
            }

Related Links

See the parameters details in our API reference: