|
| 1 | +import { flatten } from 'lodash' |
| 2 | +import { Chroma } from 'langchain/vectorstores/chroma' |
| 3 | +import { Embeddings } from 'langchain/embeddings/base' |
| 4 | +import { Document } from 'langchain/document' |
| 5 | +import { ICommonObject, INode, INodeData, INodeOutputsValue, INodeParams } from '../../../src/Interface' |
| 6 | +import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils' |
| 7 | +import { ChromaExtended } from './core' |
| 8 | + |
| 9 | +class Chroma_VectorStores implements INode { |
| 10 | + label: string |
| 11 | + name: string |
| 12 | + version: number |
| 13 | + description: string |
| 14 | + type: string |
| 15 | + icon: string |
| 16 | + category: string |
| 17 | + badge: string |
| 18 | + baseClasses: string[] |
| 19 | + inputs: INodeParams[] |
| 20 | + credential: INodeParams |
| 21 | + outputs: INodeOutputsValue[] |
| 22 | + |
| 23 | + constructor() { |
| 24 | + this.label = 'Chroma' |
| 25 | + this.name = 'chroma' |
| 26 | + this.version = 1.0 |
| 27 | + this.type = 'Chroma' |
| 28 | + this.icon = 'chroma.svg' |
| 29 | + this.category = 'Vector Stores' |
| 30 | + this.description = 'Upsert embedded data and perform similarity search upon query using Chroma, an open-source embedding database' |
| 31 | + this.baseClasses = [this.type, 'VectorStoreRetriever', 'BaseRetriever'] |
| 32 | + this.badge = 'NEW' |
| 33 | + this.credential = { |
| 34 | + label: 'Connect Credential', |
| 35 | + name: 'credential', |
| 36 | + type: 'credential', |
| 37 | + description: 'Only needed if you have chroma on cloud services with X-Api-key', |
| 38 | + optional: true, |
| 39 | + credentialNames: ['chromaApi'] |
| 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: 'Collection Name', |
| 56 | + name: 'collectionName', |
| 57 | + type: 'string' |
| 58 | + }, |
| 59 | + { |
| 60 | + label: 'Chroma URL', |
| 61 | + name: 'chromaURL', |
| 62 | + type: 'string', |
| 63 | + optional: true |
| 64 | + }, |
| 65 | + { |
| 66 | + label: 'Chroma Metadata Filter', |
| 67 | + name: 'chromaMetadataFilter', |
| 68 | + type: 'json', |
| 69 | + optional: true, |
| 70 | + additionalParams: true |
| 71 | + }, |
| 72 | + { |
| 73 | + label: 'Top K', |
| 74 | + name: 'topK', |
| 75 | + description: 'Number of top results to fetch. Default to 4', |
| 76 | + placeholder: '4', |
| 77 | + type: 'number', |
| 78 | + additionalParams: true, |
| 79 | + optional: true |
| 80 | + } |
| 81 | + ] |
| 82 | + this.outputs = [ |
| 83 | + { |
| 84 | + label: 'Chroma Retriever', |
| 85 | + name: 'retriever', |
| 86 | + baseClasses: this.baseClasses |
| 87 | + }, |
| 88 | + { |
| 89 | + label: 'Chroma Vector Store', |
| 90 | + name: 'vectorStore', |
| 91 | + baseClasses: [this.type, ...getBaseClasses(Chroma)] |
| 92 | + } |
| 93 | + ] |
| 94 | + } |
| 95 | + |
| 96 | + //@ts-ignore |
| 97 | + vectorStoreMethods = { |
| 98 | + async upsert(nodeData: INodeData, options: ICommonObject): Promise<void> { |
| 99 | + const collectionName = nodeData.inputs?.collectionName as string |
| 100 | + const docs = nodeData.inputs?.document as Document[] |
| 101 | + const embeddings = nodeData.inputs?.embeddings as Embeddings |
| 102 | + const chromaURL = nodeData.inputs?.chromaURL as string |
| 103 | + |
| 104 | + const credentialData = await getCredentialData(nodeData.credential ?? '', options) |
| 105 | + const chromaApiKey = getCredentialParam('chromaApiKey', credentialData, nodeData) |
| 106 | + |
| 107 | + const flattenDocs = docs && docs.length ? flatten(docs) : [] |
| 108 | + const finalDocs = [] |
| 109 | + for (let i = 0; i < flattenDocs.length; i += 1) { |
| 110 | + if (flattenDocs[i] && flattenDocs[i].pageContent) { |
| 111 | + finalDocs.push(new Document(flattenDocs[i])) |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + const obj: { |
| 116 | + collectionName: string |
| 117 | + url?: string |
| 118 | + chromaApiKey?: string |
| 119 | + } = { collectionName } |
| 120 | + if (chromaURL) obj.url = chromaURL |
| 121 | + if (chromaApiKey) obj.chromaApiKey = chromaApiKey |
| 122 | + |
| 123 | + try { |
| 124 | + await ChromaExtended.fromDocuments(finalDocs, embeddings, obj) |
| 125 | + } catch (e) { |
| 126 | + throw new Error(e) |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> { |
| 132 | + const collectionName = nodeData.inputs?.collectionName as string |
| 133 | + const embeddings = nodeData.inputs?.embeddings as Embeddings |
| 134 | + const chromaURL = nodeData.inputs?.chromaURL as string |
| 135 | + const output = nodeData.outputs?.output as string |
| 136 | + const topK = nodeData.inputs?.topK as string |
| 137 | + const k = topK ? parseFloat(topK) : 4 |
| 138 | + |
| 139 | + const credentialData = await getCredentialData(nodeData.credential ?? '', options) |
| 140 | + const chromaApiKey = getCredentialParam('chromaApiKey', credentialData, nodeData) |
| 141 | + |
| 142 | + const chromaMetadataFilter = nodeData.inputs?.chromaMetadataFilter |
| 143 | + |
| 144 | + const obj: { |
| 145 | + collectionName: string |
| 146 | + url?: string |
| 147 | + chromaApiKey?: string |
| 148 | + filter?: object | undefined |
| 149 | + } = { collectionName } |
| 150 | + if (chromaURL) obj.url = chromaURL |
| 151 | + if (chromaApiKey) obj.chromaApiKey = chromaApiKey |
| 152 | + if (chromaMetadataFilter) { |
| 153 | + const metadatafilter = typeof chromaMetadataFilter === 'object' ? chromaMetadataFilter : JSON.parse(chromaMetadataFilter) |
| 154 | + obj.filter = metadatafilter |
| 155 | + } |
| 156 | + |
| 157 | + const vectorStore = await ChromaExtended.fromExistingCollection(embeddings, obj) |
| 158 | + |
| 159 | + if (output === 'retriever') { |
| 160 | + const retriever = vectorStore.asRetriever(k) |
| 161 | + return retriever |
| 162 | + } else if (output === 'vectorStore') { |
| 163 | + ;(vectorStore as any).k = k |
| 164 | + return vectorStore |
| 165 | + } |
| 166 | + return vectorStore |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +module.exports = { nodeClass: Chroma_VectorStores } |
0 commit comments