自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

SpringAI更新:向量數(shù)據(jù)庫不可用的解決方案!

數(shù)據(jù)庫 其他數(shù)據(jù)庫
雖然 SimpleVectorStore 不支持了,但 Spring AI 內(nèi)置了 Redis 或 ES 作為向量數(shù)據(jù)庫的分布式存儲(chǔ)中間件,我們可以用他們來進(jìn)行向量的存儲(chǔ)。

Spring AI 前兩天(4.10 日)更新了 1.0.0-M7 版本后,原來的 SimpleVectorStore 內(nèi)存級(jí)別的向量數(shù)據(jù)庫就不能用了,Spring AI 將其全部源碼刪除了。

此時(shí)我們就需要一種成本更低的解決方案來解決這個(gè)問題,如何解決呢?我們一起來看。

解決方案:Redis 向量數(shù)據(jù)庫

雖然 SimpleVectorStore 不支持了,但 Spring AI 內(nèi)置了 Redis 或 ES 作為向量數(shù)據(jù)庫的分布式存儲(chǔ)中間件,我們可以用他們來進(jìn)行向量的存儲(chǔ)。

而在這兩種方案中,顯然 Redis 使用成本更低,因此,我們來看如何將向量存儲(chǔ)到 Redis 數(shù)據(jù)庫中。

它的具體實(shí)現(xiàn)步驟如下。

安裝Redis-Stack

  1. 下載 Docker Hub:https://www.docker.com/get-started/

圖片圖片

  1. 安裝 redis-stack-server:使用“docker run -d --name redis-stack-server -p 6379:6379 redis/redis-stack-server”。

添加依賴

我們使用阿里云百煉平臺(tái)的嵌入模型 text-embedding-v3 是兼容 OpenAI 的 SDK 的,因此,我們需要添加 OpenAI 和 Redis Vector 依賴:

<dependency>
  <groupId>org.springframework.ai</groupId>
  <artifactId>spring-ai-starter-vector-store-redis</artifactId>
</dependency>

<dependency>
  <groupId>org.springframework.ai</groupId>
  <artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>

設(shè)置配置信息

配置 Redis 連接信息,以及嵌入模型的配置信息:

spring:
  data:
    redis:
      host: localhost
      port: 6379
  ai:
    vectorstore:
      redis:
        initialize-schema: true
        index-name: custom-index
        prefix: custom-prefix
    openai:
      api-key: ${ALIYUN-AK}
      embedding:
        options:
          model: text-embedding-v3

阿里云百煉平臺(tái)支持的向量模型:

圖片圖片

代碼實(shí)現(xiàn)

Redis 添加向量數(shù)據(jù)

@Autowired
private VectorStore vectorStore;

// 構(gòu)建數(shù)據(jù)
List<Document> documents =
List.of(new Document("I like Spring Boot"),
        new Document("I love Java"));
// 添加到向量數(shù)據(jù)庫
vectorStore.add(documents);

當(dāng)然,向量數(shù)據(jù)的數(shù)據(jù)源可以是文件、圖片、音頻等資源,這里為了簡單演示整體執(zhí)行流程,使用了更簡單直觀的文本作為數(shù)據(jù)源。

VectorStore 提供的常用方法如下:

  • add(Listdocuments):添加文檔。
  • delete(ListidList):按 ID 刪除文檔。
  • delete(Filter.Expression filterExpression):按過濾表達(dá)式刪除文檔。
  • similaritySearch(String query) 和 similaritySearch(SearchRequest request):相似性搜索。

執(zhí)行結(jié)果如下:

圖片圖片

查詢向量數(shù)據(jù)

@RestController
@RequestMapping("/vector")
public class VectorController {

    @Resource
    private VectorStore vectorStore;

    @RequestMapping("/find")
    public List find(@RequestParam String query) {
        // 構(gòu)建搜索請(qǐng)求,設(shè)置查詢文本和返回的文檔數(shù)量
        SearchRequest request = SearchRequest.builder()
        .query(query)
        .topK(3)
        .build();
        List<Document> result = vectorStore.similaritySearch(request);
        System.out.println(result);
        return result;
    }
}

執(zhí)行結(jié)果如下:

圖片圖片

從上述結(jié)果可以看出,和“java”相似度最高的向量為“I love Java”,相似度評(píng)分為 0.77,如果我們 SearchRequest 對(duì)象中的 topK 設(shè)置為 1 的話,只會(huì)查詢“I love Java”這條數(shù)據(jù),如下圖所示:

圖片圖片


責(zé)任編輯:武曉燕 來源: 磊哥和Java
相關(guān)推薦

2011-07-14 09:31:22

Sql Server屬性ErrorLogF注冊(cè)表

2022-02-09 11:22:22

數(shù)據(jù)庫MySQL連接池

2025-04-02 00:00:00

2017-05-12 09:11:41

云計(jì)算數(shù)據(jù)庫高可用

2010-03-16 09:41:32

Python不可用

2023-11-22 15:34:41

oracleSQL數(shù)據(jù)庫

2009-11-18 16:10:00

2011-03-07 16:42:05

MySQL數(shù)據(jù)庫安全

2010-05-27 18:24:09

MySQL數(shù)據(jù)庫密碼

2011-03-28 13:11:18

MySQL數(shù)據(jù)庫安全

2011-03-03 18:09:14

2018-03-26 12:58:52

數(shù)據(jù)庫OracleMySQL

2011-03-24 15:41:42

數(shù)據(jù)庫

2011-01-21 09:43:10

安恒數(shù)據(jù)庫安全安全審計(jì)

2011-08-03 14:02:02

數(shù)據(jù)庫連接ACCESS

2010-05-28 11:22:07

2009-03-31 11:57:52

2024-02-06 07:56:20

數(shù)據(jù)庫分布式數(shù)據(jù)庫架構(gòu)產(chǎn)品

2010-09-15 09:50:55

2011-01-21 10:10:27

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)