@@ -2,7 +2,7 @@ import { HarmBlockThreshold, HarmCategory } from '@google/generative-ai'
2
2
import type { SafetySetting } from '@google/generative-ai'
3
3
import { BaseCache } from '@langchain/core/caches'
4
4
import { ICommonObject , IMultiModalOption , INode , INodeData , INodeOptionsValue , INodeParams } from '../../../src/Interface'
5
- import { convertMultiOptionsToStringArray , getBaseClasses , getCredentialData , getCredentialParam } from '../../../src/utils'
5
+ import { getBaseClasses , getCredentialData , getCredentialParam } from '../../../src/utils'
6
6
import { getModels , MODEL_TYPE } from '../../../src/modelLoader'
7
7
import { ChatGoogleGenerativeAI } from './FlowiseChatGoogleGenerativeAI'
8
8
import { GoogleGenerativeAIChatInput } from '@langchain/google-genai'
@@ -22,7 +22,7 @@ class GoogleGenerativeAI_ChatModels implements INode {
22
22
constructor ( ) {
23
23
this . label = 'ChatGoogleGenerativeAI'
24
24
this . name = 'chatGoogleGenerativeAI'
25
- this . version = 3.0
25
+ this . version = 3.1
26
26
this . type = 'ChatGoogleGenerativeAI'
27
27
this . icon = 'GoogleGemini.svg'
28
28
this . category = 'Chat Models'
@@ -101,58 +101,75 @@ class GoogleGenerativeAI_ChatModels implements INode {
101
101
additionalParams : true
102
102
} ,
103
103
{
104
- label : 'Harm Category ' ,
105
- name : 'harmCategory ' ,
106
- type : 'multiOptions ' ,
104
+ label : 'Safety Settings ' ,
105
+ name : 'safetySettings ' ,
106
+ type : 'array ' ,
107
107
description :
108
- 'Refer to <a target="_blank" href="https://cloud .google.com/vertex-ai /docs/generative-ai/multimodal/configure- safety-attributes#safety_attribute_definitions ">official guide</a> on how to use Harm Category ' ,
109
- options : [
108
+ 'Safety settings for the model. Refer to the <a href="https://ai .google.dev/gemini-api /docs/safety-settings ">official guide</a> on how to use Safety Settings ' ,
109
+ array : [
110
110
{
111
- label : 'Dangerous' ,
112
- name : HarmCategory . HARM_CATEGORY_DANGEROUS_CONTENT
111
+ label : 'Harm Category' ,
112
+ name : 'harmCategory' ,
113
+ type : 'options' ,
114
+ options : [
115
+ {
116
+ label : 'Dangerous' ,
117
+ name : HarmCategory . HARM_CATEGORY_DANGEROUS_CONTENT ,
118
+ description : 'Promotes, facilitates, or encourages harmful acts.'
119
+ } ,
120
+ {
121
+ label : 'Harassment' ,
122
+ name : HarmCategory . HARM_CATEGORY_HARASSMENT ,
123
+ description : 'Negative or harmful comments targeting identity and/or protected attributes.'
124
+ } ,
125
+ {
126
+ label : 'Hate Speech' ,
127
+ name : HarmCategory . HARM_CATEGORY_HATE_SPEECH ,
128
+ description : 'Content that is rude, disrespectful, or profane.'
129
+ } ,
130
+ {
131
+ label : 'Sexually Explicit' ,
132
+ name : HarmCategory . HARM_CATEGORY_SEXUALLY_EXPLICIT ,
133
+ description : 'Contains references to sexual acts or other lewd content.'
134
+ } ,
135
+ {
136
+ label : 'Civic Integrity' ,
137
+ name : HarmCategory . HARM_CATEGORY_CIVIC_INTEGRITY ,
138
+ description : 'Election-related queries.'
139
+ }
140
+ ]
113
141
} ,
114
142
{
115
- label : 'Harassment' ,
116
- name : HarmCategory . HARM_CATEGORY_HARASSMENT
117
- } ,
118
- {
119
- label : 'Hate Speech' ,
120
- name : HarmCategory . HARM_CATEGORY_HATE_SPEECH
121
- } ,
122
- {
123
- label : 'Sexually Explicit' ,
124
- name : HarmCategory . HARM_CATEGORY_SEXUALLY_EXPLICIT
125
- }
126
- ] ,
127
- optional : true ,
128
- additionalParams : true
129
- } ,
130
- {
131
- label : 'Harm Block Threshold' ,
132
- name : 'harmBlockThreshold' ,
133
- type : 'multiOptions' ,
134
- description :
135
- 'Refer to <a target="_blank" href="https://cloud.google.com/vertex-ai/docs/generative-ai/multimodal/configure-safety-attributes#safety_setting_thresholds">official guide</a> on how to use Harm Block Threshold' ,
136
- options : [
137
- {
138
- label : 'Low and Above' ,
139
- name : HarmBlockThreshold . BLOCK_LOW_AND_ABOVE
140
- } ,
141
- {
142
- label : 'Medium and Above' ,
143
- name : HarmBlockThreshold . BLOCK_MEDIUM_AND_ABOVE
144
- } ,
145
- {
146
- label : 'None' ,
147
- name : HarmBlockThreshold . BLOCK_NONE
148
- } ,
149
- {
150
- label : 'Only High' ,
151
- name : HarmBlockThreshold . BLOCK_ONLY_HIGH
152
- } ,
153
- {
154
- label : 'Threshold Unspecified' ,
155
- name : HarmBlockThreshold . HARM_BLOCK_THRESHOLD_UNSPECIFIED
143
+ label : 'Harm Block Threshold' ,
144
+ name : 'harmBlockThreshold' ,
145
+ type : 'options' ,
146
+ options : [
147
+ {
148
+ label : 'None' ,
149
+ name : HarmBlockThreshold . BLOCK_NONE ,
150
+ description : 'Always show regardless of probability of unsafe content'
151
+ } ,
152
+ {
153
+ label : 'Only High' ,
154
+ name : HarmBlockThreshold . BLOCK_ONLY_HIGH ,
155
+ description : 'Block when high probability of unsafe content'
156
+ } ,
157
+ {
158
+ label : 'Medium and Above' ,
159
+ name : HarmBlockThreshold . BLOCK_MEDIUM_AND_ABOVE ,
160
+ description : 'Block when medium or high probability of unsafe content'
161
+ } ,
162
+ {
163
+ label : 'Low and Above' ,
164
+ name : HarmBlockThreshold . BLOCK_LOW_AND_ABOVE ,
165
+ description : 'Block when low, medium or high probability of unsafe content'
166
+ } ,
167
+ {
168
+ label : 'Threshold Unspecified (Default Threshold)' ,
169
+ name : HarmBlockThreshold . HARM_BLOCK_THRESHOLD_UNSPECIFIED ,
170
+ description : 'Threshold is unspecified, block using default threshold'
171
+ }
172
+ ]
156
173
}
157
174
] ,
158
175
optional : true ,
@@ -195,8 +212,8 @@ class GoogleGenerativeAI_ChatModels implements INode {
195
212
const maxOutputTokens = nodeData . inputs ?. maxOutputTokens as string
196
213
const topP = nodeData . inputs ?. topP as string
197
214
const topK = nodeData . inputs ?. topK as string
198
- const harmCategory = nodeData . inputs ?. harmCategory as string
199
- const harmBlockThreshold = nodeData . inputs ?. harmBlockThreshold as string
215
+ const _safetySettings = nodeData . inputs ?. safetySettings as string
216
+
200
217
const cache = nodeData . inputs ?. cache as BaseCache
201
218
const streaming = nodeData . inputs ?. streaming as boolean
202
219
const baseUrl = nodeData . inputs ?. baseUrl as string | undefined
@@ -220,17 +237,32 @@ class GoogleGenerativeAI_ChatModels implements INode {
220
237
if ( temperature ) obj . temperature = parseFloat ( temperature )
221
238
if ( baseUrl ) obj . baseUrl = baseUrl
222
239
223
- // Safety Settings
224
- let harmCategories : string [ ] = convertMultiOptionsToStringArray ( harmCategory )
225
- let harmBlockThresholds : string [ ] = convertMultiOptionsToStringArray ( harmBlockThreshold )
226
- if ( harmCategories . length != harmBlockThresholds . length )
227
- throw new Error ( `Harm Category & Harm Block Threshold are not the same length` )
228
- const safetySettings : SafetySetting [ ] = harmCategories . map ( ( harmCategory , index ) => {
229
- return {
230
- category : harmCategory as HarmCategory ,
231
- threshold : harmBlockThresholds [ index ] as HarmBlockThreshold
240
+ let safetySettings : SafetySetting [ ] = [ ]
241
+ if ( _safetySettings ) {
242
+ try {
243
+ const parsedSafetySettings = typeof _safetySettings === 'string' ? JSON . parse ( _safetySettings ) : _safetySettings
244
+ if ( Array . isArray ( parsedSafetySettings ) ) {
245
+ const validSettings = parsedSafetySettings
246
+ . filter ( ( setting : any ) => setting . harmCategory && setting . harmBlockThreshold )
247
+ . map ( ( setting : any ) => ( {
248
+ category : setting . harmCategory as HarmCategory ,
249
+ threshold : setting . harmBlockThreshold as HarmBlockThreshold
250
+ } ) )
251
+
252
+ // Remove duplicates by keeping only the first occurrence of each harm category
253
+ const seenCategories = new Set < HarmCategory > ( )
254
+ safetySettings = validSettings . filter ( ( setting ) => {
255
+ if ( seenCategories . has ( setting . category ) ) {
256
+ return false
257
+ }
258
+ seenCategories . add ( setting . category )
259
+ return true
260
+ } )
261
+ }
262
+ } catch ( error ) {
263
+ console . warn ( 'Failed to parse safety settings:' , error )
232
264
}
233
- } )
265
+ }
234
266
if ( safetySettings . length > 0 ) obj . safetySettings = safetySettings
235
267
236
268
const multiModalOption : IMultiModalOption = {
0 commit comments