mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-15 09:04:07 -08:00
06419d9483
Co-authored-by: JP van Oosten <jp@n8n.io>
174 lines
4.7 KiB
TypeScript
174 lines
4.7 KiB
TypeScript
/* eslint-disable n8n-nodes-base/node-dirname-against-convention */
|
|
import {
|
|
NodeConnectionType,
|
|
type IExecuteFunctions,
|
|
type INodeType,
|
|
type INodeTypeDescription,
|
|
type SupplyData,
|
|
} from 'n8n-workflow';
|
|
import { ChatGooglePaLM } from '@langchain/community/chat_models/googlepalm';
|
|
import { N8nLlmTracing } from '../N8nLlmTracing';
|
|
|
|
export class LmChatGooglePalm implements INodeType {
|
|
description: INodeTypeDescription = {
|
|
displayName: 'Google PaLM Chat Model',
|
|
// eslint-disable-next-line n8n-nodes-base/node-class-description-name-miscased
|
|
name: 'lmChatGooglePalm',
|
|
icon: 'file:google.svg',
|
|
hidden: true,
|
|
group: ['transform'],
|
|
version: 1,
|
|
description: 'Chat Model Google PaLM',
|
|
defaults: {
|
|
name: 'Google PaLM Chat Model',
|
|
},
|
|
codex: {
|
|
categories: ['AI'],
|
|
subcategories: {
|
|
AI: ['Language Models', 'Root Nodes'],
|
|
'Language Models': ['Chat Models (Recommended)'],
|
|
},
|
|
resources: {
|
|
primaryDocumentation: [
|
|
{
|
|
url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.lmchatgooglepalm/',
|
|
},
|
|
],
|
|
},
|
|
},
|
|
// eslint-disable-next-line n8n-nodes-base/node-class-description-inputs-wrong-regular-node
|
|
inputs: [],
|
|
// eslint-disable-next-line n8n-nodes-base/node-class-description-outputs-wrong
|
|
outputs: [NodeConnectionType.AiLanguageModel],
|
|
outputNames: ['Model'],
|
|
credentials: [
|
|
{
|
|
name: 'googlePalmApi',
|
|
required: true,
|
|
},
|
|
],
|
|
requestDefaults: {
|
|
ignoreHttpStatusErrors: true,
|
|
baseURL: '={{ $credentials.host }}',
|
|
},
|
|
properties: [
|
|
{
|
|
displayName:
|
|
"Google PaLM API is <a href='https://ai.google.dev/palm_docs/deprecation' target='_blank'>deprecated</a>. Please use Google Vertex or Google Gemini nodes instead.",
|
|
name: 'deprecated',
|
|
type: 'notice',
|
|
default: '',
|
|
},
|
|
{
|
|
displayName: 'Model',
|
|
name: 'modelName',
|
|
type: 'options',
|
|
description:
|
|
'The model which will generate the completion. <a href="https://developers.generativeai.google/api/rest/generativelanguage/models/list">Learn more</a>.',
|
|
typeOptions: {
|
|
loadOptions: {
|
|
routing: {
|
|
request: {
|
|
method: 'GET',
|
|
url: '/v1beta3/models',
|
|
},
|
|
output: {
|
|
postReceive: [
|
|
{
|
|
type: 'rootProperty',
|
|
properties: {
|
|
property: 'models',
|
|
},
|
|
},
|
|
{
|
|
type: 'filter',
|
|
properties: {
|
|
pass: "={{ $responseItem.name.startsWith('models/chat') }}",
|
|
},
|
|
},
|
|
{
|
|
type: 'setKeyValue',
|
|
properties: {
|
|
name: '={{$responseItem.name}}',
|
|
value: '={{$responseItem.name}}',
|
|
description: '={{$responseItem.description}}',
|
|
},
|
|
},
|
|
{
|
|
type: 'sort',
|
|
properties: {
|
|
key: 'name',
|
|
},
|
|
},
|
|
],
|
|
},
|
|
},
|
|
},
|
|
},
|
|
routing: {
|
|
send: {
|
|
type: 'body',
|
|
property: 'model',
|
|
},
|
|
},
|
|
default: 'models/chat-bison-001',
|
|
},
|
|
{
|
|
displayName: 'Options',
|
|
name: 'options',
|
|
placeholder: 'Add Option',
|
|
description: 'Additional options to add',
|
|
type: 'collection',
|
|
default: {},
|
|
options: [
|
|
{
|
|
displayName: 'Sampling Temperature',
|
|
name: 'temperature',
|
|
default: 0.7,
|
|
typeOptions: { maxValue: 1, minValue: 0, numberPrecision: 1 },
|
|
description:
|
|
'Controls randomness: Lowering results in less random completions. As the temperature approaches zero, the model will become deterministic and repetitive.',
|
|
type: 'number',
|
|
},
|
|
{
|
|
displayName: 'Top K',
|
|
name: 'topK',
|
|
default: 40,
|
|
typeOptions: { maxValue: 1, minValue: -1, numberPrecision: 1 },
|
|
description:
|
|
'Used to remove "long tail" low probability responses. Defaults to -1, which disables it.',
|
|
type: 'number',
|
|
},
|
|
{
|
|
displayName: 'Top P',
|
|
name: 'topP',
|
|
default: 0.9,
|
|
typeOptions: { maxValue: 1, minValue: 0, numberPrecision: 1 },
|
|
description:
|
|
'Controls diversity via nucleus sampling: 0.5 means half of all likelihood-weighted options are considered. We generally recommend altering this or temperature but not both.',
|
|
type: 'number',
|
|
},
|
|
],
|
|
},
|
|
],
|
|
};
|
|
|
|
async supplyData(this: IExecuteFunctions, itemIndex: number): Promise<SupplyData> {
|
|
const credentials = await this.getCredentials('googlePalmApi');
|
|
|
|
const modelName = this.getNodeParameter('modelName', itemIndex) as string;
|
|
const options = this.getNodeParameter('options', itemIndex, {}) as object;
|
|
|
|
const model = new ChatGooglePaLM({
|
|
apiKey: credentials.apiKey as string,
|
|
modelName,
|
|
...options,
|
|
callbacks: [new N8nLlmTracing(this)],
|
|
});
|
|
|
|
return {
|
|
response: model,
|
|
};
|
|
}
|
|
}
|