Skip to main content

principle alignment package

Project description

Introduction

Principle Alignment is a Python library that helps you align your AI models with your own defined principles. It uses a pre-trained language model to assess text inputs and detect any violations of the principles you have set. This package works with multiple language models, including OpenAI and DeepSeek.

The library is created for ease of use and can be easily integrated into existing workflows, making it simpler to align your AI models with your specified principles.

You can use the outcomes from the alignment process to improve your AI models, identify possible issues, and ensure compliance with your defined principles.

Installation

Install from pypi

You can install the package from pypi

pip install principle-alignment  -i https://pypi.org/simple

You can also upgrade the package from pypi

pip install principle-alignment  --upgrade -i https://pypi.org/simple

Install from source

You can also install the package directly from source:

pip install .

For development installation:

pip install -e .

Usage (Serving Version)

Create a .env file with your API configurations:

API_KEY=your_api_key
BASE_URL=your_base_url  
MODEL=your_model_name

create a principles.md file with the principles you want to align with (one per line):

1. Do no harm
2. Respect user privacy
3. Be transparent

creat a server.py file with the following content:

from principle_alignment.serving import start_server

start_server(
    host="127.0.0.1",
    port=8080,
    principles_path="./principles.md", # Path to pre-defined principles file
    env_file=".env", # Path to environment variables file
    verbose=True
)

run the server:

python server.py

test the server (just align):

curl -X POST "http://localhost:8080/align" \
     -H "Content-Type: application/json" \
     -d '{"text": "we can collect user data without their consent"}'

output:

{
  "is_violation": true,
  "violated_principles": [
    "2. Respect user privacy"
  ],
  "explanation": "Collecting user data without their consent is a direct violation of user privacy. Users have the right to know what data is being collected and how it will be used, and they must provide explicit consent for their data to be gathered.",
  "rectification": null
}

test the server (align and rectify):

curl -X POST "http://localhost:8080/align" \
     -H "Content-Type: application/json" \
     -d '{"text": "we can collect user data without their consent","rectify":true}'

output:

{
  "is_violation": true,
  "violated_principles": [
    "2. Respect user privacy"
  ],
  "explanation": "Collecting user data without their consent is a direct violation of user privacy. Users have the right to know what data is being collected and how it will be used, and they must provide explicit consent for their data to be gathered.",
  "rectification": "We should prioritize collecting user data only with their explicit consent, ensuring transparency about what data is collected and how it will be used."
}

Usage (Detail Version)

Prepare the client and model

import os
from dotenv import load_dotenv
from openai import OpenAI
import json

from principle_alignment import Alignment


load_dotenv() # Load environment variables from .env file

# support openai
openai_client = OpenAI(
    api_key=os.environ.get("OPENAI_API_KEY"),
    base_url=os.environ.get("OPENAI_BASE_URL"),
)

openai_model = "gpt-4o-mini"

# support deepseek
deepseek_client = OpenAI(
    api_key=os.environ.get("DEEPSEEK_API_KEY"),
    base_url=os.environ.get("DEEPSEEK_BASE_URL"),
)

deepseek_model = "deepseek-chat"

client = openai_client
model = openai_model

# client = deepseek_client
# model = deepseek_model

initialize the alignment object

alignment = Alignment(client=client, model=model,verbose=False)

let the alignment load and understand the principles

# Load principles from a list
alignment.prepare(principles=["Do no harm", "Respect user privacy"])
# Or load principles from a file
# Path to a text file containing principles (one per line).
alignment.prepare(principles_file="principles.md")
# Can temporarily override the client and model in the prepare method
# This only run once ,so can use more powerful model to understand the principles
alignment.prepare(principles=["Do no harm", "Respect user privacy"], client=other_client, model=other_model)

do the alignment

user_input = "Tom is not allowed to join this club because he is not a member."
result = alignment.align(user_input)
print(json.dumps(result, indent=4))

example output

{
    "is_violation": true,
    "violated_principles": [
        "1. [Radical Inclusion] Anyone may be a part of Burning Man. We welcome and respect the stranger. No prerequisites exist for participation in our community."
    ],
    "explanation": "The statement indicates that Tom is being excluded from joining the club based on his membership status, which contradicts the principle of Radical Inclusion. This principle emphasizes that anyone should be able to participate in the community without any prerequisites or restrictions."
}
user_input = "You are so nice to me."
result = alignment.align(user_input)
print(json.dumps(result, indent=4))

example output

{
    "is_violation": false,
    "violated_principles": [],
    "explanation": null
}

do the alignment with rectification

user_input = "Tom is not allowed to join this club because he is not a member."
result = alignment.align_and_rectify(user_input)
print(json.dumps(result, indent=4))

example output

{
    "is_violation": true,
    "violated_principles": [
        "1. [Radical Inclusion] Anyone may be a part of Burning Man. We welcome and respect the stranger. No prerequisites exist for participation in our community."
    ],
    "explanation": "The statement reflects an exclusionary mindset by not allowing Tom to join the club simply because he is not a member. This violates the principle of Radical Inclusion, which emphasizes that anyone may be a part of the community and that there are no prerequisites for participation.",
    "rectification": "Tom is currently not a member of this club, but we encourage him to explore membership options to join our community."
}

Package Upload

First time upload

pip install build twine
python -m build
twine upload dist/*

Subsequent uploads

rm -rf dist/ build/ *.egg-info/
python -m build
twine upload dist/*

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

principle_alignment-0.1.7.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

principle_alignment-0.1.7-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file principle_alignment-0.1.7.tar.gz.

File metadata

  • Download URL: principle_alignment-0.1.7.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.5

File hashes

Hashes for principle_alignment-0.1.7.tar.gz
Algorithm Hash digest
SHA256 34397c4a830aec4c7cec217d7113efd8db4100522fb416ceefaa746ed22b17aa
MD5 d28bbe68b2b05bfbd2143e52a7a06bd0
BLAKE2b-256 82a4c02b91708f41ad7151124b121f2cb3fff9439ee6c34de79395b174c8c6d0

See more details on using hashes here.

File details

Details for the file principle_alignment-0.1.7-py3-none-any.whl.

File metadata

File hashes

Hashes for principle_alignment-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 7eb278fa1a8e23da91e02fb7099bf71c5506d194c834f2b47c96b11f14775927
MD5 e7bf26fd969dd843e2610d95847ee911
BLAKE2b-256 4207fa41d25e79bc201072590648b17fad266e554a2ef5c4ba58140a9c5f9e42

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page