Class InMemoryEmbeddingStore<Embedded>

java.lang.Object
dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore<Embedded>
Type Parameters:
Embedded - The class of the object that has been embedded. Typically, it is TextSegment.
All Implemented Interfaces:
EmbeddingStore<Embedded>

public class InMemoryEmbeddingStore<Embedded> extends Object implements EmbeddingStore<Embedded>
An EmbeddingStore that stores embeddings in memory.

Uses a brute force approach by iterating over all embeddings to find the best matches.

This store can be persisted using the serializeToJson() and serializeToFile(Path) methods.

It can also be recreated from JSON or a file using the fromJson(String) and fromFile(Path) methods.

  • Constructor Details

    • InMemoryEmbeddingStore

      public InMemoryEmbeddingStore()
  • Method Details

    • add

      public String add(Embedding embedding)
      Description copied from interface: EmbeddingStore
      Adds a given embedding to the store.
      Specified by:
      add in interface EmbeddingStore<Embedded>
      Parameters:
      embedding - The embedding to be added to the store.
      Returns:
      The auto-generated ID associated with the added embedding.
    • add

      public void add(String id, Embedding embedding)
      Description copied from interface: EmbeddingStore
      Adds a given embedding to the store.
      Specified by:
      add in interface EmbeddingStore<Embedded>
      Parameters:
      id - The unique identifier for the embedding to be added.
      embedding - The embedding to be added to the store.
    • add

      public String add(Embedding embedding, Embedded embedded)
      Description copied from interface: EmbeddingStore
      Adds a given embedding and the corresponding content that has been embedded to the store.
      Specified by:
      add in interface EmbeddingStore<Embedded>
      Parameters:
      embedding - The embedding to be added to the store.
      embedded - Original content that was embedded.
      Returns:
      The auto-generated ID associated with the added embedding.
    • add

      public void add(String id, Embedding embedding, Embedded embedded)
    • addAll

      public List<String> addAll(List<Embedding> embeddings)
      Description copied from interface: EmbeddingStore
      Adds multiple embeddings to the store.
      Specified by:
      addAll in interface EmbeddingStore<Embedded>
      Parameters:
      embeddings - A list of embeddings to be added to the store.
      Returns:
      A list of auto-generated IDs associated with the added embeddings.
    • addAll

      public void addAll(List<String> ids, List<Embedding> embeddings, List<Embedded> embedded)
      Description copied from interface: EmbeddingStore
      Adds multiple embeddings and their corresponding contents that have been embedded to the store.
      Specified by:
      addAll in interface EmbeddingStore<Embedded>
      Parameters:
      ids - A list of IDs associated with the added embeddings.
      embeddings - A list of embeddings to be added to the store.
      embedded - A list of original contents that were embedded.
    • removeAll

      public void removeAll(Collection<String> ids)
      Description copied from interface: EmbeddingStore
      Removes all embeddings that match the specified IDs from the store.
      Specified by:
      removeAll in interface EmbeddingStore<Embedded>
      Parameters:
      ids - A collection of unique IDs of the embeddings to be removed.
    • removeAll

      public void removeAll(Filter filter)
      Removes all entries whose embedded object matches the given Filter.

      Filtering is applied only to entries whose embedded object is a TextSegment, by testing the filter against the segment's Metadata. Entries whose embedded object is null or is not a TextSegment are considered non-matching and are therefore never removed.

      Specified by:
      removeAll in interface EmbeddingStore<Embedded>
      Parameters:
      filter - The filter to be applied to the Metadata of the TextSegment during removal. Only embeddings whose TextSegment's Metadata match the Filter will be removed.
    • removeAll

      public void removeAll()
      Description copied from interface: EmbeddingStore
      Removes all embeddings from the store.
      Specified by:
      removeAll in interface EmbeddingStore<Embedded>
    • search

      public EmbeddingSearchResult<Embedded> search(EmbeddingSearchRequest embeddingSearchRequest)
      Searches for the embeddings most similar to the query embedding, optionally constrained by a Filter.

      When a filter is present, it is applied only to entries whose embedded object is a TextSegment, by testing the filter against the segment's Metadata. Entries whose embedded object is null or is not a TextSegment are considered non-matching and are therefore excluded from the results.

      When no filter is present, all entries are eligible regardless of their embedded object type.

      Specified by:
      search in interface EmbeddingStore<Embedded>
      Parameters:
      embeddingSearchRequest - A request to search in an EmbeddingStore. Contains all search criteria.
      Returns:
      An EmbeddingSearchResult containing all found Embeddings.
    • serializeToJson

      public String serializeToJson()
    • serializeToFile

      public void serializeToFile(Path filePath)
      Serializes this store to a file in JSON format. This method uses streaming serialization to avoid holding the entire JSON document in memory. Prefer this over serializeToJson()
    • serializeToFile

      public void serializeToFile(String filePath)
      See Also:
    • fromJson

      public static InMemoryEmbeddingStore<TextSegment> fromJson(String json)
    • fromFile

      public static InMemoryEmbeddingStore<TextSegment> fromFile(Path filePath)
      Deserializes an embedding store from a JSON file. Uses streaming deserialization to avoid loading the entire JSON document into memory.
    • fromFile

      public static InMemoryEmbeddingStore<TextSegment> fromFile(String filePath)
      See Also:
    • merge

      public static <Embedded> InMemoryEmbeddingStore<Embedded> merge(Collection<InMemoryEmbeddingStore<Embedded>> stores)
      Merges given InMemoryEmbeddingStores into a single InMemoryEmbeddingStore, copying all entries from each store.
    • merge

      public static <Embedded> InMemoryEmbeddingStore<Embedded> merge(InMemoryEmbeddingStore<Embedded> first, InMemoryEmbeddingStore<Embedded> second)
      Merges given InMemoryEmbeddingStores into a single InMemoryEmbeddingStore, copying all entries from each store.
    • size

      public int size()
    • isEmpty

      public boolean isEmpty()