跳到主要内容
版本:2.0.1

Typesense

QWen 3.8 中英对照 Typesense

本节将指导你设置 TypesenseVectorStore,以存储文档嵌入并执行相似性搜索。

Typesense 是一款开源的容错搜索引擎,专为即时亚 50 毫秒搜索而优化,同时提供直观的开发者体验。它具备向量搜索功能,允许你在常规搜索数据的同时存储并查询高维向量。

先决条件​

  • 一个正在运行的 Typesense 实例。以下选项可供使用:

  • 如需要,用于 EmbeddingModel 的 API 密钥,以生成存储在 TypesenseVectorStore 中的嵌入向量。

自动配置​

备注

Spring AI 自动配置及 starter 模块的工件名称发生了重大变更。请参阅升级说明以获取更多信息。

Spring AI 为 Typesense Vector Store 提供了 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 添加到你的构建文件中。

请查看配置参数列表,以了解向量存储的默认值和配置选项。

提示

请参考 Artifact Repositories 部分,将 Maven Central 和/或 Snapshot Repositories 添加到你的构建文件中。

向量存储(vector store)的实现可以为您初始化所需的模式(schema),但您必须在 application.properties 文件中设置 …​initialize-schema=true 以启用此功能。

此外,您还需要一个配置好的 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:

属性描述默认值
spring.ai.vectorstore.typesense.initialize-schema是否初始化所需的 schemafalse
spring.ai.vectorstore.typesense.collection-name用于存储向量的 collection 名称vector_store
spring.ai.vectorstore.typesense.embedding-dimension向量的维度数1536
spring.ai.vectorstore.typesense.client.protocolHTTP Protocolhttp
spring.ai.vectorstore.typesense.client.hostHostnamelocalhost
spring.ai.vectorstore.typesense.client.portPort8108
spring.ai.vectorstore.typesense.client.api-keyAPI Keyxyz

手动配置​

除了使用 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(OpenAiEmbeddingOptions.builder().apiKey(System.getenv("OPENAI_API_KEY")).build());
}

元数据过滤​

你也可以在 Typesense 存储中使用通用的可移植 metadata filters。

例如,您可以使用文本表达式语言:

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 Search Filters。

例如,该可移植过滤器表达式:

country in ['UK', 'NL'] && year >= 2020

转换为 Typesense 专有的过滤器格式:

country: ['UK', 'NL'] && year: >=2020
备注

如果您获取的文档顺序不符合预期,或者搜索结果与预期不符,请检查您使用的嵌入模型。

嵌入模型对搜索结果有重大影响(即如果您的数据是西班牙语,请确保使用西班牙语或多语言嵌入模型)。

访问 Native Client​

Typesense Vector Store 实现通过 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 接口暴露。