nemoguardrails.llm.taskmanager
Module Contents
Classes
Data
API
Interface for interacting with an LLM in a task-oriented way.
Helper to extract the general instructions.
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.
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.
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.
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
contextis consulted first; its value seeds the candidate result. self.prompt_contextis consulted second, but it overrides the argument only when its value is itself a list. This keeps a list supplied incontextsafe from being clobbered by a scalar fallback inself.prompt_context, while still allowing a list inself.prompt_contextto 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.
Return the maximum number of tokens for the given task.
Return the stop sequence for the given task.
Parses the output of a task using the configured output parser.
Parameters:
The task for which the output is being parsed.
The output string to be parsed.
An optional parser name to force
Returns: str
The parsed text output.
Register a custom filter for the rails configuration.
Register a custom output parser for the rails configuration.
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.
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.