Typesense
本节将引导您设置 TypesenseVectorStore
来存储文档嵌入并执行相似性搜索。
This section walks you through setting up TypesenseVectorStore
to store document embeddings and perform similarity searches.
Typesense 是一个开源的容错搜索引擎,它针对即时低于 50 毫秒的搜索进行了优化,同时提供了直观的开发人员体验。它提供向量搜索功能,允许您存储和查询高维向量以及常规搜索数据。
Typesense is an open source typo tolerant search engine that is optimized for instant sub-50ms searches while providing an intuitive developer experience. It provides vector search capabilities that allow you to store and query high-dimensional vectors alongside your regular search data.
Prerequisites
-
一个正在运行的 Typesense 实例。以下选项可用:
-
Typesense Cloud (recommended)
-
Docker image typesense/typesense:latest
-
-
A running Typesense instance. The following options are available:
-
Typesense Cloud (recommended)
-
Docker image typesense/typesense:latest
-
-
如果需要, EmbeddingModel 的 API 密钥用于生成由
TypesenseVectorStore
存储的嵌入。 -
If required, an API key for the EmbeddingModel to generate the embeddings stored by the
TypesenseVectorStore
.
Auto-configuration
Spring AI 自动配置、启动器模块的工件名称发生了重大变化。请参阅 upgrade notes 以获取更多信息。 There has been a significant change in the Spring AI auto-configuration, starter modules' artifact names. Please refer to the upgrade notes for more information. |
Spring AI 为 Typesense 向量存储提供 Spring Boot 自动配置。要启用它,请将以下依赖项添加到项目的 Maven pom.xml
文件中:
Spring AI provides Spring Boot auto-configuration for the Typesense Vector Store.
To enable it add the following dependency to your project’s Maven pom.xml
file:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-vector-store-typesense</artifactId>
</dependency>
或添加到 Gradle build.gradle
构建文件中。
or to your Gradle build.gradle
build file.
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-vector-store-typesense'
}
|
Refer to the Dependency Management section to add the Spring AI BOM to your build file. |
请查看矢量存储的 configuration parameters 列表,了解默认值和配置选项。
Please have a look at the list of _configuration_properties for the vector store to learn about the default values and configuration options.
将Maven Central和/或Snapshot存储库添加到您的构建文件中,请参阅 Artifact Repositories 部分。 |
Refer to the Artifact Repositories section to add Maven Central and/or Snapshot Repositories to your build file. |
向量存储实现可以为您初始化所需的模式,但您必须通过在 application.properties
文件中设置 …initialize-schema=true
来选择加入。
The vector store implementation can initialize the requisite schema for you but you must opt-in by setting …initialize-schema=true
in the application.properties
file.
此外,您还需要一个配置的 EmbeddingModel
bean。有关详细信息,请参阅 EmbeddingModel 部分。
Additionally you will need a configured EmbeddingModel
bean. Refer to the EmbeddingModel section for more information.
现在,您可以将 TypesenseVectorStore
作为向量存储自动连接到您的应用程序中:
Now you can auto-wire the TypesenseVectorStore
as a vector store in your application:
@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());
Configuration Properties
要连接到 Typesense 并使用 TypesenseVectorStore
,您需要提供实例的访问详细信息。可以通过 Spring Boot 的 application.yml
提供简单的配置:
To connect to Typesense and use the TypesenseVectorStore
you need to provide access details for your instance.
A simple configuration can be provided via Spring Boot’s 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
:
Properties starting with spring.ai.vectorstore.typesense.*
are used to configure the TypesenseVectorStore
:
Property | Description | Default Value |
---|---|---|
|
Whether to initialize the required schema |
|
|
The name of the collection to store vectors |
|
|
The number of dimensions in the vector |
|
|
HTTP Protocol |
|
|
Hostname |
|
|
Port |
|
|
API Key |
|
Manual Configuration
除了使用 Spring Boot 自动配置之外,您还可以手动配置 Typesense 向量存储。为此,您需要将 spring-ai-typesense-store
添加到您的项目中:
Instead of using the Spring Boot auto-configuration you can manually configure the Typesense vector store. For this you need to add the spring-ai-typesense-store
to your project:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-typesense-store</artifactId>
</dependency>
或添加到 Gradle build.gradle
构建文件中。
or to your Gradle build.gradle
build file.
dependencies {
implementation 'org.springframework.ai:spring-ai-typesense-store'
}
|
Refer to the Dependency Management section to add the Spring AI BOM to your build file. |
创建一个 Typesense Client
bean:
Create a 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:
Then create the TypesenseVectorStore
bean using the builder pattern:
@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")));
}
Metadata Filtering
您也可以将通用的可移植 metadata filters 与 Typesense 存储一起使用。
You can leverage the generic portable metadata filters with Typesense store as well.
例如,您可以使用文本表达式语言:
For example you can use either the text expression language:
vectorStore.similaritySearch(
SearchRequest.builder()
.query("The World")
.topK(TOP_K)
.similarityThreshold(SIMILARITY_THRESHOLD)
.filterExpression("country in ['UK', 'NL'] && year >= 2020").build());
或使用 Filter.Expression
DSL 以编程方式:
or programmatically using the 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 Search Filters 。 |
Those (portable) filter expressions get automatically converted into Typesense Search Filters. |
例如,这个可移植的过滤器表达式:
For example this portable filter expression:
country in ['UK', 'NL'] && year >= 2020
会转换为 Typesense 专有过滤器格式:
is converted into the proprietary Typesense filter format:
country: ['UK', 'NL'] && year: >=2020
如果您未按预期顺序检索文档或搜索结果不符合预期,请检查您正在使用的嵌入模型。 If you are not retrieving the documents in the expected order or the search results are not as expected, check the embedding model you are using. 嵌入模型会对搜索结果产生显著影响(即,如果您的数据是西班牙语,请确保使用西班牙语或多语言嵌入模型)。 Embedding models can have a significant impact on the search results (i.e. make sure if your data is in Spanish to use a Spanish or multilingual embedding model). |
Accessing the Native Client
Typesense 向量存储实现通过 getNativeClient()
方法提供对底层原生 Typesense 客户端 ( Client
) 的访问:
The Typesense Vector Store implementation provides access to the underlying native Typesense client (Client
) through the getNativeClient()
method:
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
接口公开。
The native client gives you access to Typesense-specific features and operations that might not be exposed through the VectorStore
interface.