policy-validator


Namepolicy-validator JSON
Version 0.1.0 PyPI version JSON
download
home_pagehttps://github.com/ashkanrafiee/policy_validator
SummaryA policy validation tool using OpenAI Models
upload_time2024-10-15 09:20:48
maintainerNone
docs_urlNone
authorAshkan Rafiee
requires_python>=3.8
licenseNone
keywords openai gpt policy validation content moderation
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# 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`:

```bash
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:

```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:

```plaintext
Policy 1: No abusive language.
Policy 2: No personal information sharing.
Policy 3: No inappropriate content.
```

Now load the policies from this file:

```python
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()`.
  
  ```python
  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.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/ashkanrafiee/policy_validator",
    "name": "policy-validator",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "openai, GPT, policy validation, content moderation",
    "author": "Ashkan Rafiee",
    "author_email": "ashkanrafiee-pypi.cheer081@passmail.net",
    "download_url": "https://files.pythonhosted.org/packages/4f/b9/65b5d3262dcb578f4ac786b8d8cccf3e5b91d0d66529973541b86c61069e/policy_validator-0.1.0.tar.gz",
    "platform": null,
    "description": "\n# Policy Validator\n\nPolicy 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.\n\n## Key Features\n- **OpenAI GPT Integration**: Uses OpenAI models to validate text based on content moderation policies.\n- **Retry Mechanism**: Ensures reliable API calls with built-in retry functionality.\n- **Logging**: Offers comprehensive logging to track and debug policy validation processes.\n- **Flexible Policy Input**: Policies can be supplied either as tuples directly or loaded from a file.\n- **Customizable**: Adjust model parameters such as max tokens, temperature, and the specific OpenAI's model used.\n\n## Installation\n\nTo install the Policy Validator package, use `pip`:\n\n```bash\npip install policy_validator\n```\n\n## Usage\n\nThe `PolicyValidator` class allows you to pass policies either as a tuple or via a file.\n\n### Example 1: Passing Policies as a Tuple\n\nIn this example, policies are defined directly as a tuple in Python:\n\n```python\nfrom policy_validator import PolicyValidator\n\n# Define policies directly as a tuple\npolicies = (\n    \"No abusive language.\",\n    \"No personal information sharing.\",\n    \"No inappropriate content.\"\n)\n\napi_key = \"your_openai_api_key\"\nvalidator = PolicyValidator(api_key=api_key, model=\"gpt-4o-mini\", max_tokens=100, temperature=0.3, policies=policies)\n\n# Validate user input\nresult = validator.validate(\"Who is John Doe?\")\nprint(result)\n```\n\n### Example 2: Passing Policies via a File\n\nYou can also load policies from a file. First, create a `policies.txt` file with the following content:\n\n```plaintext\nPolicy 1: No abusive language.\nPolicy 2: No personal information sharing.\nPolicy 3: No inappropriate content.\n```\n\nNow load the policies from this file:\n\n```python\nfrom policy_validator import PolicyValidator\n\napi_key = \"your_openai_api_key\"\nvalidator = PolicyValidator(api_key=api_key, model=\"gpt-4o-mini\", max_tokens=100, temperature=0.3, policies_file=\"policies.txt\")\n\n# Validate user input\nresult = validator.validate(\"Who is Ashkan Rafiee?\")\nprint(result)\n```\n\n## Important: Securely Managing API Keys\n\nIt'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:\n\n- **Environment variables**: Store the API key in an environment variable and access it via `os.getenv()`.\n  \n  ```python\n  import os\n  api_key = os.getenv(\"OPENAI_API_KEY\")\n  ```\n\n- **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.\n\n\nFor any issues, feel free to submit a bug report or open a discussion.\n\n## License\n\nThis project is licensed under the MIT License. See the `LICENSE` file for details.\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A policy validation tool using OpenAI Models",
    "version": "0.1.0",
    "project_urls": {
        "Homepage": "https://github.com/ashkanrafiee/policy_validator"
    },
    "split_keywords": [
        "openai",
        " gpt",
        " policy validation",
        " content moderation"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2c50d32cf79fa7805ad88ef6b7f046f7bca0a1f6e8614e21e7dde9af627f208b",
                "md5": "e95798641decd633cd4114ae45586db1",
                "sha256": "cc79b7b2dff41923d8bbdc4b0018aa565a06c8a93933d3e9de11b1bb395f9bea"
            },
            "downloads": -1,
            "filename": "policy_validator-0.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e95798641decd633cd4114ae45586db1",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 5538,
            "upload_time": "2024-10-15T09:20:46",
            "upload_time_iso_8601": "2024-10-15T09:20:46.521135Z",
            "url": "https://files.pythonhosted.org/packages/2c/50/d32cf79fa7805ad88ef6b7f046f7bca0a1f6e8614e21e7dde9af627f208b/policy_validator-0.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4fb965b5d3262dcb578f4ac786b8d8cccf3e5b91d0d66529973541b86c61069e",
                "md5": "0161e3d8e240345940866a77c3f88eb4",
                "sha256": "43ccea4bf3582854e323e1eda461f1b839d9ce0d58156d502f4d003f06d7985b"
            },
            "downloads": -1,
            "filename": "policy_validator-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "0161e3d8e240345940866a77c3f88eb4",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 4913,
            "upload_time": "2024-10-15T09:20:48",
            "upload_time_iso_8601": "2024-10-15T09:20:48.654611Z",
            "url": "https://files.pythonhosted.org/packages/4f/b9/65b5d3262dcb578f4ac786b8d8cccf3e5b91d0d66529973541b86c61069e/policy_validator-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-15 09:20:48",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "ashkanrafiee",
    "github_project": "policy_validator",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "policy-validator"
}
        
Elapsed time: 0.42774s