-
Notifications
You must be signed in to change notification settings - Fork 914
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yalu4
committed
Apr 22, 2024
1 parent
ff60b9b
commit 8c3cf7a
Showing
4 changed files
with
113 additions
and
159 deletions.
There are no files selected for viewing
This file contains 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 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
25 changes: 11 additions & 14 deletions
25
src/promptflow-tools/promptflow/tools/template_rendering.py
This file contains 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 |
---|---|---|
@@ -1,21 +1,18 @@ | ||
# Avoid circular dependencies: Use import 'from promptflow._internal' instead of 'from promptflow' | ||
# since the code here is in promptflow namespace as well | ||
from promptflow._internal import tool | ||
from promptflow.tools.common import render_jinja_template, ExtendedStr, escape_roles | ||
from promptflow.tools.common import render_jinja_template, PromptResult, escape_roles_for_flow_inputs_and_prompt_output | ||
|
||
|
||
@tool | ||
def render_template_jinja2(template: str, **kwargs) -> ExtendedStr: | ||
flow_input_list = kwargs.pop("flow_inputs", None) | ||
updated_kwargs = kwargs | ||
if flow_input_list: | ||
# Use escape/unescape to avoid unintended parsing of role in user inputs. | ||
updated_kwargs = { | ||
key: escape_roles(value) if key in flow_input_list else value for key, value in kwargs.items() | ||
} | ||
def render_template_jinja2(template: str, **kwargs) -> PromptResult: | ||
rendered_template = render_jinja_template(template, trim_blocks=True, keep_trailing_newline=True, **kwargs) | ||
prompt_result = PromptResult(rendered_template) | ||
|
||
original_str = render_jinja_template(template, trim_blocks=True, keep_trailing_newline=True, **kwargs) | ||
escape_str = render_jinja_template(template, trim_blocks=True, keep_trailing_newline=True, **updated_kwargs) | ||
res = ExtendedStr(original_str) | ||
res.escaped_string = escape_str | ||
return res | ||
updated_kwargs = escape_roles_for_flow_inputs_and_prompt_output(kwargs.copy()) | ||
if kwargs != updated_kwargs: | ||
escaped_rendered_template = render_jinja_template( | ||
template, trim_blocks=True, keep_trailing_newline=True, **updated_kwargs | ||
) | ||
prompt_result.escaped_string = escaped_rendered_template | ||
return prompt_result |
Oops, something went wrong.