-
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?
Changes from 4 commits
136b7e0
f58094e
492073e
c78de27
b102f58
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -337,3 +337,25 @@ def test_warning_no_required_variables(self, caplog): | |||||||||
| with caplog.at_level(logging.WARNING): | ||||||||||
| _ = PromptBuilder(template="This is a {{ variable }}") | ||||||||||
| assert "but `required_variables` is not set." in caplog.text | ||||||||||
|
|
||||||||||
| def test_template_assigned_variables_from_required_inputs(self) -> None: | ||||||||||
| template = """{% if existing_documents is not none %} | ||||||||||
| {% set existing_doc_len = existing_documents|length %} | ||||||||||
| {% else %} | ||||||||||
| {% set existing_doc_len = 0 %} | ||||||||||
| {% endif %} | ||||||||||
| {% for doc in docs %} | ||||||||||
| <document reference="{{loop.index + existing_doc_len}}"> | ||||||||||
| {{ doc.content }} | ||||||||||
| </document> | ||||||||||
| {% endfor %} | ||||||||||
| """ | ||||||||||
|
|
||||||||||
| builder = PromptBuilder(template=template, required_variables="*") | ||||||||||
| docs = [Document(content="Doc 1"), Document(content="Doc 2")] | ||||||||||
|
|
||||||||||
| res = builder.run(docs=docs, existing_documents=None) | ||||||||||
|
|
||||||||||
|
Comment on lines
+357
to
+358
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's also explicitly test the found variables
Suggested change
|
||||||||||
| 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 commentThe 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"] |
||||||||||
Uh oh!
There was an error while loading. Please reload this page.
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.Forcase doesn't seem to be needed. We don't pick up for-loop variables likedocin your test.