Skip to content

Commit

Permalink
Merge pull request #1222 from vinodkiran/FEATURE/input-moderation
Browse files Browse the repository at this point in the history
ResponsibleAI - Input Moderation - …
  • Loading branch information
HenryHengZJ authored Nov 23, 2023
2 parents bd65346 + b3f44e0 commit e158376
Show file tree
Hide file tree
Showing 9 changed files with 646 additions and 1 deletion.
22 changes: 21 additions & 1 deletion packages/components/nodes/chains/LLMChain/LLMChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { BaseOutputParser } from 'langchain/schema/output_parser'
import { formatResponse, injectOutputParser } from '../../outputparsers/OutputParserHelpers'
import { BaseLLMOutputParser } from 'langchain/schema/output_parser'
import { OutputFixingParser } from 'langchain/output_parsers'
import { checkInputs, Moderation, streamResponse } from '../../moderation/Moderation'

class LLMChain_Chains implements INode {
label: string
Expand Down Expand Up @@ -47,6 +48,14 @@ class LLMChain_Chains implements INode {
type: 'BaseLLMOutputParser',
optional: true
},
{
label: 'Input Moderation',
description: 'Detect text that could generate harmful output and prevent it from being sent to the language model',
name: 'inputModeration',
type: 'Moderation',
optional: true,
list: true
},
{
label: 'Chain Name',
name: 'chainName',
Expand Down Expand Up @@ -144,14 +153,25 @@ const runPrediction = async (
const isStreaming = options.socketIO && options.socketIOClientId
const socketIO = isStreaming ? options.socketIO : undefined
const socketIOClientId = isStreaming ? options.socketIOClientId : ''

const moderations = nodeData.inputs?.inputModeration as Moderation[]
/**
* Apply string transformation to reverse converted special chars:
* FROM: { "value": "hello i am benFLOWISE_NEWLINEFLOWISE_NEWLINEFLOWISE_TABhow are you?" }
* TO: { "value": "hello i am ben\n\n\thow are you?" }
*/
const promptValues = handleEscapeCharacters(promptValuesRaw, true)

if (moderations && moderations.length > 0) {
try {
// Use the output of the moderation chain as input for the LLM chain
input = await checkInputs(moderations, chain.llm, input)
} catch (e) {
await new Promise((resolve) => setTimeout(resolve, 500))
streamResponse(isStreaming, e.message, socketIO, socketIOClientId)
return formatResponse(e.message)
}
}

if (promptValues && inputVariables.length > 0) {
let seen: string[] = []

Expand Down
28 changes: 28 additions & 0 deletions packages/components/nodes/moderation/Moderation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { BaseLanguageModel } from 'langchain/base_language'
import { Server } from 'socket.io'

export abstract class Moderation {
abstract checkForViolations(llm: BaseLanguageModel, input: string): Promise<string>
}

export const checkInputs = async (inputModerations: Moderation[], llm: BaseLanguageModel, input: string): Promise<string> => {
for (const moderation of inputModerations) {
input = await moderation.checkForViolations(llm, input)
}
return input
}

// is this the correct location for this function?
// should we have a utils files that all node components can use?
export const streamResponse = (isStreaming: any, response: string, socketIO: Server, socketIOClientId: string) => {
if (isStreaming) {
const result = response.split(/(\s+)/)
result.forEach((token: string, index: number) => {
if (index === 0) {
socketIO.to(socketIOClientId).emit('start', token)
}
socketIO.to(socketIOClientId).emit('token', token)
})
socketIO.to(socketIOClientId).emit('end')
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses } from '../../../src'
import { Moderation } from '../Moderation'
import { OpenAIModerationRunner } from './OpenAIModerationRunner'

class OpenAIModeration implements INode {
label: string
name: string
version: number
description: string
type: string
icon: string
category: string
baseClasses: string[]
inputs: INodeParams[]

constructor() {
this.label = 'OpenAI Moderation'
this.name = 'inputModerationOpenAI'
this.version = 1.0
this.type = 'Moderation'
this.icon = 'openai-moderation.png'
this.category = 'Moderation'
this.description = 'Check whether content complies with OpenAI usage policies.'
this.baseClasses = [this.type, ...getBaseClasses(Moderation)]
this.inputs = [
{
label: 'Error Message',
name: 'moderationErrorMessage',
type: 'string',
rows: 2,
default: "Cannot Process! Input violates OpenAI's content moderation policies.",
optional: true
}
]
}

async init(nodeData: INodeData): Promise<any> {
const runner = new OpenAIModerationRunner()
const moderationErrorMessage = nodeData.inputs?.moderationErrorMessage as string
if (moderationErrorMessage) runner.setErrorMessage(moderationErrorMessage)
return runner
}
}

module.exports = { nodeClass: OpenAIModeration }
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Moderation } from '../Moderation'
import { BaseLanguageModel } from 'langchain/base_language'
import { OpenAIModerationChain } from 'langchain/chains'

export class OpenAIModerationRunner implements Moderation {
private moderationErrorMessage: string = "Text was found that violates OpenAI's content policy."

async checkForViolations(llm: BaseLanguageModel, input: string): Promise<string> {
const openAIApiKey = (llm as any).openAIApiKey
if (!openAIApiKey) {
throw Error('OpenAI API key not found')
}
// Create a new instance of the OpenAIModerationChain
const moderation = new OpenAIModerationChain({
openAIApiKey: openAIApiKey,
throwError: false // If set to true, the call will throw an error when the moderation chain detects violating content. If set to false, violating content will return "Text was found that violates OpenAI's content policy.".
})
// Send the user's input to the moderation chain and wait for the result
const { output: moderationOutput, results } = await moderation.call({
input: input
})
if (results[0].flagged) {
throw Error(this.moderationErrorMessage)
}
return moderationOutput
}

setErrorMessage(message: string) {
this.moderationErrorMessage = message
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { INode, INodeData, INodeParams } from '../../../src/Interface'
import { getBaseClasses } from '../../../src'
import { Moderation } from '../Moderation'
import { SimplePromptModerationRunner } from './SimplePromptModerationRunner'

class SimplePromptModeration implements INode {
label: string
name: string
version: number
description: string
type: string
icon: string
category: string
baseClasses: string[]
inputs: INodeParams[]

constructor() {
this.label = 'Simple Prompt Moderation'
this.name = 'inputModerationSimple'
this.version = 1.0
this.type = 'Moderation'
this.icon = 'simple_moderation.png'
this.category = 'Moderation'
this.description = 'Check whether input consists of any text from Deny list, and prevent being sent to LLM'
this.baseClasses = [this.type, ...getBaseClasses(Moderation)]
this.inputs = [
{
label: 'Deny List',
name: 'denyList',
type: 'string',
rows: 4,
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.',
optional: false
},
{
label: 'Error Message',
name: 'moderationErrorMessage',
type: 'string',
rows: 2,
default: 'Cannot Process! Input violates content moderation policies.',
optional: true
}
]
}

async init(nodeData: INodeData): Promise<any> {
const denyList = nodeData.inputs?.denyList as string
const moderationErrorMessage = nodeData.inputs?.moderationErrorMessage as string

return new SimplePromptModerationRunner(denyList, moderationErrorMessage)
}
}

module.exports = { nodeClass: SimplePromptModeration }
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Moderation } from '../Moderation'
import { BaseLanguageModel } from 'langchain/base_language'

export class SimplePromptModerationRunner implements Moderation {
private readonly denyList: string = ''
private readonly moderationErrorMessage: string = ''

constructor(denyList: string, moderationErrorMessage: string) {
this.denyList = denyList
if (denyList.indexOf('\n') === -1) {
this.denyList += '\n'
}
this.moderationErrorMessage = moderationErrorMessage
}

async checkForViolations(_: BaseLanguageModel, input: string): Promise<string> {
this.denyList.split('\n').forEach((denyListItem) => {
if (denyListItem && denyListItem !== '' && input.includes(denyListItem)) {
throw Error(this.moderationErrorMessage)
}
})
return Promise.resolve(input)
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit e158376

Please sign in to comment.