-
-
Notifications
You must be signed in to change notification settings - Fork 24.7k
Expand file tree
/
Copy pathChatGoogleGenerativeAI.ts
More file actions
334 lines (319 loc) · 13.8 KB
/
Copy pathChatGoogleGenerativeAI.ts
File metadata and controls
334 lines (319 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import { HarmBlockThreshold, HarmCategory } from '@google/generative-ai'
import type { SafetySetting } from '@google/generative-ai'
import { BaseCache } from '@langchain/core/caches'
import { ICommonObject, IMultiModalOption, INode, INodeData, INodeOptionsValue, INodeParams } from '../../../src/Interface'
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
import { getModels, MODEL_TYPE } from '../../../src/modelLoader'
import { GoogleGenerativeAIChatInput } from '@langchain/google-genai'
import { ChatGoogleGenerativeAI } from './FlowiseChatGoogleGenerativeAI'
import { checkDenyList } from '../../../src/httpSecurity'
class GoogleGenerativeAI_ChatModels implements INode {
label: string
name: string
version: number
type: string
icon: string
category: string
description: string
baseClasses: string[]
credential: INodeParams
inputs: INodeParams[]
constructor() {
this.label = 'Google Gemini'
this.name = 'chatGoogleGenerativeAI'
this.version = 3.1
this.type = 'ChatGoogleGenerativeAI'
this.icon = 'GoogleGemini.svg'
this.category = 'Chat Models'
this.description = 'Wrapper around Google Gemini large language models that use the Chat endpoint'
this.baseClasses = [this.type, ...getBaseClasses(ChatGoogleGenerativeAI)]
this.credential = {
label: 'Connect Credential',
name: 'credential',
type: 'credential',
credentialNames: ['googleGenerativeAI'],
optional: false,
description: 'Google Generative AI credential.'
}
this.inputs = [
{
label: 'Cache',
name: 'cache',
type: 'BaseCache',
optional: true
},
{
label: 'Model Name',
name: 'modelName',
type: 'asyncOptions',
loadMethod: 'listModels',
default: 'gemini-1.5-flash-latest'
},
{
label: 'Custom Model Name',
name: 'customModelName',
type: 'string',
placeholder: 'gemini-1.5-pro-exp-0801',
description: 'Custom model name to use. If provided, it will override the model selected',
additionalParams: true,
optional: true
},
{
label: 'Temperature',
name: 'temperature',
type: 'number',
step: 0.1,
default: 0.9,
optional: true
},
{
label: 'Streaming',
name: 'streaming',
type: 'boolean',
default: true,
optional: true,
additionalParams: true
},
{
label: 'Allow Image Uploads',
name: 'allowImageUploads',
type: 'boolean',
description:
'Allow image input. Refer to the <a href="https://docs.flowiseai.com/using-flowise/uploads#image" target="_blank">docs</a> for more details.',
default: false,
optional: true
},
/** The thinkingLevel parameter, recommended for Gemini 3 models and onwards. */
{
label: 'Thinking Budget',
name: 'thinkingBudget',
type: 'number',
description: 'Guides the number of thinking tokens. -1 for dynamic, 0 to disable, or positive integer (Gemini 2.5 models).',
step: 1,
optional: true,
additionalParams: true,
show: {
modelName: ['gemini-2.5-pro', 'gemini-2.5-flash', 'gemini-2.5-flash-lite']
}
},
{
label: 'Thinking Level',
name: 'thinkingLevel',
type: 'options',
description: 'Adjust the amount of reasoning effort based on the complexity of the user request',
options: [
{
label: 'Low',
name: 'LOW'
},
{
label: 'Medium',
name: 'MEDIUM'
},
{
label: 'High',
name: 'HIGH'
}
],
optional: true,
additionalParams: true,
show: {
modelName: ['gemini-3.1-pro-preview', 'gemini-3.1-flash-lite-preview', 'gemini-3-flash-preview']
}
},
{
label: 'Max Output Tokens',
name: 'maxOutputTokens',
type: 'number',
step: 1,
optional: true,
additionalParams: true
},
{
label: 'Top Probability',
name: 'topP',
type: 'number',
step: 0.1,
optional: true,
additionalParams: true
},
{
label: 'Top Next Highest Probability Tokens',
name: 'topK',
type: 'number',
description: `Decode using top-k sampling: consider the set of top_k most probable tokens. Must be positive`,
step: 1,
optional: true,
additionalParams: true
},
{
label: 'Safety Settings',
name: 'safetySettings',
type: 'array',
description:
'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',
array: [
{
label: 'Harm Category',
name: 'harmCategory',
type: 'options',
options: [
{
label: 'Dangerous',
name: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
description: 'Promotes, facilitates, or encourages harmful acts.'
},
{
label: 'Harassment',
name: HarmCategory.HARM_CATEGORY_HARASSMENT,
description: 'Negative or harmful comments targeting identity and/or protected attributes.'
},
{
label: 'Hate Speech',
name: HarmCategory.HARM_CATEGORY_HATE_SPEECH,
description: 'Content that is rude, disrespectful, or profane.'
},
{
label: 'Sexually Explicit',
name: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
description: 'Contains references to sexual acts or other lewd content.'
},
{
label: 'Civic Integrity',
name: HarmCategory.HARM_CATEGORY_CIVIC_INTEGRITY,
description: 'Election-related queries.'
}
]
},
{
label: 'Harm Block Threshold',
name: 'harmBlockThreshold',
type: 'options',
options: [
{
label: 'None',
name: HarmBlockThreshold.BLOCK_NONE,
description: 'Always show regardless of probability of unsafe content'
},
{
label: 'Only High',
name: HarmBlockThreshold.BLOCK_ONLY_HIGH,
description: 'Block when high probability of unsafe content'
},
{
label: 'Medium and Above',
name: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
description: 'Block when medium or high probability of unsafe content'
},
{
label: 'Low and Above',
name: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
description: 'Block when low, medium or high probability of unsafe content'
},
{
label: 'Threshold Unspecified (Default Threshold)',
name: HarmBlockThreshold.HARM_BLOCK_THRESHOLD_UNSPECIFIED,
description: 'Threshold is unspecified, block using default threshold'
}
]
}
],
optional: true,
additionalParams: true
},
{
label: 'Base URL',
name: 'baseUrl',
type: 'string',
description: 'Base URL for the API. Leave empty to use the default.',
optional: true,
additionalParams: true
}
]
}
//@ts-ignore
loadMethods = {
async listModels(): Promise<INodeOptionsValue[]> {
return await getModels(MODEL_TYPE.CHAT, 'chatGoogleGenerativeAI')
}
}
async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
const apiKey = getCredentialParam('googleGenerativeAPIKey', credentialData, nodeData)
const temperature = nodeData.inputs?.temperature as string
const modelName = nodeData.inputs?.modelName as string
const customModelName = nodeData.inputs?.customModelName as string
const maxOutputTokens = nodeData.inputs?.maxOutputTokens as string
const topP = nodeData.inputs?.topP as string
const topK = nodeData.inputs?.topK as string
const _safetySettings = nodeData.inputs?.safetySettings as string
const cache = nodeData.inputs?.cache as BaseCache
const streaming = nodeData.inputs?.streaming as boolean
const baseUrl = nodeData.inputs?.baseUrl as string | undefined
const thinkingBudget = nodeData.inputs?.thinkingBudget as string
const thinkingLevel = nodeData.inputs?.thinkingLevel as 'LOW' | 'MEDIUM' | 'HIGH'
const allowImageUploads = nodeData.inputs?.allowImageUploads as boolean
const obj: GoogleGenerativeAIChatInput = {
apiKey: apiKey,
model: customModelName || modelName,
streaming: streaming ?? true
}
// this extra metadata is needed, as langchain does not show the model name in the callbacks.
obj.metadata = {
fw_model_name: customModelName || modelName
}
if (maxOutputTokens) obj.maxOutputTokens = parseInt(maxOutputTokens, 10)
if (topP) obj.topP = parseFloat(topP)
if (topK) obj.topK = parseFloat(topK)
if (cache) obj.cache = cache
if (temperature) obj.temperature = parseFloat(temperature)
if (baseUrl) await checkDenyList(baseUrl)
if (baseUrl) obj.baseUrl = baseUrl
if (thinkingLevel) {
obj.thinkingConfig = {
thinkingLevel: thinkingLevel,
includeThoughts: true
}
} else if (thinkingBudget) {
obj.thinkingConfig = {
thinkingBudget: parseInt(thinkingBudget, 10),
includeThoughts: true
}
}
let safetySettings: SafetySetting[] = []
if (_safetySettings) {
try {
const parsedSafetySettings = typeof _safetySettings === 'string' ? JSON.parse(_safetySettings) : _safetySettings
if (Array.isArray(parsedSafetySettings)) {
const validSettings = parsedSafetySettings
.filter((setting: any) => setting.harmCategory && setting.harmBlockThreshold)
.map((setting: any) => ({
category: setting.harmCategory as HarmCategory,
threshold: setting.harmBlockThreshold as HarmBlockThreshold
}))
// Remove duplicates by keeping only the first occurrence of each harm category
const seenCategories = new Set<HarmCategory>()
safetySettings = validSettings.filter((setting) => {
if (seenCategories.has(setting.category)) {
return false
}
seenCategories.add(setting.category)
return true
})
}
} catch (error) {
console.warn('Failed to parse safety settings:', error)
}
}
if (safetySettings.length > 0) obj.safetySettings = safetySettings
const multiModalOption: IMultiModalOption = {
image: {
allowImageUploads: allowImageUploads ?? false
}
}
const model = new ChatGoogleGenerativeAI(nodeData.id, obj)
model.setMultiModalOption(multiModalOption)
return model
}
}
module.exports = { nodeClass: GoogleGenerativeAI_ChatModels }