-
Notifications
You must be signed in to change notification settings - Fork 921
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
escape/unescape for llm node inputs and prompt tool output
- Loading branch information
yalu4
committed
Apr 22, 2024
1 parent
dfe16a4
commit ff60b9b
Showing
3 changed files
with
71 additions
and
24 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
18 changes: 15 additions & 3 deletions
18
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,9 +1,21 @@ | ||
# 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 | ||
from promptflow.tools.common import render_jinja_template, ExtendedStr, escape_roles | ||
|
||
|
||
@tool | ||
def render_template_jinja2(template: str, **kwargs) -> str: | ||
return render_jinja_template(template, trim_blocks=True, keep_trailing_newline=True, **kwargs) | ||
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() | ||
} | ||
|
||
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 |