Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement MistralCloud Chat & Embeddings nodes #8239

Merged
merged 4 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
},
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/* eslint-disable n8n-nodes-base/node-dirname-against-convention */
import {
NodeConnectionType,
type IExecuteFunctions,
type INodeType,
type INodeTypeDescription,
type SupplyData,
} from 'n8n-workflow';
import type { MistralAIEmbeddingsParams } from '@langchain/mistralai';
import { MistralAIEmbeddings } from '@langchain/mistralai';
import { logWrapper } from '../../../utils/logWrapper';
import { getConnectionHintNoticeField } from '../../../utils/sharedFields';

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',
},
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>;

const embeddings = new MistralAIEmbeddings({
apiKey: credentials.apiKey as string,
modelName,
...options,
});

return {
response: logWrapper(embeddings, this),
};
}
}
Loading
Loading