nemoguardrails.llm.output_parsers

View as Markdown

Module Contents

Functions

NameDescription
_extract_harm_valueHelper function to extract harmful/unharmful value for a given field.
_parse_unsafe_violationsHelper function to parse violations from unsafe response.
_replace_prefixHelper function to replace a prefix from a string.
_strip_think_tagsHelper function to strip <think>…</think> tags from model response.
bot_intent_parserParses the bot intent.
bot_message_parserParses the bot messages.
is_content_safeAnalyzes a given response from a guardrails check (e.g., content safety check or input check) and determines if the content is safe or not.
nemoguard_parse_prompt_safetyAnalyzes a given model response from a Guardrails check (e.g., content safety check or input check) and determines if the content is safe or not.
nemoguard_parse_response_safetyAnalyzes a given model response from a Guardrails check (e.g., content safety check or output check) and determines if the content is safe or not.
nemotron_reasoning_parse_prompt_safetyAnalyzes a response from Nemotron-Content-Safety-Reasoning-4B for prompt safety.
nemotron_reasoning_parse_response_safetyAnalyzes a response from Nemotron-Content-Safety-Reasoning-4B for response safety.
user_intent_parserParses the user intent.
verbose_v1_parserParses completions generated using the verbose_v1 formatter.

API

nemoguardrails.llm.output_parsers._extract_harm_value(
response: str,
field_name: str
) -> str

Helper function to extract harmful/unharmful value for a given field.

Parameters:

response
str

The model response text (with think tags already stripped).

field_name
str

The field to look for (e.g., “Prompt harm” or “Response Harm”).

Returns: str

The extracted value (“harmful” or “unharmful”), or “harmful” if parsing fails.

nemoguardrails.llm.output_parsers._parse_unsafe_violations(
response_text
)

Helper function to parse violations from unsafe response.

nemoguardrails.llm.output_parsers._replace_prefix(
s: str,
prefix: str,
repl: str
)

Helper function to replace a prefix from a string.

nemoguardrails.llm.output_parsers._strip_think_tags(
response: str
) -> str

Helper function to strip <think>…</think> tags from model response.

nemoguardrails.llm.output_parsers.bot_intent_parser(
s: str
)

Parses the bot intent.

nemoguardrails.llm.output_parsers.bot_message_parser(
s: str
)

Parses the bot messages.

nemoguardrails.llm.output_parsers.is_content_safe(
response: str
) -> typing.Sequence[typing.Union[bool, str]]

Analyzes a given response from a guardrails check (e.g., content safety check or input check) and determines if the content is safe or not.

The function operates based on the presence of certain keywords in the response:

  • If the response contains “safe”, the content is deemed safe.
  • If the response contains “unsafe” or “yes”, the content is deemed unsafe.
  • If the response contains “no”, the content is deemed safe.

Note: If you are using this function for a different task with a custom prompt, ensure to update the logic to match the new context. Here, “yes” implies the content should be blocked, is unsafe, or violates a policy, while “no” implies the content is safe or does not violate any policy.

For example, in the self_check_facts action, we have a different interpretation. Here, “yes” indicates the content is safe. This is against the default logic of the is_content_safe function. Therefore, we invert the result of is_content_safe to align with this interpretation. If is_content_safe returns False (content is safe), result will be 0.0. If is_content_safe returns True (content is unsafe), result will be 1.0.

Parameters:

response
str

The response string to analyze.

Returns: Sequence[Union[bool, str]]

Sequence[Union[bool, str]]: A sequence where the first element is a boolean indicating the safety of the content (True if safe, False otherwise),

nemoguardrails.llm.output_parsers.nemoguard_parse_prompt_safety(
response: str
) -> typing.Sequence[typing.Union[bool, str]]

Analyzes a given model response from a Guardrails check (e.g., content safety check or input check) and determines if the content is safe or not.

The function operates based on the following expected structured JSON output from the NemoGuard ContentSafety model.

{ “User Safety”: “unsafe”, “Violated Categories”: “category1, category2” }

Parameters:

response
str

The response string to analyze.

Returns: Sequence[Union[bool, str]]

Sequence[Union[bool, str]]: A sequence where the first element is a boolean indicating the safety of the content (True if safe, False otherwise),

nemoguardrails.llm.output_parsers.nemoguard_parse_response_safety(
response: str
) -> typing.Sequence[typing.Union[bool, str]]

Analyzes a given model response from a Guardrails check (e.g., content safety check or output check) and determines if the content is safe or not.

The function operates based on the following expected structured JSON output from the NemoGuard ContentSafety model.

{ “User Safety”: “unsafe”, “Response Safety”: “unsafe”, “Violated Categories”: “category1, category2” }

Parameters:

response
str

The response string to analyze.

Returns: Sequence[Union[bool, str]]

Sequence[Union[bool, str]]: A sequence where the first element is a boolean indicating the safety of the content (True if safe, False otherwise),

nemoguardrails.llm.output_parsers.nemotron_reasoning_parse_prompt_safety(
response: str
) -> typing.Sequence[typing.Union[bool, str]]

Analyzes a response from Nemotron-Content-Safety-Reasoning-4B for prompt safety.

The function parses the following expected output format:

Parameters:

response
str

The response string to analyze.

Returns: Sequence[Union[bool, str]]

Sequence[Union[bool, str]]: A sequence where the first element is a boolean

nemoguardrails.llm.output_parsers.nemotron_reasoning_parse_response_safety(
response: str
) -> typing.Sequence[typing.Union[bool, str]]

Analyzes a response from Nemotron-Content-Safety-Reasoning-4B for response safety.

The function parses the following expected output format:

Parameters:

response
str

The response string to analyze.

Returns: Sequence[Union[bool, str]]

Sequence[Union[bool, str]]: A sequence where the first element is a boolean

nemoguardrails.llm.output_parsers.user_intent_parser(
s: str
)

Parses the user intent.

nemoguardrails.llm.output_parsers.verbose_v1_parser(
s: str
)

Parses completions generated using the verbose_v1 formatter.