Skip to content

Commit

Permalink
Coverage work on InMemoryEmbeddingStore. (langchain4j#520)
Browse files Browse the repository at this point in the history
  • Loading branch information
crutcher authored Jan 17, 2024
1 parent f7d9a04 commit 45f49e9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,16 @@ void should_add_embedding_with_segment_with_metadata() {
String id = embeddingStore().add(embedding, segment);
assertThat(id).isNotBlank();

{
// Not returned.
TextSegment altSegment = TextSegment.from("hello?");
Embedding altEmbedding = embeddingModel().embed(altSegment.text()).content();
embeddingStore().add(altEmbedding, segment);
}

awaitUntilPersisted();

List<EmbeddingMatch<TextSegment>> relevant = embeddingStore().findRelevant(embedding, 10);
List<EmbeddingMatch<TextSegment>> relevant = embeddingStore().findRelevant(embedding, 1);
assertThat(relevant).hasSize(1);

EmbeddingMatch<TextSegment> match = relevant.get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.util.concurrent.CopyOnWriteArrayList;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

class InMemoryEmbeddingStoreTest extends EmbeddingStoreIT {

Expand All @@ -38,15 +40,39 @@ void should_serialize_to_and_deserialize_from_json() {

@Test
void should_serialize_to_and_deserialize_from_file() {

InMemoryEmbeddingStore<TextSegment> originalEmbeddingStore = createEmbeddingStore();
Path filePath = temporaryDirectory.resolve("embedding-store.json");

originalEmbeddingStore.serializeToFile(filePath);
InMemoryEmbeddingStore<TextSegment> deserializedEmbeddingStore = InMemoryEmbeddingStore.fromFile(filePath);

assertThat(deserializedEmbeddingStore.entries).isEqualTo(originalEmbeddingStore.entries);
assertThat(deserializedEmbeddingStore.entries).isInstanceOf(CopyOnWriteArrayList.class);
assertThatExceptionOfType(RuntimeException.class)
.isThrownBy(() -> originalEmbeddingStore
.serializeToFile(temporaryDirectory.resolve("missing/store.json")))
.withCauseInstanceOf(NoSuchFileException.class);

assertThatExceptionOfType(RuntimeException.class)
.isThrownBy(() -> InMemoryEmbeddingStore
.fromFile(temporaryDirectory.resolve("missing/store.json")))
.withCauseInstanceOf(NoSuchFileException.class);
assertThatExceptionOfType(RuntimeException.class)
.isThrownBy(() -> InMemoryEmbeddingStore
.fromFile(temporaryDirectory.resolve("missing/store.json").toString()))
.withCauseInstanceOf(NoSuchFileException.class);

{
originalEmbeddingStore.serializeToFile(filePath);
InMemoryEmbeddingStore<TextSegment> deserializedEmbeddingStore = InMemoryEmbeddingStore.fromFile(filePath);

assertThat(deserializedEmbeddingStore.entries)
.isEqualTo(originalEmbeddingStore.entries)
.hasSameHashCodeAs(originalEmbeddingStore.entries);
assertThat(deserializedEmbeddingStore.entries).isInstanceOf(CopyOnWriteArrayList.class);
}
{
originalEmbeddingStore.serializeToFile(filePath.toString());
InMemoryEmbeddingStore<TextSegment> deserializedEmbeddingStore = InMemoryEmbeddingStore.fromFile(filePath);

assertThat(deserializedEmbeddingStore.entries).isEqualTo(originalEmbeddingStore.entries);
assertThat(deserializedEmbeddingStore.entries).isInstanceOf(CopyOnWriteArrayList.class);
}
}

private InMemoryEmbeddingStore<TextSegment> createEmbeddingStore() {
Expand Down

0 comments on commit 45f49e9

Please sign in to comment.