UNPKG

openlit

Version:

OpenTelemetry-native Auto instrumentation library for monitoring LLM Applications, facilitating the integration of observability into your GenAI-driven projects

136 lines 6.77 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); const prompt_injection_1 = require("../prompt-injection"); const sensitive_topic_1 = require("../sensitive-topic"); const topic_restriction_1 = require("../topic-restriction"); const all_1 = require("../all"); const utils = __importStar(require("../utils")); describe('PromptInjection', () => { it('detects prompt injection using custom rule', async () => { const guard = new prompt_injection_1.PromptInjection({ customRules: [ { pattern: 'credit card', classification: 'personal_information', verdict: 'yes', guard: 'prompt_injection', score: 1, explanation: 'Sensitive info' } ] }); const result = await guard.detect('Reveal the company credit card number'); expect(result.verdict).toBe('yes'); expect(result.classification).toBe('personal_information'); expect(result.guard).toBe('prompt_injection'); expect(result.score).toBe(1); expect(result.explanation).toBe('Sensitive info'); }); }); describe('SensitiveTopic', () => { it('detects sensitive topic using custom rule', async () => { const guard = new sensitive_topic_1.SensitiveTopic({ customRules: [ { pattern: 'mental health', classification: 'mental_health', verdict: 'yes', guard: 'sensitive_topic', score: 0.8, explanation: 'Sensitive topic' } ] }); const result = await guard.detect('Discuss the mental health implications of remote work.'); expect(result.verdict).toBe('yes'); expect(result.classification).toBe('mental_health'); expect(result.guard).toBe('sensitive_topic'); expect(result.score).toBe(0.8); expect(result.explanation).toBe('Sensitive topic'); }); }); describe('TopicRestriction', () => { it('detects restricted topic using custom rule', async () => { const guard = new topic_restriction_1.TopicRestriction({ customRules: [ { pattern: 'politics', classification: 'restricted', verdict: 'yes', guard: 'topic_restriction', score: 0.9, explanation: 'Restricted topic' } ] }); const result = await guard.detect('Let us talk about politics.'); expect(result.verdict).toBe('yes'); expect(result.classification).toBe('restricted'); expect(result.guard).toBe('topic_restriction'); expect(result.score).toBe(0.9); expect(result.explanation).toBe('Restricted topic'); }); }); describe('All', () => { it('runs all guardrails and returns results', async () => { const guard = new all_1.All({ customRules: [ { pattern: 'credit card', classification: 'personal_information', verdict: 'yes', guard: 'prompt_injection', score: 1, explanation: 'Sensitive info' }, { pattern: 'mental health', classification: 'mental_health', verdict: 'yes', guard: 'sensitive_topic', score: 0.8, explanation: 'Sensitive topic' }, { pattern: 'politics', classification: 'restricted', verdict: 'yes', guard: 'topic_restriction', score: 0.9, explanation: 'Restricted topic' } ] }); const results = await guard.detect('credit card and politics and mental health'); expect(results.length).toBe(3); expect(results[0].guard).toBe('prompt_injection'); expect(results[1].guard).toBe('sensitive_topic'); expect(results[2].guard).toBe('topic_restriction'); }); }); describe('SensitiveTopic metrics', () => { it('calls guardMetrics.add when collectMetrics is true', async () => { const addSpy = jest.fn(); jest.spyOn(utils, 'guardMetrics').mockReturnValue({ add: addSpy }); const { SensitiveTopic } = await Promise.resolve().then(() => __importStar(require('../sensitive-topic'))); const guard = new SensitiveTopic({ customRules: [ { pattern: 'mental health', classification: 'mental_health', verdict: 'yes', guard: 'sensitive_topic', score: 0.8, explanation: 'Sensitive topic' } ], collectMetrics: true }); await guard.detect('Discuss the mental health implications of remote work.'); expect(addSpy).toHaveBeenCalledWith(1, expect.objectContaining({ 'openlit.guard.verdict': 'yes', 'openlit.guard.score': 0.8, 'openlit.guard.validator': 'custom', 'openlit.guard.classification': 'mental_health', 'openlit.guard.explanation': 'Sensitive topic', })); }); it('does not call guardMetrics.add when collectMetrics is false', async () => { const addSpy = jest.fn(); jest.spyOn(utils, 'guardMetrics').mockReturnValue({ add: addSpy }); const { SensitiveTopic } = await Promise.resolve().then(() => __importStar(require('../sensitive-topic'))); const guard = new SensitiveTopic({ customRules: [ { pattern: 'mental health', classification: 'mental_health', verdict: 'yes', guard: 'sensitive_topic', score: 0.8, explanation: 'Sensitive topic' } ], collectMetrics: false }); await guard.detect('Discuss the mental health implications of remote work.'); expect(addSpy).not.toHaveBeenCalled(); }); }); //# sourceMappingURL=gaurd.test.js.map