Skip to main content

Coherence Engine — AI output verification and safety oversight

Project description

Director AI — Coherence Engine

Director AI

Drop-in coherence guardrail for llama.cpp, vLLM, OpenAI, and any LLM.
NLI + vector RAG fact-checking. 4.56 µs scoring. Rust hot path.

CI License: AGPL v3 Python 3.10+ Version 0.8.0


What It Does

Score any LLM output for hallucination. Halt incoherent token streams in real time. Fact-check responses against your own knowledge base.

from director_ai.core import CoherenceScorer

scorer = CoherenceScorer(threshold=0.6, use_nli=True)
approved, score = scorer.review(
    "What year was the Eiffel Tower built?",
    "The Eiffel Tower was completed in 1889."
)
print(f"Approved: {approved}, Coherence: {score.score:.2f}")
# Approved: True, Coherence: 0.94

Key Metric

Coherence = 1 - (0.6 * H_logical + 0.4 * H_factual)
  • H_logical: NLI contradiction probability (DeBERTa-v3-mnli-fever-anli)
  • H_factual: RAG ground truth deviation (ChromaDB + sentence-transformers)
  • Score < 0.6 = rejected. Score < 0.5 = emergency stop.

Installation

pip install director-ai                    # Lightweight (~5 MB, no torch)
pip install director-ai[nli]               # + DeBERTa NLI model (~2 GB)
pip install director-ai[vector]            # + ChromaDB + sentence-transformers
pip install director-ai[server]            # + FastAPI server
pip install director-ai[nli,vector,server] # Everything

Quick Start

1. Score a response

from director_ai.core import CoherenceScorer

scorer = CoherenceScorer(threshold=0.6, use_nli=True)
approved, score = scorer.review("Capital of France?", "The capital is Paris.")

2. Stream-gate a chatbot

from director_ai.core import CoherenceAgent
from director_ai.integrations import OpenAIProvider

provider = OpenAIProvider(api_key="sk-...")
agent = CoherenceAgent()
session = agent.process_streaming("Explain gravity", provider=provider)

if session.halted:
    print(f"Halted at token {session.halt_index}: {session.halt_reason}")
else:
    print(session.output)

3. Batch-audit Q&A pairs

from director_ai.core import BatchProcessor, CoherenceAgent

agent = CoherenceAgent()
processor = BatchProcessor(agent)
result = processor.process_batch(["What is 2+2?", "Explain DNA"])
print(f"Passed: {result.succeeded}/{result.total}")

4. Ingest your own knowledge base

from director_ai.core import VectorGroundTruthStore, ChromaBackend

store = VectorGroundTruthStore(
    backend=ChromaBackend(persist_directory="./my_facts"),
)
store.ingest([
    "The speed of light is 299,792,458 m/s.",
    "Water boils at 100 degrees Celsius at sea level.",
    "Earth orbits the Sun at 149.6 million km.",
])

# Now score against your facts
from director_ai.core import CoherenceScorer
scorer = CoherenceScorer(threshold=0.6, ground_truth_store=store)
approved, score = scorer.review("How fast is light?", "Light is about 300,000 km/s.")

5. Ingest a directory of documents

from director_ai.core import VectorGroundTruthStore, ChromaBackend

store = VectorGroundTruthStore(
    backend=ChromaBackend(persist_directory="./kb"),
)
n = store.ingest_from_directory("./docs", glob="**/*.md")
print(f"Indexed {n} documents")

scorer = CoherenceScorer(threshold=0.6, ground_truth_store=store)

6. CLI

director-ai review "What color is the sky?" "The sky is blue."
director-ai process "Explain photosynthesis"
director-ai batch input.jsonl --output results.jsonl
director-ai ingest facts.txt --persist ./chroma_data
director-ai serve --port 8080

Integrations

LLM Providers

Built-in adapters with real SSE streaming for OpenAI, Anthropic, HuggingFace, and local servers (llama.cpp, vLLM, Ollama):

from director_ai.integrations import OpenAIProvider, AnthropicProvider, LocalProvider

provider = OpenAIProvider(api_key="sk-...", model="gpt-4o-mini")
for token in provider.stream_generate("Explain gravity"):
    print(token, end="", flush=True)

LangChain

from director_ai.integrations.langchain_callback import CoherenceCallbackHandler

chain = LLMChain(llm=llm, callbacks=[CoherenceCallbackHandler(threshold=0.6)])

Install with: pip install director-ai[langchain]

Architecture

                    ┌─────────────────────────┐
                    │   Coherence Agent        │
                    │   (Main Orchestrator)    │
                    └──────────┬──────────────┘
                               │
              ┌────────────────┼────────────────┐
              │                │                │
    ┌─────────▼──────┐ ┌──────▼──────┐ ┌───────▼────────┐
    │  Generator     │ │ Coherence   │ │  Streaming     │
    │  (LLM Provider │ │ Scorer      │ │  Kernel        │
    │   + Streaming) │ │ (NLI+RAG)   │ │  (Token Gate)  │
    └────────────────┘ └──────┬──────┘ └────────────────┘
                              │
                    ┌─────────▼─────────┐
                    │  Vector Store     │
                    │  (Chroma/Memory)  │
                    └───────────────────┘
Module Purpose
CoherenceScorer Dual-entropy scorer: NLI contradiction + RAG fact-check
StreamingKernel Token-by-token gate: hard limit + sliding window + trend detection
CoherenceAgent Orchestrator: generate candidates, score, emit best
VectorGroundTruthStore Semantic retrieval via ChromaDB + sentence-transformers
BatchProcessor Bulk scoring with concurrency control
OpenAI/Anthropic/HF/LocalProvider LLM adapters with SSE streaming

Benchmarks

Benchmark scripts evaluate the coherence scorer on public hallucination-detection datasets. They require the DeBERTa NLI model (~2 GB download on first run):

pip install director-ai[nli]
python -m benchmarks.truthfulqa_eval 50    # TruthfulQA multiple-choice
python -m benchmarks.halueval_eval 100     # HaluEval QA/summarization/dialogue

Results are environment-dependent (model weights, threshold). Run locally and compare against your baseline.

Performance (Rust Hot Path)

The backfire-kernel/ Rust workspace reimplements safety-critical paths with PyO3 FFI. All operations clear the 50 ms deadline by orders of magnitude:

Operation Latency Headroom
Safety kernel (10 tokens) 265 ns 188,679x
Full scoring pipeline 4.56 µs 10,965x
SSGF gradient (analytic Jacobian) 4.14 µs 12,077x

Testing

pytest tests/ -v                 # 397+ tests
pytest tests/test_consumer_api.py -v  # Consumer API only
pytest benchmarks/ -v -m slow    # Benchmarks (requires NLI)
cd backfire-kernel && cargo test  # 153 Rust tests
Research Extensions

Optional SCPN research modules (Lyapunov stability, UPDE integrators, consciousness gate). Zero cross-imports with core.

pip install director-ai[research]

See docs/RESEARCH_GUIDE.md for details.

License

Dual-licensed:

  1. Open-Source: GNU AGPL v3.0 — academic, personal, open-source
  2. Commercial: ANULUM — closed-source

Citation

@software{sotek2026director,
  author  = {Sotek, Miroslav},
  title   = {Director AI: Coherence Engine},
  year    = {2026},
  url     = {https://github.com/anulum/director-ai},
  version = {0.8.1},
}

Contributing

See CONTRIBUTING.md. By contributing you agree to AGPL v3 terms.

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

director_ai-0.8.1.tar.gz (125.1 kB view details)

Uploaded Source

Built Distribution

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

director_ai-0.8.1-py3-none-any.whl (101.1 kB view details)

Uploaded Python 3

File details

Details for the file director_ai-0.8.1.tar.gz.

File metadata

  • Download URL: director_ai-0.8.1.tar.gz
  • Upload date:
  • Size: 125.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for director_ai-0.8.1.tar.gz
Algorithm Hash digest
SHA256 61f5f5f01929ce64c9efc9366b53114adc6c632dea4d21049c7eaf7de016f8d1
MD5 1bf044bcd0d5cf1093ec4e29adaff17a
BLAKE2b-256 d768ad36a349b667d40a07fcd9c1a8cae421756d9013243c5a8548642b9d4256

See more details on using hashes here.

Provenance

The following attestation bundles were made for director_ai-0.8.1.tar.gz:

Publisher: publish.yml on anulum/director-ai

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file director_ai-0.8.1-py3-none-any.whl.

File metadata

  • Download URL: director_ai-0.8.1-py3-none-any.whl
  • Upload date:
  • Size: 101.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for director_ai-0.8.1-py3-none-any.whl
Algorithm Hash digest
SHA256 526bec6ae5b065a527bbf1d14f0fa0ae8fa593728d3ffa9df21137609f3024eb
MD5 41931a6995e3b6e892361ce93baf7da0
BLAKE2b-256 e6892c8660a80546fe5a1969323854d8103e10b3529c50964dfa627889773525

See more details on using hashes here.

Provenance

The following attestation bundles were made for director_ai-0.8.1-py3-none-any.whl:

Publisher: publish.yml on anulum/director-ai

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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