|
| 1 | +import { INode, INodeData, INodeOutputsValue, INodeParams } from '../../../src/Interface' |
| 2 | +import { Embeddings } from 'langchain/embeddings/base' |
| 3 | +import { Document } from 'langchain/document' |
| 4 | +import { getBaseClasses } from '../../../src/utils' |
| 5 | +import { SingleStoreVectorStore, SingleStoreVectorStoreConfig } from 'langchain/vectorstores/singlestore' |
| 6 | +import { flatten } from 'lodash' |
| 7 | + |
| 8 | +class SingleStoreUpsert_VectorStores implements INode { |
| 9 | + label: string |
| 10 | + name: string |
| 11 | + description: string |
| 12 | + type: string |
| 13 | + icon: string |
| 14 | + category: string |
| 15 | + baseClasses: string[] |
| 16 | + inputs: INodeParams[] |
| 17 | + outputs: INodeOutputsValue[] |
| 18 | + |
| 19 | + constructor() { |
| 20 | + this.label = 'SingleStore Upsert Document' |
| 21 | + this.name = 'singlestoreUpsert' |
| 22 | + this.type = 'SingleStore' |
| 23 | + this.icon = 'singlestore.svg' |
| 24 | + this.category = 'Vector Stores' |
| 25 | + this.description = 'Upsert documents to SingleStore' |
| 26 | + this.baseClasses = [this.type, 'VectorStoreRetriever', 'BaseRetriever'] |
| 27 | + this.inputs = [ |
| 28 | + { |
| 29 | + label: 'Document', |
| 30 | + name: 'document', |
| 31 | + type: 'Document', |
| 32 | + list: true |
| 33 | + }, |
| 34 | + { |
| 35 | + label: 'Embeddings', |
| 36 | + name: 'embeddings', |
| 37 | + type: 'Embeddings' |
| 38 | + }, |
| 39 | + { |
| 40 | + label: 'Host', |
| 41 | + name: 'host', |
| 42 | + type: 'string' |
| 43 | + }, |
| 44 | + { |
| 45 | + label: 'User', |
| 46 | + name: 'user', |
| 47 | + type: 'string' |
| 48 | + }, |
| 49 | + { |
| 50 | + label: 'Password', |
| 51 | + name: 'password', |
| 52 | + type: 'password' |
| 53 | + }, |
| 54 | + { |
| 55 | + label: 'Database', |
| 56 | + name: 'database', |
| 57 | + type: 'string' |
| 58 | + }, |
| 59 | + { |
| 60 | + label: 'Table Name', |
| 61 | + name: 'tableName', |
| 62 | + type: 'string', |
| 63 | + placeholder: 'embeddings', |
| 64 | + additionalParams: true, |
| 65 | + optional: true |
| 66 | + }, |
| 67 | + { |
| 68 | + label: 'Content Column Name', |
| 69 | + name: 'contentColumnName', |
| 70 | + type: 'string', |
| 71 | + placeholder: 'content', |
| 72 | + additionalParams: true, |
| 73 | + optional: true |
| 74 | + }, |
| 75 | + { |
| 76 | + label: 'Vector Column Name', |
| 77 | + name: 'vectorColumnName', |
| 78 | + type: 'string', |
| 79 | + placeholder: 'vector', |
| 80 | + additionalParams: true, |
| 81 | + optional: true |
| 82 | + }, |
| 83 | + { |
| 84 | + label: 'Metadata Column Name', |
| 85 | + name: 'metadataColumnName', |
| 86 | + type: 'string', |
| 87 | + placeholder: 'metadata', |
| 88 | + additionalParams: true, |
| 89 | + optional: true |
| 90 | + }, |
| 91 | + { |
| 92 | + label: 'Top K', |
| 93 | + name: 'topK', |
| 94 | + placeholder: '4', |
| 95 | + type: 'number', |
| 96 | + additionalParams: true, |
| 97 | + optional: true |
| 98 | + } |
| 99 | + ] |
| 100 | + this.outputs = [ |
| 101 | + { |
| 102 | + label: 'SingleStore Retriever', |
| 103 | + name: 'retriever', |
| 104 | + baseClasses: this.baseClasses |
| 105 | + }, |
| 106 | + { |
| 107 | + label: 'SingleStore Vector Store', |
| 108 | + name: 'vectorStore', |
| 109 | + baseClasses: [this.type, ...getBaseClasses(SingleStoreVectorStore)] |
| 110 | + } |
| 111 | + ] |
| 112 | + } |
| 113 | + |
| 114 | + async init(nodeData: INodeData): Promise<any> { |
| 115 | + const singleStoreConnectionConfig = { |
| 116 | + connectionOptions: { |
| 117 | + host: nodeData.inputs?.host as string, |
| 118 | + port: 3306, |
| 119 | + user: nodeData.inputs?.user as string, |
| 120 | + password: nodeData.inputs?.password as string, |
| 121 | + database: nodeData.inputs?.database as string |
| 122 | + }, |
| 123 | + ...(nodeData.inputs?.tableName ? { tableName: nodeData.inputs.tableName as string } : {}), |
| 124 | + ...(nodeData.inputs?.contentColumnName ? { contentColumnName: nodeData.inputs.contentColumnName as string } : {}), |
| 125 | + ...(nodeData.inputs?.vectorColumnName ? { vectorColumnName: nodeData.inputs.vectorColumnName as string } : {}), |
| 126 | + ...(nodeData.inputs?.metadataColumnName ? { metadataColumnName: nodeData.inputs.metadataColumnName as string } : {}) |
| 127 | + } as SingleStoreVectorStoreConfig |
| 128 | + |
| 129 | + const docs = nodeData.inputs?.document as Document[] |
| 130 | + const embeddings = nodeData.inputs?.embeddings as Embeddings |
| 131 | + const output = nodeData.outputs?.output as string |
| 132 | + const topK = nodeData.inputs?.topK as string |
| 133 | + const k = topK ? parseInt(topK, 10) : 4 |
| 134 | + |
| 135 | + const flattenDocs = docs && docs.length ? flatten(docs) : [] |
| 136 | + const finalDocs = [] |
| 137 | + for (let i = 0; i < flattenDocs.length; i += 1) { |
| 138 | + finalDocs.push(new Document(flattenDocs[i])) |
| 139 | + } |
| 140 | + |
| 141 | + let vectorStore: SingleStoreVectorStore |
| 142 | + |
| 143 | + vectorStore = new SingleStoreVectorStore(embeddings, singleStoreConnectionConfig) |
| 144 | + vectorStore.addDocuments.bind(vectorStore)(finalDocs) |
| 145 | + |
| 146 | + if (output === 'retriever') { |
| 147 | + const retriever = vectorStore.asRetriever(k) |
| 148 | + return retriever |
| 149 | + } else if (output === 'vectorStore') { |
| 150 | + ;(vectorStore as any).k = k |
| 151 | + return vectorStore |
| 152 | + } |
| 153 | + return vectorStore |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +module.exports = { nodeClass: SingleStoreUpsert_VectorStores } |
0 commit comments