nemoguardrails.actions.llm.utils
Colang-facing helpers for the LLM generation path.
Conversation-history rendering, event accessors, and the completion parsers that depend
on Colang intent/action vocabulary, used by the Colang runtimes and by
nemoguardrails.actions.llm.generation.
This module imports the Colang v2.x AST and runtime, so importing it pulls in the Colang runtime. Two groups of helpers were moved out for that reason, and are re-exported below so existing imports keep working:
- model invocation -> :mod:
nemoguardrails.llm.call - Colang-free completion text helpers -> :mod:
nemoguardrails.llm.completion_parsing
Prefer importing those from their canonical modules; a rail action that imports them from here will drag the Colang runtime into its dependency graph.
Module Contents
Functions
Data
API
Flatten a multimodal user-message payload into a string for colang history.
Multimodal user events carry event_text as a list of OpenAI-style
content parts ([{"type": "text", "text": "..."}, {"type": "image_url", "image_url": {...}}, ...]). Including the full list in the colang
history bloats the context with raw base64 data; this helper extracts the
visible text parts and appends a [+ image] marker when one or more
image parts were present.
Non-string text fields (None or other types) inside a content part
are skipped so the " ".join(...) step cannot crash. If the message
is image-only, the result is just "[+ image]" without a leading
space.
Parameters:
Either a string (already flat) or a list of multimodal content parts.
Returns: str
The flattened text. A list input always produces a string; a string
Check if a string has an unclosed double quote (ignoring escaped quotes).
Escape invalid keywords in flow names.
Create the dialog history based on provided events.
Extract tool_calls from runtime events.
StartToolCallBotAction carries the tool calls that passed tool-output
rails and should be returned to the caller. BotToolCalls is used as a
fallback for paths that do not emit the post-rail action event.
Converts a flow to colang format.
Example flow:
to colang:
convert log message to prompt interaction identifier.
Get the current reasoning trace and clear it from the context.
Returns: Optional[str]
Optional[str]: The reasoning trace if one exists, None otherwise.
Get the current response metadata and clear it from the context.
Returns: Optional[dict]
Optional[dict]: The response metadata if it exists, None otherwise.
Get the current tool calls and clear them from the context.
Returns: Optional[list]
Optional[list]: The tool calls if they exist, None otherwise.
Creates a history of user messages and bot responses in colang format. user “Hi, how are you today?” express greeting bot express greeting “Greetings! I am the official NVIDIA Benefits Ambassador AI bot and I’m here to assist you.” user “What can you help me with?” ask capabilities bot inform capabilities “As an AI, I can provide you with a wide range of services, such as …”
Returns first bot action.
Returns first bot intent.
Helper that returns the first non-empty line from a string
Returns first user intent.
Returns the first action before an empty line.
Returns the last user intent from the events.
Returns the last bot utterance from the events.
Returns the last user intent from the events.
Returns the last user utterance from the events.
Returns the last user utterance from the events.
Returns the last user utterance from the events.
Returns the retrieved chunks for current user utterance from the events.
Helper that returns a list with the top k non-empty lines from a string.
If there are less than k non-empty lines, it returns a smaller number of lines.
Helper that given a history in colang format, removes all texts.