|
| 1 | +import { INode, INodeData, INodeOutputsValue, INodeParams } from '../../../src/Interface' |
| 2 | +import { QdrantClient } from '@qdrant/js-client-rest' |
| 3 | +import { QdrantVectorStore, QdrantLibArgs } from 'langchain/vectorstores/qdrant' |
| 4 | +import { Embeddings } from 'langchain/embeddings/base' |
| 5 | +import { getBaseClasses } from '../../../src/utils' |
| 6 | + |
| 7 | +class Qdrant_Existing_VectorStores implements INode { |
| 8 | + label: string |
| 9 | + name: string |
| 10 | + description: string |
| 11 | + type: string |
| 12 | + icon: string |
| 13 | + category: string |
| 14 | + baseClasses: string[] |
| 15 | + inputs: INodeParams[] |
| 16 | + outputs: INodeOutputsValue[] |
| 17 | + |
| 18 | + constructor() { |
| 19 | + this.label = 'Qdrant Load Existing Index' |
| 20 | + this.name = 'qdrantExistingIndex' |
| 21 | + this.type = 'Qdrant' |
| 22 | + this.icon = 'qdrant_logo.svg' |
| 23 | + this.category = 'Vector Stores' |
| 24 | + this.description = 'Load existing index from Qdrant (i.e., documents have been upserted)' |
| 25 | + this.baseClasses = [this.type, 'VectorStoreRetriever', 'BaseRetriever'] |
| 26 | + this.inputs = [ |
| 27 | + { |
| 28 | + label: 'Embeddings', |
| 29 | + name: 'embeddings', |
| 30 | + type: 'Embeddings' |
| 31 | + }, |
| 32 | + { |
| 33 | + label: 'Qdrant Server URL', |
| 34 | + name: 'qdrantServerUrl', |
| 35 | + type: 'string', |
| 36 | + placeholder: 'http://localhost:6333' |
| 37 | + }, |
| 38 | + { |
| 39 | + label: 'Qdrant Collection Name', |
| 40 | + name: 'qdrantCollection', |
| 41 | + type: 'string' |
| 42 | + }, |
| 43 | + { |
| 44 | + label: 'Qdrant API Key', |
| 45 | + name: 'qdrantApiKey', |
| 46 | + type: 'password', |
| 47 | + optional: true |
| 48 | + }, |
| 49 | + { |
| 50 | + label: 'Qdrant Collection Cofiguration', |
| 51 | + name: 'qdrantCollectionCofiguration', |
| 52 | + type: 'json', |
| 53 | + optional: true, |
| 54 | + additionalParams: true |
| 55 | + }, |
| 56 | + { |
| 57 | + label: 'Top K', |
| 58 | + name: 'topK', |
| 59 | + description: 'Number of top results to fetch. Default to 4', |
| 60 | + placeholder: '4', |
| 61 | + type: 'number', |
| 62 | + additionalParams: true, |
| 63 | + optional: true |
| 64 | + } |
| 65 | + ] |
| 66 | + this.outputs = [ |
| 67 | + { |
| 68 | + label: 'Qdrant Retriever', |
| 69 | + name: 'retriever', |
| 70 | + baseClasses: this.baseClasses |
| 71 | + }, |
| 72 | + { |
| 73 | + label: 'Qdrant Vector Store', |
| 74 | + name: 'vectorStore', |
| 75 | + baseClasses: [this.type, ...getBaseClasses(QdrantVectorStore)] |
| 76 | + } |
| 77 | + ] |
| 78 | + } |
| 79 | + |
| 80 | + async init(nodeData: INodeData): Promise<any> { |
| 81 | + const qdrantServerUrl = nodeData.inputs?.qdrantServerUrl as string |
| 82 | + const collectionName = nodeData.inputs?.qdrantCollection as string |
| 83 | + const qdrantApiKey = nodeData.inputs?.qdrantApiKey as string |
| 84 | + let qdrantCollectionCofiguration = nodeData.inputs?.qdrantCollectionCofiguration |
| 85 | + const embeddings = nodeData.inputs?.embeddings as Embeddings |
| 86 | + const output = nodeData.outputs?.output as string |
| 87 | + const topK = nodeData.inputs?.topK as string |
| 88 | + const k = topK ? parseInt(topK, 10) : 4 |
| 89 | + |
| 90 | + // connect to Qdrant Cloud |
| 91 | + const client = new QdrantClient({ |
| 92 | + url: qdrantServerUrl, |
| 93 | + apiKey: qdrantApiKey |
| 94 | + }) |
| 95 | + |
| 96 | + const dbConfig: QdrantLibArgs = { |
| 97 | + client, |
| 98 | + collectionName |
| 99 | + } |
| 100 | + |
| 101 | + if (qdrantCollectionCofiguration) { |
| 102 | + qdrantCollectionCofiguration = |
| 103 | + typeof qdrantCollectionCofiguration === 'object' ? qdrantCollectionCofiguration : JSON.parse(qdrantCollectionCofiguration) |
| 104 | + dbConfig.collectionConfig = qdrantCollectionCofiguration |
| 105 | + } |
| 106 | + |
| 107 | + const vectorStore = await QdrantVectorStore.fromExistingCollection(embeddings, dbConfig) |
| 108 | + |
| 109 | + if (output === 'retriever') { |
| 110 | + const retriever = vectorStore.asRetriever(k) |
| 111 | + return retriever |
| 112 | + } else if (output === 'vectorStore') { |
| 113 | + ;(vectorStore as any).k = k |
| 114 | + return vectorStore |
| 115 | + } |
| 116 | + return vectorStore |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +module.exports = { nodeClass: Qdrant_Existing_VectorStores } |
0 commit comments