Skip to content

Commit

Permalink
feat(LmChatMistralCloud Node): Implement MistralCloud Chat & Embeddin…
Browse files Browse the repository at this point in the history
…gs nodes

Signed-off-by: Oleg Ivaniv <me@olegivaniv.com>
  • Loading branch information
OlegIvaniv committed Jan 5, 2024
1 parent 8a78ae1 commit b1b93e9
Show file tree
Hide file tree
Showing 8 changed files with 440 additions and 6 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@
"typescript": "^5.3.0",
"xml2js": "^0.5.0",
"cpy@8>globby": "^11.1.0",
"qqjs>globby": "^11.1.0"
"qqjs>globby": "^11.1.0",
"@langchain/core": "^0.1.8"
},
"patchedDependencies": {
"typedi@0.10.0": "patches/typedi@0.10.0.patch",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type {
IAuthenticateGeneric,
ICredentialTestRequest,
ICredentialType,
INodeProperties,
} from 'n8n-workflow';

export class MistralCloudApi implements ICredentialType {
name = 'mistralCloudApi';

displayName = 'Mistral Cloud API';

documentationUrl = 'mistralCloud';

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.mistral.ai/v1',
url: '/models',
method: 'GET'

Check failure on line 39 in packages/@n8n/nodes-langchain/credentials/MistralCloudApi.credentials.ts

View workflow job for this annotation

GitHub Actions / Lint changes

Insert `,`
},
};
}
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 { logWrapper } from '../../../utils/logWrapper';
import { getConnectionHintNoticeField } from '../../../utils/sharedFields';
import { MistralAIEmbeddings, MistralAIEmbeddingsParams } from "@langchain/mistralai";

Check warning on line 11 in packages/@n8n/nodes-langchain/nodes/embeddings/EmbeddingsMistralCloud/EmbeddingsMistralCloud.node.ts

View workflow job for this annotation

GitHub Actions / Lint changes

`@langchain/mistralai` import should occur before import of `../../../utils/logWrapper`

Check failure on line 11 in packages/@n8n/nodes-langchain/nodes/embeddings/EmbeddingsMistralCloud/EmbeddingsMistralCloud.node.ts

View workflow job for this annotation

GitHub Actions / Lint changes

Import "MistralAIEmbeddingsParams" is only used as types

Check failure on line 11 in packages/@n8n/nodes-langchain/nodes/embeddings/EmbeddingsMistralCloud/EmbeddingsMistralCloud.node.ts

View workflow job for this annotation

GitHub Actions / Lint changes

Replace `"@langchain/mistralai"` with `'@langchain/mistralai'`

export class EmbeddingsMistralCloud implements INodeType {
description: INodeTypeDescription = {
displayName: 'Embeddings Mistral Cloud',
name: 'embeddingsMistralCloud',
icon: 'file:mistral.svg',
credentials: [
{
name: 'mistralCloudApi',
required: true,
},
],
group: ['transform'],
version: 1,
description: 'Use Embeddings Mistral Cloud',
defaults: {
name: 'Embeddings Mistral Cloud',
},

codex: {
categories: ['AI'],
subcategories: {
AI: ['Embeddings'],
},
resources: {
primaryDocumentation: [
{
url: 'https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.embeddingsmistralcloud/',
},
],
},
},
// 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.AiEmbedding],
outputNames: ['Embeddings'],
requestDefaults: {
ignoreHttpStatusErrors: true,
baseURL: "https://api.mistral.ai/v1",

Check failure on line 51 in packages/@n8n/nodes-langchain/nodes/embeddings/EmbeddingsMistralCloud/EmbeddingsMistralCloud.node.ts

View workflow job for this annotation

GitHub Actions / Lint changes

Replace `"https://api.mistral.ai/v1"` with `'https://api.mistral.ai/v1'`
},
properties: [
getConnectionHintNoticeField([NodeConnectionType.AiVectorStore]),
{
displayName: 'Model',
name: 'model',
type: 'options',
description:
'The model which will compute the embeddings. <a href="https://docs.mistral.ai/platform/endpoints/">Learn more</a>.',
typeOptions: {
loadOptions: {
routing: {
request: {
method: 'GET',
url: '/models',
},
output: {
postReceive: [
{
type: 'rootProperty',
properties: {
property: 'data',
},
},
{
type: 'filter',
properties: {
pass: "={{ $responseItem.id.includes('embed') }}",
},
},
{
type: 'setKeyValue',
properties: {
name: '={{ $responseItem.id }}',
value: '={{ $responseItem.id }}',
},
},
{
type: 'sort',
properties: {
key: 'name',
},
},
],
},
},
},
},
routing: {
send: {
type: 'body',
property: 'model',
},
},
default: 'mistral-embed',
},
{
displayName: 'Options',
name: 'options',
placeholder: 'Add Option',
description: 'Additional options to add',
type: 'collection',
default: {},
options: [
{
displayName: 'Batch Size',
name: 'batchSize',
default: 512,
typeOptions: { maxValue: 2048 },
description: 'Maximum number of documents to send in each request',
type: 'number',
},
{
displayName: 'Strip New Lines',
name: 'stripNewLines',
default: true,
description: 'Whether to strip new lines from the input text',
type: 'boolean',
},
],
},
],
};

async supplyData(this: IExecuteFunctions, itemIndex: number): Promise<SupplyData> {
const credentials = await this.getCredentials('mistralCloudApi');
const modelName = this.getNodeParameter('model', itemIndex) as string;
const options = this.getNodeParameter('options', itemIndex, {}) as Partial<MistralAIEmbeddingsParams>;

Check failure on line 139 in packages/@n8n/nodes-langchain/nodes/embeddings/EmbeddingsMistralCloud/EmbeddingsMistralCloud.node.ts

View workflow job for this annotation

GitHub Actions / Lint changes

Replace `'options',·itemIndex,·{}` with `⏎↹↹↹'options',⏎↹↹↹itemIndex,⏎↹↹↹{},⏎↹↹`

const embeddings = new MistralAIEmbeddings({
apiKey: credentials!.apiKey as string,

Check warning on line 142 in packages/@n8n/nodes-langchain/nodes/embeddings/EmbeddingsMistralCloud/EmbeddingsMistralCloud.node.ts

View workflow job for this annotation

GitHub Actions / Lint changes

Forbidden non-null assertion

Check failure on line 142 in packages/@n8n/nodes-langchain/nodes/embeddings/EmbeddingsMistralCloud/EmbeddingsMistralCloud.node.ts

View workflow job for this annotation

GitHub Actions / Lint changes

This assertion is unnecessary since it does not change the type of the expression
modelName,
...options,
});

return {
response: logWrapper(embeddings, this),
};
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit b1b93e9

Please sign in to comment.