类型感知

本节将引导您完成配置TypesenseVectorStore以存储文档嵌入并执行相似性搜索的过程。spring-doc.cadn.net.cn

Typesense 是一个开源的容错搜索引擎,经过优化可实现低于50毫秒的即时搜索,同时提供直观的开发者体验。它还提供向量搜索功能,允许您在常规搜索数据之外存储和查询高维向量。spring-doc.cadn.net.cn

前提条件

自动配置

There has been a significant change in the Spring AI auto-configuration, starter modules' artifact names. Please refer to the 升级说明以获取更多信息。spring-doc.cadn.net.cn

Spring AI 为 Typesense 向量存储提供 Spring Boot 自动配置。 要启用它,请将以下依赖项添加到项目的 Maven pom.xml 文件中:spring-doc.cadn.net.cn

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-vector-store-typesense</artifactId>
</dependency>

或者添加到您的Gradle 构建脚本文件中。spring-doc.cadn.net.cn

dependencies {
    implementation 'org.springframework.ai:spring-ai-starter-vector-store-typesense'
}
参考以下依赖管理部分,添加Spring AI BOM到你的构建文件中。

请查看以下向量存储的配置参数列表,了解默认值和配置选项。这些配置参数的类型为C0。spring-doc.cadn.net.cn

引用 Artifact 仓库 部分,以在构建文件中添加 Maven Central 和/或 Snapshot 仓库。

向量存储实现可以为您初始化所需的模式,但您必须通过在application.properties文件中设置…​initialize-schema=true来选择启用。spring-doc.cadn.net.cn

此外,您还需要一个已配置的EmbeddingModel bean。有关更多信息,请参阅EmbeddingModel部分。spring-doc.cadn.net.cn

现在,你可以使用 TypesenseVectorStore 作为向量存储在你的应用中:spring-doc.cadn.net.cn

@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-doc.cadn.net.cn

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.* 开头的属性用于配置 TypesenseVectorStorespring-doc.cadn.net.cn

属性 描述 默认值

spring.ai.vectorstore.typesense.initialize-schemaspring-doc.cadn.net.cn

是否初始化所需的模式spring-doc.cadn.net.cn

falsespring-doc.cadn.net.cn

spring.ai.vectorstore.typesense.collection-namespring-doc.cadn.net.cn

存储向量的集合名称spring-doc.cadn.net.cn

vector_storespring-doc.cadn.net.cn

spring.ai.vectorstore.typesense.embedding-dimensionspring-doc.cadn.net.cn

向量中的维度数量spring-doc.cadn.net.cn

1536spring-doc.cadn.net.cn

spring.ai.vectorstore.typesense.client.protocolspring-doc.cadn.net.cn

HTTP 协议spring-doc.cadn.net.cn

httpspring-doc.cadn.net.cn

spring.ai.vectorstore.typesense.client.hostspring-doc.cadn.net.cn

主机名spring-doc.cadn.net.cn

localhostspring-doc.cadn.net.cn

spring.ai.vectorstore.typesense.client.portspring-doc.cadn.net.cn

端口spring-doc.cadn.net.cn

8108spring-doc.cadn.net.cn

spring.ai.vectorstore.typesense.client.api-keyspring-doc.cadn.net.cn

API 密钥spring-doc.cadn.net.cn

xyzspring-doc.cadn.net.cn

手动配置

与其使用 Spring Boot 自动配置,您可以手动配置 Typesense 向量存储。为此,您需要将 spring-ai-typesense-store 添加到您的项目中:spring-doc.cadn.net.cn

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-typesense-store</artifactId>
</dependency>

或者添加到您的Gradle 构建脚本文件中。spring-doc.cadn.net.cn

dependencies {
    implementation 'org.springframework.ai:spring-ai-typesense-store'
}
参考以下依赖管理部分,添加Spring AI BOM到你的构建文件中。

创建一个 Typesense Client bean:spring-doc.cadn.net.cn

@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:spring-doc.cadn.net.cn

@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 存储一起使用。spring-doc.cadn.net.cn

例如,您可以使用文本表达式语言:spring-doc.cadn.net.cn

vectorStore.similaritySearch(
    SearchRequest.builder()
        .query("The World")
        .topK(TOP_K)
        .similarityThreshold(SIMILARITY_THRESHOLD)
        .filterExpression("country in ['UK', 'NL'] && year >= 2020").build());

或通过编程方式使用 Filter.Expression DSL:spring-doc.cadn.net.cn

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 搜索过滤器

例如,这个可移植的过滤表达式:spring-doc.cadn.net.cn

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

被转换为专有的 Typesense 过滤格式:spring-doc.cadn.net.cn

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

如果您未按预期顺序检索文档,或搜索结果不符合预期,请检查您使用的嵌入模型。spring-doc.cadn.net.cn

嵌入模型会对搜索结果产生显著影响(即,确保您的数据为西班牙语时使用西班牙语或多语言嵌入模型)。spring-doc.cadn.net.cn

访问原生客户端

Typesense 向量存储实现通过 getNativeClient() 方法提供对底层原生 Typesense 客户端 (Client) 的访问:spring-doc.cadn.net.cn

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 界面公开。spring-doc.cadn.net.cn