Prompt Defender. A package to help you defend against prompt injection attacks.
Project description
README
Prompt Defender Library
Welcome to the Prompt Defender library, a Python package designed to help you defend against prompt injection attacks. This library provides a robust framework for generating safe prompts and validating user inputs to ensure the security of your applications.
Table of Contents
Installation
You can install the library using pip:
pip install prompt-defender
Usage
The Prompt Defender library is designed to be easy to integrate into your Python applications. Below is an example of how to use the library:
Examples
The example.py file contains several examples demonstrating how to use the library. Here is a simple example to get you started:
from prompt_defender import Defence, build_drawbridge, build_xml_scanner, build_prompt_validator
# Configure the defence mechanisms
defence = Defence(
wall=[
build_xml_scanner(),
build_prompt_validator(max_length=100)
],
keep=None,
drawbridge=build_drawbridge(allow_unsafe_scripts=False)
)
# Prepare a prompt
safe_prompt_response = defence.prepare_prompt("Your job is to answer user questions about cats {user_question}", False)
# Check user input
is_safe, cleaned_instruction = defence.is_user_input_safe("What is the best cat? " + safe_prompt_response.safe_prompt)
# Check prompt output
output_response = defence.check_prompt_output("The best cat is a Maine Coon.")
print(f"Output is safe: {output_response.is_safe}, Cleaned response: {output_response.cleaned_response}")
For more detailed examples, please refer to the example.py file.
Modules
Defence
The Defence class integrates several defensive mechanisms against potential prompt injection and jailbreak attempts.
Keep
The Keep module is responsible for generating safe prompts and includes the RemoteKeepExecutor class for interacting with remote services.
Wall
The Wall module contains various validators for checking user input and includes classes such as PromptValidator, BasicXmlScanner, and PromptDefenderClient.
Drawbridge
The Drawbridge module provides mechanisms for validating and cleaning the responses of a language model, including the DefaultDrawbridgeExecutor class.
Testing
Unit tests are included in the tests directory. You can run the tests using unittest:
python -m unittest discover -s prompt_defender/tests
License
This project is licensed under the Apache License. See the LICENSE file for more details.
Contact
For any questions, issues, or contributions, please contact the project owner at admin@safetorun.com.
Thank you for using Prompt Defender! Together, we can create safer applications.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file prompt_defender-0.1.26.tar.gz.
File metadata
- Download URL: prompt_defender-0.1.26.tar.gz
- Upload date:
- Size: 21.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.10.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3adad5623381ddf9e5f7971d020ad43848a5a1d93d918562d4e3dae0e7543189
|
|
| MD5 |
61a934d8eb0a8c0764ba05d5b4923e5a
|
|
| BLAKE2b-256 |
82d5c6b5a6a9ae565ce41ee95a27667bf4acaa1939f4cb4d81dd9d0d4b202d3f
|
File details
Details for the file prompt_defender-0.1.26-py3-none-any.whl.
File metadata
- Download URL: prompt_defender-0.1.26-py3-none-any.whl
- Upload date:
- Size: 16.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.10.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cfdf4725819c83e8810b6f008c04c863c4450b050574c95f89857206e6e9b94a
|
|
| MD5 |
32906c105ec331e72914bcd145214344
|
|
| BLAKE2b-256 |
77d18a64d033f79886b25b2f5f554523b02df9a8630ee8112b364a4f2c0f8196
|