cacheverifier 0.7.0__tar.gz → 0.8.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.
- {cacheverifier-0.7.0 → cacheverifier-0.8.0}/.github/workflows/ci.yml +3 -1
- {cacheverifier-0.7.0 → cacheverifier-0.8.0}/CHANGELOG.md +26 -0
- {cacheverifier-0.7.0 → cacheverifier-0.8.0}/PKG-INFO +122 -35
- {cacheverifier-0.7.0 → cacheverifier-0.8.0}/README.md +121 -34
- {cacheverifier-0.7.0 → cacheverifier-0.8.0}/cacheverifier/__init__.py +1 -1
- {cacheverifier-0.7.0 → cacheverifier-0.8.0}/cacheverifier/client.py +16 -1
- cacheverifier-0.8.0/cacheverifier/integrations/_cold_start.py +52 -0
- cacheverifier-0.8.0/cacheverifier/integrations/gptcache.py +174 -0
- {cacheverifier-0.7.0 → cacheverifier-0.8.0}/cacheverifier/integrations/langchain.py +24 -1
- {cacheverifier-0.7.0 → cacheverifier-0.8.0}/examples/gptcache_example.py +10 -4
- {cacheverifier-0.7.0 → cacheverifier-0.8.0}/examples/quickstart.py +5 -5
- {cacheverifier-0.7.0 → cacheverifier-0.8.0}/pyproject.toml +1 -1
- cacheverifier-0.8.0/tests/test_gptcache_integration.py +149 -0
- {cacheverifier-0.7.0 → cacheverifier-0.8.0}/tests/test_langchain_integration.py +62 -0
- cacheverifier-0.7.0/cacheverifier/integrations/gptcache.py +0 -102
- cacheverifier-0.7.0/tests/test_gptcache_integration.py +0 -55
- {cacheverifier-0.7.0 → cacheverifier-0.8.0}/.github/workflows/publish.yml +0 -0
- {cacheverifier-0.7.0 → cacheverifier-0.8.0}/.gitignore +0 -0
- {cacheverifier-0.7.0 → cacheverifier-0.8.0}/LICENSE +0 -0
- {cacheverifier-0.7.0 → cacheverifier-0.8.0}/cacheverifier/__main__.py +0 -0
- {cacheverifier-0.7.0 → cacheverifier-0.8.0}/cacheverifier/_healthcheck/__init__.py +0 -0
- {cacheverifier-0.7.0 → cacheverifier-0.8.0}/cacheverifier/_healthcheck/_base_models.py +0 -0
- {cacheverifier-0.7.0 → cacheverifier-0.8.0}/cacheverifier/_healthcheck/_finetune.py +0 -0
- {cacheverifier-0.7.0 → cacheverifier-0.8.0}/cacheverifier/_healthcheck/cli.py +0 -0
- {cacheverifier-0.7.0 → cacheverifier-0.8.0}/cacheverifier/_inference.py +0 -0
- {cacheverifier-0.7.0 → cacheverifier-0.8.0}/cacheverifier/integrations/__init__.py +0 -0
- {cacheverifier-0.7.0 → cacheverifier-0.8.0}/cacheverifier/local.py +0 -0
- {cacheverifier-0.7.0 → cacheverifier-0.8.0}/cacheverifier/py.typed +0 -0
- {cacheverifier-0.7.0 → cacheverifier-0.8.0}/tests/test_client.py +0 -0
- {cacheverifier-0.7.0 → cacheverifier-0.8.0}/tests/test_healthcheck.py +0 -0
- {cacheverifier-0.7.0 → cacheverifier-0.8.0}/tests/test_local.py +0 -0
|
@@ -7,7 +7,9 @@ on:
|
|
|
7
7
|
|
|
8
8
|
jobs:
|
|
9
9
|
test:
|
|
10
|
-
|
|
10
|
+
# ubuntu-latest is 26.04, which setup-python has no 3.9 build for; 3.9
|
|
11
|
+
# stays on 24.04 until it is dropped from requires-python.
|
|
12
|
+
runs-on: ${{ matrix.python-version == '3.9' && 'ubuntu-24.04' || 'ubuntu-latest' }}
|
|
11
13
|
strategy:
|
|
12
14
|
fail-fast: false
|
|
13
15
|
matrix:
|
|
@@ -1,5 +1,31 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.8.0
|
|
4
|
+
|
|
5
|
+
The integrations no longer switch your cache off during cold start. While a
|
|
6
|
+
workspace has no model of its own, `/v1/verify` rejects every call; an
|
|
7
|
+
integration sends every hit, so that rejected all of them, silently.
|
|
8
|
+
|
|
9
|
+
- `VerifiedCache` (LangChain) now serves cold-start hits exactly as the
|
|
10
|
+
wrapped cache returned them, and logs the service's notice once. Pass
|
|
11
|
+
`cold_start="reject"` for the old behavior.
|
|
12
|
+
- `CacheVerifierEvaluation` (GPTCache) takes `fallback=`, the evaluator it
|
|
13
|
+
replaces (e.g. `SearchDistanceEvaluation()`): during cold start GPTCache
|
|
14
|
+
decides with it exactly as before. Without a fallback, cold-start hits stay
|
|
15
|
+
misses, now with a warning saying how to keep them. `direct_hit=` serves
|
|
16
|
+
hits whose fallback score is at least that high without a verify call.
|
|
17
|
+
- `VerifyResult.cold_start`: True when no model decided the call.
|
|
18
|
+
|
|
19
|
+
## 0.7.1
|
|
20
|
+
|
|
21
|
+
README only, no code change, so the PyPI page matches the docs: the
|
|
22
|
+
`VerifyResult` table lists `audit`, `verification_id` and `decision_rule`;
|
|
23
|
+
the quickstart reports audited calls; certified thresholds are described as
|
|
24
|
+
informational, valid while traffic resembles the calibration data, and
|
|
25
|
+
covering only verified hits; the offline Health Check notes that the CLI
|
|
26
|
+
defaults to `ms_marco` while hosted fine-tunes default to `nli`; known limits
|
|
27
|
+
cover the stock model's MS MARCO training-data terms.
|
|
28
|
+
|
|
3
29
|
## 0.7.0
|
|
4
30
|
|
|
5
31
|
`entries_to_refresh()` lists the cache entries your feedback reported stale,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: cacheverifier
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.8.0
|
|
4
4
|
Summary: Python client for the CacheVerifier semantic-cache verification API, hosted or scored locally
|
|
5
5
|
Project-URL: Homepage, https://www.cacheverifier.com
|
|
6
6
|
Project-URL: Documentation, https://www.cacheverifier.com/docs
|
|
@@ -43,18 +43,44 @@ Description-Content-Type: text/markdown
|
|
|
43
43
|
[](https://pypi.org/project/cacheverifier/)
|
|
44
44
|
[](https://pypi.org/project/cacheverifier/)
|
|
45
45
|
|
|
46
|
-
Python client for **[CacheVerifier](https://www.cacheverifier.com)
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
46
|
+
Python client for **[CacheVerifier](https://www.cacheverifier.com)**: verification and
|
|
47
|
+
operations for semantic caches. It checks the cache hits a similarity threshold can't call,
|
|
48
|
+
then keeps your cache right after launch.
|
|
49
|
+
|
|
50
|
+
- **Verify the uncertain hits.** Given a query and a candidate cached answer, approve or
|
|
51
|
+
reject serving it in tens of milliseconds, so a match that is *close but wrong* doesn't
|
|
52
|
+
become a silent error. Fine-tuned on your own feedback.
|
|
53
|
+
- **Know your real error rate.** Randomized audits of approved hits give a conservative
|
|
54
|
+
(Clopper-Pearson) upper bound on how often a served answer is wrong, measured on your
|
|
55
|
+
traffic, not a benchmark.
|
|
56
|
+
- **Certified thresholds.** At fine-tune time, Conformal Risk Control can report a threshold
|
|
57
|
+
that keeps false reuses under a target you set. It is informational (`verify()` doesn't
|
|
58
|
+
apply it), holds only while traffic resembles the calibration data, and covers only the
|
|
59
|
+
hits you send to `verify()`, not the ones your cache serves on similarity alone. An
|
|
60
|
+
optional online (ACI) threshold does change decisions, following what the audits find.
|
|
61
|
+
- **Catch drift and recalibrate.** When traffic moves away from what the verifier learned,
|
|
62
|
+
the dashboard flags it; on Team and Scale you also get a webhook alert and it is
|
|
63
|
+
retrained on your feedback automatically.
|
|
64
|
+
- **Know which entries to fix.** `entries_to_refresh()` lists the cache entries your
|
|
65
|
+
feedback reported stale, or wrong more often than right, with your own cache keys.
|
|
66
|
+
- **Run it in your stack.** On Managed plans, `LocalVerifier` scores with your fine-tuned
|
|
67
|
+
model locally: no network hop, your texts stay with you, nothing metered.
|
|
50
68
|
|
|
51
69
|
CacheVerifier does **not** run your cache or do similarity search. Your cache backend does
|
|
52
70
|
its own lookup first; you call `verify()` only on the candidates in the similarity "gray
|
|
53
71
|
zone", where a plain threshold match might be wrong.
|
|
54
72
|
|
|
73
|
+
| | answers | examples |
|
|
74
|
+
|---|---|---|
|
|
75
|
+
| Static-threshold caches | "how similar is it?" | GPTCache, LiteLLM, Portkey, Redis LangCache |
|
|
76
|
+
| Adaptive-threshold caches | "how similar, with a learned per-entry cutoff?" (replaces your cache) | vCache |
|
|
77
|
+
| **Verification layer** | "is this cached answer right for this question?" (sits on top of your cache) | **CacheVerifier** |
|
|
78
|
+
|
|
55
79
|
- Docs / API reference: <https://www.cacheverifier.com/docs>
|
|
56
80
|
- Why similarity ≠ correctness: <https://www.cacheverifier.com/why-similarity-fails>
|
|
57
|
-
- The research behind it (
|
|
81
|
+
- The research behind it (four papers, code and raw results): <https://github.com/imxinchengyou/CacheVerifier>
|
|
82
|
+
- TypeScript / Node: no npm package yet; the docs have a dependency-free `fetch` client and a
|
|
83
|
+
LangChain.js cache wrapper to copy: <https://www.cacheverifier.com/docs#typescript>
|
|
58
84
|
|
|
59
85
|
## Install
|
|
60
86
|
|
|
@@ -73,25 +99,31 @@ are opt-in.
|
|
|
73
99
|
|
|
74
100
|
## Quickstart
|
|
75
101
|
|
|
76
|
-
Get a free API key at <https://www.cacheverifier.com> (
|
|
77
|
-
are free
|
|
102
|
+
Get a free API key at <https://www.cacheverifier.com> (verify, fine-tuning, audits and
|
|
103
|
+
entries to refresh are free, no card).
|
|
78
104
|
|
|
79
105
|
```python
|
|
80
106
|
from cacheverifier import CacheVerifier
|
|
81
107
|
|
|
82
108
|
cv = CacheVerifier(api_key="cv_...")
|
|
83
109
|
|
|
84
|
-
query = "
|
|
85
|
-
candidate = "
|
|
110
|
+
query = "can I change the delivery address after my parcel ships"
|
|
111
|
+
candidate = "You can change the delivery address any time before the parcel ships." # from your cache
|
|
86
112
|
|
|
87
113
|
result = cv.verify(query, candidate)
|
|
88
114
|
if result.approved:
|
|
89
115
|
answer = candidate # verified hit — skip the LLM call
|
|
90
116
|
else:
|
|
91
117
|
answer = call_your_llm(query) # not trustworthy — fall through
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
118
|
+
if result.audit: # a randomized audit of an approval: report whether
|
|
119
|
+
cv.feedback(query, candidate, # the cached answer matched the fresh one
|
|
120
|
+
was_correct=same_answer(answer, candidate),
|
|
121
|
+
verification_id=result.verification_id)
|
|
122
|
+
|
|
123
|
+
# Later, label the cached candidate (not the fresh LLM answer): was it right for this
|
|
124
|
+
# query? (a thumbs-down, a reopened ticket, a reviewer's call)
|
|
125
|
+
cv.feedback(query, candidate, was_correct=candidate_was_right, similarity_score=0.86,
|
|
126
|
+
verification_id=result.verification_id)
|
|
95
127
|
```
|
|
96
128
|
|
|
97
129
|
`verify()` returns a `VerifyResult`:
|
|
@@ -99,15 +131,21 @@ cv.feedback(query, answer, was_correct=True, similarity_score=0.86)
|
|
|
99
131
|
| field | meaning |
|
|
100
132
|
|---|---|
|
|
101
133
|
| `approved` | serve the cached answer (`True`) or fall through (`False`) |
|
|
102
|
-
| `
|
|
134
|
+
| `audit` | `True` when an approval was drawn for a randomized audit, so `approved` is `False`: call your LLM, serve its answer, and report whether the cached one matched |
|
|
135
|
+
| `verification_id` | set while auditing is on; pass it to `feedback()` |
|
|
136
|
+
| `score` / `threshold` | with `decision_rule="threshold"`, `approved` is `score >= threshold` |
|
|
137
|
+
| `decision_rule` | `"threshold"`, `"joint"` / `"joint_gap"` (a fitted rule that also uses your `similarity_score`), `"aci"` (online threshold), or `"cold_start"` |
|
|
103
138
|
| `model_version` | `"stock"`, `"v<id>"` (fine-tuned), `"cold_start_fail_closed"` / `"cold_start_auto_pending"`, or `"verify_unavailable"` |
|
|
104
139
|
| `latency_ms` | server-side inference time |
|
|
105
140
|
| `degraded` | `True` when the call failed and this result was synthesized client-side |
|
|
106
141
|
| `notice` | why no model decided the call (cold start); `None` otherwise |
|
|
142
|
+
| `cold_start` | property: `True` when no model decided the call (`model_version` is `"cold_start_..."`) |
|
|
107
143
|
| `shadow` | cold start only: a `ShadowVerdict` with what the shared stock model would have decided. The call is still rejected, so treat it as a preview |
|
|
108
144
|
|
|
109
145
|
A new account starts in cold start: every call returns `approved=False` until a
|
|
110
|
-
Health Check shows the shared model is accurate enough on your data.
|
|
146
|
+
Health Check shows the shared model is accurate enough on your data. (The
|
|
147
|
+
LangChain and GPTCache integrations hand cold-start hits back to your cache's
|
|
148
|
+
own decision instead, see below.) Look at
|
|
111
149
|
`result.shadow` to see what it would have decided in the meantime:
|
|
112
150
|
|
|
113
151
|
```python
|
|
@@ -144,23 +182,6 @@ cv = CacheVerifier(api_key="cv_...", fail_open=True)
|
|
|
144
182
|
There is no formal uptime SLA yet, which is the other reason the fallback path
|
|
145
183
|
is a built-in default rather than left to you.
|
|
146
184
|
|
|
147
|
-
## GPTCache
|
|
148
|
-
|
|
149
|
-
Drop the verifier into a GPTCache pipeline as its similarity evaluator — no fork required:
|
|
150
|
-
|
|
151
|
-
```python
|
|
152
|
-
from gptcache import cache
|
|
153
|
-
from cacheverifier.integrations.gptcache import CacheVerifierEvaluation
|
|
154
|
-
|
|
155
|
-
evaluator = CacheVerifierEvaluation(api_key="cv_...")
|
|
156
|
-
cache.init(similarity_evaluation=evaluator, ...)
|
|
157
|
-
|
|
158
|
-
# when you learn a served hit's real outcome:
|
|
159
|
-
evaluator.report_feedback(query, answer, was_correct=False, similarity_score=0.9)
|
|
160
|
-
```
|
|
161
|
-
|
|
162
|
-
See [`examples/gptcache_example.py`](examples/gptcache_example.py).
|
|
163
|
-
|
|
164
185
|
## LangChain
|
|
165
186
|
|
|
166
187
|
`VerifiedCache` wraps any LangChain cache, typically a semantic one, and only
|
|
@@ -185,6 +206,46 @@ set_llm_cache(
|
|
|
185
206
|
)
|
|
186
207
|
```
|
|
187
208
|
|
|
209
|
+
Until your workspace has a model of its own (cold start), the service
|
|
210
|
+
decides nothing and would reject every hit, which would switch your cache
|
|
211
|
+
off. So during cold start `VerifiedCache` serves each hit exactly as the
|
|
212
|
+
wrapped cache returned it, the same as without it, and logs a warning the
|
|
213
|
+
first time. Pass `cold_start="reject"` to fall through to your model instead,
|
|
214
|
+
for example if you loosened the wrapped cache's similarity threshold because
|
|
215
|
+
a verifier would be checking its hits.
|
|
216
|
+
|
|
217
|
+
## GPTCache
|
|
218
|
+
|
|
219
|
+
Drop the verifier into a GPTCache pipeline as its similarity evaluator — no fork required:
|
|
220
|
+
|
|
221
|
+
```python
|
|
222
|
+
from gptcache import cache
|
|
223
|
+
from gptcache.similarity_evaluation import SearchDistanceEvaluation
|
|
224
|
+
from cacheverifier.integrations.gptcache import CacheVerifierEvaluation
|
|
225
|
+
|
|
226
|
+
evaluator = CacheVerifierEvaluation(
|
|
227
|
+
api_key="cv_...",
|
|
228
|
+
fallback=SearchDistanceEvaluation(), # the evaluator you used before
|
|
229
|
+
direct_hit=0.95, # optional: trust hits this close without verifying
|
|
230
|
+
)
|
|
231
|
+
cache.init(similarity_evaluation=evaluator, ...)
|
|
232
|
+
|
|
233
|
+
# when you learn a served hit's real outcome:
|
|
234
|
+
evaluator.report_feedback(query, answer, was_correct=False, similarity_score=0.9)
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
GPTCache applies its `similarity_threshold` to whatever the evaluator returns,
|
|
238
|
+
so replacing your evaluator also drops the similarity check it did. With
|
|
239
|
+
`fallback` set:
|
|
240
|
+
|
|
241
|
+
- during cold start, hits are decided by the fallback exactly as before (the
|
|
242
|
+
first one logs a warning); `cold_start="reject"` makes them misses instead.
|
|
243
|
+
Without a fallback, cold-start hits are always misses.
|
|
244
|
+
- `direct_hit` serves hits whose fallback score is at least that high without
|
|
245
|
+
a verify call. It uses the same 0–1 scale as GPTCache's `similarity_threshold`.
|
|
246
|
+
|
|
247
|
+
See [`examples/gptcache_example.py`](examples/gptcache_example.py).
|
|
248
|
+
|
|
188
249
|
## Entries to refresh
|
|
189
250
|
|
|
190
251
|
Send your cache's own key with feedback, and the service tells you which
|
|
@@ -273,12 +334,14 @@ fully offline. `--emit-summary` writes an aggregate-only JSON file (AUCs, counts
|
|
|
273
334
|
no query or answer text) that's safe to share for a human read.
|
|
274
335
|
|
|
275
336
|
**Choosing the base model.** `--base-model` takes the same values as the hosted
|
|
276
|
-
service's `base_model` fine-tune parameter
|
|
337
|
+
service's `base_model` fine-tune parameter. The defaults differ: the CLI starts from
|
|
338
|
+
`ms_marco`, hosted fine-tunes from `nli`. To compare a local run with a hosted fine-tune,
|
|
339
|
+
pass `--base-model nli`.
|
|
277
340
|
|
|
278
341
|
| `--base-model` | model | use for |
|
|
279
342
|
|---|---|---|
|
|
280
|
-
| `ms_marco` (default) | `cross-encoder/ms-marco-MiniLM-L6-v2` | English traffic |
|
|
281
|
-
| `nli` | `cross-encoder/nli-MiniLM2-L6-H768` | English; entailment pretraining, stronger on negation / entity swaps |
|
|
343
|
+
| `ms_marco` (CLI default) | `cross-encoder/ms-marco-MiniLM-L6-v2` | English traffic |
|
|
344
|
+
| `nli` (hosted default) | `cross-encoder/nli-MiniLM2-L6-H768` | English; entailment pretraining, stronger on negation / entity swaps |
|
|
282
345
|
| `multilingual` | `BAAI/bge-reranker-base` | Chinese and other non-English traffic (~6 GB RAM to fine-tune) |
|
|
283
346
|
| `multilingual_small` | `cross-encoder/mmarco-mMiniLMv2-L12-H384-v1` | same, smaller (~3 GB RAM) |
|
|
284
347
|
|
|
@@ -308,6 +371,30 @@ verdict
|
|
|
308
371
|
IMPROVED -- fine-tuning on your own data helps this traffic
|
|
309
372
|
```
|
|
310
373
|
|
|
374
|
+
## Recommended production setup
|
|
375
|
+
|
|
376
|
+
- Keep randomized audits on (the default for new accounts) and report each audited call
|
|
377
|
+
with `feedback(..., verification_id=r.verification_id)`, or `result=r` on
|
|
378
|
+
`LocalVerifier`. That is what makes the measured error rate real.
|
|
379
|
+
- Fine-tune with `cost_ratio=` (how many times worse a wrong reuse is than an extra LLM
|
|
380
|
+
call) instead of the default threshold. ACI (hosted only) is optional: it lets the
|
|
381
|
+
threshold follow the audits, but on the one real drifting stream we tested it gave no
|
|
382
|
+
clear advantage over a fixed threshold.
|
|
383
|
+
- Send `cache_entry_id=` with feedback so `entries_to_refresh()` hands back your keys.
|
|
384
|
+
- At volume, score locally with `LocalVerifier`.
|
|
385
|
+
|
|
386
|
+
## Known limits
|
|
387
|
+
|
|
388
|
+
- The hosted verifier is **English-only** today. Don't send Chinese or other non-English
|
|
389
|
+
traffic to it; multilingual base models are evaluated in the docs but not deployed.
|
|
390
|
+
- Query + answer share a **128-token** input; longer pairs are truncated head and tail.
|
|
391
|
+
- Verify endpoints allow about **20 requests/second per API key**. Use `verify_batch()` or
|
|
392
|
+
`LocalVerifier` beyond that.
|
|
393
|
+
- No formal uptime SLA yet; `verify()` fails closed by default (see above).
|
|
394
|
+
- The shared stock model (`cross-encoder/ms-marco-MiniLM-L6-v2`) is Apache-2.0 but trained
|
|
395
|
+
on MS MARCO, whose terms describe it as for non-commercial research. Fine-tunes start from
|
|
396
|
+
the NLI base (`cross-encoder/nli-MiniLM2-L6-H768`, Apache-2.0) by default.
|
|
397
|
+
|
|
311
398
|
## API surface
|
|
312
399
|
|
|
313
400
|
| method | endpoint |
|
|
@@ -4,18 +4,44 @@
|
|
|
4
4
|
[](https://pypi.org/project/cacheverifier/)
|
|
5
5
|
[](https://pypi.org/project/cacheverifier/)
|
|
6
6
|
|
|
7
|
-
Python client for **[CacheVerifier](https://www.cacheverifier.com)
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
Python client for **[CacheVerifier](https://www.cacheverifier.com)**: verification and
|
|
8
|
+
operations for semantic caches. It checks the cache hits a similarity threshold can't call,
|
|
9
|
+
then keeps your cache right after launch.
|
|
10
|
+
|
|
11
|
+
- **Verify the uncertain hits.** Given a query and a candidate cached answer, approve or
|
|
12
|
+
reject serving it in tens of milliseconds, so a match that is *close but wrong* doesn't
|
|
13
|
+
become a silent error. Fine-tuned on your own feedback.
|
|
14
|
+
- **Know your real error rate.** Randomized audits of approved hits give a conservative
|
|
15
|
+
(Clopper-Pearson) upper bound on how often a served answer is wrong, measured on your
|
|
16
|
+
traffic, not a benchmark.
|
|
17
|
+
- **Certified thresholds.** At fine-tune time, Conformal Risk Control can report a threshold
|
|
18
|
+
that keeps false reuses under a target you set. It is informational (`verify()` doesn't
|
|
19
|
+
apply it), holds only while traffic resembles the calibration data, and covers only the
|
|
20
|
+
hits you send to `verify()`, not the ones your cache serves on similarity alone. An
|
|
21
|
+
optional online (ACI) threshold does change decisions, following what the audits find.
|
|
22
|
+
- **Catch drift and recalibrate.** When traffic moves away from what the verifier learned,
|
|
23
|
+
the dashboard flags it; on Team and Scale you also get a webhook alert and it is
|
|
24
|
+
retrained on your feedback automatically.
|
|
25
|
+
- **Know which entries to fix.** `entries_to_refresh()` lists the cache entries your
|
|
26
|
+
feedback reported stale, or wrong more often than right, with your own cache keys.
|
|
27
|
+
- **Run it in your stack.** On Managed plans, `LocalVerifier` scores with your fine-tuned
|
|
28
|
+
model locally: no network hop, your texts stay with you, nothing metered.
|
|
11
29
|
|
|
12
30
|
CacheVerifier does **not** run your cache or do similarity search. Your cache backend does
|
|
13
31
|
its own lookup first; you call `verify()` only on the candidates in the similarity "gray
|
|
14
32
|
zone", where a plain threshold match might be wrong.
|
|
15
33
|
|
|
34
|
+
| | answers | examples |
|
|
35
|
+
|---|---|---|
|
|
36
|
+
| Static-threshold caches | "how similar is it?" | GPTCache, LiteLLM, Portkey, Redis LangCache |
|
|
37
|
+
| Adaptive-threshold caches | "how similar, with a learned per-entry cutoff?" (replaces your cache) | vCache |
|
|
38
|
+
| **Verification layer** | "is this cached answer right for this question?" (sits on top of your cache) | **CacheVerifier** |
|
|
39
|
+
|
|
16
40
|
- Docs / API reference: <https://www.cacheverifier.com/docs>
|
|
17
41
|
- Why similarity ≠ correctness: <https://www.cacheverifier.com/why-similarity-fails>
|
|
18
|
-
- The research behind it (
|
|
42
|
+
- The research behind it (four papers, code and raw results): <https://github.com/imxinchengyou/CacheVerifier>
|
|
43
|
+
- TypeScript / Node: no npm package yet; the docs have a dependency-free `fetch` client and a
|
|
44
|
+
LangChain.js cache wrapper to copy: <https://www.cacheverifier.com/docs#typescript>
|
|
19
45
|
|
|
20
46
|
## Install
|
|
21
47
|
|
|
@@ -34,25 +60,31 @@ are opt-in.
|
|
|
34
60
|
|
|
35
61
|
## Quickstart
|
|
36
62
|
|
|
37
|
-
Get a free API key at <https://www.cacheverifier.com> (
|
|
38
|
-
are free
|
|
63
|
+
Get a free API key at <https://www.cacheverifier.com> (verify, fine-tuning, audits and
|
|
64
|
+
entries to refresh are free, no card).
|
|
39
65
|
|
|
40
66
|
```python
|
|
41
67
|
from cacheverifier import CacheVerifier
|
|
42
68
|
|
|
43
69
|
cv = CacheVerifier(api_key="cv_...")
|
|
44
70
|
|
|
45
|
-
query = "
|
|
46
|
-
candidate = "
|
|
71
|
+
query = "can I change the delivery address after my parcel ships"
|
|
72
|
+
candidate = "You can change the delivery address any time before the parcel ships." # from your cache
|
|
47
73
|
|
|
48
74
|
result = cv.verify(query, candidate)
|
|
49
75
|
if result.approved:
|
|
50
76
|
answer = candidate # verified hit — skip the LLM call
|
|
51
77
|
else:
|
|
52
78
|
answer = call_your_llm(query) # not trustworthy — fall through
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
79
|
+
if result.audit: # a randomized audit of an approval: report whether
|
|
80
|
+
cv.feedback(query, candidate, # the cached answer matched the fresh one
|
|
81
|
+
was_correct=same_answer(answer, candidate),
|
|
82
|
+
verification_id=result.verification_id)
|
|
83
|
+
|
|
84
|
+
# Later, label the cached candidate (not the fresh LLM answer): was it right for this
|
|
85
|
+
# query? (a thumbs-down, a reopened ticket, a reviewer's call)
|
|
86
|
+
cv.feedback(query, candidate, was_correct=candidate_was_right, similarity_score=0.86,
|
|
87
|
+
verification_id=result.verification_id)
|
|
56
88
|
```
|
|
57
89
|
|
|
58
90
|
`verify()` returns a `VerifyResult`:
|
|
@@ -60,15 +92,21 @@ cv.feedback(query, answer, was_correct=True, similarity_score=0.86)
|
|
|
60
92
|
| field | meaning |
|
|
61
93
|
|---|---|
|
|
62
94
|
| `approved` | serve the cached answer (`True`) or fall through (`False`) |
|
|
63
|
-
| `
|
|
95
|
+
| `audit` | `True` when an approval was drawn for a randomized audit, so `approved` is `False`: call your LLM, serve its answer, and report whether the cached one matched |
|
|
96
|
+
| `verification_id` | set while auditing is on; pass it to `feedback()` |
|
|
97
|
+
| `score` / `threshold` | with `decision_rule="threshold"`, `approved` is `score >= threshold` |
|
|
98
|
+
| `decision_rule` | `"threshold"`, `"joint"` / `"joint_gap"` (a fitted rule that also uses your `similarity_score`), `"aci"` (online threshold), or `"cold_start"` |
|
|
64
99
|
| `model_version` | `"stock"`, `"v<id>"` (fine-tuned), `"cold_start_fail_closed"` / `"cold_start_auto_pending"`, or `"verify_unavailable"` |
|
|
65
100
|
| `latency_ms` | server-side inference time |
|
|
66
101
|
| `degraded` | `True` when the call failed and this result was synthesized client-side |
|
|
67
102
|
| `notice` | why no model decided the call (cold start); `None` otherwise |
|
|
103
|
+
| `cold_start` | property: `True` when no model decided the call (`model_version` is `"cold_start_..."`) |
|
|
68
104
|
| `shadow` | cold start only: a `ShadowVerdict` with what the shared stock model would have decided. The call is still rejected, so treat it as a preview |
|
|
69
105
|
|
|
70
106
|
A new account starts in cold start: every call returns `approved=False` until a
|
|
71
|
-
Health Check shows the shared model is accurate enough on your data.
|
|
107
|
+
Health Check shows the shared model is accurate enough on your data. (The
|
|
108
|
+
LangChain and GPTCache integrations hand cold-start hits back to your cache's
|
|
109
|
+
own decision instead, see below.) Look at
|
|
72
110
|
`result.shadow` to see what it would have decided in the meantime:
|
|
73
111
|
|
|
74
112
|
```python
|
|
@@ -105,23 +143,6 @@ cv = CacheVerifier(api_key="cv_...", fail_open=True)
|
|
|
105
143
|
There is no formal uptime SLA yet, which is the other reason the fallback path
|
|
106
144
|
is a built-in default rather than left to you.
|
|
107
145
|
|
|
108
|
-
## GPTCache
|
|
109
|
-
|
|
110
|
-
Drop the verifier into a GPTCache pipeline as its similarity evaluator — no fork required:
|
|
111
|
-
|
|
112
|
-
```python
|
|
113
|
-
from gptcache import cache
|
|
114
|
-
from cacheverifier.integrations.gptcache import CacheVerifierEvaluation
|
|
115
|
-
|
|
116
|
-
evaluator = CacheVerifierEvaluation(api_key="cv_...")
|
|
117
|
-
cache.init(similarity_evaluation=evaluator, ...)
|
|
118
|
-
|
|
119
|
-
# when you learn a served hit's real outcome:
|
|
120
|
-
evaluator.report_feedback(query, answer, was_correct=False, similarity_score=0.9)
|
|
121
|
-
```
|
|
122
|
-
|
|
123
|
-
See [`examples/gptcache_example.py`](examples/gptcache_example.py).
|
|
124
|
-
|
|
125
146
|
## LangChain
|
|
126
147
|
|
|
127
148
|
`VerifiedCache` wraps any LangChain cache, typically a semantic one, and only
|
|
@@ -146,6 +167,46 @@ set_llm_cache(
|
|
|
146
167
|
)
|
|
147
168
|
```
|
|
148
169
|
|
|
170
|
+
Until your workspace has a model of its own (cold start), the service
|
|
171
|
+
decides nothing and would reject every hit, which would switch your cache
|
|
172
|
+
off. So during cold start `VerifiedCache` serves each hit exactly as the
|
|
173
|
+
wrapped cache returned it, the same as without it, and logs a warning the
|
|
174
|
+
first time. Pass `cold_start="reject"` to fall through to your model instead,
|
|
175
|
+
for example if you loosened the wrapped cache's similarity threshold because
|
|
176
|
+
a verifier would be checking its hits.
|
|
177
|
+
|
|
178
|
+
## GPTCache
|
|
179
|
+
|
|
180
|
+
Drop the verifier into a GPTCache pipeline as its similarity evaluator — no fork required:
|
|
181
|
+
|
|
182
|
+
```python
|
|
183
|
+
from gptcache import cache
|
|
184
|
+
from gptcache.similarity_evaluation import SearchDistanceEvaluation
|
|
185
|
+
from cacheverifier.integrations.gptcache import CacheVerifierEvaluation
|
|
186
|
+
|
|
187
|
+
evaluator = CacheVerifierEvaluation(
|
|
188
|
+
api_key="cv_...",
|
|
189
|
+
fallback=SearchDistanceEvaluation(), # the evaluator you used before
|
|
190
|
+
direct_hit=0.95, # optional: trust hits this close without verifying
|
|
191
|
+
)
|
|
192
|
+
cache.init(similarity_evaluation=evaluator, ...)
|
|
193
|
+
|
|
194
|
+
# when you learn a served hit's real outcome:
|
|
195
|
+
evaluator.report_feedback(query, answer, was_correct=False, similarity_score=0.9)
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
GPTCache applies its `similarity_threshold` to whatever the evaluator returns,
|
|
199
|
+
so replacing your evaluator also drops the similarity check it did. With
|
|
200
|
+
`fallback` set:
|
|
201
|
+
|
|
202
|
+
- during cold start, hits are decided by the fallback exactly as before (the
|
|
203
|
+
first one logs a warning); `cold_start="reject"` makes them misses instead.
|
|
204
|
+
Without a fallback, cold-start hits are always misses.
|
|
205
|
+
- `direct_hit` serves hits whose fallback score is at least that high without
|
|
206
|
+
a verify call. It uses the same 0–1 scale as GPTCache's `similarity_threshold`.
|
|
207
|
+
|
|
208
|
+
See [`examples/gptcache_example.py`](examples/gptcache_example.py).
|
|
209
|
+
|
|
149
210
|
## Entries to refresh
|
|
150
211
|
|
|
151
212
|
Send your cache's own key with feedback, and the service tells you which
|
|
@@ -234,12 +295,14 @@ fully offline. `--emit-summary` writes an aggregate-only JSON file (AUCs, counts
|
|
|
234
295
|
no query or answer text) that's safe to share for a human read.
|
|
235
296
|
|
|
236
297
|
**Choosing the base model.** `--base-model` takes the same values as the hosted
|
|
237
|
-
service's `base_model` fine-tune parameter
|
|
298
|
+
service's `base_model` fine-tune parameter. The defaults differ: the CLI starts from
|
|
299
|
+
`ms_marco`, hosted fine-tunes from `nli`. To compare a local run with a hosted fine-tune,
|
|
300
|
+
pass `--base-model nli`.
|
|
238
301
|
|
|
239
302
|
| `--base-model` | model | use for |
|
|
240
303
|
|---|---|---|
|
|
241
|
-
| `ms_marco` (default) | `cross-encoder/ms-marco-MiniLM-L6-v2` | English traffic |
|
|
242
|
-
| `nli` | `cross-encoder/nli-MiniLM2-L6-H768` | English; entailment pretraining, stronger on negation / entity swaps |
|
|
304
|
+
| `ms_marco` (CLI default) | `cross-encoder/ms-marco-MiniLM-L6-v2` | English traffic |
|
|
305
|
+
| `nli` (hosted default) | `cross-encoder/nli-MiniLM2-L6-H768` | English; entailment pretraining, stronger on negation / entity swaps |
|
|
243
306
|
| `multilingual` | `BAAI/bge-reranker-base` | Chinese and other non-English traffic (~6 GB RAM to fine-tune) |
|
|
244
307
|
| `multilingual_small` | `cross-encoder/mmarco-mMiniLMv2-L12-H384-v1` | same, smaller (~3 GB RAM) |
|
|
245
308
|
|
|
@@ -269,6 +332,30 @@ verdict
|
|
|
269
332
|
IMPROVED -- fine-tuning on your own data helps this traffic
|
|
270
333
|
```
|
|
271
334
|
|
|
335
|
+
## Recommended production setup
|
|
336
|
+
|
|
337
|
+
- Keep randomized audits on (the default for new accounts) and report each audited call
|
|
338
|
+
with `feedback(..., verification_id=r.verification_id)`, or `result=r` on
|
|
339
|
+
`LocalVerifier`. That is what makes the measured error rate real.
|
|
340
|
+
- Fine-tune with `cost_ratio=` (how many times worse a wrong reuse is than an extra LLM
|
|
341
|
+
call) instead of the default threshold. ACI (hosted only) is optional: it lets the
|
|
342
|
+
threshold follow the audits, but on the one real drifting stream we tested it gave no
|
|
343
|
+
clear advantage over a fixed threshold.
|
|
344
|
+
- Send `cache_entry_id=` with feedback so `entries_to_refresh()` hands back your keys.
|
|
345
|
+
- At volume, score locally with `LocalVerifier`.
|
|
346
|
+
|
|
347
|
+
## Known limits
|
|
348
|
+
|
|
349
|
+
- The hosted verifier is **English-only** today. Don't send Chinese or other non-English
|
|
350
|
+
traffic to it; multilingual base models are evaluated in the docs but not deployed.
|
|
351
|
+
- Query + answer share a **128-token** input; longer pairs are truncated head and tail.
|
|
352
|
+
- Verify endpoints allow about **20 requests/second per API key**. Use `verify_batch()` or
|
|
353
|
+
`LocalVerifier` beyond that.
|
|
354
|
+
- No formal uptime SLA yet; `verify()` fails closed by default (see above).
|
|
355
|
+
- The shared stock model (`cross-encoder/ms-marco-MiniLM-L6-v2`) is Apache-2.0 but trained
|
|
356
|
+
on MS MARCO, whose terms describe it as for non-commercial research. Fine-tunes start from
|
|
357
|
+
the NLI base (`cross-encoder/nli-MiniLM2-L6-H768`, Apache-2.0) by default.
|
|
358
|
+
|
|
272
359
|
## API surface
|
|
273
360
|
|
|
274
361
|
| method | endpoint |
|
|
@@ -19,7 +19,7 @@ Research behind it: https://github.com/imxinchengyou/CacheVerifier
|
|
|
19
19
|
from cacheverifier.client import CacheVerifier, CacheVerifierError, ShadowVerdict, VerifyResult
|
|
20
20
|
from cacheverifier.local import LocalVerifier, ModelCheckFailed
|
|
21
21
|
|
|
22
|
-
__version__ = "0.
|
|
22
|
+
__version__ = "0.8.0"
|
|
23
23
|
__all__ = [
|
|
24
24
|
"CacheVerifier",
|
|
25
25
|
"CacheVerifierError",
|
|
@@ -29,9 +29,14 @@ DEFAULT_VERIFY_TIMEOUT = 1.0
|
|
|
29
29
|
#: failed (timeout / connection error / 5xx) and the client fell back.
|
|
30
30
|
VERIFY_UNAVAILABLE = "verify_unavailable"
|
|
31
31
|
|
|
32
|
+
#: model_version prefix the API uses when no model ran because the workspace
|
|
33
|
+
#: is still in cold start ("cold_start_fail_closed" / "cold_start_auto_pending").
|
|
34
|
+
COLD_START_PREFIX = "cold_start_"
|
|
35
|
+
|
|
32
36
|
_log = logging.getLogger(__name__)
|
|
33
37
|
|
|
34
38
|
__all__ = [
|
|
39
|
+
"COLD_START_PREFIX",
|
|
35
40
|
"DEFAULT_BASE_URL",
|
|
36
41
|
"DEFAULT_TIMEOUT",
|
|
37
42
|
"DEFAULT_VERIFY_TIMEOUT",
|
|
@@ -102,6 +107,8 @@ class VerifyResult:
|
|
|
102
107
|
(default `False` -> `approved=False`, i.e. fall through to your LLM).
|
|
103
108
|
- `notice`: a plain-language reason when no model decided the call (cold
|
|
104
109
|
start); None otherwise.
|
|
110
|
+
- `cold_start` (property): True when no model decided the call, i.e.
|
|
111
|
+
`model_version` is `"cold_start_..."`.
|
|
105
112
|
- `shadow`: on a cold-start call, what the shared stock model would have
|
|
106
113
|
decided (a `ShadowVerdict`); None otherwise, and on servers that don't
|
|
107
114
|
send it.
|
|
@@ -129,6 +136,14 @@ class VerifyResult:
|
|
|
129
136
|
local_decision: str | None = None
|
|
130
137
|
"""`LocalVerifier` only: its handle for this decision until upload."""
|
|
131
138
|
|
|
139
|
+
@property
|
|
140
|
+
def cold_start(self) -> bool:
|
|
141
|
+
"""True when no model decided this call: the workspace has no model
|
|
142
|
+
of its own yet and runs fail-closed, so `approved` is always False
|
|
143
|
+
and `notice` says what to do next. A verdict from the shared stock
|
|
144
|
+
model is not a cold start in this sense: a model did run."""
|
|
145
|
+
return self.model_version.startswith(COLD_START_PREFIX)
|
|
146
|
+
|
|
132
147
|
@classmethod
|
|
133
148
|
def _from_json(cls, d: dict[str, Any]) -> VerifyResult:
|
|
134
149
|
shadow = d.get("shadow")
|
|
@@ -152,7 +167,7 @@ class CacheVerifier:
|
|
|
152
167
|
from cacheverifier import CacheVerifier
|
|
153
168
|
|
|
154
169
|
cv = CacheVerifier(api_key="cv_...")
|
|
155
|
-
result = cv.verify("
|
|
170
|
+
result = cv.verify("can I change my flight date", "You can change your seat until check-in closes.")
|
|
156
171
|
if result.approved:
|
|
157
172
|
... # serve the cached answer
|
|
158
173
|
else:
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"""What an integration does with a cold-start verdict.
|
|
2
|
+
|
|
3
|
+
While a workspace has no model of its own, `/v1/verify` runs fail-closed and
|
|
4
|
+
rejects every call. Rejecting a call the caller only sends in its gray zone
|
|
5
|
+
is cheap. An integration, though, sends every hit its cache backend
|
|
6
|
+
returns, and rejecting all of them switches the cache off: the hit rate
|
|
7
|
+
drops to zero, LLM spend goes up, and nothing is protected that wasn't
|
|
8
|
+
already being served before CacheVerifier was added. So by default an
|
|
9
|
+
integration hands a cold-start call back to the cache's own decision, and
|
|
10
|
+
says so once in the log.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
import logging
|
|
16
|
+
import threading
|
|
17
|
+
|
|
18
|
+
from cacheverifier.client import VerifyResult
|
|
19
|
+
|
|
20
|
+
COLD_START_MODES = ("serve", "reject")
|
|
21
|
+
|
|
22
|
+
_log = logging.getLogger("cacheverifier.integrations")
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def check_mode(cold_start: str) -> str:
|
|
26
|
+
if cold_start not in COLD_START_MODES:
|
|
27
|
+
raise ValueError(f"cold_start must be one of {COLD_START_MODES}, got {cold_start!r}")
|
|
28
|
+
return cold_start
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class ColdStartNotice:
|
|
32
|
+
"""Logs the first cold-start verdict an integration instance sees, once."""
|
|
33
|
+
|
|
34
|
+
def __init__(self, integration: str) -> None:
|
|
35
|
+
self._integration = integration
|
|
36
|
+
self._lock = threading.Lock()
|
|
37
|
+
self._logged = False
|
|
38
|
+
|
|
39
|
+
def seen(self, result: VerifyResult, action: str) -> None:
|
|
40
|
+
"""`action`: what the integration does with cold-start hits, in a
|
|
41
|
+
phrase ("serving ... unchanged until a model is ready")."""
|
|
42
|
+
with self._lock:
|
|
43
|
+
if self._logged:
|
|
44
|
+
return
|
|
45
|
+
self._logged = True
|
|
46
|
+
_log.warning(
|
|
47
|
+
"cacheverifier %s: no model decides your calls yet (%s) -- %s. %s",
|
|
48
|
+
self._integration,
|
|
49
|
+
result.model_version,
|
|
50
|
+
action,
|
|
51
|
+
result.notice or "Run a Health Check at https://www.cacheverifier.com to get one.",
|
|
52
|
+
)
|