@hazeljs/guardrails 0.7.9 → 0.8.1
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/README.md +13 -13
- package/dist/checks/hallucination.check.d.ts +14 -0
- package/dist/checks/hallucination.check.d.ts.map +1 -0
- package/dist/checks/hallucination.check.js +35 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -95,7 +95,10 @@ import { GuardrailsService, GuardrailInput, GuardrailOutput } from '@hazeljs/gua
|
|
|
95
95
|
|
|
96
96
|
@Controller({ path: '/chat' })
|
|
97
97
|
export class ChatController {
|
|
98
|
-
constructor(
|
|
98
|
+
constructor(
|
|
99
|
+
private aiService: AIService,
|
|
100
|
+
private guardrailsService: GuardrailsService
|
|
101
|
+
) {}
|
|
99
102
|
|
|
100
103
|
@GuardrailInput()
|
|
101
104
|
@GuardrailOutput()
|
|
@@ -113,24 +116,21 @@ When both `GuardrailsModule` and `AgentModule` are imported, tool input and outp
|
|
|
113
116
|
|
|
114
117
|
```typescript
|
|
115
118
|
@HazelModule({
|
|
116
|
-
imports: [
|
|
117
|
-
GuardrailsModule.forRoot(),
|
|
118
|
-
AgentModule.forRoot(),
|
|
119
|
-
],
|
|
119
|
+
imports: [GuardrailsModule.forRoot(), AgentModule.forRoot()],
|
|
120
120
|
})
|
|
121
121
|
export class AppModule {}
|
|
122
122
|
```
|
|
123
123
|
|
|
124
124
|
## Configuration
|
|
125
125
|
|
|
126
|
-
| Option
|
|
127
|
-
|
|
128
|
-
| `piiEntities`
|
|
129
|
-
| `redactPIIByDefault`
|
|
130
|
-
| `blockInjectionByDefault` | `boolean`
|
|
131
|
-
| `blockToxicityByDefault`
|
|
132
|
-
| `injectionBlocklist`
|
|
133
|
-
| `toxicityBlocklist`
|
|
126
|
+
| Option | Type | Default | Description |
|
|
127
|
+
| ------------------------- | ----------------- | --------------------------------------- | --------------------------------- |
|
|
128
|
+
| `piiEntities` | `PIIEntityType[]` | `['email','phone','ssn','credit_card']` | Entities to detect/redact |
|
|
129
|
+
| `redactPIIByDefault` | `boolean` | `false` | Redact PII in input by default |
|
|
130
|
+
| `blockInjectionByDefault` | `boolean` | `true` | Block prompt injection by default |
|
|
131
|
+
| `blockToxicityByDefault` | `boolean` | `true` | Block toxic content by default |
|
|
132
|
+
| `injectionBlocklist` | `string[]` | — | Custom injection patterns |
|
|
133
|
+
| `toxicityBlocklist` | `string[]` | — | Custom toxicity keywords |
|
|
134
134
|
|
|
135
135
|
## API
|
|
136
136
|
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Heuristic hallucination / grounding check — compares answer tokens to context.
|
|
3
|
+
* For production, combine with LLM-as-judge or @hazeljs/eval.
|
|
4
|
+
*/
|
|
5
|
+
export interface HallucinationCheckResult {
|
|
6
|
+
grounded: boolean;
|
|
7
|
+
overlapRatio: number;
|
|
8
|
+
suspiciousPhrases: string[];
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Returns true when answer appears supported by context (token overlap + hedge detection).
|
|
12
|
+
*/
|
|
13
|
+
export declare function checkHallucinationHeuristic(answer: string, context: string): HallucinationCheckResult;
|
|
14
|
+
//# sourceMappingURL=hallucination.check.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hallucination.check.d.ts","sourceRoot":"","sources":["../../src/checks/hallucination.check.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,MAAM,WAAW,wBAAwB;IACvC,QAAQ,EAAE,OAAO,CAAC;IAClB,YAAY,EAAE,MAAM,CAAC;IACrB,iBAAiB,EAAE,MAAM,EAAE,CAAC;CAC7B;AAID;;GAEG;AACH,wBAAgB,2BAA2B,CACzC,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,MAAM,GACd,wBAAwB,CAe1B"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Heuristic hallucination / grounding check — compares answer tokens to context.
|
|
4
|
+
* For production, combine with LLM-as-judge or @hazeljs/eval.
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.checkHallucinationHeuristic = checkHallucinationHeuristic;
|
|
8
|
+
const HEDGE_WORDS = ['might', 'maybe', 'possibly', 'could be', 'i think', "i'm not sure"];
|
|
9
|
+
/**
|
|
10
|
+
* Returns true when answer appears supported by context (token overlap + hedge detection).
|
|
11
|
+
*/
|
|
12
|
+
function checkHallucinationHeuristic(answer, context) {
|
|
13
|
+
const a = tokenize(answer);
|
|
14
|
+
const c = new Set(tokenize(context));
|
|
15
|
+
if (a.length === 0) {
|
|
16
|
+
return { grounded: true, overlapRatio: 1, suspiciousPhrases: [] };
|
|
17
|
+
}
|
|
18
|
+
let hit = 0;
|
|
19
|
+
for (const w of a) {
|
|
20
|
+
if (c.has(w))
|
|
21
|
+
hit++;
|
|
22
|
+
}
|
|
23
|
+
const overlapRatio = hit / a.length;
|
|
24
|
+
const lower = answer.toLowerCase();
|
|
25
|
+
const suspiciousPhrases = HEDGE_WORDS.filter((h) => lower.includes(h));
|
|
26
|
+
const grounded = overlapRatio >= 0.15 && suspiciousPhrases.length < 3;
|
|
27
|
+
return { grounded, overlapRatio, suspiciousPhrases };
|
|
28
|
+
}
|
|
29
|
+
function tokenize(s) {
|
|
30
|
+
return s
|
|
31
|
+
.toLowerCase()
|
|
32
|
+
.replace(/[^\p{L}\p{N}\s]/gu, ' ')
|
|
33
|
+
.split(/\s+/)
|
|
34
|
+
.filter((w) => w.length > 2);
|
|
35
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -11,4 +11,9 @@ export { GuardrailViolationError } from './errors/guardrail-violation.error';
|
|
|
11
11
|
export type { GuardrailInputOptions, GuardrailOutputOptions, GuardrailResult, GuardrailsModuleOptions, PIIEntityType, } from './guardrails.types';
|
|
12
12
|
export type { GuardrailPipeOptions } from './pipes/guardrail.pipe';
|
|
13
13
|
export type { GuardrailInterceptorOptions } from './interceptors/guardrail.interceptor';
|
|
14
|
+
export { checkHallucinationHeuristic } from './checks/hallucination.check';
|
|
15
|
+
export type { HallucinationCheckResult } from './checks/hallucination.check';
|
|
16
|
+
export { detectPII, redactPII } from './checks/pii.check';
|
|
17
|
+
export { checkPromptInjection } from './checks/injection.check';
|
|
18
|
+
export { checkToxicity } from './checks/toxicity.check';
|
|
14
19
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,gBAAgB,EAAE,wBAAwB,EAAE,MAAM,qBAAqB,CAAC;AACjF,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AACzD,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AACvD,OAAO,EAAE,oBAAoB,EAAE,MAAM,sCAAsC,CAAC;AAC5E,OAAO,EAAE,cAAc,EAAE,yBAAyB,EAAE,MAAM,wCAAwC,CAAC;AACnG,OAAO,EACL,eAAe,EACf,0BAA0B,GAC3B,MAAM,yCAAyC,CAAC;AACjD,OAAO,EAAE,uBAAuB,EAAE,MAAM,oCAAoC,CAAC;AAE7E,YAAY,EACV,qBAAqB,EACrB,sBAAsB,EACtB,eAAe,EACf,uBAAuB,EACvB,aAAa,GACd,MAAM,oBAAoB,CAAC;AAE5B,YAAY,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AACnE,YAAY,EAAE,2BAA2B,EAAE,MAAM,sCAAsC,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,gBAAgB,EAAE,wBAAwB,EAAE,MAAM,qBAAqB,CAAC;AACjF,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AACzD,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AACvD,OAAO,EAAE,oBAAoB,EAAE,MAAM,sCAAsC,CAAC;AAC5E,OAAO,EAAE,cAAc,EAAE,yBAAyB,EAAE,MAAM,wCAAwC,CAAC;AACnG,OAAO,EACL,eAAe,EACf,0BAA0B,GAC3B,MAAM,yCAAyC,CAAC;AACjD,OAAO,EAAE,uBAAuB,EAAE,MAAM,oCAAoC,CAAC;AAE7E,YAAY,EACV,qBAAqB,EACrB,sBAAsB,EACtB,eAAe,EACf,uBAAuB,EACvB,aAAa,GACd,MAAM,oBAAoB,CAAC;AAE5B,YAAY,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AACnE,YAAY,EAAE,2BAA2B,EAAE,MAAM,sCAAsC,CAAC;AAExF,OAAO,EAAE,2BAA2B,EAAE,MAAM,8BAA8B,CAAC;AAC3E,YAAY,EAAE,wBAAwB,EAAE,MAAM,8BAA8B,CAAC;AAC7E,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAC1D,OAAO,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AAChE,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* @hazeljs/guardrails - Content safety, PII handling, and output validation
|
|
4
4
|
*/
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.GuardrailViolationError = exports.getGuardrailOutputMetadata = exports.GuardrailOutput = exports.getGuardrailInputMetadata = exports.GuardrailInput = exports.GuardrailInterceptor = exports.GuardrailPipe = exports.GuardrailsService = exports.GUARDRAILS_SERVICE_TOKEN = exports.GuardrailsModule = void 0;
|
|
6
|
+
exports.checkToxicity = exports.checkPromptInjection = exports.redactPII = exports.detectPII = exports.checkHallucinationHeuristic = exports.GuardrailViolationError = exports.getGuardrailOutputMetadata = exports.GuardrailOutput = exports.getGuardrailInputMetadata = exports.GuardrailInput = exports.GuardrailInterceptor = exports.GuardrailPipe = exports.GuardrailsService = exports.GUARDRAILS_SERVICE_TOKEN = exports.GuardrailsModule = void 0;
|
|
7
7
|
var guardrails_module_1 = require("./guardrails.module");
|
|
8
8
|
Object.defineProperty(exports, "GuardrailsModule", { enumerable: true, get: function () { return guardrails_module_1.GuardrailsModule; } });
|
|
9
9
|
Object.defineProperty(exports, "GUARDRAILS_SERVICE_TOKEN", { enumerable: true, get: function () { return guardrails_module_1.GUARDRAILS_SERVICE_TOKEN; } });
|
|
@@ -21,3 +21,12 @@ Object.defineProperty(exports, "GuardrailOutput", { enumerable: true, get: funct
|
|
|
21
21
|
Object.defineProperty(exports, "getGuardrailOutputMetadata", { enumerable: true, get: function () { return guardrail_output_decorator_1.getGuardrailOutputMetadata; } });
|
|
22
22
|
var guardrail_violation_error_1 = require("./errors/guardrail-violation.error");
|
|
23
23
|
Object.defineProperty(exports, "GuardrailViolationError", { enumerable: true, get: function () { return guardrail_violation_error_1.GuardrailViolationError; } });
|
|
24
|
+
var hallucination_check_1 = require("./checks/hallucination.check");
|
|
25
|
+
Object.defineProperty(exports, "checkHallucinationHeuristic", { enumerable: true, get: function () { return hallucination_check_1.checkHallucinationHeuristic; } });
|
|
26
|
+
var pii_check_1 = require("./checks/pii.check");
|
|
27
|
+
Object.defineProperty(exports, "detectPII", { enumerable: true, get: function () { return pii_check_1.detectPII; } });
|
|
28
|
+
Object.defineProperty(exports, "redactPII", { enumerable: true, get: function () { return pii_check_1.redactPII; } });
|
|
29
|
+
var injection_check_1 = require("./checks/injection.check");
|
|
30
|
+
Object.defineProperty(exports, "checkPromptInjection", { enumerable: true, get: function () { return injection_check_1.checkPromptInjection; } });
|
|
31
|
+
var toxicity_check_1 = require("./checks/toxicity.check");
|
|
32
|
+
Object.defineProperty(exports, "checkToxicity", { enumerable: true, get: function () { return toxicity_check_1.checkToxicity; } });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hazeljs/guardrails",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.1",
|
|
4
4
|
"description": "Content safety, PII handling, and output validation for HazelJS AI applications",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -53,5 +53,5 @@
|
|
|
53
53
|
"peerDependencies": {
|
|
54
54
|
"@hazeljs/core": ">=0.2.0-beta.0"
|
|
55
55
|
},
|
|
56
|
-
"gitHead": "
|
|
56
|
+
"gitHead": "8b7685d1250c4622f25d83992f58e13a59bb3dba"
|
|
57
57
|
}
|