Skip to main content

In-memory

LangChain4j provides a simple in-memory implementation of an EmbeddingStore interface: InMemoryEmbeddingStore. It is useful for fast prototyping and simple use cases. It keeps Embeddings and associated TextSegments in memory. Search is also performed in memory. It can also be persisted and restored to/from a JSON string or a file.

Maven Dependency

<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j</artifactId>
<version>0.35.0</version>
</dependency>

APIs

  • InMemoryEmbeddingStore

Persisting

InMemoryEmbeddingStore can be serialized to a json string or a file:

InMemoryEmbeddingStore<TextSegment> embeddingStore = new InMemoryEmbeddingStore<>();
embeddingStore.addAll(embeddings, embedded);

String serializedStore = embeddingStore.serializeToJson();
InMemoryEmbeddingStore<TextSegment> deserializedStore = InMemoryEmbeddingStore.fromJson(serializedStore);

String filePath = "/home/me/store.json";
embeddingStore.serializeToFile(filePath);
InMemoryEmbeddingStore<TextSegment> deserializedStore = InMemoryEmbeddingStore.fromFile(filePath);

Examples