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

Feature/module flow activation #559

Merged
merged 4 commits into from
Jun 24, 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
26 changes: 25 additions & 1 deletion nemoguardrails/colang/v2_x/runtime/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
initialize_state,
run_to_completion,
)
from nemoguardrails.colang.v2_x.runtime.utils import new_readable_uid
from nemoguardrails.rails.llm.config import RailsConfig
from nemoguardrails.utils import new_event_dict

Expand Down Expand Up @@ -444,9 +445,32 @@ async def process_events(
assert state.main_flow_state is not None
main_flow_uid = state.main_flow_state.uid
if state.main_flow_state.status == FlowStatus.WAITING:
log.info("Start of story!")

# Start the main flow
input_event = InternalEvent(name="StartFlow", arguments={"flow_id": "main"})
input_events.insert(0, input_event)
log.info("Start of story!")
main_flow_state = state.flow_id_states["main"][-1]

# Start all module level flows at beginning of main flow
idx = 0
for flow_config in reversed(state.flow_configs.values()):
if "activated" in flow_config.decorators:
input_event = InternalEvent(
name="StartFlow",
arguments={
"flow_id": flow_config.id,
"source_flow_instance_uid": main_flow_state.uid,
"flow_instance_uid": new_readable_uid(flow_config.id),
"flow_hierarchy_position": f"0.0.{idx}",
"source_head_uid": list(main_flow_state.heads.values())[
0
].uid,
"activated": True,
},
)
input_events.insert(1, input_event)
idx += 1

# Check if we have new finished async local action events to add
(
Expand Down
73 changes: 73 additions & 0 deletions tests/v2_x/test_module_flow_activation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging

from rich.logging import RichHandler

from nemoguardrails import RailsConfig
from tests.utils import TestChat

FORMAT = "%(message)s"
logging.basicConfig(
level=logging.DEBUG,
format=FORMAT,
datefmt="[%X,%f]",
handlers=[RichHandler(markup=True)],
)


def test_1():
"""Test use of expression as statements."""
config = RailsConfig.from_content(
colang_content="""
@activated
flow a
match UtteranceUserActionFinished(final_transcript="A")
await UtteranceBotAction(script="A")

@activated
flow b
match UtteranceUserActionFinished(final_transcript="B")
await UtteranceBotAction(script="B")

flow main
match UtteranceUserActionFinished(final_transcript="Main")
await UtteranceBotAction(script="Main")
""",
yaml_content="""
colang_version: "2.x"
""",
)

chat = TestChat(
config,
llm_completions=[],
)

chat >> "A"
chat << "A"
chat >> "A"
chat << "A"
chat >> "B"
chat << "B"
chat >> "Main"
chat << "Main"
chat >> "A"
chat << "A"


if __name__ == "__main__":
test_1()
Loading