Skip to content

Commit aa04453

Browse files
committed
fix _get_action_details_from_flow
1 parent 4152e21 commit aa04453

File tree

1 file changed

+48
-14
lines changed

1 file changed

+48
-14
lines changed

nemoguardrails/rails/llm/llmrails.py

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,22 +1309,56 @@ def _update_explain_info():
13091309

13101310

13111311
def _get_action_details_from_flow_id(
1312-
flow_id: str, flows: List[Union[Dict, Any]]
1312+
flow_id: str,
1313+
flows: List[Union[Dict, Any]],
1314+
prefixes: Optional[List[str]] = None,
13131315
) -> Tuple[str, Any]:
1314-
"""Get the action name and parameters from the flow id."""
1316+
"""Get the action name and parameters from the flow id.
1317+
1318+
First, try to find an exact match.
1319+
If not found, then if the provided flow_id starts with one of the special prefixes,
1320+
return the first flow whose id starts with that same prefix.
1321+
"""
1322+
1323+
supported_prefixes = [
1324+
"content safety check output",
1325+
"content safety check",
1326+
"topic safety check",
1327+
]
1328+
if prefixes:
1329+
supported_prefixes.extend(prefixes)
1330+
1331+
candidate_flow = None
1332+
13151333
for flow in flows:
1334+
# If exact match, use it
1335+
if flow["id"] == flow_id:
1336+
candidate_flow = flow
1337+
break
1338+
1339+
# If no exact match, check if both the provided flow_id and this flow's id share a special prefix
1340+
for prefix in supported_prefixes:
1341+
if flow_id.startswith(prefix) and flow["id"].startswith(prefix):
1342+
candidate_flow = flow
1343+
# We don't break immediately here because an exact match would have been preferred,
1344+
# but since we’re in the else branch it’s fine to choose the first matching candidate.
1345+
# TODO:we should avoid having multiple matchin prefixes
1346+
break
1347+
1348+
if candidate_flow is not None:
1349+
break
1350+
1351+
if candidate_flow is None:
1352+
raise ValueError(f"No action found for flow_id: {flow_id}")
1353+
1354+
# we have identified a candidate, look for the run_action element.
1355+
for element in candidate_flow["elements"]:
13161356
if (
1317-
flow_id == flow["id"]
1318-
or flow_id.startswith("content safety check")
1319-
or flow_id.startswith("topic safety check")
1357+
element["_type"] == "run_action"
1358+
and element["_source_mapping"]["filename"] == "flows.v1.co"
1359+
and "execute" in element["_source_mapping"]["line_text"]
1360+
and "action_name" in element
13201361
):
1321-
for element in flow["elements"]:
1322-
if (
1323-
element["_type"] == "run_action"
1324-
and element["_source_mapping"]["filename"] == "flows.v1.co"
1325-
and "execute" in element["_source_mapping"]["line_text"]
1326-
and "action_name" in element
1327-
):
1328-
return element["action_name"], element["action_params"]
1362+
return element["action_name"], element["action_params"]
13291363

1330-
raise ValueError(f"No action found for flow_id: {flow_id}")
1364+
raise ValueError(f"No run_action element found for flow_id: {flow_id}")

0 commit comments

Comments
 (0)