Skip to content

Commit 1cd9c91

Browse files
authored
Merge pull request #823 from NVIDIA/feature/configurable-event-source-uid
Make `source_uid` of events configurable.
2 parents 67bfbb9 + 8e5a070 commit 1cd9c91

File tree

5 files changed

+54
-5
lines changed

5 files changed

+54
-5
lines changed

docs/user_guides/configuration-guide.md

+9
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,14 @@ This temperature will be used for the tasks that require deterministic behavior
556556
lowest_temperature: 0.1
557557
```
558558

559+
### Event Source ID
560+
561+
This ID will be used as the `source_uid` for all events emitted by the Colang runtime. Setting this to something else than the default value (default value is `NeMoGuardrails-Colang-2.x`) is useful if you need to distinguish multiple Colang runtimes in your system (e.g. in a multi-agent scenario).
562+
563+
```yaml
564+
event_source_uid : colang-agent-1
565+
```
566+
559567
### Custom Data
560568

561569
If you need to pass additional configuration data to any custom component for your configuration, you can use the `custom_data` field.
@@ -785,6 +793,7 @@ To enable tracing, set the enabled flag to true under the tracing section in you
785793
tracing:
786794
enabled: true
787795
```
796+
788797
> **Note**: You must install the necessary dependencies to use tracing adapters.
789798

790799
```bash

nemoguardrails/colang/v2_x/runtime/statemachine.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
InternalEvents,
7272
State,
7373
)
74+
from nemoguardrails.rails.llm.config import RailsConfig
7475
from nemoguardrails.utils import console, new_event_dict, new_readable_uuid, new_uuid
7576

7677
log = logging.getLogger(__name__)
@@ -1829,7 +1830,7 @@ def _is_done_flow(flow_state: FlowState) -> bool:
18291830

18301831

18311832
def _generate_umim_event(state: State, event: Event) -> Dict[str, Any]:
1832-
umim_event = create_umim_event(event, event.arguments)
1833+
umim_event = create_umim_event(event, event.arguments, state.rails_config)
18331834
state.outgoing_events.append(umim_event)
18341835
log.info("[bold violet]<- Action[/]: %s", event)
18351836

@@ -2385,10 +2386,14 @@ def create_internal_event(
23852386
return event
23862387

23872388

2388-
def create_umim_event(event: Event, event_args: Dict[str, Any]) -> Dict[str, Any]:
2389+
def create_umim_event(
2390+
event: Event, event_args: Dict[str, Any], config: Optional[RailsConfig]
2391+
) -> Dict[str, Any]:
23892392
"""Returns an outgoing UMIM event for the provided action data"""
23902393
new_event_args = dict(event_args)
2391-
new_event_args["source_uid"] = "NeMoGuardrails-Colang-2.x"
2394+
new_event_args.setdefault(
2395+
"source_uid", config.event_source_uid if config else "NeMoGuardrails-Colang-2.x"
2396+
)
23922397
if isinstance(event, ActionEvent) and event.action_uid is not None:
23932398
if "action_uid" in new_event_args:
23942399
event.action_uid = new_event_args["action_uid"]

nemoguardrails/rails/llm/config.py

+6
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ def _join_config(dest_config: dict, additional_config: dict):
508508
"lowest_temperature",
509509
"enable_multi_step_generation",
510510
"colang_version",
511+
"event_source_uid",
511512
"custom_data",
512513
"prompting_mode",
513514
"knowledge_base",
@@ -863,6 +864,11 @@ class RailsConfig(BaseModel):
863864
"This means it will not be altered in any way. ",
864865
)
865866

867+
event_source_uid: str = Field(
868+
default="NeMoGuardrails-Colang-2.x",
869+
description="The source ID of events sent by the Colang Runtime. Useful to identify the component that has sent an event.",
870+
)
871+
866872
tracing: TracingConfig = Field(
867873
default_factory=TracingConfig,
868874
description="Configuration for tracing.",

tests/utils.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ def is_data_in_events(
319319
return True
320320

321321

322-
def _init_state(colang_content) -> State:
322+
def _init_state(colang_content, yaml_content: Optional[str] = None) -> State:
323323
config = create_flow_configs_from_flow_list(
324324
parse_colang_file(
325325
filename="",
@@ -329,8 +329,11 @@ def _init_state(colang_content) -> State:
329329
)["flows"]
330330
)
331331

332+
rails_config = None
333+
if yaml_content:
334+
rails_config = RailsConfig.from_content(colang_content, yaml_content)
332335
json.dump(config, sys.stdout, indent=4, cls=EnhancedJsonEncoder)
333-
state = State(flow_states=[], flow_configs=config)
336+
state = State(flow_states=[], flow_configs=config, rails_config=rails_config)
334337
initialize_state(state)
335338
print("---------------------------------")
336339
json.dump(state.flow_configs, sys.stdout, indent=4, cls=EnhancedJsonEncoder)

tests/v2_x/test_event_mechanics.py

+26
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,32 @@ def test_send_umim_action_event_overwriting_default_parameters():
8181
)
8282

8383

84+
def test_change_umim_event_source_id():
85+
"""Test to send an UMIM event."""
86+
87+
content = """
88+
flow main
89+
send StartUtteranceBotAction(script="Hello world")
90+
"""
91+
92+
config = """
93+
colang_version: "2.x"
94+
event_source_uid : agent-1
95+
"""
96+
97+
state = run_to_completion(_init_state(content, config), start_main_flow_event)
98+
assert is_data_in_events(
99+
state.outgoing_events,
100+
[
101+
{
102+
"type": "StartUtteranceBotAction",
103+
"script": "Hello world",
104+
"source_uid": "agent-1",
105+
}
106+
],
107+
)
108+
109+
84110
def test_match_umim_action_event():
85111
"""Test to match an UMIM event."""
86112

0 commit comments

Comments
 (0)