此版本仍在开发中,尚未被视为稳定版本。最新的快照版本请使用 Spring AI 1.0.0-SNAPSHOT! |
Typesense 字体
本节将指导您完成设置TypesenseVectorStore
来存储文档嵌入并执行相似性搜索。
Typesense 是一个开源的错别字容忍搜索引擎,它针对低于 50 毫秒的即时搜索进行了优化,同时提供直观的开发人员体验。它提供向量搜索功能,允许您将高维向量与常规搜索数据一起存储和查询。
先决条件
-
一个正在运行的 Typesense 实例。以下选项可用:
-
Typesense Cloud(推荐)
-
Docker 镜像 typesense/typesense:latest
-
-
如果需要,EmbeddingModel 的 API 密钥,用于生成由
TypesenseVectorStore
.
自动配置
Spring AI 为 Typesense Vector Store 提供了 Spring Boot 自动配置。
要启用它,请将以下依赖项添加到项目的 Maven 中pom.xml
文件:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-typesense-spring-boot-starter</artifactId>
</dependency>
或发送到您的 Gradlebuild.gradle
build 文件。
dependencies {
implementation 'org.springframework.ai:spring-ai-typesense-spring-boot-starter'
}
请参阅 Dependency Management 部分,将 Spring AI BOM 添加到您的构建文件中。 |
请查看 vector store 的配置参数列表,了解默认值和配置选项。
请参阅 Repositories 部分,将 Maven Central 和/或 Snapshot Repositories 添加到您的构建文件中。 |
矢量存储实现可以为您初始化必要的架构,但您必须通过设置…initialize-schema=true
在application.properties
文件。
此外,您还需要一个配置的EmbeddingModel
豆。请参阅 EmbeddingModel 部分以了解更多信息。
现在,您可以自动连接TypesenseVectorStore
作为应用程序中的 vector store 中:
@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
:
财产 | 描述 | 默认值 |
---|---|---|
|
是否初始化所需的 schema |
|
|
用于存储向量的集合的名称 |
|
|
向量中的维数 |
|
|
HTTP 协议 |
|
|
主机名 |
|
|
港口 |
|
|
API 密钥 |
|
手动配置
您可以手动配置 Typesense 向量存储,而不是使用 Spring Boot 自动配置。为此,您需要添加spring-ai-typesense-store
到您的项目:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-typesense-store</artifactId>
</dependency>
或发送到您的 Gradlebuild.gradle
build 文件。
dependencies {
implementation 'org.springframework.ai:spring-ai-typesense-store'
}
请参阅 Dependency Management 部分,将 Spring AI BOM 添加到您的构建文件中。 |
创建 TypesenseClient
豆:
@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
使用 Builder 模式的 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 store 结合使用。
例如,您可以使用文本表达式语言:
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
如果未按预期顺序检索文档或搜索结果与预期不符,请检查您正在使用的嵌入模型。 嵌入模型可能会对搜索结果产生重大影响(即,确保您的数据是西班牙语还是多语言嵌入模型)。 |
访问 Native Client
Typesense Vector Store 实现提供对底层原生 Typesense 客户端 (Client
) 通过getNativeClient()
方法:
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
接口。