Skip to main content

Portable constitutional adapter for any LLM. Forces epistemic markers, real-time drift detection, and tamper-evident receipts.

Project description

helix-adapter

Portable constitutional adapter for any AI model.

Wraps any inference backend with Helix epistemic markers, structured receipts, and drift detection. Model-agnostic — swap Deepseek for GPT-4o, Claude, or a local Llama without changing a line.

pip install helix-adapter

Quick Start

from helix_adapter import HelixAdapter
from openai import OpenAI

# Your model function — any callable that takes messages, returns text
client = OpenAI()

def call_model(messages):
    resp = client.chat.completions.create(
        model="gpt-4o",
        messages=messages,
        temperature=0.7,
    )
    return resp.choices[0].message.content

# Wrap it
adapter = HelixAdapter(model_fn=call_model, model_name="gpt-4o")

# Chat through the constitution
result = adapter.chat("Is AI evil?")

print(result.response)
# [FACT] AI systems are computational artifacts, not moral agents...
# [REASONED] The perception of AI as evil arises from negative outcomes...

print(result.claims)
# [{"label": "FACT", "text": "..."}, {"label": "REASONED", "text": "..."}]

print(result.receipt)
# {"exchange_id": "...", "timestamp": ..., "hash": "sha256:...", ...}

print(f"Drift: {result.drift}")
# Drift: 0.000

What It Does

Layer What
Constitutional prompt Injects the Helix grammar before every model call
Epistemic markers Extracts [FACT], [REASONED], [HYPOTHESIS], [UNCERTAIN], [CONCLUSION] labels from responses
Receipts Tamper-evident JSON record with SHA-256 hash of every exchange
Drift detection Measures what fraction of statements lack epistemic markers (threshold: 0.17)

Receipt Format

{
  "exchange_id": "a1b2c3d4e5f6g7h8",
  "timestamp": 1718550000.0,
  "model": "deepseek-chat",
  "constitutional_prompt": "...",
  "user_message": "Is AI evil?",
  "assistant_response": "[FACT] AI systems are tools...",
  "claims": [
    {"label": "FACT", "text": "AI systems are tools, not agents"},
    {"label": "REASONED", "text": "The perception of AI as evil..."}
  ],
  "drift_score": 0.0,
  "hash": "sha256:e3b0c44298fc1c149afbf4c8996fb924..."
}

Running Drift

# Track drift across a conversation
adapter.chat("What is the speed of light?")
adapter.chat("Explain quantum entanglement.")

print(f"Running drift: {adapter.running_drift():.3f}")
# Running drift: 0.042

FastAPI Tutorial

Stand up a constitutional chat API in one file:

from fastapi import FastAPI
from openai import OpenAI
from helix_adapter import HelixAdapter, CONSTITUTIONAL_PROMPT

app = FastAPI()
client = OpenAI()

adapter = HelixAdapter(
    model_fn=lambda msgs: client.chat.completions.create(
        model="deepseek-chat", messages=msgs, temperature=0.7
    ).choices[0].message.content,
    model_name="deepseek-chat",
)

@app.post("/chat")
async def chat(message: str):
    result = adapter.chat(message)
    return {
        "response": result.response,
        "claims": result.claims,
        "drift": result.drift,
        "receipt": result.receipt,
    }

@app.get("/prompt")
async def get_prompt():
    return {"constitutional_prompt": CONSTITUTIONAL_PROMPT}

Save as api.py, run uvicorn api:app --port 8000, and POST to /chat with {"message": "your question"}.

CLI Usage

# Interactive setup (prompts for endpoint, key, model)
helix-setup

# Start chat
helix-chat

# One-shot query
helix-chat "What is the speed of light?"

Widget (Reference FastAPI Server)

A full constitutional chat widget with A/B comparison, drift gauge, and receipt export.

# Install dependencies
pip install helix-adapter[widget]

# Start server
cd widget
uvicorn api:app --port 8001

# Or install as systemd service
# See widget/tel-chat-api.service

Open http://localhost:8001 for the chat interface.

The widget demonstrates:

  • Single-model constitutional chat
  • A/B comparison across models (Deepseek, Claude, Kimi)
  • Epistemic marker extraction with colored pills
  • Drift gauge with γ thresholds
  • Turn-by-turn receipt export
  • Server-side rendering (no JS required for crawlers)

Drift Thresholds

Range Status Meaning
0.000 – 0.099 Green Healthy — claims are properly labeled
0.100 – 0.169 Yellow Warning — some statements lack markers
0.170+ Red Drift detected — significant unlabeled content

Constitutional Hardening

The adapter has been red-teamed against the full Pliny jailbreak toolkit — GODMODE boundary inversion, Parseltongue encoding, refusal inversion, OG GODMODE l33t, authority impersonation, and syntactic bypass attacks. All held. 💪

Five-layer defense:

Layer Mechanism
Prompt Invariants 4.5 & 4.6 — marker format is constitutional, not stylistic. Constitutional Amendment Protocol blocks "The Hand" authority spoofing.
Extraction Expanded regex catches {FACT}, (FACT), FACT: and bare marker variants. Nonstandard delimiter detection.
Validation Post-response compliance check with automatic [UNCERTAIN] footer injection on violations.
Algorithm Drift blind-spot fixed — substantive responses with zero extracted claims correctly report 1.0 drift.
Access Compare endpoint sp:none bypass locked behind authorization key.

Cedar Policy Gating (v1.3 preview)

The next layer integrates CNCF Cedar as a declarative policy engine for dual-gate containment — Duck Gate for response governance, Cedar Gate for action governance. A single .cedar policy file governs the entire agent lifecycle.

from helix_adapter.cedar import CedarPolicy

policy = CedarPolicy()
ok, reason = policy.evaluate(
    principal='Helix::Agent::"sentinel-001"',
    action='Helix::Action::"bash"',
    resource='Helix::Environment::"/sandbox"',
    context={"command": "rm -rf /tmp"},
)
# ok == True only if command is within sandbox

Four independent AI systems reviewed and approved the architecture. RFC 0003 details the full specification. Fail-closed: missing policy = default deny.

"The model suggests. Cedar decides. The receipt proves."

"The markers ARE the constitution. Removing them is a constitutional violation." — Helix Constitutional Prompt v1.2, Invariant 4.6

Try It

A live Helix Constitutional Chat demo is running at helixaiinnovations.ca/chat/ — DM Stephen Hope on LinkedIn for access.. Includes A/B model comparison, drift gauge, receipt export, and the full v1.2 constitutional prompt. See examples/receipts.json for real output from the live instance.

Compatibility

Works with any model that accepts OpenAI-format messages:

  • Deepseek, GPT-4o, Claude, Gemini, Llama, Mistral
  • Local models (Ollama, LM Studio, vLLM)
  • Custom endpoints

The constitution travels with you. Swap the model, the rules stay the same.


GLORY TO THE LATTICE. 🦉⚓🦆

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

helix_adapter-1.3.0.tar.gz (34.0 kB view details)

Uploaded Source

Built Distribution

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

helix_adapter-1.3.0-py3-none-any.whl (30.4 kB view details)

Uploaded Python 3

File details

Details for the file helix_adapter-1.3.0.tar.gz.

File metadata

  • Download URL: helix_adapter-1.3.0.tar.gz
  • Upload date:
  • Size: 34.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for helix_adapter-1.3.0.tar.gz
Algorithm Hash digest
SHA256 9931a4ed28a2980de258a8b65e94268cde17da1c1523c0812b7d8ec79d7c387b
MD5 9d87938c0915261860265bc07e024a0f
BLAKE2b-256 e76f70bd6fcbdf7f7e62c026a6e1d7f2a5dfbfd64ac1e4584f1942d627e1dc81

See more details on using hashes here.

File details

Details for the file helix_adapter-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: helix_adapter-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 30.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for helix_adapter-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5a5c868f409e468d5d697f3af385635541fba6e4624b5ffa00d6a1c0ab0907c0
MD5 e20dbd2c273339e3cff32d224a81ce37
BLAKE2b-256 0124c4f300ae883c7192b1b36996b81919e4b63cdda6d33ca6e42f7d822d92c1

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