nemoguardrails.llm.output_parsers
nemoguardrails.llm.output_parsers
Module Contents
Functions
API
Helper function to extract harmful/unharmful value for a given field.
Parameters:
The model response text (with think tags already stripped).
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.
Helper function to parse violations from unsafe response.
Helper function to replace a prefix from a string.
Helper function to strip <think>…</think> tags from model response.
Parses the bot intent.
Parses the bot messages.
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:
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),
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:
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),
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:
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),
Analyzes a response from Nemotron-Content-Safety-Reasoning-4B for prompt safety.
The function parses the following expected output format:
Parameters:
The response string to analyze.
Returns: Sequence[Union[bool, str]]
Sequence[Union[bool, str]]: A sequence where the first element is a boolean
Analyzes a response from Nemotron-Content-Safety-Reasoning-4B for response safety.
The function parses the following expected output format:
Parameters:
The response string to analyze.
Returns: Sequence[Union[bool, str]]
Sequence[Union[bool, str]]: A sequence where the first element is a boolean
Parses the user intent.
Parses completions generated using the verbose_v1 formatter.