Spring AI Embeddings 和 Vector 入门

2024-03-22 09:12

本文主要是介绍Spring AI Embeddings 和 Vector 入门,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在这里插入图片描述

在前面 Spring AI Chat 简单示例 中介绍了 Chat 的基本用法,本文在此基础(主要是pom.xml)上继续探索 Embedding 和 Vector。

官方文档:

  • embeddings: https://docs.spring.io/spring-ai/reference/api/embeddings/openai-embeddings.html
  • redis: https://docs.spring.io/spring-ai/reference/api/vectordbs/redis.html

Embeddings 介绍

文本嵌入(Embeddings)将文本转换为数值数组或向量,使人工智能模型能够处理和理解语言数据。这种从文本到数字的转换以及反向转换,是人工智能如何与人类语言互动和理解它的关键要素。对于探索人工智能的 Java 开发者来说,没有必要理解这些向量表示背后复杂的技术原理或是具体实现细节。只要基本了解它们在人工智能系统中的作用和功能就足够了,尤其是在将人工智能功能集成到应用程序中的时候。

文本嵌入在诸如检索增强生成 (RAG) 模式等实际应用中尤为重要。它们可以让数据在语义空间中表示为点,类似于欧几里得几何中的二维空间,只不过维度更高。这意味着就像欧几里得几何平面上的点可以根据其坐标相近或较远一样,在语义空间中,点的相近程度反映了含义的相似性。相似主题的句子在这个多维空间中会更靠近,就像图表上彼此靠近的点一样。这种相近性有助于文本分类、语义搜索甚至产品推荐等任务,因为它允许人工智能根据这些概念在扩展语义空间中的“位置”来识别和分组相关概念。

您可以将语义空间理解为一个具有多个维度的向量。每个维度代表一个语义特征,例如词性、主题、情感等。在语义空间中,每个词或句子都表示为一个向量。向量的各个分量代表该词或句子在各个语义特征上的得分。例如,一个表示“猫”的向量可能在“动物”维度上得分很高,而在“颜色”维度上得分较低。

Vector Databases 介绍

向量数据库(Vector Databases)是一种在人工智能应用中扮演着重要角色的特殊数据库类型。

与传统的关系型数据库不同,向量数据库中的查询不是进行精确匹配,而是执行相似性搜索。当使用向量作为查询时,向量数据库会返回与查询向量“相似”的向量。有关如何计算相似度的高级细节可以在 “向量相似性” 中找到。

向量数据库用于将您的数据与人工智能模型集成。使用它们的第一步是将您的数据加载到向量数据库中。然后,当用户查询需要发送到人工智能模型时,首先会检索一组相似的文档。然后,这些文档连同用户的查询一起作为用户问题的上下文发送到人工智能模型。这种技术称为检索增强生成 (Retrieval Augmented Generation, RAG)。

准备工作

在前一批 pom.xml 基础上增加依赖:

<dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-redis</artifactId>
</dependency><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>5.1.0</version>
</dependency>

Spring AI 支持下面几种向量数据库:

  • Azure Vector Search - The Azure vector store.
  • ChromaVectorStore - The Chroma vector store.
  • MilvusVectorStore - The Milvus vector store.
  • Neo4jVectorStore - The Neo4j vector store.
  • PgVectorStore - The PostgreSQL/PGVector vector store.
  • PineconeVectorStore - PineCone vector store.
  • QdrantVectorStore - Qdrant vector store.
  • RedisVectorStore - The Redis vector store.
  • WeaviateVectorStore - The Weaviate vector store.
  • SimpleVectorStore - A simple implementation of persistent vector storage, good for educational purposes.

在本文示例中我们使用 RedisVectorStoreSimpleVectorStore,RedisVectorStore 可以通过 Docker compose 快速启动一个测试环境:

version: '3'
services:redis:image: redis/redis-stackports:- "6379:6379"volumes:- redis_data:/datavolumes:redis_data:

如果你不方便使用 Docker,SimpleVectorStore 就是就简单的选择,一个纯内存实现,不需要任何中间件。

EmbeddingClient 示例

代码如下:

// HTTPS 代理
System.setProperty("https.proxyHost", "localhost");
System.setProperty("https.proxyPort", "7890");
// 和前面创建方式一样
var openAiApi = new OpenAiApi(System.getenv("OPENAI_API_KEY"));
// 在Api基础上创建 Embedding 客户端
var embeddingClient = new OpenAiEmbeddingClient(openAiApi,MetadataMode.EMBED,OpenAiEmbeddingOptions.builder().withModel("text-embedding-ada-002").withUser("user-1").build(),RetryUtils.DEFAULT_RETRY_TEMPLATE);

可以看到这里仍然复用了 openAiApi,通过组合可以很方便的复用公共类,公共类基于接口使得实现可以被替换,组合后的类可以扩展出更多职责单一的方法。

下面是 EmbeddingClient 常用的几个方法示例:

//单个文档
List<Double> list = embeddingClient.embed("MyBatis 分页插件 PageHelper");
//多个文档
List<List<Double>> lists = embeddingClient.embed(List.of("MyBatis 分页插件 PageHelper", "MyBatis 通用Mapper", "MyBatis 最新的 mybatis-mapper"));
//指定额外参数
EmbeddingResponse embeddingResponse = embeddingClient.call(new EmbeddingRequest(List.of("Hello World", "World is big and salvation is near"),OpenAiEmbeddingOptions.builder().withModel("Different-Embedding-Model-Deployment-Name").build()));
List<Embedding> embeddings = embeddingResponse.getResults();
for (Embedding embedding : embeddings) {List<Double> output = embedding.getOutput();// Do something with the output.
}

上面的方法返回了一个 Double 集合,需要将这个结果存入到向量数据库中,需要在数据库中记录向量数据还有原始内容,通过向量查询匹配后还需要取出原始内容。Spring AI 中定义了 Document:

public class Document {public static final ContentFormatter DEFAULT_CONTENT_FORMATTER = DefaultContentFormatter.defaultConfig();private final String id;private Map<String, Object> metadata;private String content;@JsonProperty(index = 100)private List<Double> embedding;@JsonIgnoreprivate ContentFormatter contentFormatter;//省略其他
}

VectorStore 示例

配合Spring AI 的 VectorStore 使用的时候,我们不需要手动通过 EmbeddingClient 获取 Embedding,这里分别提供基于 RedisVectorStoreSimpleVectorStore 的实现:

private static VectorStore redisVectorStore(EmbeddingClient embeddingClient) {RedisVectorStore.RedisVectorStoreConfigconfig = RedisVectorStore.RedisVectorStoreConfig.builder().withURI("redis://localhost:6379").withMetadataFields(RedisVectorStore.MetadataField.numeric("year")).build();RedisVectorStore vectorStore = new RedisVectorStore(config, embeddingClient);//手动创建时必须执行下面方法,这会初始化 spring-ai-index 索引vectorStore.afterPropertiesSet();return vectorStore;
}private static VectorStore simpleVectorStore(EmbeddingClient embeddingClient) {return new SimpleVectorStore(embeddingClient);
}

在前面创建完成 var embeddingClient 后,我们可以创建 VectorStore 来存储文档的向量数据:

VectorStore vectorStore = redisVectorStore(embeddingClient);List<Document> documents = List.of(new Document("MyBatis 分页插件 PageHelper", Map.of("year", 2014)),new Document("MyBatis 通用Mapper", Map.of("year", 2014)),new Document("MyBatis 最新的 mybatis-mapper", Map.of("year", 2019)));vectorStore.add(documents);

在调用 vectorStore.add 方法的内部会根据 documents 循环调用 embeddingClient.embed(document) 获取向量数据并存储向量存储中。在使用 redisVectorStore 实现时需要注意,由于 Redis 持久化,反复执行会存入多次数据,因此如果已经添加过文档,后续执行当前代码时可以考虑注释 vectorStore.add 行代码,如果使用的 SimpleVectorStore,内存会在每次启动时初始化,需要反复计算向量数据才能使用。

有了数据之后就可以通过向量数据库进行查询:

List<Document> results = vectorStore.similaritySearch(SearchRequest.query("插件").withTopK(5));
results.forEach(d -> System.out.println(d.getContent()));

输出结果:

MyBatis 分页插件 PageHelper
MyBatis 通用Mapper
MyBatis 最新的 mybatis-mapper

使用 redis 时的数据:

127.0.0.1:6379> keys *
1) "embedding:e9f7cea2-b296-4674-abba-d2611360afac"
2) "embedding:3d8c0f61-2192-452c-a8db-884277dcbf7d"
3) "embedding:8e1b0cb8-c125-40e2-9cdb-5ab9a6f4705d"
127.0.0.1:6379> ft._list
1) spring-ai-index

上面的查询在使用 SimpleVectorStore 的时候也可以正常运行,但是 SimpleVectorStore 不支持下面针对元数据的表达式查询(定义时也没有元数据相关的选项):

results = vectorStore.similaritySearch(SearchRequest.query("MyBatis").withFilterExpression("year == 2014"));

有了向量数据和向量查询后,后续可以结合 Chat 在使用过程中将自己的数据放到上下文中使用,本文后续会继续介绍 Spring AI 相关的内容。

这篇关于Spring AI Embeddings 和 Vector 入门的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/835007

相关文章

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

SpringBoot+Docker+Graylog 如何让错误自动报警

《SpringBoot+Docker+Graylog如何让错误自动报警》SpringBoot默认使用SLF4J与Logback,支持多日志级别和配置方式,可输出到控制台、文件及远程服务器,集成ELK... 目录01 Spring Boot 默认日志框架解析02 Spring Boot 日志级别详解03 Sp

java中反射Reflection的4个作用详解

《java中反射Reflection的4个作用详解》反射Reflection是Java等编程语言中的一个重要特性,它允许程序在运行时进行自我检查和对内部成员(如字段、方法、类等)的操作,本文将详细介绍... 目录作用1、在运行时判断任意一个对象所属的类作用2、在运行时构造任意一个类的对象作用3、在运行时判断

java如何解压zip压缩包

《java如何解压zip压缩包》:本文主要介绍java如何解压zip压缩包问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java解压zip压缩包实例代码结果如下总结java解压zip压缩包坐在旁边的小伙伴问我怎么用 java 将服务器上的压缩文件解压出来,

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Spring WebFlux 与 WebClient 使用指南及最佳实践

《SpringWebFlux与WebClient使用指南及最佳实践》WebClient是SpringWebFlux模块提供的非阻塞、响应式HTTP客户端,基于ProjectReactor实现,... 目录Spring WebFlux 与 WebClient 使用指南1. WebClient 概述2. 核心依

Spring Boot @RestControllerAdvice全局异常处理最佳实践

《SpringBoot@RestControllerAdvice全局异常处理最佳实践》本文详解SpringBoot中通过@RestControllerAdvice实现全局异常处理,强调代码复用、统... 目录前言一、为什么要使用全局异常处理?二、核心注解解析1. @RestControllerAdvice2

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

从入门到精通MySQL联合查询

《从入门到精通MySQL联合查询》:本文主要介绍从入门到精通MySQL联合查询,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下... 目录摘要1. 多表联合查询时mysql内部原理2. 内连接3. 外连接4. 自连接5. 子查询6. 合并查询7. 插入查询结果摘要前面我们学习了数据库设计时要满

Spring事务传播机制最佳实践

《Spring事务传播机制最佳实践》Spring的事务传播机制为我们提供了优雅的解决方案,本文将带您深入理解这一机制,掌握不同场景下的最佳实践,感兴趣的朋友一起看看吧... 目录1. 什么是事务传播行为2. Spring支持的七种事务传播行为2.1 REQUIRED(默认)2.2 SUPPORTS2