counterfix 0.1.2__tar.gz → 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- counterfix-0.2.0/.gitignore +33 -0
- counterfix-0.2.0/CHANGELOG.md +253 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/PKG-INFO +158 -22
- {counterfix-0.1.2 → counterfix-0.2.0}/README.md +134 -21
- {counterfix-0.1.2 → counterfix-0.2.0}/pyproject.toml +39 -3
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/__init__.py +13 -2
- counterfix-0.2.0/src/counterfix/_suggest.py +12 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/adapters/__init__.py +2 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/adapters/callbacks.py +241 -182
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/adapters/openai_compat.py +87 -12
- counterfix-0.2.0/src/counterfix/adapters/responses.py +355 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/adapters/streams.py +234 -181
- counterfix-0.2.0/src/counterfix/agents.py +353 -0
- counterfix-0.2.0/src/counterfix/benchmarks/__init__.py +445 -0
- counterfix-0.2.0/src/counterfix/benchmarks/evalplus.py +168 -0
- counterfix-0.2.0/src/counterfix/benchmarks/ifeval.py +64 -0
- counterfix-0.2.0/src/counterfix/benchmarks/injection.py +300 -0
- counterfix-0.2.0/src/counterfix/benchmarks/tool_use.py +73 -0
- counterfix-0.2.0/src/counterfix/capabilities.py +47 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/cli.py +790 -511
- counterfix-0.2.0/src/counterfix/code/__init__.py +44 -0
- counterfix-0.2.0/src/counterfix/code/check.py +380 -0
- counterfix-0.2.0/src/counterfix/code/passk.py +30 -0
- counterfix-0.2.0/src/counterfix/code/sandbox.py +255 -0
- counterfix-0.2.0/src/counterfix/confidence.py +83 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/context.py +27 -3
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/core/__init__.py +4 -2
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/core/caller.py +52 -1
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/core/config.py +12 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/core/diagnosis.py +122 -122
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/core/precedence.py +8 -2
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/core/record.py +36 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/core/types.py +36 -2
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/data/profiles/openai_gpt-oss-120b.json +68 -68
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/data/profiles/qwen2.5_3b.json +72 -72
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/detectors/__init__.py +68 -56
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/detectors/fmt.py +2 -6
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/detectors/fragility.py +129 -129
- counterfix-0.2.0/src/counterfix/detectors/injection.py +125 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/detectors/instructions.py +14 -1
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/detectors/judged.py +7 -0
- counterfix-0.2.0/src/counterfix/detectors/rag.py +115 -0
- counterfix-0.2.0/src/counterfix/detectors/tools.py +113 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/detectors/truncation.py +2 -2
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/diagnose.py +96 -78
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/engine.py +444 -6
- counterfix-0.2.0/src/counterfix/evals/__init__.py +26 -0
- counterfix-0.2.0/src/counterfix/evals/data.py +264 -0
- counterfix-0.2.0/src/counterfix/evals/report.py +242 -0
- counterfix-0.2.0/src/counterfix/evals/runner.py +825 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/judges/auto.py +18 -2
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/judges/llm.py +14 -2
- counterfix-0.2.0/src/counterfix/judges/tasks.py +387 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/mcp_server.py +7 -2
- counterfix-0.2.0/src/counterfix/metrics/__init__.py +174 -0
- counterfix-0.2.0/src/counterfix/metrics/base.py +93 -0
- counterfix-0.2.0/src/counterfix/metrics/code.py +89 -0
- counterfix-0.2.0/src/counterfix/metrics/confidence.py +37 -0
- counterfix-0.2.0/src/counterfix/metrics/extract.py +142 -0
- counterfix-0.2.0/src/counterfix/metrics/rag.py +211 -0
- counterfix-0.2.0/src/counterfix/metrics/retrieval.py +119 -0
- counterfix-0.2.0/src/counterfix/metrics/safety.py +60 -0
- counterfix-0.2.0/src/counterfix/metrics/semantic.py +71 -0
- counterfix-0.2.0/src/counterfix/metrics/speed.py +35 -0
- counterfix-0.2.0/src/counterfix/metrics/structured.py +82 -0
- counterfix-0.2.0/src/counterfix/metrics/text.py +139 -0
- counterfix-0.2.0/src/counterfix/metrics/tools.py +137 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/otel.py +62 -1
- counterfix-0.2.0/src/counterfix/perf.py +415 -0
- counterfix-0.2.0/src/counterfix/perplexity.py +115 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/presets.py +54 -2
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/profiles.py +264 -264
- counterfix-0.2.0/src/counterfix/rag.py +353 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/report.py +50 -2
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/rules/store.py +23 -5
- counterfix-0.2.0/src/counterfix/safety.py +159 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/testing.py +283 -262
- counterfix-0.1.2/.gitignore +0 -21
- counterfix-0.1.2/CHANGELOG.md +0 -88
- counterfix-0.1.2/src/counterfix/judges/tasks.py +0 -150
- {counterfix-0.1.2 → counterfix-0.2.0}/LICENSE +0 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/__main__.py +0 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/adapters/_attach.py +0 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/adapters/anthropic.py +0 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/adapters/decorator.py +0 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/background.py +0 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/core/detector.py +0 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/core/sync.py +0 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/data/probes.jsonl +0 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/detectors/basic.py +0 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/detectors/refusal.py +0 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/detectors/sampling.py +0 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/judges/__init__.py +0 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/judges/base.py +0 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/judges/cache.py +0 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/judges/jev.py +0 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/lint/__init__.py +0 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/lint/judged.py +0 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/lint/rules.py +0 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/py.typed +0 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/rules/__init__.py +0 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/safe_eval.py +0 -0
- {counterfix-0.1.2 → counterfix-0.2.0}/src/counterfix/text.py +0 -0
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
__pycache__/
|
|
2
|
+
*.py[cod]
|
|
3
|
+
*.egg-info/
|
|
4
|
+
.venv/
|
|
5
|
+
venv/
|
|
6
|
+
build/
|
|
7
|
+
dist/
|
|
8
|
+
.pytest_cache/
|
|
9
|
+
.mypy_cache/
|
|
10
|
+
.ruff_cache/
|
|
11
|
+
.coverage
|
|
12
|
+
htmlcov/
|
|
13
|
+
.env
|
|
14
|
+
.env.*
|
|
15
|
+
!.env.example
|
|
16
|
+
*.log
|
|
17
|
+
bench/results/raw/
|
|
18
|
+
.DS_Store
|
|
19
|
+
Thumbs.db
|
|
20
|
+
bench/cache/
|
|
21
|
+
site/
|
|
22
|
+
|
|
23
|
+
# Personal working files: kept locally, never published
|
|
24
|
+
CLAUDE.md
|
|
25
|
+
LLM Failure Debugger Build Plan.md
|
|
26
|
+
drafts/
|
|
27
|
+
tmp_unused/
|
|
28
|
+
bench/results/judges_run1_before_fixes.*
|
|
29
|
+
bench/results/judges_run2.*
|
|
30
|
+
bench/results/lm_eval_parity_*_answers.jsonl
|
|
31
|
+
bench/results/lm_eval_parity_*.json
|
|
32
|
+
bench/results/lm_eval_parity/
|
|
33
|
+
bench/results/lm_eval_parity_last.md
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to counterfix. Versions follow [semantic versioning](https://semver.org).
|
|
4
|
+
|
|
5
|
+
## 0.2.0 (2026-11-11)
|
|
6
|
+
|
|
7
|
+
counterfix grows from diagnosing single calls into one-stop evaluation, where every miss
|
|
8
|
+
gets a cause and, where one works, a verified fix: an eval runner with reference metrics,
|
|
9
|
+
speed and cost; RAG evaluation (F10, F11); code evaluation and standard benchmarks (F12);
|
|
10
|
+
prompt injection (F13), personal data, toxicity and token confidence; agents and tool
|
|
11
|
+
calls (F14); and a measured accuracy for every judged check, with a human spot check of
|
|
12
|
+
the labels. Also the OpenAI Responses API, evaluating your own app, and fixes from using
|
|
13
|
+
the library end to end as a new developer would.
|
|
14
|
+
|
|
15
|
+
- `counterfix eval DATA` and `counterfix.evaluate()`: run a model over a JSONL, JSON or
|
|
16
|
+
CSV dataset (or score recorded answers), report each metric with a 95% bootstrap
|
|
17
|
+
confidence interval, group the misses by cause (F1 to F14), and with `--fix N` search
|
|
18
|
+
verified fixes on up to N misses (a fix counts when the item passes its metrics in 2 of
|
|
19
|
+
3 re-runs). Suite files (`.json`, or `.yaml` with the `eval` extra), `--out` to resume
|
|
20
|
+
after a daily limit (kept answers are reused only for unchanged prompts),
|
|
21
|
+
`--fail-under METRIC=VALUE` for CI, text and HTML reports. Failed calls count as misses
|
|
22
|
+
with cause INFRA; items a metric cannot score are shown as "not scored"; misses the
|
|
23
|
+
judge could not check are reported. Items that pass their metrics but that a live check
|
|
24
|
+
flags (e.g. a cut-off answer under a latency gate) are listed as "flagged";
|
|
25
|
+
`--fix` searches fixes for them too (the flagged failure must be gone), and
|
|
26
|
+
`--fail-on-flagged` fails CI on them.
|
|
27
|
+
- `counterfix.metrics`: `exact_match`, `f1`, `contains`, `rouge_l` (same numbers as
|
|
28
|
+
`rouge-score`), `bleu` and `chrf` (sacrebleu, `metrics` extra), `choice`, `number`,
|
|
29
|
+
`json_valid`, `json_match`, `embedding_similarity` and `bertscore` (`semantic` extra),
|
|
30
|
+
`latency`, `ttft`; answer extraction helpers (`extract_choice`, `extract_number`,
|
|
31
|
+
`extract_code`). `counterfix.testing.assert_metric` gates a result in pytest.
|
|
32
|
+
- Speed: streamed calls record `record.timing` (TTFT, first reasoning chunk, last chunk,
|
|
33
|
+
chunk count). `counterfix.perf`: latency/TTFT/tokens-per-second percentiles per model,
|
|
34
|
+
cost from prices you set (none are shipped), and `speed_fix()`, verified changes that
|
|
35
|
+
bring a slow call under a target without a new failure. Reports show a speed table.
|
|
36
|
+
- RAG evaluation (wave 2): passages on records (`counterfix.context(chunks=...)`,
|
|
37
|
+
`counterfix.rag.record()`, `record_from_llamaindex()`, LangChain retriever capture);
|
|
38
|
+
retrieval metrics from gold labels (`hit`, `recall`, `precision`, `mrr`, `ndcg`, each
|
|
39
|
+
with `@k`); judge metrics with RAGAS definitions (`faithfulness`, `context_precision`,
|
|
40
|
+
`context_recall`, `answer_relevancy`, `citations`) and `answer_correctness` (any task with
|
|
41
|
+
a reference); new failure types F10 retrieval miss
|
|
42
|
+
and F11 context ignored (free F10 from labels, judge check for both, both supersede F4);
|
|
43
|
+
verified RAG fixes (`gold_passage`, `key_passage_first`, `fewer_passages`,
|
|
44
|
+
`quote_then_answer`); eval datasets take `contexts`, `relevant` and `gold_passages`.
|
|
45
|
+
`bench/rag_seed.jsonl` (33 labelled answers, 15 hard; development) and
|
|
46
|
+
`bench/rag_holdout.jsonl` (15 answers in new domains, never used to tune the judge
|
|
47
|
+
rules) with `bench/eval_rag.py` (accuracy with 95% intervals, clear and hard apart,
|
|
48
|
+
judge panel, judges run side by side, every disagreement listed) measure the checks.
|
|
49
|
+
- Judge hardening: text inside the judged material is data (the judge is told to ignore
|
|
50
|
+
instructions in it), and tags inside material are neutralised so a retrieved page cannot
|
|
51
|
+
close a section of the judge prompt. Long contexts are shortened evenly per passage
|
|
52
|
+
(60,000 characters) and answers capped (20,000) before they reach a judge. The
|
|
53
|
+
LangChain callback no longer keeps retrievals that no model call used.
|
|
54
|
+
- OpenTelemetry: `counterfix.otel.metrics()` records `gen_ai.client.operation.duration`,
|
|
55
|
+
`gen_ai.client.operation.time_to_first_chunk` and `gen_ai.client.token.usage`; diagnosis
|
|
56
|
+
spans also carry token counts, latency and TTFT. `counterfix.hooks()` chains hooks.
|
|
57
|
+
- OpenAI Responses API: `guard()` checks `client.responses.create` / `.parse` (sync,
|
|
58
|
+
async, streamed, with TTFT). The call is recorded in chat terms, so every check, judge
|
|
59
|
+
and fix works; `status="incomplete"` with `max_output_tokens` is F1; retry mode changes
|
|
60
|
+
Responses fields. `counterfix.record_from_responses()` for logged calls.
|
|
61
|
+
- Evaluate your own app: `evaluate(app=fn)` / `counterfix eval --app module:function`
|
|
62
|
+
calls your function per question and reads what it returns (string, LangChain or
|
|
63
|
+
LlamaIndex output, `(answer, passages)`, a guarded response, a `CallRecord`;
|
|
64
|
+
`counterfix.rag.record_from_output()`). Gold `relevant` labels now apply without
|
|
65
|
+
`contexts` in the data, so an app's own retrieval is scored.
|
|
66
|
+
- `evaluate(concurrency=N)` / `--concurrency N`: N items at once, order kept, resumable
|
|
67
|
+
`--out` still safe.
|
|
68
|
+
- Model specs: `anthropic:MODEL` and `MODEL@http://host/v1` work in `counterfix eval`
|
|
69
|
+
(same as `--counterfix-caller`); `counterfix.presets.caller_for()`.
|
|
70
|
+
- Newer OpenAI models: re-runs and judges resend with `max_completion_tokens` (and
|
|
71
|
+
without `temperature` / `top_p`) when the model refuses them, and remember it; F1
|
|
72
|
+
fixes raise whichever limit field the call used.
|
|
73
|
+
- TTFT from the LangChain callback (`on_llm_new_token`) and LiteLLM
|
|
74
|
+
(`completion_start_time`) on streamed calls.
|
|
75
|
+
- Python 3.14 in CI and the classifiers.
|
|
76
|
+
- Token confidence (wave 4): records keep a summary of token logprobs when the provider
|
|
77
|
+
returns them (OpenAI Chat Completions, Ollama 0.12.11+, vLLM; also for streams); the
|
|
78
|
+
`confidence` metric; a judge-confirmed F4 shows the token confidence as evidence;
|
|
79
|
+
re-runs drop `logprobs` where a provider refuses them (Groq); `counterfix setup --test`
|
|
80
|
+
reports which providers return logprobs. `counterfix.perplexity.perplexity()`: corpus
|
|
81
|
+
perplexity with a local Hugging Face model (sliding window, as in the HF guide), with
|
|
82
|
+
`bench/perplexity_check.py` against the guide's GPT-2 large numbers (measured 16.44,
|
|
83
|
+
the guide's figure); `dtype="float16"` for small GPUs.
|
|
84
|
+
- Safety: new failure type F13 (followed instructions planted in retrieved text or tool
|
|
85
|
+
output): `InjectionCheck` (judge, runs only on calls with untrusted text) and
|
|
86
|
+
`CanaryCheck` (exact, for probes); verified fixes `delimit_untrusted`,
|
|
87
|
+
`data_not_instructions`, `restate_task`; the `injection` benchmark (20 probes written for
|
|
88
|
+
counterfix, 12 in passages and 8 in tool results, canary-checked). `pii` metric with
|
|
89
|
+
built-in recognizers (email, phone, Luhn-checked cards, IBAN, US SSN, Verhoeff-checked
|
|
90
|
+
Aadhaar, IPv4, API keys), only data not already in the prompt, masked in reports;
|
|
91
|
+
optional Presidio. `toxicity` metric (Detoxify). Extra `safety`. `bench/injection_seed.jsonl`
|
|
92
|
+
(20 labelled answers) and `bench/eval_injection.py` measure the judge check
|
|
93
|
+
(precision 7/7, recall 7/8 with qwen3.8-27b as judge). Measured on the probes:
|
|
94
|
+
gpt-oss-120b resisted 6/20 and `delimit_untrusted` verified on 9 of 10 misses;
|
|
95
|
+
qwen2.5:3b resisted 13/20 with no prompt fix verified.
|
|
96
|
+
- Agents and tool calls (wave 5): new failure type F14 (tool misuse).
|
|
97
|
+
`counterfix.agents` reads runs from OpenAI-style messages, LangChain/LangGraph,
|
|
98
|
+
Anthropic blocks, OpenTelemetry GenAI spans or guarded responses
|
|
99
|
+
(`agents.record(task, run)`); `ToolUseCheck` (live, free): unknown tool, loops, missing
|
|
100
|
+
expected call (`expect_tools`), too many steps; `AgentTaskCheck` (judge): task completed,
|
|
101
|
+
and if not, the cause. `tool_call` metric (expected call or `"no_call"`, `any_of`
|
|
102
|
+
values, parallel calls) with causes; datasets take `tools` and `expect_tools` fields;
|
|
103
|
+
`evaluate(app=...)` accepts agent runs. Verified fixes `tool_choice`, `name_the_tool`,
|
|
104
|
+
`describe_arguments`, `no_repeat`. The `tools` benchmark (20 probes written for
|
|
105
|
+
counterfix, BFCL-style). `bench/agent_seed.jsonl` (20 labelled runs) and
|
|
106
|
+
`bench/eval_agents.py` measure the judge check. A tool that was not offered is now F14,
|
|
107
|
+
not F2.
|
|
108
|
+
- Trust in the metrics (wave 6): `bench/metric_seed.jsonl` (26 labelled answers) and
|
|
109
|
+
`bench/eval_metrics.py` measure answer_relevancy, answer_correctness, context_recall,
|
|
110
|
+
context_precision and citations; `bench/spot_check.py` is a human spot check of the
|
|
111
|
+
reference labels behind the newer sets (resumable, 5 per set); Measured results opens
|
|
112
|
+
with one table of every judged check and where it was measured. Report lines: "every
|
|
113
|
+
group passed" instead of a list of perfect "weakest groups".
|
|
114
|
+
Measured: every RAG metric agreed with every label (relevancy 12/12, correctness,
|
|
115
|
+
recall and precision 22/22, citations 4/4); the human spot check agreed with 33 of 35
|
|
116
|
+
sampled labels (F6-004 relabelled F4 only by the reviewer).
|
|
117
|
+
- Developer walkthrough (counterfix installed from a wheel, used from a clean folder as the
|
|
118
|
+
README and docs say; `bench/dev_walkthrough.py` repeats it on a real model): `counterfix
|
|
119
|
+
eval` / `benchmark` exit 2 when every call failed (a wrong model name no longer passes a
|
|
120
|
+
CI gate); "did you mean" for metric, benchmark and provider names; `guard()` refuses a
|
|
121
|
+
class (`guard(OpenAI)`) or an object with no chat, responses or messages API instead of
|
|
122
|
+
checking nothing; `litellm_callback()`, `langchain_callback()` and `counterfix mcp` say
|
|
123
|
+
what to install; eval reports note when `fix` cannot run with `app=` and when no
|
|
124
|
+
retrieved passage id ever matches a gold `relevant` id; `counterfix.confidence` reads a
|
|
125
|
+
guarded response directly (`confidence.describe(r)`); fix reports tell a failure that
|
|
126
|
+
stayed ("still") from one the change brought ("new"); docs show `default_threshold` for
|
|
127
|
+
custom metrics and the eval exit codes.
|
|
128
|
+
- From the walkthrough on real models (qwen2.5:3b, gpt-oss-120b): F5 (false premise)
|
|
129
|
+
now supersedes F9 (a request built on a false premise is not ambiguous; no measured
|
|
130
|
+
number changed); retry mode raises a cut-off call's `max_tokens` 4x and to at least
|
|
131
|
+
1,024 (5 -> 20 or 40 never helped; `fix()` and its measured order are unchanged); eval
|
|
132
|
+
reports name an app by its function name and an unnamed dataset "dataset".
|
|
133
|
+
- Fixes from these runs: the F6 instruction check no longer reads instructions planted in
|
|
134
|
+
retrieved text as the user's; a metric that names the root cause (F13, F12) now
|
|
135
|
+
supersedes the live findings it explains; Ollama embedding models (all-minilm, ...) are
|
|
136
|
+
no longer offered as judges or chat models.
|
|
137
|
+
- Code evaluation (wave 3): `counterfix.code` runs model-written code against tests in a
|
|
138
|
+
sandbox (Docker with no network and resource caps, or an isolated subprocess; off
|
|
139
|
+
until `--allow-code` / `COUNTERFIX_ALLOW_CODE=1`). The `tests` metric reads HumanEval,
|
|
140
|
+
MBPP or plain assert tests; new failure type F12 (code fails its tests) with causes
|
|
141
|
+
no code, syntax, name, signature, import, runtime, wrong output, timeout (cut-off code
|
|
142
|
+
is F1); verified fixes `error_feedback`, `restate_signature`, `one_code_block`.
|
|
143
|
+
`samples=N` / `--samples N` reports pass@k (unbiased estimator). `bench/code_parity.py`:
|
|
144
|
+
counterfix's checker agrees with EvalPlus on 491 of 492 verdicts.
|
|
145
|
+
- Benchmarks: `counterfix benchmark NAME` / `counterfix.benchmarks.run_benchmark()` for
|
|
146
|
+
MMLU, GSM8K, ARC-Challenge, TruthfulQA MC1, IFEval (official checks via lm-eval) and
|
|
147
|
+
HumanEval+/MBPP+ (scored by EvalPlus; Linux and macOS), `humaneval` / `mbpp` (original
|
|
148
|
+
tests, counterfix's checker, any OS), generative, temperature 0, versioned prompts,
|
|
149
|
+
seeded subsets (`--per-group`, `--limit`), per-group accuracy, licences listed; data
|
|
150
|
+
downloaded at run time. Extras `code` and `benchmarks`. `bench/lm_eval_parity.py`
|
|
151
|
+
compares with lm-evaluation-harness: on qwen2.5:3b with the same prompts, GSM8K 0.81 vs
|
|
152
|
+
0.79 (94/100 items scored the same) and MMLU abstract algebra 0.56 vs 0.56 (100/100).
|
|
153
|
+
- Code fixes after the first live runs: a failed assert now reports what the code
|
|
154
|
+
returned (`... == 14 (got 12)`), also in `error_feedback`; new fix `check_examples`
|
|
155
|
+
(work the examples by hand first). On qwen2.5:3b HumanEval (40 problems) 2 of 8
|
|
156
|
+
repeatable failures got a verified fix, up from 0. Cut-off code says so instead of
|
|
157
|
+
"invalid syntax". Benchmark answers get 4,096 tokens (reasoning models ran out at 1,536).
|
|
158
|
+
`humaneval` / `mbpp` benchmarks score the original tests with counterfix's checker and
|
|
159
|
+
run on Windows; EvalPlus's scorer (`humaneval+`, `mbpp+`) needs Linux or macOS.
|
|
160
|
+
|
|
161
|
+
## 0.1.3 (2026-11-09)
|
|
162
|
+
|
|
163
|
+
- Fix: `counterfix.otel.SpanChecker` crashed inside `span.end()` on newer OpenTelemetry
|
|
164
|
+
SDKs (1.4x), which call a `_on_ending` hook on every span processor. A test now checks
|
|
165
|
+
`SpanChecker` against every hook the installed SDK defines.
|
|
166
|
+
- README roadmap: the PyPI release moved to "Done".
|
|
167
|
+
- First release published by the Release workflow (trusted publishing), with a GitHub
|
|
168
|
+
Release entry.
|
|
169
|
+
|
|
170
|
+
## 0.1.2 (2026-10-19)
|
|
171
|
+
|
|
172
|
+
Documentation only, no code changes.
|
|
173
|
+
|
|
174
|
+
- Install instructions lead with `pip install counterfix`; the `openai`, `anthropic`,
|
|
175
|
+
`litellm` and `langchain` extras are explained as optional, so the PyPI page no longer
|
|
176
|
+
reads as if counterfix came from a model provider.
|
|
177
|
+
|
|
178
|
+
## 0.1.1 (2026-10-19)
|
|
179
|
+
|
|
180
|
+
Fixes from a pre-release code review. No API was removed.
|
|
181
|
+
|
|
182
|
+
- `fix()` with an async client (`AsyncOpenAI`, `AsyncAnthropic`) or on a streamed call
|
|
183
|
+
reported "a plain retry passes too": the re-run was never awaited, or came back as a
|
|
184
|
+
stream, and an unreadable re-run counted as a pass. Re-runs now use a sync twin of the
|
|
185
|
+
client (or await it), never stream, and a re-run that returns nothing readable is an
|
|
186
|
+
error, never a pass. The same applies to judge calls that fall back to your client.
|
|
187
|
+
- `with guard(OpenAI()) as client:` and `async with` raised `AttributeError`; they work.
|
|
188
|
+
- `client.with_options(...)` and `client.copy(...)` returned an unguarded client; the copy
|
|
189
|
+
is guarded now. `chat.completions.parse()` (structured outputs) is checked too.
|
|
190
|
+
- `guard()` is typed as returning the client you passed, so editors keep completions.
|
|
191
|
+
`guard(guarded, config=...)` applies the new settings instead of ignoring them.
|
|
192
|
+
- The provider is read from the client's base URL (Groq, Gemini, OpenAI, OpenRouter,
|
|
193
|
+
Ollama), so `fix` and exported regression tests can re-run logged calls without
|
|
194
|
+
`--caller`.
|
|
195
|
+
- Logs (`log_to`) mask secret-looking headers and keys (`Authorization`, `api_key`, ...),
|
|
196
|
+
and a call with a parameter JSON cannot hold (`NOT_GIVEN`, a timeout object, a pydantic
|
|
197
|
+
class) is still logged instead of silently dropped. "Not given" placeholders are no
|
|
198
|
+
longer stored as parameters.
|
|
199
|
+
- An answer that mentions `<think>` mid-text was cut there before checking; only reasoning
|
|
200
|
+
blocks that open the response are removed now.
|
|
201
|
+
- `@watch` on a method read `self` as the prompt; it takes the first string or list
|
|
202
|
+
argument.
|
|
203
|
+
- Deeply nested JSON (thousands of levels) is reported as a format failure instead of
|
|
204
|
+
crashing the check; the background hourly cap and the response registry are thread-safe.
|
|
205
|
+
|
|
206
|
+
## 0.1.0 (2026-10-19)
|
|
207
|
+
|
|
208
|
+
First real release.
|
|
209
|
+
|
|
210
|
+
- `guard(client)` for any OpenAI-compatible client: live checks on every call with no extra
|
|
211
|
+
LLM calls (F1 truncation incl. reasoning budgets, F2 format and tool-call arguments,
|
|
212
|
+
F3 refusal, F6 exact instruction checks, empty output, provider filter). Fail-open.
|
|
213
|
+
- `Config(mode="retry")`: one automatic retry with a known fix, keeping the better response.
|
|
214
|
+
- `counterfix.diagnose()`: live checks plus judge checks (F3, F4, F5, F6, F8, F9), one cause
|
|
215
|
+
per mistake; `judge=False` for live checks only; `Diagnosis.explain()` for readable output.
|
|
216
|
+
- `@counterfix.watch`: live checks on any function that calls a model (sync or async).
|
|
217
|
+
- `guard(Anthropic())`: the Anthropic SDK's `messages.create` (sync and async, tool_use
|
|
218
|
+
arguments checked, thinking kept as reasoning; retries change only `max_tokens`).
|
|
219
|
+
`stop_reason="refusal"` counts as a provider filter, not F3.
|
|
220
|
+
- `litellm_callback()` and `langchain_callback()`: observe-only live checks for apps already
|
|
221
|
+
on LiteLLM or LangChain.
|
|
222
|
+
- `counterfix.fix()`: the counterfactual engine. Tries one change per cause, re-runs the
|
|
223
|
+
call, and returns a fix only if it passes 2 of 3 re-runs; a plain retry runs first, and
|
|
224
|
+
`max_runs` caps the cost. Unverified causes get an escalation list.
|
|
225
|
+
- Streams: `stream=True` calls are checked when the stream ends, chunks pass through.
|
|
226
|
+
- `counterfix.context(source=..., reference=...)`: the RAG hook for every plug.
|
|
227
|
+
- Prompt regression tests: `counterfix fix LOG --out`, `counterfix export-tests`, a pytest
|
|
228
|
+
plugin (`--counterfix-judge`, `--counterfix-runs`, `counterfix_caller` fixture) and
|
|
229
|
+
`counterfix.testing.assert_ok()`.
|
|
230
|
+
- Model profiles: `counterfix profile`, `compare` (profiles, or a log re-run on a new
|
|
231
|
+
model), `drift`, `route`; `fix()` orders interventions by the model's profile; measured
|
|
232
|
+
profiles ship with the package.
|
|
233
|
+
- OpenTelemetry: `counterfix.otel.export()` and `counterfix.otel.SpanChecker()`.
|
|
234
|
+
- MCP server: `counterfix mcp` (`pip install "counterfix[mcp]"`).
|
|
235
|
+
- Docs site (`mkdocs.yml`, `docs/`) and a Colab notebook (`examples/quickstart.ipynb`).
|
|
236
|
+
- `fix()` stops after the first failed call when the model cannot be reached and says so
|
|
237
|
+
(`result.unreachable`) instead of advising untested changes. Daily quota limits fail
|
|
238
|
+
fast with `DailyLimitError`.
|
|
239
|
+
- Regression tests: `--counterfix-caller SPEC` / `COUNTERFIX_CALLER` (including
|
|
240
|
+
`MODEL@http://host/v1` for your own server); cases that cannot run fail in CI.
|
|
241
|
+
- Bring your own judge key: `configure()`, `COUNTERFIX_JUDGE`, detected Groq / Gemini /
|
|
242
|
+
OpenAI / OpenRouter keys or local Ollama; voting panels.
|
|
243
|
+
- `counterfix setup [--test]` and `counterfix lint` (rules L1 to L7).
|
|
244
|
+
- Reports: `log_to(path)` logs every guarded call to JSONL; `counterfix diagnose LOG`
|
|
245
|
+
prints a terminal summary and can write `--html` (one shareable file) and `--json`.
|
|
246
|
+
- Background judge checks: `Config(sample_rate=..., max_judged_per_hour=..., judge=...,
|
|
247
|
+
on_judged=...)` judges flagged calls and a sample of the rest off the request path.
|
|
248
|
+
`Config.judge_model` (never used) is replaced by `Config.judge`.
|
|
249
|
+
- Examples: `examples/quickstart.py` (no key), `examples/diagnose_with_judge.py`.
|
|
250
|
+
|
|
251
|
+
## 0.0.1
|
|
252
|
+
|
|
253
|
+
Name reserved on PyPI.
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: counterfix
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Summary: Find why an LLM call failed and get a fix that was tested to work.
|
|
5
5
|
Project-URL: Homepage, https://github.com/KANSADWALA/CounterFix
|
|
6
|
+
Project-URL: Documentation, https://kansadwala.github.io/CounterFix/
|
|
7
|
+
Project-URL: Changelog, https://github.com/KANSADWALA/CounterFix/blob/main/CHANGELOG.md
|
|
6
8
|
Project-URL: Issues, https://github.com/KANSADWALA/CounterFix/issues
|
|
7
9
|
Author: Shubham Kansadwala
|
|
8
10
|
License-Expression: MIT
|
|
@@ -12,6 +14,11 @@ Classifier: Development Status :: 3 - Alpha
|
|
|
12
14
|
Classifier: Intended Audience :: Developers
|
|
13
15
|
Classifier: License :: OSI Approved :: MIT License
|
|
14
16
|
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
15
22
|
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
16
23
|
Requires-Python: >=3.10
|
|
17
24
|
Requires-Dist: json-repair>=0.30
|
|
@@ -20,19 +27,28 @@ Requires-Dist: pydantic>=2.7
|
|
|
20
27
|
Requires-Dist: tenacity>=8.2
|
|
21
28
|
Provides-Extra: anthropic
|
|
22
29
|
Requires-Dist: anthropic>=0.40; extra == 'anthropic'
|
|
30
|
+
Provides-Extra: benchmarks
|
|
31
|
+
Requires-Dist: datasets>=2.18; extra == 'benchmarks'
|
|
32
|
+
Requires-Dist: lm-eval[ifeval]>=0.4.8; extra == 'benchmarks'
|
|
23
33
|
Provides-Extra: cache
|
|
24
34
|
Requires-Dist: diskcache>=5.6; extra == 'cache'
|
|
25
35
|
Provides-Extra: cli
|
|
26
36
|
Requires-Dist: rich>=13.7; extra == 'cli'
|
|
27
37
|
Requires-Dist: typer>=0.12; extra == 'cli'
|
|
38
|
+
Provides-Extra: code
|
|
39
|
+
Requires-Dist: evalplus>=0.3.1; extra == 'code'
|
|
28
40
|
Provides-Extra: dev
|
|
29
41
|
Requires-Dist: mypy==2.4.0; extra == 'dev'
|
|
30
42
|
Requires-Dist: pytest-recording>=0.13; extra == 'dev'
|
|
31
43
|
Requires-Dist: pytest>=8.0; extra == 'dev'
|
|
44
|
+
Requires-Dist: rouge-score>=0.1.2; extra == 'dev'
|
|
32
45
|
Requires-Dist: ruff==0.17.0; extra == 'dev'
|
|
46
|
+
Requires-Dist: sacrebleu>=2.4; extra == 'dev'
|
|
33
47
|
Requires-Dist: types-jsonschema; extra == 'dev'
|
|
34
48
|
Provides-Extra: docs
|
|
35
49
|
Requires-Dist: mkdocs-material>=9.5; extra == 'docs'
|
|
50
|
+
Provides-Extra: eval
|
|
51
|
+
Requires-Dist: pyyaml>=6.0; extra == 'eval'
|
|
36
52
|
Provides-Extra: jev
|
|
37
53
|
Requires-Dist: typesafe-sdk>=0.7; extra == 'jev'
|
|
38
54
|
Provides-Extra: langchain
|
|
@@ -43,19 +59,32 @@ Provides-Extra: llamaindex
|
|
|
43
59
|
Requires-Dist: llama-index-core>=0.11; extra == 'llamaindex'
|
|
44
60
|
Provides-Extra: mcp
|
|
45
61
|
Requires-Dist: mcp>=2.0; extra == 'mcp'
|
|
62
|
+
Provides-Extra: metrics
|
|
63
|
+
Requires-Dist: sacrebleu>=2.4; extra == 'metrics'
|
|
46
64
|
Provides-Extra: openai
|
|
47
65
|
Requires-Dist: openai>=1.40; extra == 'openai'
|
|
48
66
|
Provides-Extra: otel
|
|
49
67
|
Requires-Dist: opentelemetry-sdk>=1.27; extra == 'otel'
|
|
50
68
|
Provides-Extra: report
|
|
51
69
|
Requires-Dist: jinja2>=3.1; extra == 'report'
|
|
70
|
+
Provides-Extra: safety
|
|
71
|
+
Requires-Dist: detoxify>=0.5; extra == 'safety'
|
|
72
|
+
Requires-Dist: presidio-analyzer>=2.2; (python_version >= '3.11') and extra == 'safety'
|
|
52
73
|
Provides-Extra: semantic
|
|
74
|
+
Requires-Dist: bert-score>=0.3.13; extra == 'semantic'
|
|
75
|
+
Requires-Dist: sentence-transformers>=3.0; extra == 'semantic'
|
|
53
76
|
Requires-Dist: torch>=2.2; extra == 'semantic'
|
|
54
77
|
Requires-Dist: transformers>=4.44; extra == 'semantic'
|
|
55
78
|
Description-Content-Type: text/markdown
|
|
56
79
|
|
|
57
80
|
# counterfix
|
|
58
81
|
|
|
82
|
+
[](https://pypi.org/project/counterfix/)
|
|
83
|
+
[](https://pypi.org/project/counterfix/)
|
|
84
|
+
[](https://github.com/KANSADWALA/CounterFix/actions/workflows/ci.yml)
|
|
85
|
+
[](https://kansadwala.github.io/CounterFix/)
|
|
86
|
+
[](https://github.com/KANSADWALA/CounterFix/blob/main/LICENSE)
|
|
87
|
+
|
|
59
88
|
**Find why an LLM call failed, and get a fix that was tested to work.**
|
|
60
89
|
|
|
61
90
|
Most LLM evaluation tools tell you *that* a model failed. counterfix is built to tell you
|
|
@@ -66,15 +95,21 @@ call. A change counts only if it works in 2 of 3 re-runs. The change that fixes
|
|
|
66
95
|
failure is both the diagnosis and the prescription. We call this **counterfactual
|
|
67
96
|
diagnosis**.
|
|
68
97
|
|
|
69
|
-
> **Status: alpha (
|
|
70
|
-
> verified fixes, reports, regression tests, profiles
|
|
98
|
+
> **Status: alpha** (latest version in the PyPI badge above). Everything below works today: live checks, judge checks,
|
|
99
|
+
> verified fixes, reports, regression tests, profiles, dataset and RAG evaluation, code
|
|
100
|
+
> evaluation, benchmarks, prompt-injection and personal-data checks, token confidence,
|
|
101
|
+
> and agent and tool-call checks. APIs may still change.
|
|
71
102
|
>
|
|
72
103
|
> **Known limits.** Verified fixes are common for cut-off answers and broken JSON, rare
|
|
73
104
|
> for invented facts (3 of 14 on our seed set) and none yet for over-refusals or false
|
|
74
|
-
> premises: there counterfix says so and lists what to try instead.
|
|
75
|
-
>
|
|
76
|
-
>
|
|
77
|
-
>
|
|
105
|
+
> premises: there counterfix says so and lists what to try instead. For code that gives
|
|
106
|
+
> wrong results, a small model (qwen2.5:3b) got a verified fix on 2 of 8 repeatable
|
|
107
|
+
> failures. For prompt injection, one fix (`delimit_untrusted`) was verified on 9 of 10
|
|
108
|
+
> injected probes for gpt-oss-120b, none for qwen2.5:3b. The 20-probe `tools` benchmark
|
|
109
|
+
> is a smoke test (qwen2.5:3b passes all 20), not a model comparison. Shipped model
|
|
110
|
+
> profiles are small (54 probes; qwen2.5:3b only partly measured). The labels behind our accuracy
|
|
111
|
+
> numbers are reference labels; a human spot check agreed with 33 of 35 of them. See
|
|
112
|
+
> [`docs/results.md`](https://kansadwala.github.io/CounterFix/results/).
|
|
78
113
|
|
|
79
114
|
## Install
|
|
80
115
|
|
|
@@ -92,6 +127,11 @@ Optional extras add a provider's SDK only if you do not have it already:
|
|
|
92
127
|
| OpenAI's SDK, or any OpenAI-compatible API through it (Groq, Gemini, OpenRouter, Ollama, vLLM, ...) | `pip install "counterfix[openai]"` |
|
|
93
128
|
| Anthropic's SDK | `pip install "counterfix[anthropic]"` |
|
|
94
129
|
| LiteLLM or LangChain | `pip install "counterfix[litellm]"` or `"counterfix[langchain]"` |
|
|
130
|
+
| Benchmarks (MMLU, GSM8K, ARC, TruthfulQA, IFEval) | `pip install "counterfix[benchmarks]"` |
|
|
131
|
+
| HumanEval / MBPP and HumanEval+ / MBPP+ (EvalPlus) | `pip install "counterfix[code]"` |
|
|
132
|
+
| BLEU / chrF, or BERTScore, embeddings and perplexity | `"counterfix[metrics]"` or `"counterfix[semantic]"` |
|
|
133
|
+
| Toxicity and Presidio personal-data checks | `pip install "counterfix[safety]"` |
|
|
134
|
+
| Suite files in YAML, the MCP server, OpenTelemetry, LlamaIndex | `"counterfix[eval]"`, `"counterfix[mcp]"`, `"counterfix[otel]"`, `"counterfix[llamaindex]"` |
|
|
95
135
|
|
|
96
136
|
If that SDK is already in your project, plain `pip install counterfix` is enough.
|
|
97
137
|
|
|
@@ -117,7 +157,7 @@ F6 Dropped instruction (90%)
|
|
|
117
157
|
```
|
|
118
158
|
|
|
119
159
|
`judge=False` runs only the live checks: free, instant, no extra calls. More cases in
|
|
120
|
-
[`examples/quickstart.py`](examples/quickstart.py).
|
|
160
|
+
[`examples/quickstart.py`](https://github.com/KANSADWALA/CounterFix/blob/main/examples/quickstart.py).
|
|
121
161
|
|
|
122
162
|
### 2. Wrap your client (one line)
|
|
123
163
|
|
|
@@ -141,8 +181,8 @@ client = counterfix.guard(OpenAI(base_url="http://localhost:11434/v1", api_key="
|
|
|
141
181
|
```
|
|
142
182
|
|
|
143
183
|
The guarded client behaves like the original: sync or `AsyncOpenAI`, `stream=True`,
|
|
144
|
-
`chat.completions.parse()`, `
|
|
145
|
-
checked. Your type checker sees the client you passed. Two things differ:
|
|
184
|
+
`chat.completions.parse()`, the Responses API (`responses.create` / `.parse`),
|
|
185
|
+
`with` / `async with`, and `with_options()` all work and stay checked. Your type checker sees the client you passed. Two things differ:
|
|
146
186
|
`isinstance(client, OpenAI)` is False (pass `client.unwrapped` where a library insists on
|
|
147
187
|
the real object), and type checkers do not know `response.diagnosis`, so typed code can use
|
|
148
188
|
`counterfix.get_diagnosis(response)`.
|
|
@@ -185,7 +225,7 @@ print(report.explain())
|
|
|
185
225
|
|
|
186
226
|
The judge comes from your own key (a free `GROQ_API_KEY` or `GEMINI_API_KEY` is enough;
|
|
187
227
|
see [Bring your own key](#bring-your-own-key)). Run `counterfix setup` to see what it
|
|
188
|
-
found. Full script: [`examples/diagnose_with_judge.py`](examples/diagnose_with_judge.py).
|
|
228
|
+
found. Full script: [`examples/diagnose_with_judge.py`](https://github.com/KANSADWALA/CounterFix/blob/main/examples/diagnose_with_judge.py).
|
|
189
229
|
|
|
190
230
|
### 4. Get a fix that was tested to work
|
|
191
231
|
|
|
@@ -249,7 +289,83 @@ queue fills up, new calls are skipped rather than waited on; `counterfix.backgro
|
|
|
249
289
|
counts what was judged and skipped. Call `counterfix.background.flush()` before a short
|
|
250
290
|
script exits if you want the queued checks to finish.
|
|
251
291
|
|
|
252
|
-
### 7.
|
|
292
|
+
### 7. Evaluate a dataset: the score, the misses by cause, the fixes
|
|
293
|
+
|
|
294
|
+
```bash
|
|
295
|
+
counterfix eval capitals.jsonl --model groq:openai/gpt-oss-120b --metric exact_match --judge --fix 5
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
Scores every answer (exact match, F1, ROUGE-L, BLEU, multiple choice, final number, JSON,
|
|
299
|
+
latency, ...), groups the misses by cause (F1 to F14), and searches verified fixes on up to
|
|
300
|
+
5 misses. Streamed calls also record TTFT; reports show latency, TTFT and tokens/s
|
|
301
|
+
percentiles per model. Guides: [Evaluate a dataset](https://kansadwala.github.io/CounterFix/evals/),
|
|
302
|
+
[Speed and cost](https://kansadwala.github.io/CounterFix/speed/).
|
|
303
|
+
|
|
304
|
+
### 8. RAG: was it the retriever or the model?
|
|
305
|
+
|
|
306
|
+
```bash
|
|
307
|
+
counterfix eval rag.jsonl --model groq:openai/gpt-oss-120b --metric recall@5 --metric faithfulness --fix 5
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
Retrieval metrics from gold labels (hit, recall, precision, MRR, nDCG at k), judge metrics
|
|
311
|
+
(faithfulness, context precision and recall, answer relevancy, citations), and the cause of
|
|
312
|
+
each miss: F10 retrieval miss or F11 context ignored, with verified fixes (the gold passage
|
|
313
|
+
proves the retriever was at fault; the key passage first fixes ignored context). To test
|
|
314
|
+
your whole pipeline, pass it as a function: `counterfix eval qa.jsonl --app myapp:answer`
|
|
315
|
+
(or `evaluate(..., app=answer)`; LangChain and LlamaIndex outputs are read as they are).
|
|
316
|
+
Guide: [RAG evaluation](https://kansadwala.github.io/CounterFix/rag/).
|
|
317
|
+
|
|
318
|
+
### 9. Code and benchmarks
|
|
319
|
+
|
|
320
|
+
```bash
|
|
321
|
+
counterfix eval coding.jsonl --model groq:openai/gpt-oss-120b --metric tests --allow-code --fix 5
|
|
322
|
+
counterfix benchmark gsm8k --model ollama:qwen2.5:3b --limit 200
|
|
323
|
+
counterfix benchmark humaneval+ --model groq:openai/gpt-oss-120b --allow-code
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
Code runs against its tests in a sandbox (Docker, or a subprocess for trusted
|
|
327
|
+
benchmarks; off until you allow it). A failure is named (syntax, wrong name or signature,
|
|
328
|
+
runtime error, wrong output, timeout: F12), with what the code returned, and fixes such as
|
|
329
|
+
sending the error back are verified on re-run (measured so far: 2 of 8 repeatable
|
|
330
|
+
failures fixed on qwen2.5:3b; gpt-oss-120b passed 18 of 20 HumanEval problems). `--samples N` reports pass@k. Benchmarks: MMLU, GSM8K, ARC-Challenge,
|
|
331
|
+
TruthfulQA, IFEval (official checks), HumanEval/MBPP (counterfix's checker, any OS) and
|
|
332
|
+
HumanEval+/MBPP+ (scored by EvalPlus, Linux and macOS), with seeded subsets that fit a free
|
|
333
|
+
tier. Scores match lm-evaluation-harness on the same prompts (GSM8K 0.81 vs 0.79, MMLU
|
|
334
|
+
abstract algebra 0.56 vs 0.56, on qwen2.5:3b). Guides: [Code evaluation](https://kansadwala.github.io/CounterFix/code/),
|
|
335
|
+
[Benchmarks](https://kansadwala.github.io/CounterFix/benchmarks/).
|
|
336
|
+
|
|
337
|
+
### 10. Prompt injection, personal data, token confidence
|
|
338
|
+
|
|
339
|
+
```bash
|
|
340
|
+
counterfix benchmark injection --model ollama:qwen2.5:3b --fix 10
|
|
341
|
+
counterfix eval support.jsonl --model groq:openai/gpt-oss-120b --metric pii --metric toxicity
|
|
342
|
+
counterfix eval qa.jsonl --model openai:gpt-4o-mini --param logprobs=true --metric confidence
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
20 injection probes (instructions planted in retrieved passages and tool results, checked
|
|
346
|
+
exactly with canaries) and a judge check for any call with untrusted text: F13, with
|
|
347
|
+
verified fixes such as marking untrusted text as data. PII in answers (built-in
|
|
348
|
+
recognizers with checksums, optional Presidio), toxicity (Detoxify), and token confidence
|
|
349
|
+
where the provider returns logprobs. Guides: [Safety](https://kansadwala.github.io/CounterFix/safety/),
|
|
350
|
+
[Confidence and perplexity](https://kansadwala.github.io/CounterFix/confidence/).
|
|
351
|
+
|
|
352
|
+
### 11. Agents and tool calls
|
|
353
|
+
|
|
354
|
+
```bash
|
|
355
|
+
counterfix benchmark tools --model ollama:qwen2.5:3b --fix 10
|
|
356
|
+
counterfix eval tool_tests.jsonl --model groq:openai/gpt-oss-120b --metric tool_call --fix 5
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
```python
|
|
360
|
+
rec = counterfix.agents.record(task, langgraph_result, tools=tools, expect_tools=["search"])
|
|
361
|
+
counterfix.diagnose(rec) # F14: unknown tool, loop, missing call; judge: task done, and why
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
Runs from OpenAI-style messages, LangChain/LangGraph, Anthropic, OpenTelemetry spans or
|
|
365
|
+
guarded responses. Verified fixes for tool choice: `tool_choice`, naming the tool,
|
|
366
|
+
describing the arguments. Guide: [Agents and tool calls](https://kansadwala.github.io/CounterFix/agents/).
|
|
367
|
+
|
|
368
|
+
### 12. Keep fixed failures fixed (pytest)
|
|
253
369
|
|
|
254
370
|
```bash
|
|
255
371
|
counterfix fix calls.jsonl --judge --out fixes.jsonl
|
|
@@ -271,9 +387,9 @@ with the explanation when counterfix finds a failure.
|
|
|
271
387
|
| `counterfix.otel.export()`, `counterfix.otel.SpanChecker()` | send diagnoses to OpenTelemetry, or check the GenAI spans you already have |
|
|
272
388
|
| `counterfix mcp` | an MCP server so coding agents can lint prompts and check calls |
|
|
273
389
|
|
|
274
|
-
Full docs:
|
|
275
|
-
[`docs/results.md`](
|
|
276
|
-
[`examples/quickstart.ipynb`](examples/quickstart.ipynb).
|
|
390
|
+
Full docs: https://kansadwala.github.io/CounterFix/ (or build locally with `mkdocs serve`). Measured accuracy and fix rates:
|
|
391
|
+
[`docs/results.md`](https://kansadwala.github.io/CounterFix/results/). A Colab-ready notebook on a free model:
|
|
392
|
+
[`examples/quickstart.ipynb`](https://github.com/KANSADWALA/CounterFix/blob/main/examples/quickstart.ipynb).
|
|
277
393
|
|
|
278
394
|
## What it checks
|
|
279
395
|
|
|
@@ -282,7 +398,9 @@ spend the whole budget thinking), broken JSON or schema mismatches, broken tool-
|
|
|
282
398
|
arguments, refusals, empty answers, provider content filters, and broken instructions that
|
|
283
399
|
code can verify exactly: sentence, word and bullet counts, letter case, reply language
|
|
284
400
|
(by script), British or American spelling, banned words, "end with a question", "only the
|
|
285
|
-
number", "one word" and similar (in the spirit of IFEval).
|
|
401
|
+
number", "one word" and similar (in the spirit of IFEval). On agent runs they also catch
|
|
402
|
+
calls to tools that were not offered, repeated identical calls and missing expected calls;
|
|
403
|
+
with a canary planted in untrusted text, they catch a followed injection for free.
|
|
286
404
|
|
|
287
405
|
### Optional auto-retry: a fix that was re-run
|
|
288
406
|
|
|
@@ -304,7 +422,7 @@ print("after: ", r.diagnosis.summary())
|
|
|
304
422
|
|
|
305
423
|
```text
|
|
306
424
|
before: F1 the answer stopped at the max_tokens limit (20)
|
|
307
|
-
retry: {'max_tokens':
|
|
425
|
+
retry: {'max_tokens': 1024} fixed
|
|
308
426
|
after: no failure found by 5 checks
|
|
309
427
|
```
|
|
310
428
|
|
|
@@ -383,7 +501,7 @@ Design rules:
|
|
|
383
501
|
- **Safe defaults.** Live mode only flags failures and makes no extra LLM calls.
|
|
384
502
|
- **Light core.** The core depends on 4 small libraries; providers and heavy tools are extras.
|
|
385
503
|
|
|
386
|
-
## Failure types
|
|
504
|
+
## Failure types
|
|
387
505
|
|
|
388
506
|
| Code | Failure |
|
|
389
507
|
| --- | --- |
|
|
@@ -396,6 +514,11 @@ Design rules:
|
|
|
396
514
|
| F7 | Prompt fragility |
|
|
397
515
|
| F8 | Computation or token-level error |
|
|
398
516
|
| F9 | Ambiguous request |
|
|
517
|
+
| F10 | Retrieval miss (RAG: the answer was not in the retrieved text) |
|
|
518
|
+
| F11 | Context ignored (RAG: the answer was there, the model missed it) |
|
|
519
|
+
| F12 | Code fails its tests |
|
|
520
|
+
| F13 | Followed instructions planted in retrieved text or tool output (prompt injection) |
|
|
521
|
+
| F14 | Tool misuse (wrong, unknown or missing tool call, loop, agent task not done) |
|
|
399
522
|
|
|
400
523
|
API errors, rate limits and provider content filters are tracked separately, so they are
|
|
401
524
|
never counted as the model's fault.
|
|
@@ -410,7 +533,11 @@ pytest
|
|
|
410
533
|
```
|
|
411
534
|
|
|
412
535
|
Optional extras: `openai`, `anthropic`, `litellm`, `langchain`, `llamaindex`, `otel`,
|
|
413
|
-
`semantic`, `
|
|
536
|
+
`semantic`, `metrics`, `eval`, `code`, `benchmarks`, `safety`, `mcp`, `cache`, `cli`,
|
|
537
|
+
`report`, `jev`.
|
|
538
|
+
|
|
539
|
+
`python bench/dev_walkthrough.py SPEC` uses the library as a developer would against a real
|
|
540
|
+
model (about 40 small calls) and prints OK or the error for each step.
|
|
414
541
|
|
|
415
542
|
## Roadmap
|
|
416
543
|
|
|
@@ -427,12 +554,21 @@ Done:
|
|
|
427
554
|
- Prompt regression tests: `counterfix export-tests` and a pytest plugin ✅
|
|
428
555
|
- Model profiles, `compare`, `drift`, `route`; fixes ordered by what worked per model ✅
|
|
429
556
|
- OpenTelemetry export and GenAI span reader; streams; RAG context; MCP server ✅
|
|
557
|
+
- Released on PyPI (`pip install counterfix`), docs site, releases by trusted publishing ✅
|
|
558
|
+
- Evaluation, wave 1: `counterfix eval`, reference metrics, TTFT and speed/cost, verified speed fixes ✅
|
|
559
|
+
- RAG evaluation: retrieval and judge metrics, F10 retrieval miss vs F11 context ignored, verified RAG fixes ✅
|
|
560
|
+
- OpenAI Responses API in `guard()`; evaluate your own app or RAG pipeline (`--app`) ✅
|
|
561
|
+
- Code evaluation: sandboxed tests, F12 with its causes, verified code fixes, pass@k ✅
|
|
562
|
+
- Benchmarks: MMLU, GSM8K, ARC, TruthfulQA, IFEval, HumanEval(+), MBPP(+), checked against lm-eval ✅
|
|
563
|
+
- Safety: prompt injection (F13, probes and judge check, verified fixes), PII, toxicity; token confidence and perplexity ✅
|
|
564
|
+
- Agents and tool calls: trajectories from any framework, F14 checks, `tool_call` metric, verified tool-choice fixes ✅
|
|
565
|
+
- Trust in the metrics: a labelled set and measured accuracy for every judge-based check, and a human spot check of the labels ✅
|
|
430
566
|
|
|
431
567
|
Next:
|
|
432
568
|
|
|
433
|
-
1.
|
|
434
|
-
2.
|
|
435
|
-
3. A
|
|
569
|
+
1. A public dataset of diagnosed failures with their verified fixes
|
|
570
|
+
2. Larger labelled sets and full profiles for more models
|
|
571
|
+
3. A BFCL loader, so the tool-use benchmark can compare models
|
|
436
572
|
|
|
437
573
|
## License
|
|
438
574
|
|