|
此版本仍在开发中,尚未被视为稳定版。如需最新的快照版本,请使用 Spring AI 1.1.3! |
Typesense
本节将指导您设置 TypesenseVectorStore 以存储文档嵌入并执行相似性搜索。
Typesense 是一个开源的拼写容错搜索引擎,针对即时50毫秒以下的搜索进行了优化,同时提供直观的开发者体验。它提供了向量搜索功能,允许您将高维向量与常规搜索数据一起存储和查询。
前置条件
-
正在运行的 Typesense 实例。以下选项可用:
-
Typesense Cloud (推荐)
-
Docker 镜像 typesense/typesense:latest
-
-
如果需要,为EmbeddingModel提供一个API密钥,用于生成由
TypesenseVectorStore存储的嵌入向量。
Auto-configuration
|
Spring AI自动配置和starter模块的artifact名称有了重大变化。 请参阅升级说明获取更多信息。 |
Spring AI 为 Typesense 向量存储提供了 Spring Boot 自动配置。
要启用它,请将以下依赖项添加到您项目的 Maven pom.xml 文件中:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-vector-store-typesense</artifactId>
</dependency>
请将以下内容添加到您的Gradle build.gradle 构建文件中。
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-vector-store-typesense'
}
| 请参阅依赖管理部分,将Spring AI BOM添加到您的构建文件中。 |
请查看 向量存储的配置参数 列表,以了解默认值和配置选项。
| 请参阅 构件仓库 部分,将 Maven 中央仓库和/或快照仓库添加到您的构建文件中。 |
向量存储实现可以为您初始化所需的架构,但您必须在 …initialize-schema=true 文件中设置 application.properties 来选择启用。
此外,您还需要一个配置好的 EmbeddingModel bean。有关更多信息,请参阅 EmbeddingModel 部分。
现在您可以在应用程序中将 TypesenseVectorStore 自动装配为向量存储:
@Autowired VectorStore vectorStore;
// ...
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 Typesense
vectorStore.add(documents);
// Retrieve documents similar to a query
List<Document> results = vectorStore.similaritySearch(SearchRequest.builder().query("Spring").topK(5).build());
配置属性
要连接到 Typesense 并使用 TypesenseVectorStore,您需要为您的实例提供访问详细信息。
可以通过 Spring Boot 的 application.yml 提供简单的配置:
spring:
ai:
vectorstore:
typesense:
initialize-schema: true
collection-name: vector_store
embedding-dimension: 1536
client:
protocol: http
host: localhost
port: 8108
api-key: xyz
以 spring.ai.vectorstore.typesense.* 开头的属性用于配置 TypesenseVectorStore:
| <property> </property> | <description> </description> | 默认值 |
|---|---|---|
|
是否初始化所需的模式 |
|
|
用于存储向量的集合名称 |
|
|
向量中的维度数量 |
|
|
HTTP 协议 |
|
|
主机名 |
|
|
端口 |
|
|
apiKey |
|
手动配置
除了使用 Spring Boot 自动配置,您还可以手动配置 Typesense 向量存储。为此,您需要将 spring-ai-typesense-store 添加到您的项目中:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-typesense-store</artifactId>
</dependency>
请将以下内容添加到您的Gradle build.gradle 构建文件中。
dependencies {
implementation 'org.springframework.ai:spring-ai-typesense-store'
}
| 请参阅依赖管理部分,将Spring AI BOM添加到您的构建文件中。 |
创建一个 Typesense Client bean:
@Bean
public Client typesenseClient() {
List<Node> nodes = new ArrayList<>();
nodes.add(new Node("http", "localhost", "8108"));
Configuration configuration = new Configuration(nodes, Duration.ofSeconds(5), "xyz");
return new Client(configuration);
}
然后使用构建器模式创建 TypesenseVectorStore bean:
@Bean
public VectorStore vectorStore(Client client, EmbeddingModel embeddingModel) {
return TypesenseVectorStore.builder(client, embeddingModel)
.collectionName("custom_vectors") // Optional: defaults to "vector_store"
.embeddingDimension(1536) // Optional: defaults to 1536
.initializeSchema(true) // Optional: defaults to false
.batchingStrategy(new TokenCountBatchingStrategy()) // Optional: defaults to TokenCountBatchingStrategy
.build();
}
// This can be any EmbeddingModel implementation
@Bean
public EmbeddingModel embeddingModel() {
return new OpenAiEmbeddingModel(new OpenAiApi(System.getenv("OPENAI_API_KEY")));
}
元数据过滤
您也可以利用通用可移植的 元数据过滤器 与 Typesense 存储一起使用。
例如,您可以使用文本表达式语言:
vectorStore.similaritySearch(
SearchRequest.builder()
.query("The World")
.topK(TOP_K)
.similarityThreshold(SIMILARITY_THRESHOLD)
.filterExpression("country in ['UK', 'NL'] && year >= 2020").build());
或以编程方式使用 Filter.Expression DSL:
FilterExpressionBuilder b = new FilterExpressionBuilder();
vectorStore.similaritySearch(SearchRequest.builder()
.query("The World")
.topK(TOP_K)
.similarityThreshold(SIMILARITY_THRESHOLD)
.filterExpression(b.and(
b.in("country", "UK", "NL"),
b.gte("year", 2020)).build()).build());
| 那些(可移植的)过滤器表达式会自动转换为 Typesense 搜索过滤器。 |
例如这个可移植的过滤器表达式:
country in ['UK', 'NL'] && year >= 2020
被转换为专有的 Typesense 过滤器格式:
country: ['UK', 'NL'] && year: >=2020
|
如果您未按预期顺序检索文档,或者搜索结果不符合预期,请检查您使用的嵌入模型。 嵌入模型可能会对搜索结果产生重大影响(即,如果您的数据是西班牙语,请确保使用西班牙语或多语言嵌入模型)。 |
访问原生客户端
Typesense 向量存储实现通过 getNativeClient() 方法提供了对底层原生 Typesense 客户端(Client)的访问权限:
TypesenseVectorStore vectorStore = context.getBean(TypesenseVectorStore.class);
Optional<Client> nativeClient = vectorStore.getNativeClient();
if (nativeClient.isPresent()) {
Client client = nativeClient.get();
// Use the native client for Typesense-specific operations
}
原生客户端使您能够访问 Typesense 特有的功能和操作,这些功能和操作可能无法通过 VectorStore 接口暴露。