-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
98 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters