-
Notifications
You must be signed in to change notification settings - Fork 196
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
20 changed files
with
386 additions
and
169 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
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
2 changes: 1 addition & 1 deletion
2
apps/webapp/src/components/Configuration/RAGSettings/RAGSettingsPage.tsx
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
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
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
47 changes: 25 additions & 22 deletions
47
packages/api/src/@core/rag/vecdb/chromadb/chromadb.service.ts
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,55 @@ | ||
import { EnvironmentService } from '@@core/@core-services/environment/environment.service'; | ||
import { ProcessedChunk } from '@@core/rag/types'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { ChromaClient } from 'chromadb'; | ||
import { ChromaClient, Collection } from 'chromadb'; | ||
|
||
@Injectable() | ||
export class ChromaDBService { | ||
private client: ChromaClient; | ||
private collection: Collection; | ||
|
||
constructor(private envService: EnvironmentService) { | ||
//this.initialize(); | ||
constructor(private envService: EnvironmentService) {} | ||
|
||
async onModuleInit() { | ||
return; | ||
} | ||
|
||
async initialize() { | ||
async initialize(credentials: string[]) { | ||
this.client = new ChromaClient({ | ||
path: this.envService.getChromaCreds(), | ||
path: credentials[0], | ||
}); | ||
this.collection = await this.client.getOrCreateCollection({ | ||
name: credentials[1], | ||
}); | ||
} | ||
|
||
async storeEmbeddings( | ||
fileId: string, | ||
chunks: ProcessedChunk[], | ||
embeddings: number[][], | ||
linkedUserId: string, | ||
) { | ||
const collection = await this.client.createCollection({ name: fileId }); | ||
await collection.add({ | ||
await this.collection.add({ | ||
ids: chunks.map((_, i) => `${fileId}_${i}`), | ||
embeddings: embeddings, | ||
metadatas: chunks.map((chunk) => ({ | ||
text: chunk.text, | ||
...chunk.metadata, | ||
user_id: `ns_${linkedUserId}`, | ||
})), | ||
}); | ||
} | ||
|
||
async queryEmbeddings(queryEmbedding: number[], topK: number) { | ||
const collections = await this.client.listCollections(); | ||
const results = await Promise.all( | ||
collections.map(async (collection) => { | ||
const collectionInstance = await this.client.getCollection({ | ||
name: collection.name, | ||
}); | ||
const result = await collectionInstance.query({ | ||
queryEmbeddings: [queryEmbedding], | ||
nResults: topK, | ||
}); | ||
return result.metadatas[0]; | ||
}), | ||
); | ||
return results.flat().slice(0, topK); | ||
async queryEmbeddings( | ||
queryEmbedding: number[], | ||
topK: number, | ||
linkedUserId: string, | ||
) { | ||
const result = await this.collection.query({ | ||
queryEmbeddings: [queryEmbedding], | ||
nResults: topK, | ||
where: { user_id: `ns_${linkedUserId}` }, | ||
}); | ||
return result.metadatas[0]; | ||
} | ||
} |
Oops, something went wrong.