Skip to content

Commit 214b312

Browse files
committed
add input moderation to conversation chain
1 parent 436b3aa commit 214b312

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

packages/components/nodes/chains/ConversationChain/ConversationChain.ts

+25-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { ConsoleCallbackHandler, CustomChainHandler, additionalCallbacks } from
77
import { RunnableSequence } from 'langchain/schema/runnable'
88
import { StringOutputParser } from 'langchain/schema/output_parser'
99
import { ConsoleCallbackHandler as LCConsoleCallbackHandler } from '@langchain/core/tracers/console'
10+
import { checkInputs, Moderation, streamResponse } from '../../moderation/Moderation'
11+
import { formatResponse } from '../../outputparsers/OutputParserHelpers'
1012

1113
let systemMessage = `The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.`
1214
const inputKey = 'input'
@@ -26,7 +28,7 @@ class ConversationChain_Chains implements INode {
2628
constructor(fields?: { sessionId?: string }) {
2729
this.label = 'Conversation Chain'
2830
this.name = 'conversationChain'
29-
this.version = 2.0
31+
this.version = 3.0
3032
this.type = 'ConversationChain'
3133
this.icon = 'conv.svg'
3234
this.category = 'Chains'
@@ -60,6 +62,14 @@ class ConversationChain_Chains implements INode {
6062
optional: true,
6163
list: true
6264
},*/
65+
{
66+
label: 'Input Moderation',
67+
description: 'Detect text that could generate harmful output and prevent it from being sent to the language model',
68+
name: 'inputModeration',
69+
type: 'Moderation',
70+
optional: true,
71+
list: true
72+
},
6373
{
6474
label: 'System Message',
6575
name: 'systemMessagePrompt',
@@ -80,8 +90,21 @@ class ConversationChain_Chains implements INode {
8090
return chain
8191
}
8292

83-
async run(nodeData: INodeData, input: string, options: ICommonObject): Promise<string> {
93+
async run(nodeData: INodeData, input: string, options: ICommonObject): Promise<string | object> {
8494
const memory = nodeData.inputs?.memory
95+
const moderations = nodeData.inputs?.inputModeration as Moderation[]
96+
97+
if (moderations && moderations.length > 0) {
98+
try {
99+
// Use the output of the moderation chain as input for the LLM chain
100+
input = await checkInputs(moderations, input)
101+
} catch (e) {
102+
await new Promise((resolve) => setTimeout(resolve, 500))
103+
streamResponse(options.socketIO && options.socketIOClientId, e.message, options.socketIO, options.socketIOClientId)
104+
return formatResponse(e.message)
105+
}
106+
}
107+
85108
const chain = prepareChain(nodeData, this.sessionId, options.chatHistory)
86109

87110
const loggerHandler = new ConsoleCallbackHandler(options.logger)

packages/server/marketplaces/chatflows/Claude LLM.json

+11-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"data": {
7171
"id": "conversationChain_0",
7272
"label": "Conversation Chain",
73-
"version": 2,
73+
"version": 3,
7474
"name": "conversationChain",
7575
"type": "ConversationChain",
7676
"baseClasses": ["ConversationChain", "LLMChain", "BaseChain", "Runnable"],
@@ -110,9 +110,19 @@
110110
"description": "Override existing prompt with Chat Prompt Template. Human Message must includes {input} variable",
111111
"optional": true,
112112
"id": "conversationChain_0-input-chatPromptTemplate-ChatPromptTemplate"
113+
},
114+
{
115+
"label": "Input Moderation",
116+
"description": "Detect text that could generate harmful output and prevent it from being sent to the language model",
117+
"name": "inputModeration",
118+
"type": "Moderation",
119+
"optional": true,
120+
"list": true,
121+
"id": "conversationChain_0-input-inputModeration-Moderation"
113122
}
114123
],
115124
"inputs": {
125+
"inputModeration": "",
116126
"model": "{{chatAnthropic_0.data.instance}}",
117127
"memory": "{{bufferMemory_0.data.instance}}",
118128
"chatPromptTemplate": "{{chatPromptTemplate_0.data.instance}}",

packages/server/marketplaces/chatflows/Simple Conversation Chain.json

+11-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@
269269
"data": {
270270
"id": "conversationChain_0",
271271
"label": "Conversation Chain",
272-
"version": 2,
272+
"version": 3,
273273
"name": "conversationChain",
274274
"type": "ConversationChain",
275275
"baseClasses": ["ConversationChain", "LLMChain", "BaseChain", "Runnable"],
@@ -309,9 +309,19 @@
309309
"description": "Override existing prompt with Chat Prompt Template. Human Message must includes {input} variable",
310310
"optional": true,
311311
"id": "conversationChain_0-input-chatPromptTemplate-ChatPromptTemplate"
312+
},
313+
{
314+
"label": "Input Moderation",
315+
"description": "Detect text that could generate harmful output and prevent it from being sent to the language model",
316+
"name": "inputModeration",
317+
"type": "Moderation",
318+
"optional": true,
319+
"list": true,
320+
"id": "conversationChain_0-input-inputModeration-Moderation"
312321
}
313322
],
314323
"inputs": {
324+
"inputModeration": "",
315325
"model": "{{chatOpenAI_0.data.instance}}",
316326
"memory": "{{bufferMemory_0.data.instance}}",
317327
"chatPromptTemplate": "",

0 commit comments

Comments
 (0)