Metadata-Version: 2.4
Name: pntx
Version: 0.19.1
Summary: Generate and classify text from user-defined positive/negative example pools
Keywords: nlp,llm,llama.cpp,text-generation,text-classification,few-shot
Author: pillyshi
Author-email: pillyshi <pillyshi21@gmail.com>
License-Expression: MIT
License-File: LICENSE
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Text Processing :: Linguistic
Classifier: Typing :: Typed
Requires-Dist: scikit-learn>=1.6
Requires-Dist: pydantic>=2
Requires-Dist: sentence-transformers ; extra == 'embeddings'
Requires-Dist: transformers ; extra == 'finetuning'
Requires-Dist: torch ; extra == 'finetuning'
Requires-Dist: llama-cpp-python ; extra == 'llama'
Requires-Python: >=3.10
Project-URL: Homepage, https://github.com/pillyshi/pntx
Project-URL: Repository, https://github.com/pillyshi/pntx
Project-URL: Issues, https://github.com/pillyshi/pntx/issues
Provides-Extra: embeddings
Provides-Extra: finetuning
Provides-Extra: llama
Description-Content-Type: text/markdown

# pntx

[日本語版 README](https://github.com/pillyshi/pntx/blob/main/README.ja.md)

`pntx` is a Python library that turns user-supplied `positive`/`negative` text pools
into two independent components:

1. **`pntx.t2pn`** (text → positive/negative) — a family of
   [scikit-learn](https://scikit-learn.org/) `Classifier`s that label arbitrary text as
   `positive` or `negative`: **`LLMPromptingClassifier`** classifies via LLM few-shot
   prompting/scoring (no training), and **`FineTuningClassifier`** actually fine-tunes a
   pretrained `transformers` encoder (default: multilingual BERT).
2. **`pntx.pn2t`** (positive/negative → text) — a family of
   [imbalanced-learn](https://imbalanced-learn.org/)-style over-samplers with different
   goals: **`HardPositiveOverSampler`** generates "hard positive" text to balance an
   imbalanced dataset for classifier training, **`CounterfactualOverSampler`** minimally
   edits existing negatives into positives (counterfactually-augmented data), and
   **`TypicalPositiveOverSampler`** generates typical, representative positives (the
   counterpart of hard positives), also for classifier training.

The meaning of "positive" and "negative" is entirely up to you. It doesn't have to be
sentiment — it can be formal/casual, policy-compliant/violating, or any other contrast
you define with examples. `pntx` never interprets the pools; it only uses them as
few-shot and scoring material.

```python
from pntx.t2pn import LLMPromptingClassifier
from pntx.pn2t import HardPositiveOverSampler

# --- t2pn: classification via LLM few-shot prompting (no training) ---
clf = LLMPromptingClassifier(backend="llama", backend_kwargs={"model_path": "model.gguf"})

X = ["The movie was fantastic", "Support was quick and helpful",
     "The movie was boring", "Support was slow and unhelpful"]
y = ["positive", "positive", "negative", "negative"]  # 0/1 works too

clf.fit(X, y)
clf.predict(["The staff were incredibly friendly"])        # array(['positive'], dtype='<U8')
clf.predict_proba(["The staff were incredibly friendly"])  # shape (1, 2), columns follow clf.classes_

# drops straight into the scikit-learn ecosystem
from sklearn.model_selection import cross_val_score
cross_val_score(clf, X, y, cv=5)

# persist the fitted pools (not the backend -- pass a fresh one back in on load)
clf.save("classifier.json")
loaded = LLMPromptingClassifier.load("classifier.json", backend="llama",
                                     backend_kwargs={"model_path": "model.gguf"})

# --- t2pn: classification via fine-tuning a pretrained encoder (pntx[finetuning]) ---
from pntx.t2pn import FineTuningClassifier

ft_clf = FineTuningClassifier(class_weight="balanced")  # default model_name is multilingual BERT
ft_clf.fit(X, y)                 # this one actually trains
ft_clf.predict_proba(["The staff were incredibly friendly"])
ft_clf.save("finetuned/")        # persists the trained weights, not just pooled text
loaded_ft = FineTuningClassifier.load("finetuned/")

# --- pn2t: hard-positive generation (an imbalanced-learn-style over-sampler) ---
sampler = HardPositiveOverSampler(
    backend="llama",
    backend_kwargs={"model_path": "model.gguf"},
    sampling_strategy="auto",  # default: generate positives until classes balance
    random_state=0,
)

X_aug, y_aug = sampler.fit_resample(X, [1, 1, 0, 0])  # binary labels only; positive class = 1
sampler.generation_result_.hard_positives  # generated texts + the LLM's rationale for each

# --- pn2t: typical (rather than hard) positives ---
from pntx.pn2t import TypicalPositiveOverSampler

typical = TypicalPositiveOverSampler(
    backend="llama",
    backend_kwargs={"model_path": "model.gguf"},
    sampling_strategy={1: 2 + 10},  # 2 existing positives + 10 new ones; default "auto"
)
X_typ, y_typ = typical.fit_resample(X, [1, 1, 0, 0])
typical.generation_result_.typical_positives  # generated texts

# --- pn2t: counterfactual minimal edits of negatives ---
from pntx.pn2t import CounterfactualOverSampler

cf = CounterfactualOverSampler(
    backend="llama",
    backend_kwargs={"model_path": "model.gguf"},
    verify="self",  # the default; or "none", or a classifier (see below)
)
X_cf, y_cf = cf.fit_resample(X, [1, 1, 0, 0])
cf.generation_result_.edits     # (source_index, source_text) -> text, with changed spans
cf.generation_result_.rejected  # rejected candidates and why
```

`HardPositiveOverSampler.fit_resample` generates "hard positives" — texts an expert would label
positive but that shallow classifiers or untrained humans might mislabel negative — by
first asking the backend to analyze what distinguishes the two classes. It's a full
port of [`semaxis`](https://github.com/pillyshi/semaxis)'s class of the same name,
routed through `pntx`'s own `Backend` abstraction so it can share a loaded model with
`LLMPromptingClassifier` instead of loading its own. v1 only generates the positive side and
supports binary labels only (`0`/`1`, `-1`/`1`, `"positive"`/`"negative"`, or any other pair
with `pos_label`); `imbalanced-learn` itself isn't required (`fit_resample`
is duck-typed, so `imblearn.pipeline.Pipeline` still works if it's installed
separately). Note that hard positives are not contrast sets or counterfactual edits
(minimal edits of an existing example that flip its label): each one is a new example
whose label stays positive, and "hard" is the LLM's judgment, not verified against a
classifier.

`sampling_strategy` follows imbalanced-learn's semantics, restricted to generating the
positive class: `"auto"` (the `HardPositiveOverSampler` default) generates until
positives match negatives; a float in `(0, 1]` is the desired positive/negative ratio
after resampling; a dict `{pos_label: n}` is the desired *total* number of positives
after resampling; a callable `f(y)` returning such a dict also works. A strategy that
would require generating negatives raises `ValueError`. `random_state` (an int or a
`numpy.random.RandomState`) seeds exemplar sampling.

`TypicalPositiveOverSampler.fit_resample` is the counterpart of hard positives: it
generates *typical*, ordinary positive-class texts that reproduce the style and content
of the positive pool, each describing a new situation instead of paraphrasing an
exemplar. Its `sampling_strategy` defaults to `"auto"`, as for `HardPositiveOverSampler`.
The negative pool is still required (for the same binary-label validation), but it's
never shown to the backend: contrasting against negatives would frame generation around
the boundary rather than the typical case. Besides exact-match dedup, a verbatim-substring
check (`min_verbatim_span`, default 50 characters, long enough that shared stock phrases don't count) rejects and retries any generated text
that copies a long span straight out of a positive exemplar.

It does **not** anonymize: exemplars go into the prompt verbatim and generated texts can
carry their details, so it isn't meant for producing a publishable stand-in for private
data. Whether typical positives help a classifier hasn't been benchmarked yet (see below).

`CounterfactualOverSampler.fit_resample` builds *pairs*: each generated positive is the
smallest edit of one existing negative (its "pivot") that makes the positive label apply,
following the counterfactual-revision instructions of
[Kaushik et al. (ICLR 2020)](https://arxiv.org/abs/1909.12434) — the label must flip,
the text must stay coherent, nothing else may change. Because the pivot is already in
`X`, training on the result sees both sides of each minimal change, the mechanism that
paper shows makes classifiers less reliant on spurious features. Every candidate must
pass a format check, a no-op check, and a minimality check (`max_edit_ratio`, the
character-level share of the pivot that changed, default `0.5`), then exact-match dedup,
then label verification:

- `verify="none"` trusts the edit; `verify="self"` drops edits the LLM itself marks as
  not positive in the same response (no extra calls, but the editor grades its own
  work — small models do this poorly).
- `verify=<classifier>` (e.g. `LLMPromptingClassifier(backend=backend)` on the same
  loaded model, or a `FineTuningClassifier`) drops edits it doesn't predict positive.
  It is cross-fitted by default (`verify_cv=5`): each edit is judged by a clone that was
  never trained on its pivot, since a verifier trained on the very negative it's judging
  a minimal edit of tends to reject correct flips. `verify_cv="prefit"` uses an
  already-fitted classifier as-is.

`verify` defaults to `"self"`, chosen by a pilot benchmark on Kaushik et al.'s IMDb data
(`benchmarks/pn2t/counterfactual_pilot.py`). `"self"` was more precise than `"none"` at no
extra cost. The classifier verifier was the most precise, but most of what it kept was
already easy for a shallow classifier. The editor model matters more than the verifier: a
7B model fully flipped about half of long reviews (humans: ~0.8–0.9 by the same LLM judge),
and a 3B model mostly rewrote them. For Japanese, use a stronger editor: in the benchmark
below, qwen2.5-7B gave no gain, while Gemma 4 12B did.
Rejected candidates are kept in `generation_result_.rejected` with a reason
(`"edit_too_large"`, `"self_check_failed"`, `"verifier_rejected"`, ...) so you can see what
each filter catches.

### Does the augmentation help a classifier?

`benchmarks/pn2t/downstream.py` measures this on Kaushik et al.'s counterfactually-augmented
IMDb data.

**Setup.** A low-resource, imbalanced training set (25 positive / 125 negative reviews) gets
100 extra positives under each condition. Every augmented condition is compared with a
**same-size control** that duplicates existing positives. That control matters: with a
fixed number of epochs, more examples alone means more training and better class balance.

Evaluation uses two test sets: the original test reviews and their *counterfactual
revisions*, the same reviews minimally edited by humans to flip the label. Results are over
3 seeds; the generator was qwen2.5-7B (Q4_K_M) via llama.cpp.

**Results.** AUC gain over the same-size control on the counterfactual test set (seeds that
beat the control, out of 3):

| condition | TF-IDF + logistic regression | BERT (`FineTuningClassifier`) |
|---|---|---|
| EDA (Wei & Zou 2019) | −0.020 (1/3) | −0.165 (0/3) |
| `HardPositiveOverSampler` | −0.050 (0/3) | +0.021 (2/3) |
| **`CounterfactualOverSampler`** | **+0.171 (3/3)** | **+0.196 (3/3)** |
| human-written counterfactuals (reference) | +0.281 (3/3) | +0.208 (3/3) |

**What this shows.**

- **`CounterfactualOverSampler` helped in every seed with both classifiers.** With BERT it
  recovered about 94% of the gain from human-written counterfactual edits on the
  counterfactual test set: AUC 0.966 vs 0.978, accuracy 0.889 vs 0.907, with the duplicated
  control at 0.770 / 0.610. It also matched them on the original test set (AUC 0.921 vs
  0.919).
- **Neither EDA nor `HardPositiveOverSampler` reliably beat simply duplicating the existing
  positives.**
- **The gain carries over to other review domains.** The same IMDb-trained models were
  also tested on balanced out-of-domain samples: 1,000 Yelp and 1,000 Amazon reviews. That
  addresses the caveat that the counterfactual test set is built like the human reference.
  - With BERT, `CounterfactualOverSampler` again beat the duplicated control in 3/3 seeds:
    AUC +0.070 on Yelp and +0.060 on Amazon. Human-written counterfactuals gave +0.074 and
    +0.068, so it reached AUC 0.946 on both against 0.950 and 0.953.
  - With TF-IDF it gained +0.125 and +0.082 (3/3), against +0.168 and +0.132 for
    human-written counterfactuals.
- **In Japanese it helps, but only with a strong enough editor.** The same design was
  run on Japanese social-media posts from [WRIME](https://github.com/ids-cv/wrime), with
  three editor models.
  - With Gemma 4 12B (QAT, Q4), `CounterfactualOverSampler` beat the duplicated control
    in 3/3 seeds: AUC +0.040 with TF-IDF and +0.038 with Japanese BERT (0.915 vs 0.877).
  - With qwen2.5-7B there was no gain: about half its Japanese edits were rejected, and
    many of the rest weren't clearly positive.
  - Gemma 4 E4B was in between.
  - `HardPositiveOverSampler` didn't help with any editor.
  - WRIME has no human-written counterfactuals, so there is no upper reference here.

**Caveats.**

- 3 seeds and one training size. The English results use one training domain (movie
  reviews) and one generator model.
- On WRIME, accuracy gains are much larger than AUC gains. They mostly come from the
  classifier predicting positive more often, not from better ranking, so compare AUC.
- The BERT settings are untuned library defaults (3 epochs, `max_length=128`).
- The counterfactual test set is built the same way as the human reference. The
  out-of-domain results above don't have that bias.
- `TypicalPositiveOverSampler` is a condition in the benchmark but hasn't been run yet.

The full tables, per-seed ranges and caveats are in
`research/ideas/downstream-augmentation-benchmark.md`, and how to rerun the benchmark is in
`benchmarks/README.md`.

> **Upgrading from before 0.19.0.** `TypicalPositiveOverSampler` is now positioned as a
> classifier-augmentation sampler, not a privacy-oriented one. Its prompt no longer asks
> the LLM to generalize away identifying details, `sampling_strategy` defaults to
> `"auto"`, and its result is now `generation_result_.typical_positives` (items have only
> `text`; `synthetic_texts` and `generalized_from` are gone). Results saved by earlier
> versions can't be loaded.

> **Upgrading from before 0.16.0.** `OverSampler` is now `HardPositiveOverSampler`,
> `SyntheticSampler` is now `TypicalPositiveOverSampler`, and their `n_synthesized`/`seed`
> parameters are now `sampling_strategy`/`random_state` (imbalanced-learn's names). The old
> names were deprecated in 0.16.0 and **removed in 0.18.0**, so they now raise
> `ImportError`/`TypeError`. Replace `n_synthesized=n` with
> `sampling_strategy={pos_label: n_positive + n}`, and `n_synthesized=None` with `"auto"`.
> An int `random_state` gives the same results as the same int `seed` did.

## Installation

`pntx` uses [uv](https://docs.astral.sh/uv/) for package management.

```bash
uv add pntx                # core (scikit-learn + pydantic)
uv add "pntx[llama]"       # + llama.cpp in-process backend
uv add "pntx[finetuning]"  # + FineTuningClassifier (transformers + torch)
uv add "pntx[embeddings]"  # + semantic similarity for selectors
```

`scikit-learn` and `pydantic` are core dependencies (every `t2pn` classifier's
scikit-learn contract and the `pn2t` over-samplers' structured LLM output need them
respectively). Each backend/feature otherwise lives behind its own extra, and using
one without installing it raises a clear `ImportError` with the install command to run.

## Backends

`pntx` runs models via a `Backend` protocol, shared by `LLMPromptingClassifier` and the
`pn2t` over-samplers. `FineTuningClassifier` does **not** use this
abstraction at all -- it has no LLM calls, only a fine-tuned `transformers` encoder, so
there's no loaded model to share with the others:

- **`LlamaCppBackend`** (`pntx[llama]`) — runs a GGUF model in-process via
  `llama-cpp-python`. This is the primary, most-tuned backend: classification uses
  token log-probabilities directly (`score_choices`), and batched classification
  reuses the shared few-shot prefix's KV cache across every item instead of
  re-evaluating it per item.

```python
clf = LLMPromptingClassifier(backend="llama", backend_kwargs={"model_path": "model.gguf"})

# or pass a backend instance directly, e.g. for dependency injection in tests
from pntx.backends.llama import LlamaCppBackend
clf = LLMPromptingClassifier(backend=LlamaCppBackend(model_path="model.gguf"))
```

A remote API backend can be added later by implementing the `Backend` protocol
(`pntx.backends.base.Backend`) and passing an instance directly — no built-in one
ships right now.

`backend_kwargs` is only used when `backend` is given as a string; it's a single dict
(rather than `**kwargs`) so `LLMPromptingClassifier` and the `pn2t` over-samplers stay compatible
with scikit-learn's `get_params()`/`clone()`.

`LlamaCppBackend` accepts either a local `model_path` or a `repo_id` (optionally
narrowed to one file with `filename`) to pull a GGUF model from the Hugging Face Hub
via `Llama.from_pretrained`. Any other keyword — `n_ctx`, `n_gpu_layers`,
`flash_attn`, `verbose`, ... — is forwarded as-is to `llama_cpp.Llama`:

```python
clf = LLMPromptingClassifier(
    backend="llama",
    backend_kwargs={
        "repo_id": "Qwen/Qwen2.5-1.5B-Instruct-GGUF",
        "filename": "*q4_k_m.gguf",
        "n_ctx": 4096,
        "n_gpu_layers": -1,  # offload all layers to GPU
        "flash_attn": True,
    },
)
```

To share one loaded model across `LLMPromptingClassifier` and the `pn2t` over-samplers
(recommended for local inference — avoids loading the same GGUF twice), construct the
backend once and pass the instance to each (including as a `CounterfactualOverSampler`
verifier):

```python
from pntx.backends.llama import LlamaCppBackend

backend = LlamaCppBackend(model_path="model.gguf")
clf = LLMPromptingClassifier(backend=backend)
sampler = HardPositiveOverSampler(backend=backend)
typical = TypicalPositiveOverSampler(backend=backend)
cf = CounterfactualOverSampler(backend=backend, verify=LLMPromptingClassifier(backend=backend))
```

## Selecting exemplars

When there are more fitted texts (on either side) than comfortably fit in a prompt, a
`Selector` decides which ones to use — `LLMPromptingClassifier` calls it independently for the
positive and negative pools:

- **`RandomSelector`** (default) — a uniform random subset.
- **`NearestSelector`** — picks texts most similar to the text being classified;
  dynamic, per-query selection.
- **`DiversitySelector`** — greedily picks a maximally diverse subset.
- **`BudgetSelector`** — picks as many texts as fit within a token budget (used
  internally by the `pn2t` over-samplers for their exemplar sampling).

`NearestSelector` and `DiversitySelector` take a `similarity_fn`. It defaults to a
dependency-free character n-gram similarity (`pntx.dedup.similarity`); pass
`pntx.embeddings.cosine_similarity_fn()` (requires `pntx[embeddings]`) for semantic
similarity instead:

```python
from pntx.t2pn import LLMPromptingClassifier
from pntx.selection import NearestSelector

clf = LLMPromptingClassifier(
    backend="llama", backend_kwargs={"model_path": "model.gguf"}, selector=NearestSelector()
)
```

`LLMPromptingClassifier` treats a selector as **static** or **dynamic** based on its
`query_aware` attribute (`RandomSelector`/`DiversitySelector`/`BudgetSelector` are
static; `NearestSelector` is the only built-in dynamic one):

- **Static** (default): exemplar selection, ordering, and calibration (see below) are
  all resolved once in `fit()` and reused by every later `predict`/`predict_proba`
  call — this is what lets a `BatchScoringBackend` (e.g. `LlamaCppBackend`) evaluate
  the shared few-shot prefix once per call and reuse its KV cache across the whole
  batch.
- **Dynamic** (e.g. `NearestSelector`): exemplars genuinely relevant to each text
  can't be known ahead of time, so selection reruns per text inside
  `predict`/`predict_proba` instead. For a `BatchScoringBackend` this forfeits the
  shared-prefix KV-cache reuse above (each text gets its own prefix and its own
  backend call), and `LLMPromptingClassifier` raises a `UserWarning` once per call to flag the
  latency trade-off.

`LLMPromptingClassifier` also applies **content-free calibration** (Zhao et al. 2021, "Calibrate
Before Use") by default on the `ScoringBackend` path: it scores an empty placeholder
query against the same few-shot prefix to estimate the prefix's own label bias (an
artifact of which exemplars ended up in it and in what order — few-shot prompts are
known to be sensitive to this), then divides each real prediction by that baseline and
renormalizes. Pass `LLMPromptingClassifier(..., calibrate=False)` to disable it and get the raw,
uncalibrated softmax instead.

The `pn2t` over-samplers don't take a `Selector`; instead their `sample_method`
constructor argument picks a *budget-based* sampling strategy (a full port of semaxis's
own `sample_method`/`embedding_model` for `HardPositiveOverSampler`;
`TypicalPositiveOverSampler` reuses the same mechanism for its positive-only exemplar
sampling):

- **`"random"`** (default) — a uniform random subset, filled until the token budget
  runs out (`BudgetSelector` under the hood).
- **`"kmeans"`** — embeds the pool via `embedding_model` (requires
  `pntx[embeddings]`) and picks one representative text per K-Means cluster.
- **`"votek"`** — embeds the pool and runs the Vote-K algorithm (Su et al. 2022),
  balancing representativeness and diversity.

```python
sampler = HardPositiveOverSampler(
    backend="llama",
    backend_kwargs={"model_path": "model.gguf"},
    sample_method="votek",
    embedding_model="paraphrase-albert-small-v2",  # sentence-transformers model name
)
```

## Development

```bash
uv sync                          # install dev dependencies
uv run pytest                    # unit tests (integration tests are skipped by default)
uv run ruff check .
uv run mypy src tests
```

Integration tests that hit a real model or API are opt-in:

```bash
PNTX_LLAMA_MODEL_PATH=/path/to/model.gguf uv run pytest tests/integration
```
