类型定义
本节将引导你如何设置TypesenseVectorStore用于存储文档嵌入并进行相似性搜索。
Typesense 是一个开源、容错字的搜索引擎,优化为瞬间低于 50 毫秒的搜索,同时提供直观的开发者体验。它提供向量搜索功能,允许您在常规搜索数据的同时存储和查询高维向量。
前提条件
-
一个正在运行的Typesense实例。以下选项可供选择:
-
Typesense Cloud(推荐)
-
Docker image typesense/typesense:latest
-
-
如有需要,需为 EmbeddingModel 提供 API 密钥,以生成存储于
TypesenseVectorStore.
自动配置
|
春季AI自动配置、起始模块的工件名称发生了重大变化。 更多信息请参阅升级说明。 |
Spring AI 为 Typesense 向量存储提供了 Spring Boot 自动配置。
要启用它,请在项目的Maven中添加以下依赖pom.xml文件:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-vector-store-typesense</artifactId>
</dependency>
或者去你的Gradlebuild.gradle构建文件。
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-vector-store-typesense'
}
| 请参考依赖管理部分,将Spring AI的物料清单添加到你的构建文件中。 |
请查看矢量存储的配置参数列表,了解默认值和配置选项。
| 请参阅“遗物仓库”部分,将Maven Central和/或快照仓库添加到你的构建文件中。 |
向量存储实现可以帮你初始化所需的模式,但你必须通过设置来选择加入…initialize-schema=true在application.properties文件。
此外,你还需要一个配置好的嵌入模型豆。 更多信息请参阅嵌入模型部分。
现在你可以自动接线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:
| 属性 | 描述 | 默认值 |
|---|---|---|
|
是否初始化所需的模式 |
|
|
集合名称用于存储向量 |
|
|
向量中的维数 |
|
|
HTTP 协议 |
|
|
主机名 |
|
|
端口 |
|
|
API 密钥 |
|
手动配置
你可以不用 Spring Boot 的自动配置,而是手动配置 Typesense 矢量存储。为此你需要添加Spring-ai-typesense-store致你的项目:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-typesense-store</artifactId>
</dependency>
或者去你的Gradlebuild.gradle构建文件。
dependencies {
implementation 'org.springframework.ai:spring-ai-typesense-store'
}
| 请参考依赖管理部分,将Spring AI的物料清单添加到你的构建文件中。 |
创建类型感客户端豆:
@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
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());
或者程序化地使用滤波。表达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搜索筛选。 |
例如,这个可移植的Filter表达式:
country in ['UK', 'NL'] && year >= 2020
转换为专有的TypesenseFilter格式:
country: ['UK', 'NL'] && year: >=2020
|
如果你检索文档的顺序不符合预期,或者搜索结果不符合预期,请检查你所使用的嵌入模型。 嵌入模型对搜索结果有显著影响(例如,确保你的数据是西班牙语的,使用西班牙语或多语言嵌入模型)。 |
访问本地客户端
Typesense 向量存储实现提供了对底层本地 Typesense 客户端的访问(客户端)通过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接口。