feat: Add an init boolean parameter to PromptBuilder to require all defined jinja variables
There are often times I'd prefer the PromptBuilder to always require all variables defined within the Jinja2 template before running. I realize we can accomplish this with the required_variables parameter, but it would be nice if I could toggle this behavior instead of needing to keep a list up to date that can often change when doing prompt engineering.
Would it be possible to add an init parameter to the PromptBuilder that makes the default behavior require all parameters?
One could already work around this writing something like this:
from jinja2 import Environment, meta
from haystack.components.builders import PromptBuilder
prompt_template = """
Given these documents, answer the question.
Documents:
{% for doc in documents %}
{{ doc.content }}
{% endfor %}
Question: {{query}}
Answer:
"""
parsed = Environment().parse(prompt_template)
required_variables = list(meta.find_undeclared_variables(parsed))
PromptBuilder(prompt_template, required_variables=required_variables)
The PromptBuilder already expects different combination of various arguments and is overtly complex for my taste. I would very much prefer we don't add extra arguments if there's already a way to handle this case.
@silvanocerza fair enough on not wanting to add more variables to this already complex component. Your suggestion does cover the use case at the level of python code, but is there a way we could do this at the yaml level as well? In deepset Cloud we don't have the ability to utilize a solution like this since we create pipelines at the yaml level.
@silvanocerza maybe we could take the approach of allowing "*" as a valid input to required_variables which when will auto set all variables to be required?