nemoguardrails.cli.migration

View as Markdown

Module Contents

Functions

NameDescription
_add_active_decoratorAdds an ‘@active’ decorator above each flow id in the new lines.
_add_imports-
_add_main_co_fileAdd the main co file to the given file path.
_append_colang_versionAppends the Colang version “2.x” at the end of the config file.
_comment_rails_flows_in_config-
_confirm_and_tag_replace-
_create_main_co_if_not_existsCheck if the main co file exists in the directory of the file.
_generate_main_flowAdds a ‘main’ flow to the new lines that activates all other flows.
_generate_rails_flowsGenerates flow definitions from the list of flows.
_get_co_files_to_processGet a list of .co files to process.
_get_config_files_to_processGet a list of .yaml or .yml files to process.
_get_flow_idsReturns the flow ids in the content.
_get_flow_ids_from_newlinesReturns the flow ids in the new lines.
_get_rails_flowsExtracts the list of flows from the raw_config dictionary.
_get_raw_configread the yaml file and get rails key
_globalize_variable_assignment-
_is_anonymous_flowChecks if the flow is an anonymous flow.
_is_flowChecks if the line is a flow definition. it is a flow definition if it starts with “flow” and other words after flow.
_process_co_filesProcesses the Colang files in the given path.
_process_config_filesProcesses the config files in the given path.
_process_sample_conversation_in_configProcesses the sample_conversation section in the config file.
_read_file_lines-
_remove_files_from_pathRemove files from the path.
_remove_rails_flows_from_config-
_revise_anonymous_flowRevises an anonymous flow by giving it a unique name.
_set_colang_versionSets the ‘colang_version’ in the given raw_config.
_validate_fileValidates the file after the conversion.
_write_rails_flows_to_fileWrites the rails flows to a file.
_write_to_fileWrites new lines to a file. If the file does not exist, it is created.
_write_transformed_content_and_rename_originalWrites the transformed content to the file.
convert_colang_1_syntaxConverts a co file from v1 format to v2.
convert_colang_2alpha_syntaxConvert a co file form v2-alpha to v2-beta
convert_sample_conversation_syntaxConverts the sample_conversation section from the old format to the new format.
migrateMigrates all .co files in a directory from the old format to the new format.

Data

_FILES_TO_EXCLUDE_ALPHA

_LIBS_USED

API

nemoguardrails.cli.migration._add_active_decorator(
new_lines: typing.List
) -> typing.List

Adds an ‘@active’ decorator above each flow id in the new lines.

Parameters:

new_lines
List

The lines to add the decorators to.

Returns: List

The lines with the decorators added.

nemoguardrails.cli.migration._add_imports(
new_lines: typing.List,
libraries: typing.List[str]
) -> typing.List
nemoguardrails.cli.migration._add_main_co_file(
file_path: str,
libraries: typing.Optional[typing.List[str]] = None
) -> bool

Add the main co file to the given file path. Args: file_path (str): The path to the file to add the main co file to. libraries (list[str]): The list of libraries to import in the main co file. Returns: bool: True if the main co file was added successfully, False otherwise.

nemoguardrails.cli.migration._append_colang_version(
file_path
)

Appends the Colang version “2.x” at the end of the config file.

nemoguardrails.cli.migration._comment_rails_flows_in_config(
file_path
)
nemoguardrails.cli.migration._confirm_and_tag_replace(
line,
original_line,
name
)
nemoguardrails.cli.migration._create_main_co_if_not_exists(
file_path
)

Check if the main co file exists in the directory of the file.

If the main co file does not exist, it creates an empty main co file in the directory of the given file.

Parameters:

file_path
str

The path to the file to check for the main co file.

nemoguardrails.cli.migration._generate_main_flow(
new_lines: typing.List
) -> typing.List

Adds a ‘main’ flow to the new lines that activates all other flows.

The ‘main’ flow is added at the beginning of the new lines. It includes an ‘activate’ command for each flow id found in the new lines.

Examples: new_lines = [“flow my_flow”, “flow another_flow”] include_main_flow(new_lines)

'''flow main

activate my_flow

activate another_flow

Parameters:

new_lines
list

The new lines to add the main flow to.

Returns: List

The new lines with the ‘main’ flow added at the beginning.

nemoguardrails.cli.migration._generate_rails_flows(
flows
)

Generates flow definitions from the list of flows. Args: flows (dict): The dictionary of flows. Returns: str: The flow definitions.

nemoguardrails.cli.migration._get_co_files_to_process(
path
)

Get a list of .co files to process.

Parameters:

path
str

The path to the file or directory to process.

Returns:

The list of .co files to process.

nemoguardrails.cli.migration._get_config_files_to_process(
path
)

Get a list of .yaml or .yml files to process.

Parameters:

path
str

The path to the file or directory to process.

Returns:

The list of .yaml or .yml files to process.

nemoguardrails.cli.migration._get_flow_ids(
content: str
) -> typing.List

Returns the flow ids in the content.

Args: content (str): The content to search for flow ids. Returns: list: A list of flow ids.

Examples: content = “flow my_flow” get_flow_ids(content)

[‘my_flow’]

content = “flow my_flow is better than flow another_flow” get_flow_ids(content)

[‘my_flow is better than’, ‘another_flow’]

flow user and flow bot are not considered as flow ids

content = “flow user express greeting” get_flow_ids(content)

[]

nemoguardrails.cli.migration._get_flow_ids_from_newlines(
new_lines: typing.List
) -> typing.List

Returns the flow ids in the new lines. Args: new_lines (list): The new lines to search for flow ids. Returns: list: A list of flow ids. Examples: new_lines = [“flow my_flow is better than”, “flow another_flow”] get_flow_ids_from_newlines(new_lines)

[‘my_flow is better than’, ‘another_flow’]

nemoguardrails.cli.migration._get_rails_flows(
raw_config
)

Extracts the list of flows from the raw_config dictionary.

Parameters:

raw_config
dict

The raw configuration dictionary.

Returns:

The list of flows.

nemoguardrails.cli.migration._get_raw_config(
config_path: str
)

read the yaml file and get rails key

nemoguardrails.cli.migration._globalize_variable_assignment(
line
)
nemoguardrails.cli.migration._is_anonymous_flow(
flow_content: str
) -> bool

Checks if the flow is an anonymous flow.

an anonymous flow is a flow that does not start with a name. so it only has define flow without a name.

Returns: bool: True if the flow is anonymous, False otherwise. Examples:

flow_content = “flow ” is_anonymous_flow(flow_content)

True

flow_content = “flow my_flow” is_anonymous_flow(flow_content)

False

Parameters:

flow_content
str

The content of the flow.

nemoguardrails.cli.migration._is_flow(
line: str
) -> bool

Checks if the line is a flow definition. it is a flow definition if it starts with “flow” and other words after flow.

nemoguardrails.cli.migration._process_co_files(
co_files_to_process: typing.List[str],
from_version: typing.Literal['1.0', '2.0-alpha'],
include_main_flow: bool,
use_active_decorator: bool,
validate: bool
) -> int

Processes the Colang files in the given path.

It converts the Colang files from the given version to the latest version and writes the changes to the files.

Returns: int: The total number of files changed.

Parameters:

co_files_to_process
List[str]

The list of Colang files to process.

from_version
str

The version of the Colang files to convert from.

include_main_flow
bool

Whether to include the main flow.

use_active_decorator
bool

Whether to use the active decorator.

validate
bool

Whether to validate the files.

nemoguardrails.cli.migration._process_config_files(
config_files_to_process: typing.List[str]
) -> int

Processes the config files in the given path.

It extracts the rails flows from the config files and writes them to a new file.

Parameters:

config_files_to_process
List[str]

The list of config files to process.

Returns: int

The total number of config files changed.

nemoguardrails.cli.migration._process_sample_conversation_in_config(
file_path: str
)

Processes the sample_conversation section in the config file.

Parameters:

file_path
str

The path to the config file.

nemoguardrails.cli.migration._read_file_lines(
file_path
)
nemoguardrails.cli.migration._remove_files_from_path(
path,
filenames: typing.List[str]
)

Remove files from the path.

Parameters:

path
str

The path to the directory to remove files from.

filenames
list[str]

The list of filenames to remove.

nemoguardrails.cli.migration._remove_rails_flows_from_config(
raw_config
)
nemoguardrails.cli.migration._revise_anonymous_flow(
flow_content: str,
first_message: str
) -> str

Revises an anonymous flow by giving it a unique name. Args: flow_content (str): The content of the anonymous flow. first_message (str): The first message in the flow. Returns: str: The revised flow content with a unique name.

nemoguardrails.cli.migration._set_colang_version(
file_path: str,
version: str
)

Sets the ‘colang_version’ in the given raw_config.

Parameters:

file_path
str

The path to the config file.

version
str

The version to set.

nemoguardrails.cli.migration._validate_file(
file_path,
new_lines
)

Validates the file after the conversion.

It validates the file by parsing it and checking for any errors.

Parameters:

file_path
str

The path to the file to validate.

new_lines
list

The new lines of the file after the conversion.

Raises:

  • Exception: If the validation fails.
nemoguardrails.cli.migration._write_rails_flows_to_file(
file_path,
rails_flows
)

Writes the rails flows to a file. Args: file_path (str): The path to the file to write to. rails_flows (list): The rails flows to write to the file.

nemoguardrails.cli.migration._write_to_file(
file_path,
new_lines
)

Writes new lines to a file. If the file does not exist, it is created.

Parameters:

file_path
str

The path to the file to write to.

new_lines
list

The new lines to write to the file.

nemoguardrails.cli.migration._write_transformed_content_and_rename_original(
file_path,
new_lines,
co_extension = '.v1.co'
)

Writes the transformed content to the file.

nemoguardrails.cli.migration.convert_colang_1_syntax(
lines: typing.List[str]
) -> typing.List[str]

Converts a co file from v1 format to v2.

Returns: List: The new lines of the file after successful migration.

Parameters:

lines
List[str]

The lines of the file to convert.

nemoguardrails.cli.migration.convert_colang_2alpha_syntax(
lines: typing.List[str]
) -> typing.List[str]

Convert a co file form v2-alpha to v2-beta

Returns: List (str): The new lines of the file after successful migration.

Parameters:

lines
List[str]

The lines of the file to convert

nemoguardrails.cli.migration.convert_sample_conversation_syntax(
lines: typing.List[str]
) -> typing.List[str]

Converts the sample_conversation section from the old format to the new format.

Parameters:

lines
List[str]

The lines of the sample_conversation to convert.

Returns: List[str]

List[str]: The new lines of the sample_conversation after conversion.

nemoguardrails.cli.migration.migrate(
path,
include_main_flow = False,
use_active_decorator: bool = True,
from_version: typing.Literal['1.0', '2.0-alpha'] = '1.0',
validate: bool = True
)

Migrates all .co files in a directory from the old format to the new format.

Parameters:

path
str

The path to the directory containing the files to migrate.

include_main_flow
boolDefaults to False

Whether to add main flow to the files.

use_active_decorator
boolDefaults to True

Whether to use the active decorator.

from_version
strDefaults to '1.0'

The version of the colang files to convert from. Any of ‘1.0’ or ‘2.0-alpha’.

validate
boolDefaults to True

Whether to validate the files.

nemoguardrails.cli.migration._FILES_TO_EXCLUDE_ALPHA = ['ccl.co', 'core_flo_library.co']
nemoguardrails.cli.migration._LIBS_USED = set()