Skip to content

Way to write out embeddings to a file? #663

@edeandrea

Description

@edeandrea

Discussed in #643

Originally posted by edeandrea May 30, 2024
I'm using the easy-rag extension which loads a pdf file at startup (in dev mode) to an embedding model which is stored in redis.

Is there a way that I could write out the embeddings to a file rather than re-computing the embeddings? I'm using ollama with mistral, so my dev mode startup is about 10-15 minutes, and then 10-15 mins each time I make a change while in dev mode.

If it were to write out the embeddings to a file the first time, then each subsequent time it would only need to load the embeddings into the store, that would be much faster, especially since (in my case) the file isn't changing.

It actually already is there in upstream, at least when using the in memory embedding store. See https://github.com/langchain4j/langchain4j/blob/eba2b1cd500f7b8874d4da5e406f09935febb9b3/langchain4j/src/main/java/dev/langchain4j/store/embedding/inmemory/InMemoryEmbeddingStore.java#L132-L164

I think all we'd need here in the short term would be some stuff in the easy rag extension that did something with these methods.

What I'm thinking is that, for now, we only support it for in memory embeddings. We introduce these properties

# Whether or not to turn on this feature
# Default is false, preserving existing functionality
quarkus.langchain4j.easy-rag.reuse-embeddings.enabled=false|true

# Path to where to store/read the embeddings file
# Must be set to something if quarkus.langchain4j.easy-rag.reuse-embeddings.enabled==true
quarkus.langchain4j.easy-rag.reuse-embeddings.file=<some file path>

If quarkus.langchain4j.easy-rag.reuse-embeddings.enabled==false then there won't be any change to existing functionality.

If quarkus.langchain4j.easy-rag.reuse-embeddings.enabled==true, then on startup quarkus.langchain4j.easy-rag.reuse-embeddings.file will be examined. If it exists, then the embedding store will be loaded from it (rather than querying the EmbeddingModel). If it doesn't exist, then the embeddings will be loaded as they are today, and then they will be written out to that file, so that during a restart the EmbeddingModel doesn't need to be re-queried.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions