package ch.phoenixtechnologies.lc4j.workshop;

import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.rag.DefaultRetrievalAugmentor;
import dev.langchain4j.rag.RetrievalAugmentor;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.store.embedding.EmbeddingStore;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Produces;

public class RagRetriever {
    @Produces
    @ApplicationScoped
    public RetrievalAugmentor create(EmbeddingStore store, EmbeddingModel model) {
        var contentRetriever = EmbeddingStoreContentRetriever.builder()
                .embeddingModel(model)
                .embeddingStore(store)
                .maxResults(3)
                .build();

        return DefaultRetrievalAugmentor.builder()
                .contentRetriever(contentRetriever)
                .contentInjector((contents, userMessage) -> {
                    StringBuffer prompt = new StringBuffer(userMessage.singleText());
                    //prompt.append("\nPlease, only use the following information:\n");
                    contents.forEach(content -> prompt.append("- ")
                            .append(content.textSegment().text())
                            .append("\n"));
                    return new UserMessage(prompt.toString());
                })
                .build();
    }
}
