A package for sentinel detectors and utilities
Project description
Prompt Sentinel
Table of Contents
Introduction
Prompt Sentinel is a Python library that helps safeguard sensitive data during interactions with language models (LLMs). It automatically sanitizes information like passwords, tokens, and secrets before sending input to the LLM, minimizing the risk of unintentional exposure. Once the response is received, the original masked values are restored.
Features
-
Sensitive Data Detection:
Use detectors likeLLMSecretDetectorandPythonStringDataDetectorto identify sensitive or private data in your text. -
Automatic Sanitization through secret masking:
Mechanisms for replacing detected secrets with unique mask tokens (e.g.,__SECRET_1__) so that the LLM operates on sanitized input. -
Following the LLM returned output, the response is decoded to reinstate the original secrets.
-
Decorator Integration:
Easily integrate secret sanitization into your LLM calling pipeline using the@sentineldecorator. Preprocess your messages before they reach the LLM and post-process the responses to decode tokens. -
Caching:
Implement caching for repeated detections on the same text to reduce redundant API calls and improve performance. -
Async LLM calls Support async function/method LLM call decoration.
Installation
Install the package using pip (or include it in your project as needed):
pip install prompt-sentinel
Note: This package requires Python 3.7 or higher.
Usage
Below are examples of how to use Prompt Sentinel in different LLM pipelines. For detailed examples, please refer to the examples directory in the repository.
Decorating an LLM Function Call
@sentinel(detector=LLMSecretDetector(...))
def call_llm(messages):
# Call the LLM with sanitized messages
return response
Wrapping an Entire BaseChatModel
llm = BaseChatModel(...)
wrapped_llm = wrap_chat_model_with_sentinel(llm, detector=LLMSecretDetector(...))
response = wrapped_llm.invoke(messages)
Wrapping LangChain Tools to Automatically Decode Input
from prompt_sentinel.wrappers import wrap_tool_with_decoder
wrapped_tool = wrap_tool_with_decoder(tool)
This ensures tools receive decoded (original) values like passwords, API keys, or tokens.
How It Works
Step-by-step flow:
-
User Input
The user submits a prompt containing potential secrets. -
Sanitize Input via
@sentinel
The decorator intercepts the prompt before it reaches the LLM. -
Detect Secrets with SecretDetector
A detector scans the prompt for sensitive information like passwords, keys, or tokens. -
Replace Secrets with Tokens (e.g.,
__SECRET_1__)
Each secret is replaced by a unique placeholder token and stored in a mapping. -
Send Sanitized Input to LLM
The modified, tokenized prompt is passed to the language model. -
LLM Generates Response with Tokens
The response from the model may include those placeholder tokens. -
Decode LLM Output using Secret Mapping
Tokens are replaced with their original secrets using the stored mapping.
Customization
-
Detectors:
You can implement your own secret detectors by extending theSecretDetectorabstract base class. Check out the provided implementations in thesentinel_detectorsmodule for guidance. -
Context Management:
Internally, a singleton context is used to persist secret mappings during LLM interaction and tool invocation. This ensures secrets encoded in the LLM prompt are automatically decoded before tool execution. -
Caching:
The detectors can use caching to avoid redundant API calls. In the provided implementation ofLLMSecretDetector, caching is handled via an instance variable (_detect_cache).
Contributing
Contributions are welcome! Feel free to submit issues or pull requests on GitHub. When contributing, please follow the guidelines in our CONTRIBUTING.md.
License
This project is licensed under the MIT License. See the LICENSE file for details.
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_sentinel-0.1.2.tar.gz.
File metadata
- Download URL: prompt_sentinel-0.1.2.tar.gz
- Upload date:
- Size: 13.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2c28e3679a7c21f252f0f9ec47c7b48b334404195934b2026c25bd590266aa23
|
|
| MD5 |
0e525846567d7e0ff0331221f3b1c3e3
|
|
| BLAKE2b-256 |
ccec61bd0e15e717576b868901f9f4fe08f814cdabaff1acb13ac1550f4190f4
|
File details
Details for the file prompt_sentinel-0.1.2-py3-none-any.whl.
File metadata
- Download URL: prompt_sentinel-0.1.2-py3-none-any.whl
- Upload date:
- Size: 14.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ec5f7f9038a1e6bfcc1aa30e743245d5890e19492f65ed215e895fe65db26621
|
|
| MD5 |
ce4ffcf29375fefc5f2114d06c23ef49
|
|
| BLAKE2b-256 |
6aae2e852fc5d913f2129e04a293010d7de0f4c54669a5ac437be5daf388158f
|