Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

Commit

Permalink
Ensure unique embeddings for a given file and model (#1638)
Browse files Browse the repository at this point in the history
  • Loading branch information
vishnukvmd authored Jan 6, 2024
2 parents ab1b3ec + 0fad642 commit 0da0df0
Show file tree
Hide file tree
Showing 7 changed files with 327 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lib/db/embeddings_db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ class EmbeddingsDB {

Future<void> put(Embedding embedding) {
return _isar.writeTxn(() async {
await _isar.embeddings.put(embedding);
await _isar.embeddings.putByIndex(Embedding.index, embedding);
Bus.instance.fire(EmbeddingUpdatedEvent());
});
}

Future<void> putMany(List<Embedding> embeddings) {
return _isar.writeTxn(() async {
await _isar.embeddings.putAll(embeddings);
await _isar.embeddings.putAllByIndex(Embedding.index, embeddings);
Bus.instance.fire(EmbeddingUpdatedEvent());
});
}
Expand Down
5 changes: 4 additions & 1 deletion lib/models/embedding.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ part 'embedding.g.dart';

@collection
class Embedding {
Id id = Isar.autoIncrement; // you can also use id = null to auto increment
static const index = 'unique_file_model_embedding';

Id id = Isar.autoIncrement;
final int fileID;
@enumerated
@Index(name: index, composite: [CompositeIndex('fileID')], unique: true, replace: true)
final Model model;
final List<double> embedding;
int? updationTime;
Expand Down
305 changes: 304 additions & 1 deletion lib/models/embedding.g.dart

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

5 changes: 5 additions & 0 deletions lib/services/semantic_search/embedding_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ class EmbeddingStore {
unawaited(_pushEmbedding(file, embedding));
}

Future<void> clearEmbeddings(Model model) async {
await EmbeddingsDB.instance.deleteAllForModel(model);
await _preferences.remove(kEmbeddingsSyncTimeKey);
}

Future<void> _pushEmbedding(EnteFile file, Embedding embedding) async {
final encryptionKey = getFileKey(file);
final embeddingJSON = jsonEncode(embedding.embedding);
Expand Down
Loading

0 comments on commit 0da0df0

Please sign in to comment.