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

Improve alpha to beta bot migration #878

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
24 changes: 22 additions & 2 deletions nemoguardrails/cli/migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,17 @@ def convert_colang_2alpha_syntax(lines: List[str]) -> List[str]:
line = re.sub(r"flow_start_uid", "flow_instance_uid", line)
# line = re.sub(r'r"(.*)"', r'regex("\1")', line)
line = re.sub(r'r"(.*)"', r'regex("(\1)")', line)
line = re.sub(r"r'(.*)'", r"regex('(\1)')", line)
line = re.sub(r'"\{\{(.*)\}\}"', r'"{\1}"', line)
line = re.sub(r"'\{\{(.*)\}\}'", r"'{\1}'", line)
line = re.sub(r"findall", "find_all", line)

# Convert triple quotes to ellipsis followed by double quotes for inline NLDs
if line.strip().startswith("$") and '"""' in line:
line = re.sub(r'"""(.*)"""', r'..."\1"', line)
if line.strip().startswith("$"):
if '"""' in line:
line = re.sub(r'"""(.*)"""', r'..."\1"', line)
elif "'''" in line:
line = re.sub(r"'''(.*)'''", r"...'\1'", line)

# Replace specific phrases based on the file
# if "core.co" in file_path:
Expand All @@ -111,6 +116,7 @@ def convert_colang_2alpha_syntax(lines: List[str]) -> List[str]:
line = line.replace("track bot talking state", "tracking bot talking state")
line = line.replace("track user talking state", "tracking user talking state")
line = line.replace("track user utterance state", "tracking user talking state")
line = line.replace("track bot utterance state", "tracking bot talking state")

# we must import core library
_confirm_and_tag_replace(line, original_line, "core")
Expand Down Expand Up @@ -144,6 +150,20 @@ def convert_colang_2alpha_syntax(lines: List[str]) -> List[str]:
line = line.replace("manage thinking posture", "managing thinking posture")
line = line.replace("manage bot postures", "managing bot postures")

# Flows that were deprecated and have no replacement
line = line.replace(
"manage attentive posture",
"!!!! DEPRECATED FLOW (please remove): manage attentive posture",
)
line = line.replace(
"track user presence state",
"!!!! DEPRECATED FLOW (please remove): track user presence state",
)
line = line.replace(
"user became no longer present",
"!!!! DEPRECATED FLOW (please remove): user became no longer present",
)

# we must import avatar library
_confirm_and_tag_replace(line, original_line, "avatars")

Expand Down
14 changes: 12 additions & 2 deletions tests/test_cli_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@ def test_flow_start_uid_replacement(self):
expected_output = ["flow_instance_uid: 12345"]
assert convert_colang_2alpha_syntax(input_lines) == expected_output

def test_regex_replacement(self):
def test_regex_replacement_1(self):
input_lines = ['r"(?i).*({{$text}})((\s*\w+\s*){0,2})\W*$"']
expected_output = ['regex("((?i).*({{$text}})((\\s*\\w+\\s*){0,2})\\W*$)")']
assert convert_colang_2alpha_syntax(input_lines) == expected_output

def test_regex_replacement_2(self):
input_lines = ["r'(?i).*({{$text}})((\s*\w+\s*){0,2})\W*$'"]
expected_output = ["regex('((?i).*({{$text}})((\\s*\\w+\\s*){0,2})\\W*$)')"]
assert convert_colang_2alpha_syntax(input_lines) == expected_output

def test_curly_braces_replacement(self):
input_lines = ['"{{variable}}"']
expected_output = ['"{variable}"']
Expand All @@ -49,11 +54,16 @@ def test_findall_replacement(self):
expected_output = ["find_all matches"]
assert convert_colang_2alpha_syntax(input_lines) == expected_output

def test_triple_quotes_replacement(self):
def test_triple_quotes_replacement_1(self):
input_lines = ['$ """some text"""']
expected_output = ['$ ..."some text"']
assert convert_colang_2alpha_syntax(input_lines) == expected_output

def test_triple_quotes_replacement_2(self):
input_lines = ["$ '''some text'''"]
expected_output = ["$ ...'some text'"]
assert convert_colang_2alpha_syntax(input_lines) == expected_output

def test_specific_phrases_replacement(self):
input_lines = [
"catch colang errors",
Expand Down