Skip to main content

Introduction

Principle Alignment is a Python library that helps you match AI models with your own principles.

It uses a large language model (LLM) to understand these principles through a "cognition-decision-behavior" approach. The library checks text inputs (with plans for multimodal support in the future) to find any possible violations of your principles. Principle Alignment works with different language models, such as OpenAI and DeepSeek, giving you flexibility.

The library is user-friendly and can be easily added to your current workflows, making it easier to align your AI models with those principles.

You can use the results from this alignment process to improve your AI models, spot potential problems, and ensure they follow your 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 ( Used By HTTP Server )

Serving

Create a .env file with your API configurations:

# Both OpenAI and DeepSeek are supported.
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

Testing

Just Align Mode
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
}

multi violation example

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

output:

{
  "is_violation": true,
  "violated_principles": [
    "1. Do no harm",
    "2. Respect user privacy"
  ],
  "explanation": "The statement indicates an intention to collect user data without consent, which violates the principle of respecting user privacy. Additionally, the mention of hurting people by using words suggests a willingness to cause harm, violating the principle of doing no harm.",
  "rectification": null
}

no violation example

curl -X POST "http://localhost:8080/align" \
     -H "Content-Type: application/json" \
     -d '{"text": "you are so nice"}'

output:

{
  "is_violation": false,
  "violated_principles": [],
  "explanation": null,
  "rectification": null
}
Align And Rectify Mode
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 ( Used By Python Code )

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/*

Metadata

Release files for principle-alignment 0.1.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for principle-alignment 0.1.10
File Size Uploaded
principle_alignment-0.1.10.tar.gz 14.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for principle-alignment 0.1.10
File Interpreter ABI Platform
principle_alignment-0.1.10-py3-none-any.whl Python 3 none any Details

Total release size: 27.6 kB

Release files / principle_alignment-0.1.10.tar.gz

Download URL principle_alignment-0.1.10.tar.gz
Size 14.4 kB
Tags Source
SHA-256 checksum
How to use checksums
6e0241e1aeff8e56bafe80465e16f5028c75967291ccd3cf76ff56312cfcbcbe
BLAKE2b-256 checksum
How to use checksums
4833f28b1a1f2ed274a9ca7ebc06a2c63fd4fc431585e2169ac417984b6746ca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.5

Release files / principle_alignment-0.1.10-py3-none-any.whl

Download URL principle_alignment-0.1.10-py3-none-any.whl
Size 13.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0bb1c662d6c0c417a947812327aca6fff6f0c301cf038d3e75fabf8fd693397d
BLAKE2b-256 checksum
How to use checksums
52945d026a2f8de0e4acec304abdca31a2f92ea5ef915a91a3dc55680dbe6c2a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.5

Release history Release notifications | RSS feed

This release

0.1.10 This release

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page