-
Couldn't load subscription status.
- Fork 2.5k
fix: prompt-builder - jinja2 template set vars still shows required #9932
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
@srini047 is attempting to deploy a commit to the deepset Team on Vercel. A member of the Team first needs to authorize it. |
Pull Request Test Coverage Report for Build 18847537461Details
💛 - Coveralls |
| assigned_variables = set() | ||
| for node in ast.find_all((nodes.Assign, nodes.For)): | ||
| if hasattr(node.target, "name"): | ||
| assigned_variables.add(node.target.name) | ||
|
|
||
| variables = list(template_variables - assigned_variables) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the addition, but this doesn't quite cover all the cases we would like. Let's use the following instead which also handles tuple and list unpacking when using set.
Also the nodes.For case doesn't seem to be needed. We don't pick up for-loop variables like doc in your test.
| assigned_variables = set() | |
| for node in ast.find_all((nodes.Assign, nodes.For)): | |
| if hasattr(node.target, "name"): | |
| assigned_variables.add(node.target.name) | |
| variables = list(template_variables - assigned_variables) | |
| assigned_variables = set() | |
| for node in ast.find_all((nodes.Assign,)): | |
| if isinstance(node.target, nodes.Name): | |
| assigned_variables.add(node.target.name) | |
| elif isinstance(node.target, (nodes.List, nodes.Tuple)): | |
| for name_node in node.target.items: | |
| if isinstance(name_node, nodes.Name): | |
| assigned_variables.add(name_node.name) | |
| variables = list(template_variables - assigned_variables) |
| res = builder.run(docs=docs, existing_documents=None) | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's also explicitly test the found variables
| res = builder.run(docs=docs, existing_documents=None) | |
| builder = PromptBuilder(template=template, required_variables="*") | |
| assert set(builder.variables) == {"docs", "existing_documents"} | |
|
|
||
| assert "<document reference=" in res["prompt"] | ||
| assert "Doc 1" in res["prompt"] | ||
| assert "Doc 2" in res["prompt"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's also add a few more tests to test tuple and list unpacking
def test_variables_correct_with_tuple_assignment(self):
template = """{% if existing_documents is not none %}
{% set x, y = (existing_documents|length, 1) %}
{% else %}
{% set x, y = (0, 1) %}
{% endif %}
x={{ x }}, y={{ y }}
"""
builder = PromptBuilder(template=template, required_variables="*")
assert set(builder.variables) == {"existing_documents"}
res = builder.run(existing_documents=None)
assert "x=0, y=1" in res["prompt"]
def test_variables_correct_with_list_assignment(self):
template = """{% if existing_documents is not none %}
{% set x, y = [existing_documents|length, 1] %}
{% else %}
{% set x, y = [0, 1] %}
{% endif %}
x={{ x }}, y={{ y }}
"""
builder = PromptBuilder(template=template, required_variables="*")
assert set(builder.variables) == {"existing_documents"}
res = builder.run(existing_documents=None)
assert "x=0, y=1" in res["prompt"]|
With the above listed changes I think we could then extend this to the other components you mention. It may be wise to add this as a utility method in |
Related Issues
PromptBuilderincorrectly marks internally set Jinja2 variables as required inputs when using `required_variables='*' #9916Proposed Changes:
Previously we checked only the undeclared variables and left the set variables.
Now, we find all the variables assigned using
setin the template and push it to the variables list.This behaviour would be seen in multiple components namely:
How did you test it?
Run the exisiting suite and added test case for the this scenario.
Notes for the reviewer
Checklist
fix:,feat:,build:,chore:,ci:,docs:,style:,refactor:,perf:,test:and added!in case the PR includes breaking changes.