Skip to content
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

[Bugfix] Support parsing chat prompt if role property has \r around colon #3007

Merged
merged 6 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/promptflow-tools/promptflow/tools/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def validate_tools(tools):
def try_parse_name_and_content(role_prompt):
# customer can add ## in front of name/content for markdown highlight.
# and we still support name/content without ## prefix for backward compatibility.
pattern = r"\n*#{0,2}\s*name:\n+\s*(\S+)\s*\n*#{0,2}\s*content:\n?(.*)"
pattern = r"\n*#{0,2}\s*name\s*:\s*\n+\s*(\S+)\s*\n*#{0,2}\s*content\s*:\s*\n?(.*)"
match = re.search(pattern, role_prompt, re.DOTALL)
if match:
return match.group(1), match.group(2)
Expand All @@ -232,7 +232,7 @@ def try_parse_name_and_content(role_prompt):
def try_parse_tool_call_id_and_content(role_prompt):
# customer can add ## in front of tool_call_id/content for markdown highlight.
# and we still support tool_call_id/content without ## prefix for backward compatibility.
pattern = r"\n*#{0,2}\s*tool_call_id:\n+\s*(\S+)\s*\n*#{0,2}\s*content:\n?(.*)"
pattern = r"\n*#{0,2}\s*tool_call_id\s*:\s*\n+\s*(\S+)\s*\n*#{0,2}\s*content\s*:\s*\n?(.*)"
match = re.search(pattern, role_prompt, re.DOTALL)
if match:
return match.group(1), match.group(2)
Expand Down
27 changes: 26 additions & 1 deletion src/promptflow-tools/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,10 @@ def test_success_parse_role_prompt(self, chat_str, images, image_detail, expecte
("\nsystem:\nname:\n\n content:\nfirst", [
{'role': 'system', 'content': 'name:\n\n content:\nfirst'}]),
("\nsystem:\nname:\n\n", [
{'role': 'system', 'content': 'name:'}])
{'role': 'system', 'content': 'name:'}]),
# portal may add extra \r to new line character.
("function:\r\nname:\r\n AI\ncontent :\r\nfirst", [
{'role': 'function', 'name': 'AI', 'content': 'first'}]),
],
)
def test_parse_chat_with_name_in_role_prompt(self, chat_str, expected_result):
Expand All @@ -240,6 +243,20 @@ def test_try_parse_chat_with_tools(self, example_prompt_template_with_tool, pars
actual_result = parse_chat(example_prompt_template_with_tool)
assert actual_result == parsed_chat_with_tools

@pytest.mark.parametrize(
"chat_str, expected_result",
[
("\n#tool:\n## tool_call_id:\nid \n content:\nfirst\n\n#user:\nsecond", [
{'role': 'tool', 'tool_call_id': 'id', 'content': 'first'}, {'role': 'user', 'content': 'second'}]),
# portal may add extra \r to new line character.
("\ntool:\ntool_call_id :\r\nid\n content:\r\n", [
{'role': 'tool', 'tool_call_id': 'id', 'content': ''}]),
],
)
def test_parse_tool_call_id_and_content(self, chat_str, expected_result):
actual_result = parse_chat(chat_str)
assert actual_result == expected_result

@pytest.mark.parametrize("chunk, error_msg, success", [
("""
## tool_calls:
Expand Down Expand Up @@ -275,6 +292,14 @@ def test_try_parse_chat_with_tools(self, example_prompt_template_with_tool, pars
"function": {"name": "func1", "arguments": ""}
}]
""", "", True),
# portal may add extra \r to new line character.
("""
## tool_calls:\r
[{
"id": "tool_call_id", "type": "function",
"function": {"name": "func1", "arguments": ""}
}]
""", "", True),
])
def test_parse_tool_calls_for_assistant(self, chunk: str, error_msg: str, success: bool):
last_message = {'role': 'assistant'}
Expand Down
Loading