forked from FlowiseAI/Flowise
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request FlowiseAI#1222 from vinodkiran/FEATURE/input-moder…
…ation ResponsibleAI - Input Moderation - …
- Loading branch information
Showing
9 changed files
with
646 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
packages/components/nodes/moderation/OpenAIModeration/OpenAIModeration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
31 changes: 31 additions & 0 deletions
31
packages/components/nodes/moderation/OpenAIModeration/OpenAIModerationRunner.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Binary file added
BIN
+46.8 KB
packages/components/nodes/moderation/OpenAIModeration/openai-moderation.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 55 additions & 0 deletions
55
packages/components/nodes/moderation/SimplePromptModeration/SimplePromptModeration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
24 changes: 24 additions & 0 deletions
24
packages/components/nodes/moderation/SimplePromptModeration/SimplePromptModerationRunner.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Binary file added
BIN
+43.8 KB
packages/components/nodes/moderation/SimplePromptModeration/simple_moderation.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.