Skip to content

Commit

Permalink
feat(Groq Chat Model Node): Add support for Groq chat models (#9250)
Browse files Browse the repository at this point in the history
Signed-off-by: Oleg Ivaniv <me@olegivaniv.com>
Co-authored-by: Michael Kret <michael.k@radency.com>
  • Loading branch information
OlegIvaniv and michael-radency authored Apr 30, 2024
1 parent abae635 commit 96f02bd
Show file tree
Hide file tree
Showing 7 changed files with 276 additions and 3 deletions.
41 changes: 41 additions & 0 deletions packages/@n8n/nodes-langchain/credentials/GroqApi.credentials.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import type {
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';

export class GroqApi implements ICredentialType {
name = 'groqApi';

displayName = 'Groq';

documentationUrl = 'groq';

properties: INodeProperties[] = [
{
displayName: 'API Key',
name: 'apiKey',
type: 'string',
typeOptions: { password: true },
required: true,
default: '',
},
];

authenticate: IAuthenticateGeneric = {
type: 'generic',
properties: {
headers: {
Authorization: '=Bearer {{$credentials.apiKey}}',
},
},
};

test: ICredentialTestRequest = {
request: {
baseURL: 'https://api.groq.com/openai/v1',
url: '/models',
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ function getInputs(
filter: {
nodes: [
'@n8n/n8n-nodes-langchain.lmChatAnthropic',
'@n8n/n8n-nodes-langchain.lmChatGroq',
'@n8n/n8n-nodes-langchain.lmChatOllama',
'@n8n/n8n-nodes-langchain.lmChatOpenAi',
'@n8n/n8n-nodes-langchain.lmChatGooglePalm',
Expand Down
151 changes: 151 additions & 0 deletions packages/@n8n/nodes-langchain/nodes/llms/LmChatGroq/LmChatGroq.node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/* eslint-disable n8n-nodes-base/node-dirname-against-convention */
import {
NodeConnectionType,
type IExecuteFunctions,
type INodeType,
type INodeTypeDescription,
type SupplyData,
} from 'n8n-workflow';

import { ChatGroq } from '@langchain/groq';
import { logWrapper } from '../../../utils/logWrapper';
import { getConnectionHintNoticeField } from '../../../utils/sharedFields';

export class LmChatGroq implements INodeType {
description: INodeTypeDescription = {
displayName: 'Groq Chat Model',
// eslint-disable-next-line n8n-nodes-base/node-class-description-name-miscased
name: 'lmChatGroq',
icon: 'file:groq.svg',
group: ['transform'],
version: 1,
description: 'Language Model Groq',
defaults: {
name: 'Groq Chat Model',
},
codex: {
categories: ['AI'],
subcategories: {
AI: ['Language Models'],
},
resources: {
primaryDocumentation: [
{
url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.lmchatgroq/',
},
],
},
},
// 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: 'groqApi',
required: true,
},
],
requestDefaults: {
baseURL: 'https://api.groq.com/openai/v1',
},
properties: [
getConnectionHintNoticeField([NodeConnectionType.AiChain, NodeConnectionType.AiChain]),
{
displayName: 'Model',
name: 'model',
type: 'options',
typeOptions: {
loadOptions: {
routing: {
request: {
method: 'GET',
url: '/models',
},
output: {
postReceive: [
{
type: 'rootProperty',
properties: {
property: 'data',
},
},
{
type: 'filter',
properties: {
pass: '={{ $responseItem.active === true && $responseItem.object === "model" }}',
},
},
{
type: 'setKeyValue',
properties: {
name: '={{$responseItem.id}}',
value: '={{$responseItem.id}}',
},
},
],
},
},
},
},
routing: {
send: {
type: 'body',
property: 'model',
},
},
description:
'The model which will generate the completion. <a href="https://console.groq.com/docs/models">Learn more</a>.',
default: 'llama3-8b-8192',
},
{
displayName: 'Options',
name: 'options',
placeholder: 'Add Option',
description: 'Additional options to add',
type: 'collection',
default: {},
options: [
{
displayName: 'Maximum Number of Tokens',
name: 'maxTokensToSample',
default: 4096,
description: 'The maximum number of tokens to generate in the completion',
type: 'number',
},
{
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',
},
],
},
],
};

async supplyData(this: IExecuteFunctions, itemIndex: number): Promise<SupplyData> {
const credentials = await this.getCredentials('groqApi');

const modelName = this.getNodeParameter('model', itemIndex) as string;
const options = this.getNodeParameter('options', itemIndex, {}) as {
maxTokensToSample?: number;
temperature: number;
};

const model = new ChatGroq({
apiKey: credentials.apiKey as string,
modelName,
maxTokens: options.maxTokensToSample,
temperature: options.temperature,
});

return {
response: logWrapper(model, this),
};
}
}
20 changes: 20 additions & 0 deletions packages/@n8n/nodes-langchain/nodes/llms/LmChatGroq/groq.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions packages/@n8n/nodes-langchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"dist/credentials/AzureOpenAiApi.credentials.js",
"dist/credentials/CohereApi.credentials.js",
"dist/credentials/GooglePalmApi.credentials.js",
"dist/credentials/GroqApi.credentials.js",
"dist/credentials/HuggingFaceApi.credentials.js",
"dist/credentials/MotorheadApi.credentials.js",
"dist/credentials/MistralCloudApi.credentials.js",
Expand Down Expand Up @@ -70,6 +71,7 @@
"dist/nodes/llms/LmChatAwsBedrock/LmChatAwsBedrock.node.js",
"dist/nodes/llms/LmChatGooglePalm/LmChatGooglePalm.node.js",
"dist/nodes/llms/LmChatGoogleGemini/LmChatGoogleGemini.node.js",
"dist/nodes/llms/LmChatGroq/LmChatGroq.node.js",
"dist/nodes/llms/LmChatMistralCloud/LmChatMistralCloud.node.js",
"dist/nodes/llms/LMChatOllama/LmChatOllama.node.js",
"dist/nodes/llms/LMChatOpenAi/LmChatOpenAi.node.js",
Expand Down Expand Up @@ -140,6 +142,7 @@
"@langchain/community": "0.0.53",
"@langchain/core": "0.1.61",
"@langchain/google-genai": "^0.0.12",
"@langchain/groq": "^0.0.8",
"@langchain/mistralai": "0.0.19",
"@langchain/openai": "^0.0.28",
"@langchain/pinecone": "^0.0.4",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
}}
</n8n-tooltip>
</li>
<li v-if="(consumedTokensSum?.totalTokens ?? 0) > 0">
<li v-if="(consumedTokensSum?.totalTokens ?? 0) > 0" :class="$style.tokensUsage">
{{
$locale.baseText('runData.aiContentBlock.tokens', {
interpolate: {
Expand Down Expand Up @@ -197,4 +197,9 @@ const runMeta = computed(() => {
padding-left: var(--spacing-3xs);
}
}
.tokensUsage {
display: flex;
align-items: center;
gap: var(--spacing-3xs);
}
</style>
56 changes: 54 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 96f02bd

Please sign in to comment.