nemoguardrails.library.prompt_security.actions

View as Markdown

Prompt/Response protection using Prompt Security.

Module Contents

Functions

NameDescription
protect_textProtects the given user_prompt or bot_response.
protect_text_mappingMapping for protect_text action.
ps_protect_api_asyncCalls Prompt Security Protect API asynchronously.

Data

log

API

nemoguardrails.library.prompt_security.actions.protect_text(
user_prompt: typing.Optional[str] = None,
bot_response: typing.Optional[str] = None,
kwargs = {}
)
async

Protects the given user_prompt or bot_response. Args: user_prompt: The user message to protect. bot_response: The bot message to protect. Returns: A dictionary with the following items:

  • is_blocked: True if the text should be blocked, False otherwise.
  • is_modified: True if the text should be modified, False otherwise.
  • modified_text: The modified text if is_modified is True, None otherwise. Raises: ValueError is returned in one of the following cases:
  1. If PS_PROTECT_URL env variable is not set.
  2. If PS_APP_ID env variable is not set.
  3. If no user_prompt and no bot_response is provided.
nemoguardrails.library.prompt_security.actions.protect_text_mapping(
result: dict
) -> bool

Mapping for protect_text action.

Returns: bool

True if the response should be blocked (i.e. if “is_blocked” is True),

nemoguardrails.library.prompt_security.actions.ps_protect_api_async(
ps_protect_url: str,
ps_app_id: str,
prompt: typing.Optional[str] = None,
system_prompt: typing.Optional[str] = None,
response: typing.Optional[str] = None,
user: typing.Optional[str] = None
)
async

Calls Prompt Security Protect API asynchronously.

Parameters:

ps_protect_url
str

the URL of the protect endpoint given by Prompt Security.

URL is https

//[REGION].prompt.security/api/protect where REGION is eu, useast or apac

ps_app_id
str

the application ID given by Prompt Security (similar to an API key).

Get it from the admin portal at https

//[REGION].prompt.security/ where REGION is eu, useast or apac

prompt
Optional[str]Defaults to None

the user message to protect.

system_prompt
Optional[str]Defaults to None

the system message for context.

response
Optional[str]Defaults to None

the bot message to protect.

user
Optional[str]Defaults to None

the user ID or username for context.

Returns:

A dictionary with the following items:

nemoguardrails.library.prompt_security.actions.log = logging.getLogger(__name__)