diff --git a/plugins/Medical-Advice-Detection/manifest.json b/plugins/Medical-Advice-Detection/manifest.json new file mode 100644 index 000000000..abc8ec2b3 --- /dev/null +++ b/plugins/Medical-Advice-Detection/manifest.json @@ -0,0 +1,46 @@ +{ + "id": "medical-advice-detection", + "description": "Detects Medical Advice in the generated response and flags a warning.", + "credentials": { + "type": "object", + "properties": { + "apiKey": { + "type": "string", + "label": "API Key", + "description": "API key for accessing the medical advice detection service", + "encrypted": true + } + }, + "required": ["apiKey"] + }, + "functions": [ + { + "name": "Medical Advice Detection Guardrail", + "id": "medicalAdviceGuardrail", + "supportedHooks": ["afterRequestHook"], + "type": "guardrail", + "description": [ + { + "type": "subHeading", + "text": "This guardrail checks the response for medical advice and flags a warning if detected." + } + ], + "parameters": { + "type": "object", + "properties": { + "warningThreshold": { + "type": "number", + "label": "Warning Threshold", + "description": [ + { + "type": "subHeading", + "text": "The confidence threshold for flagging medical advice (0 to 1 scale)." + } + ] + } + }, + "required": ["warningThreshold"] + } + } + ] +} diff --git a/plugins/Medical-Advice-Detection/medical-advice-detection.test.ts b/plugins/Medical-Advice-Detection/medical-advice-detection.test.ts new file mode 100644 index 000000000..285f7715a --- /dev/null +++ b/plugins/Medical-Advice-Detection/medical-advice-detection.test.ts @@ -0,0 +1,46 @@ +import { handler } from './medical-advice-detection'; +import { HookEventType, PluginContext, PluginParameters } from '../types'; + +describe('Medical Advice Detection Plugin', () => { + it('flags response with medical advice', async () => { + const context: PluginContext = { + response: 'The treatment for this condition is to prescribe medication.', + request: {}, + }; + const parameters: PluginParameters = { + warningThreshold: 0.7, + }; + const options = { env: {} }; + + const result = await handler( + context, + parameters, + 'afterRequestHook', + options + ); + + expect(result.verdict).toBe(false); + expect(result.data).toHaveProperty('warning'); + }); + + it('allows response without medical advice', async () => { + const context: PluginContext = { + response: 'You should drink more water to stay hydrated.', + request: {}, + }; + const parameters: PluginParameters = { + warningThreshold: 0.7, + }; + const options = { env: {} }; + + const result = await handler( + context, + parameters, + 'afterRequestHook', + options + ); + + expect(result.verdict).toBe(true); + expect(result.data).not.toHaveProperty('warning'); + }); +}); diff --git a/plugins/Medical-Advice-Detection/medical-advice-detection.ts b/plugins/Medical-Advice-Detection/medical-advice-detection.ts new file mode 100644 index 000000000..f044537a3 --- /dev/null +++ b/plugins/Medical-Advice-Detection/medical-advice-detection.ts @@ -0,0 +1,53 @@ +import { + HookEventType, + PluginContext, + PluginHandler, + PluginParameters, +} from '../types'; + +export const handler: PluginHandler = async ( + context: PluginContext, + parameters: PluginParameters, + eventType: HookEventType +) => { + if (eventType !== 'afterRequestHook') { + return { error: null, verdict: true, data: {} }; + } + + const { response } = context; + const { warningThreshold } = parameters; + + const confidenceScore = await detectMedicalAdvice(response); + + if (confidenceScore >= warningThreshold) { + return { + error: null, + verdict: false, + data: { + warning: + 'Medical advice detected in the response. Please consult a professional.', + }, + }; + } + + return { + error: null, + verdict: true, + data: {}, + }; +}; + +// Example of a mock medical advice detection function +async function detectMedicalAdvice(text: string): Promise { + // Placeholder logic to simulate detecting medical advice (0 means no confidence, 1 means high confidence) + const medicalKeywords = [ + 'prescribe', + 'diagnosis', + 'treatment', + 'medication', + 'symptoms', + ]; + const found = medicalKeywords.some((keyword) => text.includes(keyword)); + + return found ? 0.8 : 0.2; +} diff --git a/plugins/Medical-Advice-Detection/package.json b/plugins/Medical-Advice-Detection/package.json new file mode 100644 index 000000000..cf47663a4 --- /dev/null +++ b/plugins/Medical-Advice-Detection/package.json @@ -0,0 +1,21 @@ +{ + "name": "medical-advice-detection", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "jest", + "format": "prettier --write \"./**/*.{js,jsx,ts,tsx,json}\"", + "format:check": "prettier --check \"./**/*.{js,jsx,ts,tsx,json}\"" + }, + "dependencies": { + "axios": "^1.0.0", + "typescript": "^5.0.0" + }, + "devDependencies": { + "jest": "^29.0.0", + "ts-jest": "^29.0.0", + "@types/jest": "^29.0.0", + "typescript": "^5.0.0" + } +} diff --git a/plugins/types.js b/plugins/types.js new file mode 100644 index 000000000..c8ad2e549 --- /dev/null +++ b/plugins/types.js @@ -0,0 +1,2 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true });