1:引入依赖

<properties>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <latest_version_here>1.0.0-beta3</latest_version_here>
</properties>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.2.0</version> <!-- 最新稳定版,可根据需求调整 -->
    <relativePath/> <!-- 从仓库获取,不使用本地路径 -->
</parent>
<dependencies>
    <!-- Web 开发 starter(最核心) -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- 测试 starter(必选,含 JUnit、MockMvc 等) -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope> <!-- 仅测试环境生效 -->
    </dependency>

    <!-- 2. MySQL 驱动(必选,运行时生效) -->
    <dependency>
        <groupId>com.mysql</groupId>
        <artifactId>mysql-connector-j</artifactId>
        <scope>runtime</scope>
    </dependency>

    <!-- 3. MyBatis-Plus 核心 Starter(必选,替代原生 MyBatis Starter) -->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.5.5</version> <!-- 最新稳定版,与 Spring Boot 3.x 兼容 -->
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>

    <dependency>
        <groupId>dev.langchain4j</groupId>
        <artifactId>langchain4j</artifactId>
        <version>${latest_version_here}</version>
    </dependency>

    <dependency>
        <groupId>dev.langchain4j</groupId>
        <artifactId>langchain4j-easy-rag</artifactId>
        <version>${latest_version_here}</version>
    </dependency>
    <dependency>
        <groupId>dev.langchain4j</groupId>
        <artifactId>langchain4j-community-dashscope-spring-boot-starter</artifactId>
        <version>${latest_version_here}</version>
    </dependency>

    <dependency>
        <groupId>dev.langchain4j</groupId>
        <artifactId>langchain4j-community-redis</artifactId>
        <version>${latest_version_here}</version>
    </dependency>
    <dependency>
        <groupId>dev.langchain4j</groupId>
        <artifactId>langchain4j-community-clickhouse</artifactId>
        <version>1.0.0-beta2</version>
    </dependency>`

2:

   public static void main(String[] args) {
       //对话模型
       ChatLanguageModel chatLanguageModel = QwenChatModel.builder()
               .apiKey("sk-4axxxx4")
               .modelName("qwen-turbo")
               .build();
       List<Document> documents = FileSystemDocumentLoader.loadDocuments("C:\\Users\\admin\\Desktop\\错误登记");
       // Second, let's create an assistant that will have access to our documents
       Assistant assistant = AiServices.builder(Assistant.class)
               .chatLanguageModel(chatLanguageModel) // it should use OpenAI LLM
               .chatMemory(MessageWindowChatMemory.withMaxMessages(10)) // it should remember 10 latest messages
               .contentRetriever(createContentRetriever(documents)) // it should have access to our documents
               .build();

       startConversationWith(assistant);
   }

private static ContentRetriever createContentRetriever(List<Document> documents) {
        // 1. 初始化嵌入模型(通义千问嵌入模型,核心!)
        EmbeddingModel embeddingModel = QwenEmbeddingModel.builder()
                .apiKey("sk-4a92fdaaa2bd440bb2e56111129adef4") // 和对话模型共用一个API Key即可
                .modelName("text-embedding-v2") // 嵌入模型名(不是对话模型!)
                .build();
        // 将元数据键映射到 ClickHouse 数据类型

        Map<String, ClickHouseDataType> metadataTypeMap = new HashMap<>();
        ClickHouseSettings settings = ClickHouseSettings.builder()
                .url("http://10.101.8.4:9123")
                .database("test_db")
                .table("doc_vector_table")
                .username("default")
                .password("zerody#test")
                .dimension(embeddingModel.dimension())
                .metadataTypeMap(metadataTypeMap)
                .build();
        Client allowExperimentalVectorSimilarityIndex = new Client.Builder()
                .addEndpoint(settings.getUrl())
                .setDefaultDatabase(settings.getDatabase())
                .setUsername(settings.getUsername())
                .setPassword(settings.getPassword())
                .serverSetting("allow_experimental_usearch_index", "1")
                .build();
        ClickHouseEmbeddingStore embeddingStore = ClickHouseEmbeddingStore.builder()
                .client(allowExperimentalVectorSimilarityIndex)
                .settings(settings)
                .build();


//        // Here, we create an empty in-memory store for our documents and their embeddings.
//        InMemoryEmbeddingStore<TextSegment> embeddingStore = new InMemoryEmbeddingStore<>();

        // Here, we are ingesting our documents into the store.
        // Under the hood, a lot of "magic" is happening, but we can ignore it for now.
        EmbeddingStoreIngestor.ingest(documents, embeddingStore);

        // Lastly, let's create a content retriever from an embedding store.
        return EmbeddingStoreContentRetriever.from(embeddingStore);
    }
public interface Assistant {

    String answer(String query);
}
@Slf4j
public class SharedUtils {
    public static void startConversationWith(Assistant assistant) {

        try (Scanner scanner = new Scanner(System.in)) {
            while (true) {
                log.info("==================================================");
                log.info("User: ");
                String userQuery = scanner.nextLine();
                log.info("==================================================");

                if ("exit".equalsIgnoreCase(userQuery)) {
                    break;
                }

                String agentAnswer = assistant.answer(userQuery);
                log.info("==================================================");
                log.info("Assistant: " + agentAnswer);
            }
        }
    }

    public static PathMatcher glob(String glob) {
        return FileSystems.getDefault().getPathMatcher("glob:" + glob);
    }

    public static Path toPath(String relativePath) {
        try {
            URL fileUrl = Utils.class.getClassLoader().getResource(relativePath);
            return Paths.get(fileUrl.toURI());
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }
}
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐