Skip to content

Commit

Permalink
feat(core): support chroma
Browse files Browse the repository at this point in the history
  • Loading branch information
LarchLiu committed Jul 1, 2023
1 parent f1d6005 commit da58789
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 3 deletions.
4 changes: 4 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,17 @@
},
"peerDependencies": {
"@supabase/supabase-js": "^2.26.0",
"chromadb": "^1.5.3",
"langchain": "^0.0.96",
"ofetch": "^1.1.1"
},
"peerDependenciesMeta": {
"@supabase/supabase-js": {
"optional": true
},
"chromadb": {
"optional": true
},
"langchain": {
"optional": true
},
Expand Down
64 changes: 64 additions & 0 deletions packages/core/src/storage/chroma/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Chroma } from 'langchain/vectorstores/chroma'
import { Document } from 'langchain/document'
import { RecursiveCharacterTextSplitter } from 'langchain/text_splitter'
import type { WebInfoData } from '../../types'
import { VectorStorage } from '../types'
import type { StorageType, VectorConfig } from '../types'

export interface ChromaVectorConfig extends VectorConfig {
url: string
}
export class ChromaVectorStorage extends VectorStorage<ChromaVectorConfig> {
constructor(config: ChromaVectorConfig, data?: WebInfoData) {
super(config, data)
}

async save(data?: WebInfoData) {
if (!data && !this.data)
throw new Error('VectorStorage error: No Storage Data')

const storageData = (data || this.data)!
const rawDoc = new Document({ pageContent: storageData.content, metadata: this.config.metaData })

/* Split text into chunks */
const textSplitter = new RecursiveCharacterTextSplitter({
chunkSize: 300,
chunkOverlap: 40,
})

const docs = await textSplitter.splitDocuments([rawDoc])
await Chroma.fromDocuments(docs, this.config.embeddingsInfo.embeddings, {
collectionName: this.config.embeddingsInfo.indexName,
url: this.config.url,
})
}

async getRetriever() {
const vectorStore = await Chroma.fromExistingCollection(
this.config.embeddingsInfo.embeddings,
{
collectionName: this.config.embeddingsInfo.indexName,
url: this.config.url,
// TODO: remove this eslint rule after langchainjs add filter options
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
filter: {
appName: this.config.metaData.appName,
botId: this.config.metaData.botId,
userId: this.config.metaData.userId,
},
})
return vectorStore.asRetriever()
}

getConfig(): ChromaVectorConfig {
return this.config
}

getType(): StorageType {
return {
type: 'VectorStorage',
name: 'ChromaVectorConfig',
}
}
}
2 changes: 2 additions & 0 deletions packages/core/src/storage/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { NotionDataStorage } from './notion'
import { SupabaseImageStorage, SupabaseVectorStorage } from './supabase'
import { ChromaVectorStorage } from './chroma'
import type { TStorage } from './types'

export * from './types'
Expand All @@ -20,5 +21,6 @@ export const storageInfo: StorageInfo = {
},
VectorStorage: {
SupabaseVectorStorage,
ChromaVectorStorage,
},
}
2 changes: 2 additions & 0 deletions packages/core/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ export default defineConfig({
external: [
'@supabase/supabase-js',
/langchain\/.*/,
'chromadb',
'ofetch',
],
output: {
// Provide global variables to use in the UMD build
// for externalized deps
globals: {
'@supabase/supabase-js': '@supabase/supabase-js',
'chromadb': 'chromadb',
'ofetch': 'ofetch',
},
},
Expand Down
17 changes: 14 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions server/nuxt3/composables/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,17 @@ export const vectorStorageConfig: BasicConfig<ModelsConfig> = {
},
output: '',
},
ChromaVectorStorage: {
displayName: 'Chroma Storage',
config: {
url: {
label: 'Chroma URL',
value: '',
require: true,
},
},
output: '',
},
},
}
export const llmConfig: BasicConfig<ModelsConfig> = {
Expand Down
1 change: 1 addition & 0 deletions server/nuxt3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@vue-flow/controls": "^1.1.0",
"@vue-flow/core": "^1.20.2",
"@vue-flow/minimap": "^1.1.1",
"chromadb": "^1.5.3",
"langchain": "^0.0.96",
"satori": "^0.10.1",
"satori-html": "^0.3.2"
Expand Down

0 comments on commit da58789

Please sign in to comment.