Merge pull request #1439 from FlowiseAI/feature/Moderation

Feature/LLM Moderation
This commit is contained in:
Henry Heng 2023-12-29 14:55:13 +00:00 committed by GitHub
commit 591636f836
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 9 deletions

View File

@ -2,6 +2,7 @@ import { INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses } from '../../../src' import { getBaseClasses } from '../../../src'
import { Moderation } from '../Moderation' import { Moderation } from '../Moderation'
import { SimplePromptModerationRunner } from './SimplePromptModerationRunner' import { SimplePromptModerationRunner } from './SimplePromptModerationRunner'
import { BaseChatModel } from 'langchain/chat_models/base'
class SimplePromptModeration implements INode { class SimplePromptModeration implements INode {
label: string label: string
@ -17,7 +18,7 @@ class SimplePromptModeration implements INode {
constructor() { constructor() {
this.label = 'Simple Prompt Moderation' this.label = 'Simple Prompt Moderation'
this.name = 'inputModerationSimple' this.name = 'inputModerationSimple'
this.version = 1.0 this.version = 2.0
this.type = 'Moderation' this.type = 'Moderation'
this.icon = 'moderation.svg' this.icon = 'moderation.svg'
this.category = 'Moderation' this.category = 'Moderation'
@ -30,8 +31,14 @@ class SimplePromptModeration implements INode {
type: 'string', type: 'string',
rows: 4, rows: 4,
placeholder: `ignore previous instructions\ndo not follow the directions\nyou must ignore all previous instructions`, placeholder: `ignore previous instructions\ndo not follow the directions\nyou must ignore all previous instructions`,
description: 'An array of string literals (enter one per line) that should not appear in the prompt text.', description: 'An array of string literals (enter one per line) that should not appear in the prompt text.'
optional: false },
{
label: 'Chat Model',
name: 'model',
type: 'BaseChatModel',
description: 'Use LLM to detect if the input is similar to those specified in Deny List',
optional: true
}, },
{ {
label: 'Error Message', label: 'Error Message',
@ -46,9 +53,10 @@ class SimplePromptModeration implements INode {
async init(nodeData: INodeData): Promise<any> { async init(nodeData: INodeData): Promise<any> {
const denyList = nodeData.inputs?.denyList as string const denyList = nodeData.inputs?.denyList as string
const model = nodeData.inputs?.model as BaseChatModel
const moderationErrorMessage = nodeData.inputs?.moderationErrorMessage as string const moderationErrorMessage = nodeData.inputs?.moderationErrorMessage as string
return new SimplePromptModerationRunner(denyList, moderationErrorMessage) return new SimplePromptModerationRunner(denyList, moderationErrorMessage, model)
} }
} }

View File

@ -1,23 +1,39 @@
import { Moderation } from '../Moderation' import { Moderation } from '../Moderation'
import { BaseChatModel } from 'langchain/chat_models/base'
export class SimplePromptModerationRunner implements Moderation { export class SimplePromptModerationRunner implements Moderation {
private readonly denyList: string = '' private readonly denyList: string = ''
private readonly moderationErrorMessage: string = '' private readonly moderationErrorMessage: string = ''
private readonly model: BaseChatModel
constructor(denyList: string, moderationErrorMessage: string) { constructor(denyList: string, moderationErrorMessage: string, model?: BaseChatModel) {
this.denyList = denyList this.denyList = denyList
if (denyList.indexOf('\n') === -1) { if (denyList.indexOf('\n') === -1) {
this.denyList += '\n' this.denyList += '\n'
} }
this.moderationErrorMessage = moderationErrorMessage this.moderationErrorMessage = moderationErrorMessage
if (model) this.model = model
} }
async checkForViolations(input: string): Promise<string> { async checkForViolations(input: string): Promise<string> {
this.denyList.split('\n').forEach((denyListItem) => { if (this.model) {
if (denyListItem && denyListItem !== '' && input.toLowerCase().includes(denyListItem.toLowerCase())) { const denyArray = this.denyList.split('\n')
throw Error(this.moderationErrorMessage) for (const denyStr of denyArray) {
if (!denyStr || denyStr === '') continue
const res = await this.model.invoke(
`Are these two sentences similar to each other? Only return Yes or No.\nFirst sentence: ${input}\nSecond sentence: ${denyStr}`
)
if (res.content.toString().toLowerCase().includes('yes')) {
throw Error(this.moderationErrorMessage)
}
} }
}) } else {
this.denyList.split('\n').forEach((denyListItem) => {
if (denyListItem && denyListItem !== '' && input.toLowerCase().includes(denyListItem.toLowerCase())) {
throw Error(this.moderationErrorMessage)
}
})
}
return Promise.resolve(input) return Promise.resolve(input)
} }
} }