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:PromptBuildercan receive documents to add to the prompt from aRanker.Generators:PromptBuildersends the rendered prompt to aGenerator.
Inputs
| Parameter | Type | Default | Description |
|---|---|---|---|
| template | Optional[str] | None | An optional string template to overwrite PromptBuilder's default template. If None, the default template provided at initialization is used. |
| template_variables | Optional[Dict[str, Any]] | None | An optional dictionary of template variables to overwrite the pipeline variables. |
| kwargs | Any | Pipeline variables used for rendering the prompt. |
Outputs
| Parameter | Type | Default | Description |
|---|---|---|---|
| prompt | str | A 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:
| Parameter | Type | Default | Description |
|---|---|---|---|
| template | str | A 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_variables | Optional[Union[List[str], Literal['*']]] | None | List 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. |
| variables | Optional[List[str]] | None | List 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.
| Parameter | Type | Default | Description |
|---|---|---|---|
| template | Optional[str] | None | An optional string template to overwrite PromptBuilder's default template. If None, the default template provided at initialization is used. |
| template_variables | Optional[Dict[str, Any]] | None | An optional dictionary of template variables to overwrite the pipeline variables. |
| kwargs | Any | Pipeline variables used for rendering the prompt. |
Was this page helpful?