代码使用如下

package com.example.springai.controller;

import org.springframework.ai.document.Document;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.embedding.EmbeddingResponse;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
class AIController {

    @Autowired VectorStore vectorStore;

    private final EmbeddingModel embeddingModel;

    @Autowired
    public AIController(EmbeddingModel embeddingModel) {
        this.embeddingModel = embeddingModel;
    }

    @GetMapping("/ai")
    public Map ai() {
        EmbeddingResponse embeddingResponse = this.embeddingModel.embedForResponse(List.of("春天的诗"));

        List<Document> documents = List.of(
                new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("meta1", "meta1")),
                new Document("The World is Big and Salvation Lurks Around the Corner"),
                new Document("You walk forward facing the past and you turn back toward the future.", Map.of("meta2", "meta2")));

        // Add the documents to PGVector 把文本写入到向量数据库
        vectorStore.add(documents);

        // Retrieve documents similar to a query 从向量数据库中查询
        List<Document> results = this.vectorStore.similaritySearch(SearchRequest.query("Spring").withTopK(8));
        results.forEach(System.out::println);
        return Map.of("embedding", embeddingResponse);
    }

}

 class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">

运行项目
访问接口:http://localhost:8080/ai

查询postgres是否入库
在这里插入图片描述

根据关键信息查询出来数据打印到控制台
在这里插入图片描述

后续可以结合Embedding模型把私有知识保存到向量数据库,建立私有知识库。如公司的公告文件,文档,手册,图片,视频等,这些比较隐私性的东西,不方便放公网大模型,所以建立私有知识库,私有知识库查询出来脱敏后结合大模型输出结构化消息。

id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"> class="blog_extension blog_extension_type1" id="blog_extension"> class="blog_extension_card" data-report-click="{"spm":"1001.2101.3001.6470"}"> class="blog_extension_card_left"> class="blog_extension_card_cont"> class="blog_extension_card_cont_l"> 阿亮说技术 class="blog_extension_card_cont_r"> 微信公众号 记录程序开发过程中的点点滴滴, 涉及:java
注:本文转载自blog.csdn.net的beyond阿亮的文章"https://blog.csdn.net/yinjl123456/article/details/143526832"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接

评论记录:

未查询到任何数据!