-
Notifications
You must be signed in to change notification settings - Fork 7
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
1 parent
cdb9846
commit 9dc35cf
Showing
13 changed files
with
289 additions
and
251 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
Empty file.
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
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 |
---|---|---|
@@ -1,52 +1,57 @@ | ||
import { DynamicModule, Global, Logger, Module } from '@nestjs/common'; | ||
import { Chroma } from '@langchain/community/vectorstores/chroma'; | ||
import { OpenAIEmbeddings } from '@langchain/openai'; | ||
import { TogetherAIEmbeddings } from '@langchain/community/embeddings/togetherai'; | ||
|
||
@Global() | ||
@Module({}) | ||
export class ChromaModule { | ||
static register (apiKey: string): DynamicModule { | ||
return { | ||
module: ChromaModule, | ||
global: true, | ||
providers: [ | ||
{ | ||
provide: 'CHROMA_DB', | ||
useFactory: async (): Promise<Chroma> => { | ||
try { | ||
static register(apiKey: string): DynamicModule { | ||
return { | ||
module: ChromaModule, | ||
global: true, | ||
providers: [ | ||
{ | ||
provide: 'CHROMA_DB', | ||
useFactory: async (): Promise<Chroma> => { | ||
try { | ||
if (!process.env.OPENAI_API_KEY) | ||
throw new Error('OpenAI API key is required'); | ||
|
||
if (!process.env.OPENAI_API_KEY) throw new Error('OpenAI API key is required'); | ||
|
||
Logger.debug(process.env.CHROMA_URL, 'ChromaModule'); | ||
Logger.debug(process.env.CHROMA_COLLECTION_NAME, 'ChromaModule'); | ||
Logger.debug(process.env.CHROMA_URL, 'ChromaModule'); | ||
Logger.debug(process.env.CHROMA_COLLECTION_NAME, 'ChromaModule'); | ||
|
||
const vectorStore = await Chroma.fromExistingCollection( | ||
new OpenAIEmbeddings({ | ||
openAIApiKey: apiKey, | ||
modelName: process.env.MODEL_EMBEDDING, | ||
}), | ||
{ | ||
collectionName: process.env.CHROMA_COLLECTION_NAME, | ||
url: process.env.CHROMA_URL, | ||
} | ||
); | ||
|
||
const ensure = await vectorStore.ensureCollection(); | ||
Logger.debug(JSON.stringify(ensure), 'ChromaModule:ensureCollection'); | ||
const vectorStore = await Chroma.fromExistingCollection( | ||
new TogetherAIEmbeddings({ | ||
apiKey: process.env.TOGETHER_AI_API_KEY, | ||
modelName: process.env.MODEL_EMBEDDING, | ||
}), | ||
{ | ||
collectionName: process.env.CHROMA_COLLECTION_NAME, | ||
url: process.env.CHROMA_URL, | ||
}, | ||
); | ||
|
||
const docCount = await vectorStore.collection.count(); | ||
Logger.debug(`Loaded ${docCount} documents from ChromaDB`, 'ChromaModule'); | ||
const ensure = await vectorStore.ensureCollection(); | ||
Logger.debug( | ||
JSON.stringify(ensure), | ||
'ChromaModule:ensureCollection', | ||
); | ||
|
||
return vectorStore; | ||
|
||
} catch (e) { | ||
Logger.error('ChromaDB connection failure:', e, 'ChromaDBModule'); | ||
throw new Error('CHROMADB_CONNECTION_FAILURE'); | ||
} | ||
}, | ||
}, | ||
], | ||
exports: ['CHROMA_DB'], | ||
}; | ||
} | ||
} | ||
const docCount = await vectorStore.collection.count(); | ||
Logger.debug( | ||
`Loaded ${docCount} documents from ChromaDB`, | ||
'ChromaModule', | ||
); | ||
|
||
return vectorStore; | ||
} catch (e) { | ||
Logger.error('ChromaDB connection failure:', e, 'ChromaDBModule'); | ||
throw new Error('CHROMADB_CONNECTION_FAILURE'); | ||
} | ||
}, | ||
}, | ||
], | ||
exports: ['CHROMA_DB'], | ||
}; | ||
} | ||
} |
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
Oops, something went wrong.