From 6748db9c3b104aae23c6c3f432e3ac61b5dbef1c Mon Sep 17 00:00:00 2001 From: Jon Date: Mon, 14 Apr 2025 11:58:29 +0100 Subject: [PATCH] fix: Fix issue with Qdrant not always connecting (#14328) Co-authored-by: Yiorgis Gozadinos --- .../credentials/QdrantApi.credentials.ts | 4 +- .../VectorStoreQdrant/Qdrant.utils.ts | 39 +++++++++++++++++++ .../VectorStoreQdrant.node.ts | 13 ++++--- .../methods/listSearch.ts | 9 ++--- 4 files changed, 52 insertions(+), 13 deletions(-) create mode 100644 packages/@n8n/nodes-langchain/nodes/vector_store/VectorStoreQdrant/Qdrant.utils.ts diff --git a/packages/@n8n/nodes-langchain/credentials/QdrantApi.credentials.ts b/packages/@n8n/nodes-langchain/credentials/QdrantApi.credentials.ts index 25c1bec32a..fbd3766ef2 100644 --- a/packages/@n8n/nodes-langchain/credentials/QdrantApi.credentials.ts +++ b/packages/@n8n/nodes-langchain/credentials/QdrantApi.credentials.ts @@ -42,9 +42,7 @@ export class QdrantApi implements ICredentialType { test: ICredentialTestRequest = { request: { baseURL: '={{$credentials.qdrantUrl}}', - headers: { - accept: 'application/json; charset=utf-8', - }, + url: '/collections', }, }; } diff --git a/packages/@n8n/nodes-langchain/nodes/vector_store/VectorStoreQdrant/Qdrant.utils.ts b/packages/@n8n/nodes-langchain/nodes/vector_store/VectorStoreQdrant/Qdrant.utils.ts new file mode 100644 index 0000000000..7cbd8e5c18 --- /dev/null +++ b/packages/@n8n/nodes-langchain/nodes/vector_store/VectorStoreQdrant/Qdrant.utils.ts @@ -0,0 +1,39 @@ +import { QdrantClient } from '@qdrant/js-client-rest'; +import { UserError } from 'n8n-workflow'; + +export type QdrantCredential = { + qdrantUrl: string; + apiKey: string; +}; + +function parseQdrantUrl(url: string): { protocol: string; host: string; port: number } { + try { + const parsedUrl = new URL(url); + return { + protocol: parsedUrl.protocol, + host: parsedUrl.hostname, + port: parsedUrl.port + ? parseInt(parsedUrl.port, 10) + : parsedUrl.protocol === 'https:' + ? 443 + : 80, + }; + } catch (error) { + throw new UserError( + `Invalid Qdrant URL: ${url}. Please provide a valid URL with protocol (http/https)`, + ); + } +} + +export function createQdrantClient(credentials: QdrantCredential): QdrantClient { + const { protocol, host, port } = parseQdrantUrl(credentials.qdrantUrl); + + const qdrantClient = new QdrantClient({ + host, + apiKey: credentials.apiKey, + https: protocol === 'https:', + port, + }); + + return qdrantClient; +} diff --git a/packages/@n8n/nodes-langchain/nodes/vector_store/VectorStoreQdrant/VectorStoreQdrant.node.ts b/packages/@n8n/nodes-langchain/nodes/vector_store/VectorStoreQdrant/VectorStoreQdrant.node.ts index 17f7ef2026..ff73376b53 100644 --- a/packages/@n8n/nodes-langchain/nodes/vector_store/VectorStoreQdrant/VectorStoreQdrant.node.ts +++ b/packages/@n8n/nodes-langchain/nodes/vector_store/VectorStoreQdrant/VectorStoreQdrant.node.ts @@ -2,9 +2,10 @@ import type { Callbacks } from '@langchain/core/callbacks/manager'; import type { Embeddings } from '@langchain/core/embeddings'; import type { QdrantLibArgs } from '@langchain/qdrant'; import { QdrantVectorStore } from '@langchain/qdrant'; -import type { Schemas as QdrantSchemas } from '@qdrant/js-client-rest'; +import { type Schemas as QdrantSchemas } from '@qdrant/js-client-rest'; import type { IDataObject, INodeProperties } from 'n8n-workflow'; +import { createQdrantClient, type QdrantCredential } from './Qdrant.utils'; import { createVectorStoreNode } from '../shared/createVectorStoreNode/createVectorStoreNode'; import { qdrantCollectionsSearch } from '../shared/createVectorStoreNode/methods/listSearch'; import { qdrantCollectionRLC } from '../shared/descriptions'; @@ -106,9 +107,10 @@ export class VectorStoreQdrant extends createVectorStoreNode