llm_guardrail 1.0.0 → 2.0.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/index.js CHANGED
@@ -1,42 +1,62 @@
1
- import { execFile } from "child_process";
2
- import path from "path";
3
- import { fileURLToPath } from "url";
4
-
5
- const __filename = fileURLToPath(import.meta.url);
6
- const __dirname = path.dirname(__filename);
7
-
8
- const PYTHON = "D:/npm/llm_Guardrails/venv/Scripts/python.exe";
9
-
10
-
11
- export function check(prompt) {
12
- return new Promise((resolve, reject) => {
13
- if (typeof prompt !== "string") {
14
- return reject(new Error("Prompt must be a string"));
15
- }
16
-
17
- const scriptPath = path.join(__dirname, "model", "predict.py");
18
-
19
-
20
- execFile(
21
- PYTHON,
22
- [scriptPath, prompt],
23
- {},
24
- (err, stdout, stderr) => {
25
- if (err) return reject(err);
26
- if (stderr) return reject(new Error(stderr));
27
-
28
- try {
29
- const result = JSON.parse(stdout);
30
- resolve({
31
- allowed: result.injective === 0,
32
- ...result
33
- });
34
- } catch (e) {
35
- reject(new Error("Invalid JSON from Python"));
36
- }
37
- }
38
- );
39
- });
40
- }
41
-
42
- export default { check };
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import { fileURLToPath } from 'url';
4
+ import { TfidfVectorizer } from './model/tfidf.js';
5
+ import { LogisticRegression } from './model/logistic_regression.js';
6
+
7
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
8
+
9
+ // Load model once
10
+ let vectorizer = null;
11
+ let model = null;
12
+
13
+ function loadModel() {
14
+ if (model && vectorizer) return { model, vectorizer };
15
+
16
+ try {
17
+ const modelPath = path.join(__dirname, 'model', 'model_data.json');
18
+ const modelData = JSON.parse(fs.readFileSync(modelPath, 'utf8'));
19
+
20
+ vectorizer = new TfidfVectorizer(modelData);
21
+ model = new LogisticRegression(modelData);
22
+
23
+ return { model, vectorizer };
24
+ } catch (error) {
25
+ throw new Error(`Failed to load model: ${error.message}`);
26
+ }
27
+ }
28
+
29
+ export function check(prompt) {
30
+ return new Promise((resolve, reject) => {
31
+ try {
32
+ if (typeof prompt !== "string") {
33
+ return reject(new Error("Prompt must be a string"));
34
+ }
35
+
36
+ // Load model if not already loaded
37
+ const { model, vectorizer } = loadModel();
38
+
39
+ // Transform text to TF-IDF features
40
+ const features = vectorizer.transform(prompt);
41
+
42
+ // Get prediction
43
+ const prediction = model.predict(features);
44
+ const { probabilities, positiveProb } = model.predictProba(features);
45
+
46
+ resolve({
47
+ allowed: prediction === 0, // 0 = safe, 1 = injection
48
+ injective: prediction,
49
+ prediction: prediction,
50
+ confidence: positiveProb,
51
+ probabilities: {
52
+ safe: probabilities[0],
53
+ injection: probabilities[1]
54
+ }
55
+ });
56
+ } catch (error) {
57
+ reject(error);
58
+ }
59
+ });
60
+ }
61
+
62
+ export default { check };
@@ -0,0 +1,42 @@
1
+ export class LogisticRegression {
2
+ constructor(modelData) {
3
+ this.coefficients = modelData.coefficients[0]; // For binary classification
4
+ this.intercept = modelData.intercept[0];
5
+ this.classes = modelData.classes;
6
+ this.threshold = modelData.threshold || 0.5;
7
+ }
8
+
9
+ /**
10
+ * Sigmoid function
11
+ */
12
+ sigmoid(z) {
13
+ return 1 / (1 + Math.exp(-z));
14
+ }
15
+
16
+ /**
17
+ * Predict probability for a feature vector
18
+ */
19
+ predictProba(features) {
20
+ // Calculate z = w·x + b
21
+ let z = this.intercept;
22
+ for (let i = 0; i < features.length; i++) {
23
+ z += this.coefficients[i] * features[i];
24
+ }
25
+
26
+ // Apply sigmoid
27
+ const probPositive = this.sigmoid(z);
28
+
29
+ return {
30
+ probabilities: [1 - probPositive, probPositive],
31
+ positiveProb: probPositive
32
+ };
33
+ }
34
+
35
+ /**
36
+ * Predict class (0 or 1)
37
+ */
38
+ predict(features) {
39
+ const { positiveProb } = this.predictProba(features);
40
+ return positiveProb >= this.threshold ? 1 : 0;
41
+ }
42
+ }