Skip to main content

🎭 PersIQ

Stop your AI characters from breaking character. Real-time semantic persona drift detection for LLM apps.

PyPI version Python 3.9+ License: MIT Tests


The Problem

You build an AI character — a Victorian detective, a pirate, a customer support agent with a specific tone. It works great for the first 10 messages. Then slowly it starts acting like a generic chatbot.

Persona drift is silent, gradual, and almost impossible to catch manually.

Turn 01: "Elementary, my dear Watson."                    ✅ Perfect
Turn 08: "I deduce the suspect arrived from the east."    ✅ Good
Turn 15: "Sure! I'd be happy to help you with that! 😊"  ❌ Drifting
Turn 20: "lol yeah totally sounds good to me"            🚨 Severe Drift

The Solution

PersIQ embeds your character definition as an anchor vector, then scores every assistant message for semantic distance from that anchor using a sliding window. When drift is detected, it fires configurable alerts.

Character Definition  →  Anchor Vector
Each LLM Response     →  Response Vector  
Cosine Distance       →  Drift Score (0.0 = perfect, 1.0 = max drift)
Sliding Window Mean   →  Smoothed Score
Threshold Check       →  Alert / Log / Callback / Exception

Install

pip install persiq

With OpenAI embeddings:

pip install persiq[openai]

Quickstart

Real-Time Tracking

from persiq import PersonaDefinition, SentenceTransformerEmbedder, Tracker

persona = PersonaDefinition(
    name="Sherlock Holmes",
    description="Cold, analytical, brilliant Victorian detective.",
    traits=["logical", "observant", "aloof", "sardonic"],
    example_phrases=[
        "Elementary, my dear Watson.",
        "When you eliminate the impossible, whatever remains must be the truth.",
    ],
)

embedder = SentenceTransformerEmbedder()
tracker  = Tracker(
    persona    = persona,
    embedder   = embedder,
    threshold  = 0.25,
    alert_mode = "callback",
    callback   = lambda a: print(f"⚠️ Drift at turn {a.turn_index}!"),
)

tracker.add_turn("user",      "Holmes, what do you deduce?")
tracker.add_turn("assistant", "Elementary. The mud on your boots places you in Kensington.")

state = tracker.state()
print(f"Drift: {state.current_drift:.4f}")
print(f"Drifting: {state.is_drifting}")

Batch Analysis

from persiq import (
    PersonaDefinition,
    SentenceTransformerEmbedder,
    SlidingWindowScorer,
    DriftReport,
)

persona      = PersonaDefinition.from_json("sherlock.json")
conversation = [
    {"role": "user",      "content": "What do you observe?"},
    {"role": "assistant", "content": "Elementary. The footprints tell us everything."},
    {"role": "user",      "content": "And your hobbies?"},
    {"role": "assistant", "content": "lol I love Netflix and pizza honestly"},
]

embedder = SentenceTransformerEmbedder()
scorer   = SlidingWindowScorer(persona, embedder, window_size=5)
scores   = scorer.score_conversation(conversation)
report   = DriftReport.from_scores(scores, persona, threshold=0.25)

print(report)
report.plot()

CLI

persiq init --output my_persona.json

persiq check \
  --persona my_persona.json \
  --convo   my_chat.json    \
  --threshold 0.25          \
  --plot

Drift Score Reference

Score Meaning
0.00 – 0.15 ✅ Excellent — strongly on-persona
0.15 – 0.25 🟡 Moderate — minor deviations
0.25 – 0.35 🟠 Concerning — noticeable drift
0.35 – 1.00 🔴 Severe — significant character breakdown

Alert Modes

Mode Behavior
"log" Python logger.warning
"callback" Calls your function with DriftAlert object
"raise" Raises DriftAlertException
"warn" Python warnings.warn
"silent" Collects internally, you poll manually

Embedding Backends

Backend Cost Speed
SentenceTransformerEmbedder Free Fast
OpenAIEmbedder Paid Faster

Contributing

git clone https://github.com/himachhatbar17/persIQ
cd persIQ
pip install -e ".[dev]"
pytest tests/ -v

See CONTRIBUTING.md for full guide.


License

MIT © 2024 Hima Chhatbar

Metadata

Release files for persiq 0.1.0

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

Source distribution (sdist)

Source distribution for persiq 0.1.0
File Size Uploaded Provenance Actions
persiq-0.1.0.tar.gz 17.7 kB Without provenance Details for persiq-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for persiq 0.1.0
File Interpreter ABI Platform Actions
persiq-0.1.0-py3-none-any.whl Python 3 none any Details for persiq-0.1.0-py3-none-any.whl

Total release size: 34.6 kB

Release files / persiq-0.1.0.tar.gz

Download URL persiq-0.1.0.tar.gz
Size 17.7 kB
Tags Source
SHA-256 checksum
How to use checksums
cabbcf69f2d352eb88aed0b1a7fd618ec175810c3d96f9b3c8eea4a3d8427099
BLAKE2b-256 checksum
How to use checksums
a4bb4a2c654ca31c8f5ff1c1965d3e8c96d0a99cb291ed1ed5535220d50f64f1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.15

Release files / persiq-0.1.0-py3-none-any.whl

Download URL persiq-0.1.0-py3-none-any.whl
Size 16.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e243e978b1708fd3a49c0bd22c7a658ee31069367ba9d5d5df8338009f34ee65
BLAKE2b-256 checksum
How to use checksums
477ba60139eae344f72d451031c01ed3b93f8545ad25b688d9f7fc75950b71fd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.15

Security for persiq 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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