11import { flatten } from 'lodash'
2- import { Chroma } from '@langchain/community/vectorstores/chroma'
32import { Embeddings } from '@langchain/core/embeddings'
43import { Document } from '@langchain/core/documents'
54import { ICommonObject , INode , INodeData , INodeOutputsValue , INodeParams , IndexingResult } from '../../../src/Interface'
65import { getBaseClasses , getCredentialData , getCredentialParam , parseJsonBody } from '../../../src/utils'
7- import { ChromaExtended } from './core'
6+ import { Chroma } from './core'
87import { index } from '../../../src/indexing'
98
109class Chroma_VectorStores implements INode {
@@ -123,21 +122,11 @@ class Chroma_VectorStores implements INode {
123122 }
124123 }
125124
126- const obj : {
127- collectionName : string
128- url ?: string
129- chromaApiKey ?: string
130- chromaTenant ?: string
131- chromaDatabase ?: string
132- } = { collectionName }
133- if ( chromaURL ) obj . url = chromaURL
134- if ( chromaApiKey ) obj . chromaApiKey = chromaApiKey
135- if ( chromaTenant ) obj . chromaTenant = chromaTenant
136- if ( chromaDatabase ) obj . chromaDatabase = chromaDatabase
125+ const obj = _buildChromaConfig ( collectionName , chromaURL , chromaApiKey , chromaTenant , chromaDatabase )
137126
138127 try {
139128 if ( recordManager ) {
140- const vectorStore = await ChromaExtended . fromExistingCollection ( embeddings , obj )
129+ const vectorStore = await Chroma . fromExistingCollection ( embeddings , obj )
141130 await recordManager . createSchema ( )
142131 const res = await index ( {
143132 docsSource : finalDocs ,
@@ -151,7 +140,7 @@ class Chroma_VectorStores implements INode {
151140 } )
152141 return res
153142 } else {
154- await ChromaExtended . fromDocuments ( finalDocs , embeddings , obj )
143+ await Chroma . fromDocuments ( finalDocs , embeddings , obj )
155144 return { numAdded : finalDocs . length , addedDocs : finalDocs }
156145 }
157146 } catch ( e ) {
@@ -169,17 +158,7 @@ class Chroma_VectorStores implements INode {
169158 const chromaTenant = getCredentialParam ( 'chromaTenant' , credentialData , nodeData )
170159 const chromaDatabase = getCredentialParam ( 'chromaDatabase' , credentialData , nodeData )
171160
172- const obj : {
173- collectionName : string
174- url ?: string
175- chromaApiKey ?: string
176- chromaTenant ?: string
177- chromaDatabase ?: string
178- } = { collectionName }
179- if ( chromaURL ) obj . url = chromaURL
180- if ( chromaApiKey ) obj . chromaApiKey = chromaApiKey
181- if ( chromaTenant ) obj . chromaTenant = chromaTenant
182- if ( chromaDatabase ) obj . chromaDatabase = chromaDatabase
161+ const obj = _buildChromaConfig ( collectionName , chromaURL , chromaApiKey , chromaTenant , chromaDatabase )
183162
184163 try {
185164 if ( recordManager ) {
@@ -192,12 +171,12 @@ class Chroma_VectorStores implements INode {
192171 }
193172 const keys : string [ ] = await recordManager . listKeys ( filterKeys )
194173
195- const chromaStore = new ChromaExtended ( embeddings , obj )
174+ const chromaStore = new Chroma ( embeddings , obj )
196175
197176 await chromaStore . delete ( { ids : keys } )
198177 await recordManager . deleteKeys ( keys )
199178 } else {
200- const chromaStore = new ChromaExtended ( embeddings , obj )
179+ const chromaStore = new Chroma ( embeddings , obj )
201180 await chromaStore . delete ( { ids } )
202181 }
203182 } catch ( e ) {
@@ -220,24 +199,14 @@ class Chroma_VectorStores implements INode {
220199 const chromaDatabase = getCredentialParam ( 'chromaDatabase' , credentialData , nodeData )
221200 const chromaMetadataFilter = nodeData . inputs ?. chromaMetadataFilter
222201
223- const obj : {
224- collectionName : string
225- url ?: string
226- chromaApiKey ?: string
227- chromaTenant ?: string
228- chromaDatabase ?: string
229- filter ?: object | undefined
230- } = { collectionName }
231- if ( chromaURL ) obj . url = chromaURL
232- if ( chromaApiKey ) obj . chromaApiKey = chromaApiKey
233- if ( chromaTenant ) obj . chromaTenant = chromaTenant
234- if ( chromaDatabase ) obj . chromaDatabase = chromaDatabase
202+ const obj : ICommonObject = _buildChromaConfig ( collectionName , chromaURL , chromaApiKey , chromaTenant , chromaDatabase )
203+
235204 if ( chromaMetadataFilter ) {
236205 const metadatafilter = typeof chromaMetadataFilter === 'object' ? chromaMetadataFilter : parseJsonBody ( chromaMetadataFilter )
237206 obj . filter = metadatafilter
238207 }
239208
240- const vectorStore = await ChromaExtended . fromExistingCollection ( embeddings , obj )
209+ const vectorStore = await Chroma . fromExistingCollection ( embeddings , obj )
241210
242211 if ( output === 'retriever' ) {
243212 const retriever = vectorStore . asRetriever ( k )
@@ -253,4 +222,41 @@ class Chroma_VectorStores implements INode {
253222 }
254223}
255224
225+ const _buildChromaConfig = (
226+ collectionName : string ,
227+ chromaURL : string | undefined ,
228+ chromaApiKey : string | undefined ,
229+ chromaTenant : string | undefined ,
230+ chromaDatabase : string | undefined
231+ ) : ICommonObject => {
232+ const obj : {
233+ collectionName : string
234+ url ?: string
235+ chromaCloudAPIKey ?: string
236+ clientParams ?: {
237+ host ?: string
238+ port ?: number
239+ ssl ?: boolean
240+ tenant ?: string
241+ database ?: string
242+ }
243+ } = { collectionName }
244+
245+ if ( chromaURL ) obj . url = chromaURL
246+ if ( chromaApiKey ) obj . chromaCloudAPIKey = chromaApiKey
247+
248+ if ( chromaTenant || chromaDatabase ) {
249+ obj . clientParams = { }
250+ if ( chromaTenant ) obj . clientParams . tenant = chromaTenant
251+ if ( chromaDatabase ) obj . clientParams . database = chromaDatabase
252+ if ( chromaApiKey ) {
253+ obj . clientParams . host = 'api.trychroma.com'
254+ obj . clientParams . port = 8000
255+ obj . clientParams . ssl = true
256+ }
257+ }
258+
259+ return obj
260+ }
261+
256262module . exports = { nodeClass : Chroma_VectorStores }
0 commit comments