import dev.langchain4j.data.embedding.Embedding;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.model.embedding.AllMiniLmL6V2EmbeddingModel;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.store.embedding.EmbeddingMatch;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.milvus.MilvusEmbeddingStore;

import java.util.List;

public class MilvusEmbeddingStoreExample {

    /**
     * First run Milvus locally:
     * Run "docker compose up -d" inside "../resources" directory.
     * If you want to create a fresh Milvus instance, don't forget to remove "../resources/volumes" directory.
     */

    public static void main(String[] args) {

        EmbeddingStore<TextSegment> embeddingStore = MilvusEmbeddingStore.builder()
                .host("localhost")
                .port(19530)
                .dimension(384)
                .build();

        EmbeddingModel embeddingModel = new AllMiniLmL6V2EmbeddingModel();

        TextSegment segment1 = TextSegment.from("I like football.");
        Embedding embedding1 = embeddingModel.embed(segment1).content();
        embeddingStore.add(embedding1, segment1);

        TextSegment segment2 = TextSegment.from("The weather is good today.");
        Embedding embedding2 = embeddingModel.embed(segment2).content();
        embeddingStore.add(embedding2, segment2);

        Embedding queryEmbedding = embeddingModel.embed("What is your favourite sport?").content();
        List<EmbeddingMatch<TextSegment>> relevant = embeddingStore.findRelevant(queryEmbedding, 1);
        EmbeddingMatch<TextSegment> embeddingMatch = relevant.get(0);

        System.out.println(embeddingMatch.score()); // 0.8144287765026093
        System.out.println(embeddingMatch.embedded().text()); // I like football.
    }
}

[((795, 946), 'dev.langchain4j.store.embedding.milvus.MilvusEmbeddingStore.builder'), ((795, 921), 'dev.langchain4j.store.embedding.milvus.MilvusEmbeddingStore.builder'), ((795, 889), 'dev.langchain4j.store.embedding.milvus.MilvusEmbeddingStore.builder'), ((795, 860), 'dev.langchain4j.store.embedding.milvus.MilvusEmbeddingStore.builder')]