Skip to main content

PromptBuilder

Render a prompt filling in any variables and send it to a Generator.

Basic Information

  • Type: components.builders.prompt_builder.PromptBuilder
  • Components it can connect with:
    • Rankers: PromptBuilder can receive documents to add to the prompt from a Ranker.
    • Generators: PromptBuilder sends the rendered prompt to a Generator.

Inputs

ParameterTypeDefaultDescription
templateOptional[str]NoneAn optional string template to overwrite PromptBuilder's default template. If None, the default template provided at initialization is used.
template_variablesOptional[Dict[str, Any]]NoneAn optional dictionary of template variables to overwrite the pipeline variables.
kwargsAnyPipeline variables used for rendering the prompt.

Outputs

ParameterTypeDefaultDescription
promptstrA dictionary with the following keys: - prompt: The updated prompt text after rendering the prompt template.

Overview

Renders a prompt filling in any variables so that it can send it to a Generator.

The prompt uses Jinja2 template syntax. The variables in the default template are used as PromptBuilder's input and are all optional. If they're not provided, they're replaced with an empty string in the rendered prompt. To try out different prompts, you can replace the prompt template at runtime by providing a template for each pipeline run invocation.#### In a Pipeline

This is an example of a RAG pipeline where PromptBuilder renders a custom prompt template and fills it with the contents of the retrieved documents and a query. The rendered prompt is then sent to a Generator.

from haystack import Pipeline, Document
from haystack.utils import Secret
from haystack.components.generators import OpenAIGenerator
from haystack.components.builders.prompt_builder import PromptBuilder

# in a real world use case documents could come from a retriever, web, or any other source
documents = [Document(content="Joe lives in Berlin"), Document(content="Joe is a software engineer")]
prompt_template = """
Given these documents, answer the question.
Documents:
{% for doc in documents %}
{{ doc.content }}
{% endfor %}

Question: {{query}}
Answer:
"""
p = Pipeline()
p.add_component(instance=PromptBuilder(template=prompt_template), name="prompt_builder")
p.add_component(instance=OpenAIGenerator(api_key=Secret.from_env_var("OPENAI_API_KEY")), name="llm")
p.connect("prompt_builder", "llm")

question = "Where does Joe live?"
result = p.run({"prompt_builder": {"documents": documents, "query": question}})
print(result)

Changing the template at runtime (prompt engineering)

You can change the prompt template of an existing pipeline, like in this example:

documents = [
Document(content="Joe lives in Berlin", meta={"name": "doc1"}),
Document(content="Joe is a software engineer", meta={"name": "doc1"}),
]
new_template = """
You are a helpful assistant.
Given these documents, answer the question.
Documents:
{% for doc in documents %}
Document {{ loop.index }}:
Document name: {{ doc.meta['name'] }}
{{ doc.content }}
{% endfor %}

Question: {{ query }}
Answer:
"""
p.run({
"prompt_builder": {
"documents": documents,
"query": question,
"template": new_template,
},
})

To replace the variables in the default template when testing your prompt, pass the new variables in the variables parameter.

Overwriting variables at runtime

To overwrite the values of variables, use template_variables during runtime:

language_template = """
You are a helpful assistant.
Given these documents, answer the question.
Documents:
{% for doc in documents %}
Document {{ loop.index }}:
Document name: {{ doc.meta['name'] }}
{{ doc.content }}
{% endfor %}

Question: {{ query }}
Please provide your answer in {{ answer_language | default('English') }}
Answer:
"""
p.run({
"prompt_builder": {
"documents": documents,
"query": question,
"template": language_template,
"template_variables": {"answer_language": "German"},
},
})

Note that language_template introduces variable answer_language which is not bound to any pipeline variable. If not set otherwise, it will use its default value 'English'. This example overwrites its value to 'German'. Use template_variables to overwrite pipeline variables (such as documents) as well.

Usage Example

components:
PromptBuilder:
type: components.builders.prompt_builder.PromptBuilder
init_parameters:

Parameters

Init Parameters

These are the parameters you can configure in Pipeline Builder:

ParameterTypeDefaultDescription
templatestrA prompt template that uses Jinja2 syntax to add variables. For example: "Summarize this document: {{ documents[0].content }}\nSummary:" It's used to render the prompt. The variables in the default template are input for PromptBuilder and are all optional, unless explicitly specified. If an optional variable is not provided, it's replaced with an empty string in the rendered prompt.
required_variablesOptional[Union[List[str], Literal['*']]]NoneList variables that must be provided as input to PromptBuilder. If a variable listed as required is not provided, an exception is raised. If set to "*", all variables found in the prompt are required. Optional.
variablesOptional[List[str]]NoneList input variables to use in prompt templates instead of the ones inferred from the template parameter. For example, to use more variables during prompt engineering than the ones present in the default template, you can provide them here.

Run Method Parameters

These are the parameters you can configure for the component's run() method. This means you can pass these parameters at query time through the API, in Playground, or when running a job. For details, see Modify Pipeline Parameters at Query Time.

ParameterTypeDefaultDescription
templateOptional[str]NoneAn optional string template to overwrite PromptBuilder's default template. If None, the default template provided at initialization is used.
template_variablesOptional[Dict[str, Any]]NoneAn optional dictionary of template variables to overwrite the pipeline variables.
kwargsAnyPipeline variables used for rendering the prompt.