nemoguardrails.llm.taskmanager

View as Markdown

Module Contents

Classes

NameDescription
LLMTaskManagerInterface for interacting with an LLM in a task-oriented way.

Data

_SINGLE_VAR_PATTERN

API

class nemoguardrails.llm.taskmanager.LLMTaskManager(
config: nemoguardrails.rails.llm.config.RailsConfig
)

Interface for interacting with an LLM in a task-oriented way.

env
= SandboxedEnvironment()
output_parsers
Dict[Optional[str], Callable]
prompt_context
= {}
nemoguardrails.llm.taskmanager.LLMTaskManager._get_general_instructions()

Helper to extract the general instructions.

nemoguardrails.llm.taskmanager.LLMTaskManager._get_messages_text_length(
messages: typing.List[dict]
) -> int

Return the length of the text in the messages for token counting purposes.

This method calculates text length for token limit checks, using placeholders for base64 images instead of counting their full encoded size. This allows multimodal content with large base64 images to pass the length checks while still preserving the actual content.

nemoguardrails.llm.taskmanager.LLMTaskManager._render_messages(
message_templates: typing.List[typing.Union[str, nemoguardrails.rails.llm.config.MessageTemplate]],
context: typing.Optional[dict] = None,
events: typing.Optional[typing.List[dict]] = None
) -> typing.List[dict]

Render a sequence of messages.

:param message_templates: The message templates to render. :param context: The context for rendering the prompt. :param events: The history of events so far. :return: The rendered messages.

nemoguardrails.llm.taskmanager.LLMTaskManager._render_string(
template_str: str,
context: typing.Optional[dict] = None,
events: typing.Optional[typing.List[dict]] = None
) -> str

Render a template using the provided context information.

:param template_str: The template to render. :param context: The context for rendering the prompt. :param events: The history of events so far. :return: The rendered template. :rtype: str.

nemoguardrails.llm.taskmanager.LLMTaskManager._resolve_message_content(
template_str: str,
context: typing.Optional[dict] = None,
events: typing.Optional[typing.List[dict]] = None
) -> typing.Union[str, list]

Resolve a message-template body to either a list or a rendered string.

When template_str matches a single-variable pattern such as "{{ user_input }}", the variable is looked up directly so that a list value (for example, multimodal [{"type": "text", ...}, ...] content) is preserved as a list instead of being stringified by Jinja.

Lookup precedence for the single-variable case:

  • The argument context is consulted first; its value seeds the candidate result.
  • self.prompt_context is consulted second, but it overrides the argument only when its value is itself a list. This keeps a list supplied in context safe from being clobbered by a scalar fallback in self.prompt_context, while still allowing a list in self.prompt_context to win when the caller did not pass one.

If neither path yields a list (or template_str is not a single variable), the method falls back to the regular Jinja render, which coerces values to strings.

nemoguardrails.llm.taskmanager.LLMTaskManager.get_max_tokens(
task: typing.Union[str, nemoguardrails.llm.types.Task]
) -> typing.Optional[int]

Return the maximum number of tokens for the given task.

nemoguardrails.llm.taskmanager.LLMTaskManager.get_stop_tokens(
task: typing.Union[str, nemoguardrails.llm.types.Task]
) -> typing.Optional[typing.List[str]]

Return the stop sequence for the given task.

nemoguardrails.llm.taskmanager.LLMTaskManager.has_output_parser(
task: nemoguardrails.llm.types.Task
)
nemoguardrails.llm.taskmanager.LLMTaskManager.parse_task_output(
task: nemoguardrails.llm.types.Task,
output: str,
forced_output_parser: typing.Optional[str] = None
) -> str

Parses the output of a task using the configured output parser.

Parameters:

task
Task

The task for which the output is being parsed.

output
str

The output string to be parsed.

forced_output_parser
Optional[str]Defaults to None

An optional parser name to force

Returns: str

The parsed text output.

nemoguardrails.llm.taskmanager.LLMTaskManager.register_filter(
filter_fn: typing.Callable,
name: typing.Optional[str] = None
)

Register a custom filter for the rails configuration.

nemoguardrails.llm.taskmanager.LLMTaskManager.register_output_parser(
output_parser: typing.Callable,
name: str
)

Register a custom output parser for the rails configuration.

nemoguardrails.llm.taskmanager.LLMTaskManager.register_prompt_context(
name: str,
value_or_fn: typing.Any
)

Register a value to be included in the prompt context.

:name: The name of the variable or function that will be used. :value_or_fn: The value or function that will be used to generate the value.

nemoguardrails.llm.taskmanager.LLMTaskManager.render_task_prompt(
task: typing.Union[str, nemoguardrails.llm.types.Task],
context: typing.Optional[dict] = None,
events: typing.Optional[typing.List[dict]] = None,
force_string_to_message: typing.Optional[bool] = False
) -> typing.Union[str, typing.List[dict]]

Render the prompt for a specific task.

:param task: The name of the task. :param context: The context for rendering the prompt :param events: The history of events so far. :param force_string_to_message: Force the string message to a user message. This should be used for chat models that receive a single message in the task prompt.

:return: A string, for completion models, or an array of messages for chat models.

Note that even chat models can have task prompts defined using a string and not an array of messages. In this case, the chat model will through an error. If you want to solve this problem, use the force_string_to_message parameter to force the string message to a user message.

nemoguardrails.llm.taskmanager._SINGLE_VAR_PATTERN = re.compile('^\\{\\{\\s*(\\w+)\\s*\\}\\}$')