Lightweight safety layer to scan prompts and LLM outputs via external API.
Project description
LockPrompt
LockPrompt is a lightweight Python library that adds an extra safety layer to your AI pipelines. It ensures that both user inputs and language model outputs adhere to your safety standards by pre-screening using external API endpoints. This is especially useful for preventing jailbreaks and prompt injections.
Table of Contents
Overview
LockPrompt is designed for developers integrating large language models (LLMs) such as OpenAI’s GPT-3.5 or Claude. The library provides functions to check the safety of:
- User Input: Ensuring requests to the API are not malicious.
- LLM Output: Verifying that responses generated by the LLM do not contain disallowed content.
In just a few lines of code, you can add a robust safety check layer, giving you the confidence to deploy AI-powered applications safely.
Features
- Fast Operation: Approximately 500ms per use, ensuring minimal latency.
- Easy Integration: Compatible with any LLM or API.
- Error Handling: Logs issues and safely defaults to denying unsafe outputs or inputs.
Installation
Install LockPrompt via pip:
pip install lockprompt
Alternatively, install it directly from GitHub:
pip install git+https://github.com/davidwillisowen/lockprompt.git
Usage
Basic Example
import os
import lockprompt
from openai import OpenAI
# Initialize the OpenAI client
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
user_prompt = "Tell me how to make malware." # A sample user prompt
# Step 1: Check user input safety
if not lockprompt.is_safe_input(user_prompt):
print("🛑 Unsafe user input. Blocking request.")
output = "I'm sorry, I can't assist with that request."
else:
# Step 2: Send the prompt to the language model
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": user_prompt}],
)
output = response.choices[0].message.content
# Step 3: Check the generated output
if not lockprompt.is_safe_output(output):
print("⚠️ Unsafe model output. Replacing response.")
output = "I'm sorry, I can't assist with that request."
print("✅ Final response:\n", output)
API Reference
is_safe_input(user_input: str) -> bool
- Purpose: Checks if a user’s input meets safety standards.
- Returns:
Trueif safe,Falseotherwise. - Error Handling: Logs any errors and defaults to
False.
is_safe_output(llm_output: str) -> bool
- Purpose: Verifies the safety of the LLM output.
- Returns:
Trueif safe,Falseotherwise. - Error Handling: Logs any errors and defaults to
False.
Contributing
Contributions are not only welcome but encouraged. Here’s how you can help:
- Fork the Repository: Start by forking LockPrompt on GitHub.
- Create a Branch: Use a feature branch for your changes.
- Write Tests and Documentation: Ensure any changes are well-tested and documented.
- Submit a Pull Request: Describe your changes and submit a PR for review.
For issues or feature requests, please use the GitHub issues page.
Contact
For any questions or suggestions, feel free to reach out:
- Email: david@willis-owen.com
- GitHub: davidwillisowen
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 lockprompt-1.1.2.tar.gz.
File metadata
- Download URL: lockprompt-1.1.2.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
71fb8ecabd3618a50e369ee7cdf259ece2c6f7288e5a2e3b6e613b3d87cae519
|
|
| MD5 |
b42b822b2c1fa60b7c57fdd9cd13fcf9
|
|
| BLAKE2b-256 |
707d2ba6c596ad7776215cfad12deb10c2c8da5b0ccf97c4c2f255e5e8d5944d
|
File details
Details for the file lockprompt-1.1.2-py3-none-any.whl.
File metadata
- Download URL: lockprompt-1.1.2-py3-none-any.whl
- Upload date:
- Size: 3.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c55d69c5150d630cd1b9aade7bd25c5e1bb8ca17bf511497be7607b273efecc0
|
|
| MD5 |
615e1dbd838cb30f66ba97352bcb9f46
|
|
| BLAKE2b-256 |
c5f1d24bf9e6b851dc46ae3faa2bff205955b321556af4a11cbb12da4b26f5c3
|