package com.iwe3.langchain4j;

import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import dev.langchain4j.data.document.parser.TextDocumentParser;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.nio.file.FileSystems;

@SpringBootTest
public class RagUploadTest {

    @Autowired
    private EmbeddingStore embeddingStore;

    @Autowired
    private EmbeddingModel embeddingModel;

    @Test
    public void testUploadKnowledgeLibrary() {
        // 使用FileSystemDocumentLoader读取指定目录下的知识库文档
        var pathMatcher = FileSystems.getDefault()
                .getPathMatcher("glob:*\\.md");
        var documents = FileSystemDocumentLoader
                .loadDocuments("D:/documents", pathMatcher,
                        new TextDocumentParser());

        // 文本向量化并存入向量数据库：将每个片段进行向量化，得到一个嵌入向量
        EmbeddingStoreIngestor
                .builder()
                .embeddingStore(embeddingStore)
                .embeddingModel(embeddingModel)
                .build()
                .ingest(documents);
    }
}
