haystack icon indicating copy to clipboard operation
haystack copied to clipboard

feat: Add an init boolean parameter to PromptBuilder to require all defined jinja variables

Open sjrl opened this issue 1 year ago • 3 comments

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?

sjrl avatar Sep 24 '24 10:09 sjrl

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 avatar Sep 30 '24 12:09 silvanocerza

@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.

sjrl avatar Oct 07 '24 06:10 sjrl

@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?

sjrl avatar Oct 09 '24 11:10 sjrl