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

add new separator for role parsing logic #797

Merged
merged 5 commits into from
Oct 17, 2023
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 @@ -82,7 +82,7 @@ def validate_functions(functions):


def parse_function_role_prompt(function_str):
pattern = r"\n*name:\n\s*(\S+)\s*\n*content:\n(.*)"
pattern = r"\n*#{0,2}\s*name:\n\s*(\S+)\s*\n*#{0,2}\s*content:\n(.*)"
melionel marked this conversation as resolved.
Show resolved Hide resolved
match = re.search(pattern, function_str, re.DOTALL)
if match:
return match.group(1), match.group(2)
Expand All @@ -98,7 +98,7 @@ def parse_function_role_prompt(function_str):

def parse_chat(chat_str):
# openai chat api only supports below roles.
separator = r"(?i)\n+\s*(system|user|assistant|function)\s*:\s*\n"
separator = r"(?i)\n+\s*#?\s*(system|user|assistant|function)\s*:\s*\n"
melionel marked this conversation as resolved.
Show resolved Hide resolved
# Add a newline at the beginning to ensure consistent formatting of role lines.
# extra new line is removed when appending to the chat list.
chunks = re.split(separator, '\n'+chat_str)
Expand Down
10 changes: 10 additions & 0 deletions src/promptflow-tools/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,23 @@ def test_parse_function_role_prompt(self):
[
("system:\nthis is my function:\ndef hello", [
{'role': 'system', 'content': 'this is my function:\ndef hello'}]),
("#system:\nthis is my ##function:\ndef hello", [
{'role': 'system', 'content': 'this is my ##function:\ndef hello'}]),
(" \n system:\nthis is my function:\ndef hello", [
{'role': 'system', 'content': 'this is my function:\ndef hello'}]),
(" \n # system:\nthis is my function:\ndef hello", [
{'role': 'system', 'content': 'this is my function:\ndef hello'}]),
("user:\nhi\nassistant:\nanswer\nfunction:\nname:\nn\ncontent:\nc", [
{'role': 'user', 'content': 'hi'},
{'role': 'assistant', 'content': 'answer'},
{'role': 'function', 'name': 'n', 'content': 'c'}]),
("#user :\nhi\n #assistant:\nanswer\n# function:\nname:\nn\n##content:\nc", [
melionel marked this conversation as resolved.
Show resolved Hide resolved
{'role': 'user', 'content': 'hi'},
{'role': 'assistant', 'content': 'answer'},
{'role': 'function', 'name': 'n', 'content': 'c'}]),
("\nsystem:\nfirst\n\nsystem:\nsecond", [
{'role': 'system', 'content': 'first'}, {'role': 'system', 'content': 'second'}]),
("\n#system:\nfirst\n\n#system:\nsecond", [
{'role': 'system', 'content': 'first'}, {'role': 'system', 'content': 'second'}])
]
)
Expand Down