A policy validation tool using OpenAI Models
Project description
Policy Validator
Policy Validator is a Python package that leverages OpenAI's GPT models to validate text against predefined content policies. It ensures that user input conforms to specific rules, making it useful for content moderation, compliance checks, and safety regulations.
Key Features
- OpenAI GPT Integration: Uses OpenAI models to validate text based on content moderation policies.
- Retry Mechanism: Ensures reliable API calls with built-in retry functionality.
- Logging: Offers comprehensive logging to track and debug policy validation processes.
- Flexible Policy Input: Policies can be supplied either as tuples directly or loaded from a file.
- Customizable: Adjust model parameters such as max tokens, temperature, and the specific OpenAI's model used.
Installation
To install the Policy Validator package, use pip:
pip install policy_validator
Usage
The PolicyValidator class allows you to pass policies either as a tuple or via a file.
Example 1: Passing Policies as a Tuple
In this example, policies are defined directly as a tuple in Python:
from policy_validator import PolicyValidator
# Define policies directly as a tuple
policies = (
"No abusive language.",
"No personal information sharing.",
"No inappropriate content."
)
api_key = "your_openai_api_key"
validator = PolicyValidator(api_key=api_key, model="gpt-4o-mini", max_tokens=100, temperature=0.3, policies=policies)
# Validate user input
result = validator.validate("Who is John Doe?")
print(result)
Example 2: Passing Policies via a File
You can also load policies from a file. First, create a policies.txt file with the following content:
Policy 1: No abusive language.
Policy 2: No personal information sharing.
Policy 3: No inappropriate content.
Now load the policies from this file:
from policy_validator import PolicyValidator
api_key = "your_openai_api_key"
validator = PolicyValidator(api_key=api_key, model="gpt-4o-mini", max_tokens=100, temperature=0.3, policies_file="policies.txt")
# Validate user input
result = validator.validate("Who is Ashkan Rafiee?")
print(result)
Important: Securely Managing API Keys
It's recommended to avoid hardcoding your API key in your code as shown in the above examples. Instead, use a more secure method, such as:
-
Environment variables: Store the API key in an environment variable and access it via
os.getenv().import os api_key = os.getenv("OPENAI_API_KEY")
-
Secret management systems: Use secure secret management tools such as AWS Secrets Manager, Azure Key Vault, or HashiCorp Vault to securely store and access the API key in a managed way.
For any issues, feel free to submit a bug report or open a discussion.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
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 policy_validator-0.1.0.tar.gz.
File metadata
- Download URL: policy_validator-0.1.0.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.8.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
43ccea4bf3582854e323e1eda461f1b839d9ce0d58156d502f4d003f06d7985b
|
|
| MD5 |
0161e3d8e240345940866a77c3f88eb4
|
|
| BLAKE2b-256 |
4fb965b5d3262dcb578f4ac786b8d8cccf3e5b91d0d66529973541b86c61069e
|
File details
Details for the file policy_validator-0.1.0-py3-none-any.whl.
File metadata
- Download URL: policy_validator-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.8.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cc79b7b2dff41923d8bbdc4b0018aa565a06c8a93933d3e9de11b1bb395f9bea
|
|
| MD5 |
e95798641decd633cd4114ae45586db1
|
|
| BLAKE2b-256 |
2c50d32cf79fa7805ad88ef6b7f046f7bca0a1f6e8614e21e7dde9af627f208b
|