llm-entropy-filter 1.1.1 β 1.2.0
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.
- package/CHANGELOG.md +155 -51
- package/LICENSE +93 -93
- package/README.md +297 -352
- package/dist/index.cjs +104 -70
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +31 -1
- package/dist/index.d.ts +31 -1
- package/dist/index.js +104 -70
- package/dist/index.js.map +1 -1
- package/integrations/express.mjs +117 -117
- package/integrations/fastify.mjs +106 -106
- package/integrations/langchain.mjs +98 -98
- package/integrations/vercel-ai-sdk.mjs +44 -44
- package/package.json +99 -97
- package/rulesets/default.js +8 -2
- package/rulesets/default.json +77 -73
- package/rulesets/public-api.js +7 -2
- package/rulesets/public-api.json +179 -27
- package/rulesets/schema +24 -24
- package/rulesets/strict.js +7 -2
- package/rulesets/strict.json +173 -25
- package/rulesets/support.js +7 -2
- package/rulesets/support.json +22 -22
package/README.md
CHANGED
|
@@ -1,352 +1,297 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
If
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
### 3) RAG pipelines (pre-retrieval gate)
|
|
300
|
-
Before retrieval:
|
|
301
|
-
- block low-signal queries that would waste retrieval + reranking
|
|
302
|
-
- normalize/clean input to improve recall precision
|
|
303
|
-
- prevent adversarial queries from polluting retrieval traces
|
|
304
|
-
|
|
305
|
-
### 4) Multi-agent systems
|
|
306
|
-
In agent loops:
|
|
307
|
-
- prevent βreasoning driftβ from noisy inputs
|
|
308
|
-
- keep agents from spending cycles on incoherent or adversarial prompts
|
|
309
|
-
- add structured telemetry for agent decisions (`flags`, `intention`, `entropy_score`)
|
|
310
|
-
|
|
311
|
-
### 5) Tooling & SDK pre-gates (LangChain / Vercel AI SDK)
|
|
312
|
-
Drop in as a deterministic guard:
|
|
313
|
-
- before `callLLM()`
|
|
314
|
-
- before `streamText()`
|
|
315
|
-
- before tool selection / agent routing
|
|
316
|
-
|
|
317
|
-
The output can be used as:
|
|
318
|
-
- a routing signal (ALLOW/WARN/BLOCK)
|
|
319
|
-
- a logging payload for audits and dashboards
|
|
320
|
-
|
|
321
|
-
βWhatβs missing to be production-readyβ
|
|
322
|
-
## Production readiness checklist
|
|
323
|
-
|
|
324
|
-
The core gate is stable, but βproduction-readyβ requires:
|
|
325
|
-
|
|
326
|
-
### 1) Configurable rulesets
|
|
327
|
-
- `default` (balanced)
|
|
328
|
-
- `strict` (aggressive spam/coercion blocking)
|
|
329
|
-
- `support` (customer support / fewer false positives)
|
|
330
|
-
- `public-api` (open endpoints / hardened)
|
|
331
|
-
|
|
332
|
-
### 2) Reproducible metrics (precision / recall)
|
|
333
|
-
Bench scripts should emit:
|
|
334
|
-
- precision/recall/F1
|
|
335
|
-
- confusion matrix
|
|
336
|
-
- false-positive rate on normal conversations
|
|
337
|
-
- top flags per dataset
|
|
338
|
-
|
|
339
|
-
### 3) Copy-paste integrations
|
|
340
|
-
Provide ready-to-use adapters:
|
|
341
|
-
- Express middleware
|
|
342
|
-
- Fastify plugin
|
|
343
|
-
- Next.js / Vercel edge wrapper
|
|
344
|
-
- βpre-gateβ helpers for LangChain-style pipelines
|
|
345
|
-
|
|
346
|
-
### 4) One real production example
|
|
347
|
-
A minimal public case study:
|
|
348
|
-
- traffic volume
|
|
349
|
-
- % blocked
|
|
350
|
-
- cost avoided
|
|
351
|
-
- rate-limit incidents reduced
|
|
352
|
-
- latency improvement for blocked traffic
|
|
1
|
+
llm-entropy-filter
|
|
2
|
+
|
|
3
|
+
Deterministic linguistic entropy gate for LLM inputs.
|
|
4
|
+
|
|
5
|
+
llm-entropy-filter is a lightweight, configurable middleware that evaluates text using linguistic and logical entropy signals before it reaches an LLM.
|
|
6
|
+
|
|
7
|
+
It is not an AI model.
|
|
8
|
+
It is a deterministic decision layer.
|
|
9
|
+
|
|
10
|
+
Why This Exists
|
|
11
|
+
|
|
12
|
+
Modern LLM systems face:
|
|
13
|
+
|
|
14
|
+
Spam floods
|
|
15
|
+
|
|
16
|
+
Phishing attempts
|
|
17
|
+
|
|
18
|
+
Fraud requests
|
|
19
|
+
|
|
20
|
+
Prompt injection
|
|
21
|
+
|
|
22
|
+
Manipulative urgency
|
|
23
|
+
|
|
24
|
+
Entropic noise
|
|
25
|
+
|
|
26
|
+
Most systems try to solve this reactively inside the model.
|
|
27
|
+
|
|
28
|
+
This project solves it before the model.
|
|
29
|
+
|
|
30
|
+
Order before generation.
|
|
31
|
+
Criteria before probability.
|
|
32
|
+
|
|
33
|
+
Core Principles
|
|
34
|
+
|
|
35
|
+
Deterministic (no randomness)
|
|
36
|
+
|
|
37
|
+
Linguistic + logical analysis
|
|
38
|
+
|
|
39
|
+
Configurable via rulesets
|
|
40
|
+
|
|
41
|
+
Fully reproducible scoring
|
|
42
|
+
|
|
43
|
+
No external API calls
|
|
44
|
+
|
|
45
|
+
Middleware-ready
|
|
46
|
+
|
|
47
|
+
Architecture Overview
|
|
48
|
+
Text
|
|
49
|
+
β
|
|
50
|
+
Normalization
|
|
51
|
+
β
|
|
52
|
+
Hard Triggers
|
|
53
|
+
β
|
|
54
|
+
Topic Signals
|
|
55
|
+
β
|
|
56
|
+
Entropy Scoring
|
|
57
|
+
β
|
|
58
|
+
Policy Overrides
|
|
59
|
+
β
|
|
60
|
+
Threshold Decision
|
|
61
|
+
β
|
|
62
|
+
ALLOW | WARN | BLOCK
|
|
63
|
+
|
|
64
|
+
Installation
|
|
65
|
+
npm install llm-entropy-filter
|
|
66
|
+
|
|
67
|
+
Quick Usage
|
|
68
|
+
import { gate } from "llm-entropy-filter";
|
|
69
|
+
import ruleset from "./rulesets/public-api.js";
|
|
70
|
+
|
|
71
|
+
const result = gate("FREE prize winner click now claim $100!!!", {
|
|
72
|
+
ruleset
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
console.log(result);
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
Example output:
|
|
79
|
+
|
|
80
|
+
{
|
|
81
|
+
"action": "BLOCK",
|
|
82
|
+
"entropy_score": 0.85,
|
|
83
|
+
"flags": ["spam_sales", "money_signal", "shouting"],
|
|
84
|
+
"intention": "marketing_spam",
|
|
85
|
+
"confidence": 0.85
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
Actions
|
|
89
|
+
Action Meaning
|
|
90
|
+
ALLOW Low entropy, safe to process
|
|
91
|
+
WARN Suspicious signals detected
|
|
92
|
+
BLOCK High entropy or deterministic policy match
|
|
93
|
+
Rulesets
|
|
94
|
+
|
|
95
|
+
Behavior is controlled entirely by rulesets.
|
|
96
|
+
|
|
97
|
+
Each ruleset defines:
|
|
98
|
+
|
|
99
|
+
thresholds
|
|
100
|
+
|
|
101
|
+
normalization
|
|
102
|
+
|
|
103
|
+
hard triggers
|
|
104
|
+
|
|
105
|
+
topic signals
|
|
106
|
+
|
|
107
|
+
policy overrides
|
|
108
|
+
|
|
109
|
+
Example:
|
|
110
|
+
|
|
111
|
+
{
|
|
112
|
+
"name": "public-api",
|
|
113
|
+
"thresholds": { "warn": 0.4, "block": 0.6 },
|
|
114
|
+
"policy": {
|
|
115
|
+
"block_flags": ["phishing_2fa_code"],
|
|
116
|
+
"warn_flags": ["scam_wfh"]
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
Threshold Logic
|
|
121
|
+
|
|
122
|
+
Decision is score-based unless overridden by policy.
|
|
123
|
+
|
|
124
|
+
if score >= block β BLOCK
|
|
125
|
+
else if score >= warn β WARN
|
|
126
|
+
else β ALLOW
|
|
127
|
+
|
|
128
|
+
Policy Overrides (Deterministic Control Layer)
|
|
129
|
+
|
|
130
|
+
Rulesets may define deterministic overrides:
|
|
131
|
+
|
|
132
|
+
"policy": {
|
|
133
|
+
"block_flags": ["phishing_2fa_code"],
|
|
134
|
+
"warn_flags": ["fraud_payment_request"]
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
Behavior:
|
|
139
|
+
|
|
140
|
+
If any block_flag matches β BLOCK (independent of score)
|
|
141
|
+
|
|
142
|
+
If any warn_flag matches β at least WARN
|
|
143
|
+
|
|
144
|
+
Otherwise β threshold decision
|
|
145
|
+
|
|
146
|
+
This allows:
|
|
147
|
+
|
|
148
|
+
Fraud cluster blocking
|
|
149
|
+
|
|
150
|
+
Linguistic certainty escalation
|
|
151
|
+
|
|
152
|
+
Safe tuning without lowering global thresholds
|
|
153
|
+
|
|
154
|
+
Logical Clusters
|
|
155
|
+
|
|
156
|
+
Entropy is not triggered by single words.
|
|
157
|
+
|
|
158
|
+
It increases when patterns accumulate.
|
|
159
|
+
|
|
160
|
+
Example combinations:
|
|
161
|
+
|
|
162
|
+
spam_kw_click + spam_kw_claim + spam_kw_free
|
|
163
|
+
|
|
164
|
+
urgency + fraud_payment_request
|
|
165
|
+
|
|
166
|
+
phishing_verify_threat + money_signal
|
|
167
|
+
|
|
168
|
+
Suspicion grows with structural density.
|
|
169
|
+
|
|
170
|
+
This reflects a linguistic-logical principle, not keyword matching alone.
|
|
171
|
+
|
|
172
|
+
Deterministic Stability Guarantee
|
|
173
|
+
|
|
174
|
+
For identical input + identical ruleset:
|
|
175
|
+
|
|
176
|
+
Output is deterministic
|
|
177
|
+
|
|
178
|
+
Score is reproducible
|
|
179
|
+
|
|
180
|
+
Flags are traceable
|
|
181
|
+
|
|
182
|
+
No external calls are made
|
|
183
|
+
|
|
184
|
+
This makes the gate:
|
|
185
|
+
|
|
186
|
+
Auditable
|
|
187
|
+
|
|
188
|
+
Compliance-friendly
|
|
189
|
+
|
|
190
|
+
Safe for logging
|
|
191
|
+
|
|
192
|
+
Open-Source Scope
|
|
193
|
+
|
|
194
|
+
This core version does NOT include:
|
|
195
|
+
|
|
196
|
+
Context memory
|
|
197
|
+
|
|
198
|
+
Session tracking
|
|
199
|
+
|
|
200
|
+
Behavioral anomaly detection
|
|
201
|
+
|
|
202
|
+
Identity verification
|
|
203
|
+
|
|
204
|
+
AI secondary review
|
|
205
|
+
|
|
206
|
+
Adaptive learning
|
|
207
|
+
|
|
208
|
+
Those belong to orchestration layers or enterprise systems.
|
|
209
|
+
|
|
210
|
+
This package focuses strictly on:
|
|
211
|
+
|
|
212
|
+
Linguistic entropy filtering.
|
|
213
|
+
|
|
214
|
+
Benchmarks
|
|
215
|
+
|
|
216
|
+
Run metrics:
|
|
217
|
+
|
|
218
|
+
npm run bench:metrics:public-api
|
|
219
|
+
npm run bench:metrics:strict
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
Reports include:
|
|
223
|
+
|
|
224
|
+
Accuracy
|
|
225
|
+
|
|
226
|
+
Precision / Recall
|
|
227
|
+
|
|
228
|
+
F1 score
|
|
229
|
+
|
|
230
|
+
Confusion matrix
|
|
231
|
+
|
|
232
|
+
Per-tag accuracy
|
|
233
|
+
|
|
234
|
+
Overblock / Underblock rates
|
|
235
|
+
|
|
236
|
+
The system is fully reproducible.
|
|
237
|
+
|
|
238
|
+
Tuning Strategy
|
|
239
|
+
|
|
240
|
+
Do NOT blindly lower thresholds.
|
|
241
|
+
|
|
242
|
+
Prefer:
|
|
243
|
+
|
|
244
|
+
Add deterministic block_flags
|
|
245
|
+
|
|
246
|
+
Adjust pattern weights
|
|
247
|
+
|
|
248
|
+
Improve signal density
|
|
249
|
+
|
|
250
|
+
Then adjust thresholds if necessary
|
|
251
|
+
|
|
252
|
+
This preserves precision while increasing recall safely.
|
|
253
|
+
|
|
254
|
+
Middleware Mode
|
|
255
|
+
|
|
256
|
+
Typical production flow:
|
|
257
|
+
|
|
258
|
+
User Input
|
|
259
|
+
β
|
|
260
|
+
Entropy Gate
|
|
261
|
+
β
|
|
262
|
+
ALLOW β LLM
|
|
263
|
+
WARN β Log / Rate-limit / Secondary review
|
|
264
|
+
BLOCK β Reject
|
|
265
|
+
|
|
266
|
+
Philosophy
|
|
267
|
+
|
|
268
|
+
This project is grounded in a simple idea:
|
|
269
|
+
|
|
270
|
+
Entropy precedes manipulation.
|
|
271
|
+
|
|
272
|
+
Linguistic disorder precedes exploitation.
|
|
273
|
+
|
|
274
|
+
A gate should not think.
|
|
275
|
+
It should filter.
|
|
276
|
+
|
|
277
|
+
Version
|
|
278
|
+
|
|
279
|
+
Current stable: v1.2.x
|
|
280
|
+
|
|
281
|
+
Features:
|
|
282
|
+
|
|
283
|
+
Deterministic entropy scoring
|
|
284
|
+
|
|
285
|
+
Policy override system
|
|
286
|
+
|
|
287
|
+
Configurable thresholds
|
|
288
|
+
|
|
289
|
+
Hard trigger architecture
|
|
290
|
+
|
|
291
|
+
Cluster-sensitive scoring
|
|
292
|
+
|
|
293
|
+
Zero BLOCKβALLOW leaks in benchmark dataset
|
|
294
|
+
|
|
295
|
+
License
|
|
296
|
+
|
|
297
|
+
APACHE 2.0
|