mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-28 17:01:00 +03:00
ResponsibleAI - Input Moderation - Renaming of files for better clarity and alignment to functionality
This commit is contained in:
@@ -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 }
|
||||
@@ -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 not shown.
|
After Width: | Height: | Size: 47 KiB |
Reference in New Issue
Block a user