-
Notifications
You must be signed in to change notification settings - 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
Open
srini047
wants to merge
21
commits into
deepset-ai:main
Choose a base branch
from
srini047:jinja2-set-vars-incorrect
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
136b7e0
fix: prompt-builder - jinja2 template set vars still shows required
srini047 f58094e
Merge branch 'main' into jinja2-set-vars-incorrect
srini047 492073e
Merge branch 'main' into jinja2-set-vars-incorrect
srini047 c78de27
Merge branch 'main' into jinja2-set-vars-incorrect
srini047 b102f58
Merge branch 'main' into jinja2-set-vars-incorrect
srini047 de715ff
fix: review comments
srini047 1f24f61
Merge branch 'main' into jinja2-set-vars-incorrect
srini047 3db96ab
fix: lint issues
srini047 6a5705e
feat: refactor ChatPromptBuilder to make use of extract_declared_vari…
srini047 d3f4b80
fix: add rn
srini047 54f22c4
fix: type issues
srini047 cb2ca2d
Merge branch 'main' into jinja2-set-vars-incorrect
srini047 b7d4988
fix: review comments
srini047 d84f0bf
fix: typing issue
srini047 5b27e56
Merge branch 'main' into jinja2-set-vars-incorrect
srini047 8f0cd97
Revert "fix: typing issue"
srini047 d06d649
Revert "fix: review comments"
srini047 0b81e55
Merge branch 'main' into jinja2-set-vars-incorrect
srini047 00ab8bd
fix: convert jinja2 utils from func to class
srini047 a1d8998
fix: lint issues
srini047 24b0c63
Merge branch 'main' into jinja2-set-vars-incorrect
srini047 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # SPDX-FileCopyrightText: 2022-present deepset GmbH <[email protected]> | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| from typing import Optional | ||
|
|
||
| from jinja2 import Environment, nodes | ||
|
|
||
|
|
||
| class Jinja2TemplateVariableExtractor: | ||
| """ | ||
| A utility class for extracting declared variables from Jinja2 templates. | ||
| """ | ||
|
|
||
| def __init__(self, env: Optional[Environment] = None): | ||
| self.env = env or Environment() | ||
|
|
||
| def _extract_from_text(self, template_str: str, role: Optional[str] = None) -> set[str]: | ||
| """ | ||
| Extract declared variables from a Jinja2 template string. | ||
|
|
||
| :param template_str: The Jinja2 template string to analyze. | ||
| :param env: The Jinja2 Environment. Defaults to None. | ||
|
|
||
| :returns: | ||
| A set of variable names used in the template. | ||
| """ | ||
| try: | ||
| ast = self.env.parse(template_str) | ||
| except Exception as e: | ||
| raise RuntimeError(f"Failed to parse Jinja2 template: {e}") | ||
|
|
||
| # Collect all variables assigned inside the template via {% set %} | ||
| 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) | ||
|
|
||
| return assigned_variables |
7 changes: 7 additions & 0 deletions
7
releasenotes/notes/jinja2-set-vars-parsing-919ae03d3c8a1465.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| fixes: | ||
| - | | ||
| Fixed an issue where Jinja2 variable assignments using the `set` directive | ||
| were not being parsed correctly in certain contexts. This fix ensures that | ||
| variables assigned with `{% set var = value %}` are now properly recognized | ||
| and can be used as expected within templates inside `PromptBuilder` and | ||
| `ChatPromptBuilder`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Could we make this a class function instead of inline. Please also keep it as private so fine to leave the name as
_extract_from_textThere 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.
Hey @srini047 it looks like this function wasn't made into a class function with signature
yet
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.
@sjrl Can you take a look now. Hope this is the expectation.