Metadata-Version: 2.4
Name: cogniform
Version: 0.1.0
Summary: Learn, inspect, transfer and evaluate decision policies from expert demonstrations.
License-Expression: MIT
Project-URL: Repository, https://github.com/AMallur/Cogniform
Project-URL: Issues, https://github.com/AMallur/Cogniform/issues
Keywords: ai,decision-policy,imitation-learning,agents,explainability
Classifier: Development Status :: 3 - Alpha
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# Cogniform

**Learn how an expert decides, not just what they answer.**

Cogniform is a zero-dependency Python SDK for learning portable, inspectable decision policies from structured expert demonstrations. It learns a decision tree by information gain, exposes the conditions behind each prediction, and abstains when it encounters uncertainty or out-of-domain values. Optional LLM adapters convert natural-language situations into structured facts before the policy runs.

Version 0.1.0 is an alpha release candidate. It is not a foundation model, cognitive emulator, or guarantee of expert judgment. It has not been validated on real-world expert datasets. The name and public registry availability must be confirmed at publication.

## Install

After the package is published to PyPI:

```sh
pip install cogniform
```

Until then, install from the source directory:

```sh
git clone https://github.com/AMallur/Cogniform.git
cd Cogniform
python -m pip install .
```

Python 3.10+; no required runtime dependencies.

## 60-second quickstart

```python
from cogniform import Judgment

examples = [
    {"facts": {"risk": "low", "missed_deadlines": 0}, "decision": "continue"},
    {"facts": {"risk": "low", "missed_deadlines": 1}, "decision": "continue"},
    {"facts": {"risk": "high", "missed_deadlines": 0}, "decision": "escalate"},
    {"facts": {"risk": "high", "missed_deadlines": 1}, "decision": "escalate"},
]

expert = Judgment.learn(examples)
result = expert.apply({"risk": "high", "missed_deadlines": 1})
print(result.decision)      # escalate
print(result.evidence)      # observed facts and learned branch conditions
print(result.uncertainty)   # 1 - observed leaf class proportion, not calibrated risk
expert.save("supplier.json")
```

Each example is an expert's observed situation and selected action. Facts are finite JSON scalars (strings, numbers, booleans, null). Optional rationale text is accepted as a note in demonstrations; v0.1 does not learn from rationale text. Use domain-specific features and obtain consent to collect expert data.

## How the engine works

1. Learn candidate conditions on observed categories and numeric values.
2. Rank splits by information gain and fit a bounded-depth decision tree.
3. Follow the learned policy for a new situation, returning evidence and empirical class proportions.
4. Abstain on ties, low support, low confidence, incompatible required facts or unseen observed values in strict mode. Missing values may follow a branch explicitly learned from missing examples.
5. Export the profile as versioned JSON; share it across applications and model providers without retraining.

Numeric split thresholds use the lower observed value, not an inferred exact expert threshold. Strict mode (default) rejects supplied in-schema feature values outside the observed numeric range or categorical vocabulary. It does not detect every out-of-distribution combination. Unknown feature keys supplied directly are ignored by the policy. Use strict=False to permit extrapolation deliberately. Uncertainty is an empirical impurity measure, not calibrated probability. Human review is needed for high-impact decisions.

## Natural language input

```python
from cogniform import Judgment, OpenAIModel
from openai import OpenAI

expert = Judgment.load("supplier.json")
model = OpenAIModel(OpenAI(), model="YOUR_MODEL_ID")
result = expert.apply("Risk is high; the supplier missed one deadline.", model=model)
print(result.decision)
```

Install openai or anthropic yourself if using their client. AnthropicModel(client, model=...) and CallableModel(fn) are also available. A callable takes (system_prompt, user_prompt) and returns JSON text. Models are only used for fact extraction; they do not choose the decision. Extracted JSON is type-checked, but a model can still omit or hallucinate facts despite the prompt. Review extracted evidence when accuracy matters. Text and schema are sent to your configured model provider; direct structured prediction is entirely local. No telemetry is included.

## Evaluation and audit

```python
from cogniform import evaluate, split_demonstrations, Judgment
import json

rows = json.load(open("examples/demonstrations.json"))
train, test = split_demonstrations(rows, seed=42)
profile = Judgment.learn(train)
report = evaluate(profile, test)
print(report.accuracy, report.coverage, report.selective_accuracy)
print(report.baseline_accuracy)
print(profile.rules())
```

Accuracy counts abstentions as unsuccessful cases; coverage is the fraction answered; selective accuracy is accuracy among answered cases, or None if none are answered. The baseline is the training set majority class. Duplicate fact vectors are grouped together for the split; correlated or near-duplicate observations can still leak, so use external validation when possible. Avoid tuning against the final held-out set.

Run the included synthetic supplier benchmark:

```sh
python examples/demo.py
```

It contains 28 training cases and 12 disjoint held-out cases generated from a known simple policy. It is a functionality demonstration, not evidence of performance on human experts or production data.

## CLI

```sh
cogniform learn examples/demonstrations.json -o supplier.json
cogniform rules supplier.json
cogniform apply supplier.json examples/situation.json
cogniform evaluate supplier.json examples/heldout.json
```

Use --max-depth, --min-leaf, --min-confidence, and --min-support with learn. Use --allow-extrapolation with apply or evaluate to disable observed-domain checks. Each command outputs JSON. A profile ID is derived from its serialized contents for version tracking. Profiles contain derived training statistics and category values; review them before sharing.

## Development

```sh
python -m pip install -e . pytest
python -m pytest -q
python examples/demo.py
```

CI tests the supported Python versions and builds distributions. The release workflow runs tests, builds distributions, validates metadata, and publishes via PyPI Trusted Publishing only when manually triggered and authorized. See [PUBLISHING.md](PUBLISHING.md).
