|
| 1 | +import { getCredentialData, getCredentialParam } from '../../../src' |
| 2 | +import { ICommonObject, INode, INodeData, INodeOutputsValue, INodeParams } from '../../../src/Interface' |
| 3 | +import { Meilisearch } from 'meilisearch' |
| 4 | +import { MeilisearchRetriever } from './core' |
| 5 | +import { flatten } from 'lodash' |
| 6 | +import { Document } from '@langchain/core/documents' |
| 7 | +import { v4 as uuidv4 } from 'uuid' |
| 8 | +import { Embeddings } from '@langchain/core/embeddings' |
| 9 | + |
| 10 | +class MeilisearchRetriever_node implements INode { |
| 11 | + label: string |
| 12 | + name: string |
| 13 | + version: number |
| 14 | + description: string |
| 15 | + type: string |
| 16 | + icon: string |
| 17 | + category: string |
| 18 | + baseClasses: string[] |
| 19 | + inputs: INodeParams[] |
| 20 | + credential: INodeParams |
| 21 | + badge: string |
| 22 | + outputs: INodeOutputsValue[] |
| 23 | + author?: string |
| 24 | + |
| 25 | + constructor() { |
| 26 | + this.label = 'Meilisearch' |
| 27 | + this.name = 'meilisearch' |
| 28 | + this.version = 1.0 |
| 29 | + this.type = 'Meilisearch' |
| 30 | + this.icon = 'Meilisearch.png' |
| 31 | + this.category = 'Vector Stores' |
| 32 | + this.badge = 'NEW' |
| 33 | + this.description = `Upsert embedded data and perform similarity search upon query using Meilisearch hybrid search functionality` |
| 34 | + this.baseClasses = ['BaseRetriever'] |
| 35 | + this.credential = { |
| 36 | + label: 'Connect Credential', |
| 37 | + name: 'credential', |
| 38 | + type: 'credential', |
| 39 | + credentialNames: ['meilisearchApi'] |
| 40 | + } |
| 41 | + this.inputs = [ |
| 42 | + { |
| 43 | + label: 'Document', |
| 44 | + name: 'document', |
| 45 | + type: 'Document', |
| 46 | + list: true, |
| 47 | + optional: true |
| 48 | + }, |
| 49 | + { |
| 50 | + label: 'Embeddings', |
| 51 | + name: 'embeddings', |
| 52 | + type: 'Embeddings' |
| 53 | + }, |
| 54 | + { |
| 55 | + label: 'Host', |
| 56 | + name: 'host', |
| 57 | + type: 'string', |
| 58 | + description: 'This is the URL for the desired Meilisearch instance' |
| 59 | + }, |
| 60 | + { |
| 61 | + label: 'Index Uid', |
| 62 | + name: 'indexUid', |
| 63 | + type: 'string', |
| 64 | + description: 'UID for the index to answer from' |
| 65 | + }, |
| 66 | + { |
| 67 | + label: 'Top K', |
| 68 | + name: 'K', |
| 69 | + type: 'number', |
| 70 | + description: 'number of top searches to return as context', |
| 71 | + additionalParams: true, |
| 72 | + optional: true |
| 73 | + }, |
| 74 | + { |
| 75 | + label: 'Semantic Ratio', |
| 76 | + name: 'semanticRatio', |
| 77 | + type: 'number', |
| 78 | + description: 'percentage of sematic reasoning in meilisearch hybrid search', |
| 79 | + additionalParams: true, |
| 80 | + optional: true |
| 81 | + } |
| 82 | + ] |
| 83 | + this.outputs = [ |
| 84 | + { |
| 85 | + label: 'Meilisearch Retriever', |
| 86 | + name: 'MeilisearchRetriever', |
| 87 | + description: 'retrieve answers', |
| 88 | + baseClasses: this.baseClasses |
| 89 | + } |
| 90 | + ] |
| 91 | + this.outputs = [ |
| 92 | + { |
| 93 | + label: 'Meilisearch Retriever', |
| 94 | + name: 'retriever', |
| 95 | + baseClasses: this.baseClasses |
| 96 | + } |
| 97 | + ] |
| 98 | + } |
| 99 | + //@ts-ignore |
| 100 | + vectorStoreMethods = { |
| 101 | + async upsert(nodeData: INodeData, options: ICommonObject): Promise<any> { |
| 102 | + const credentialData = await getCredentialData(nodeData.credential ?? '', options) |
| 103 | + const meilisearchAdminApiKey = getCredentialParam('meilisearchAdminApiKey', credentialData, nodeData) |
| 104 | + const docs = nodeData.inputs?.document as Document[] |
| 105 | + const host = nodeData.inputs?.host as string |
| 106 | + const indexUid = nodeData.inputs?.indexUid as string |
| 107 | + const embeddings = nodeData.inputs?.embeddings as Embeddings |
| 108 | + let embeddingDimension: number = 384 |
| 109 | + const client = new Meilisearch({ |
| 110 | + host: host, |
| 111 | + apiKey: meilisearchAdminApiKey |
| 112 | + }) |
| 113 | + const flattenDocs = docs && docs.length ? flatten(docs) : [] |
| 114 | + const finalDocs = [] |
| 115 | + for (let i = 0; i < flattenDocs.length; i += 1) { |
| 116 | + if (flattenDocs[i] && flattenDocs[i].pageContent) { |
| 117 | + const uniqueId = uuidv4() |
| 118 | + const { pageContent, metadata } = flattenDocs[i] |
| 119 | + const docEmbedding = await embeddings.embedQuery(pageContent) |
| 120 | + embeddingDimension = docEmbedding.length |
| 121 | + const documentForIndexing = { |
| 122 | + pageContent, |
| 123 | + metadata, |
| 124 | + objectID: uniqueId, |
| 125 | + _vectors: { |
| 126 | + ollama: { |
| 127 | + embeddings: docEmbedding, |
| 128 | + regenerate: false |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + finalDocs.push(documentForIndexing) |
| 133 | + } |
| 134 | + } |
| 135 | + let index: any |
| 136 | + try { |
| 137 | + index = await client.getIndex(indexUid) |
| 138 | + } catch (error) { |
| 139 | + console.error('Error fetching index:', error) |
| 140 | + await client.createIndex(indexUid, { primaryKey: 'objectID' }) |
| 141 | + } finally { |
| 142 | + index = await client.getIndex(indexUid) |
| 143 | + } |
| 144 | + |
| 145 | + try { |
| 146 | + await index.updateSettings({ |
| 147 | + embedders: { |
| 148 | + ollama: { |
| 149 | + source: 'userProvided', |
| 150 | + dimensions: embeddingDimension |
| 151 | + } |
| 152 | + } |
| 153 | + }) |
| 154 | + await index.addDocuments(finalDocs) |
| 155 | + } catch (error) { |
| 156 | + console.error('Error occurred while adding documents:', error) |
| 157 | + } |
| 158 | + return |
| 159 | + } |
| 160 | + } |
| 161 | + async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> { |
| 162 | + const credentialData = await getCredentialData(nodeData.credential ?? '', options) |
| 163 | + const meilisearchSearchApiKey = getCredentialParam('meilisearchSearchApiKey', credentialData, nodeData) |
| 164 | + const host = nodeData.inputs?.host as string |
| 165 | + const indexUid = nodeData.inputs?.indexUid as string |
| 166 | + const K = nodeData.inputs?.K as string |
| 167 | + const semanticRatio = nodeData.inputs?.semanticRatio as string |
| 168 | + const embeddings = nodeData.inputs?.embeddings as Embeddings |
| 169 | + |
| 170 | + const hybridsearchretriever = new MeilisearchRetriever(host, meilisearchSearchApiKey, indexUid, K, semanticRatio, embeddings) |
| 171 | + return hybridsearchretriever |
| 172 | + } |
| 173 | +} |
| 174 | +module.exports = { nodeClass: MeilisearchRetriever_node } |
0 commit comments