nemoguardrails.library.prompt_security.actions

View as Markdown

Prompt/Response protection using Prompt Security.

Module Contents

Functions

NameDescription
_protect_text_outcome-
protect_textProtects the given user_prompt or bot_response.
ps_protect_api_asyncCalls Prompt Security Protect API asynchronously.

Data

log

API

nemoguardrails.library.prompt_security.actions._protect_text_outcome(
result: dict,
target: nemoguardrails.actions.rail_outcome.TransformTarget
) -> nemoguardrails.actions.rail_outcome.RailOutcome
nemoguardrails.library.prompt_security.actions.protect_text(
user_prompt: typing.Optional[str] = None,
bot_response: typing.Optional[str] = None,
http_client: typing.Optional[nemoguardrails.http.HTTPClient] = None,
kwargs = {}
) -> nemoguardrails.actions.rail_outcome.RailOutcome
async

Protects the given user_prompt or bot_response.

Provider results may allow, block, or transform the selected message. Provider failures fail open. A bot response takes precedence when both content arguments are provided.

Returns: RailOutcome.block(), RailOutcome.transform(), or RailOutcome.allow(). 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.

Parameters:

user_prompt
Optional[str]Defaults to None

The user message to protect.

bot_response
Optional[str]Defaults to None

The bot message to protect.

http_client
Optional[HTTPClient]Defaults to None

Optional caller-owned HTTP client.

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,
http_client: typing.Optional[nemoguardrails.http.HTTPClient] = None
)
async

Calls Prompt Security Protect API asynchronously.

Provider failures are logged and fail open with the default log action.

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.

http_client
Optional[HTTPClient]Defaults to None

Optional caller-owned HTTP client.

Returns:

A dictionary with the following items:

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