Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[New Plugin] Medical Advice Detection #688

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions plugins/Medical-Advice-Detection/manifest.json
Original file line number Diff line number Diff line change
@@ -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"]
}
}
]
}
46 changes: 46 additions & 0 deletions plugins/Medical-Advice-Detection/medical-advice-detection.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
53 changes: 53 additions & 0 deletions plugins/Medical-Advice-Detection/medical-advice-detection.ts
Original file line number Diff line number Diff line change
@@ -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<number> {
// 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;
}
21 changes: 21 additions & 0 deletions plugins/Medical-Advice-Detection/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
2 changes: 2 additions & 0 deletions plugins/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });