Pinecone

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

Pinecone 是一种流行的基于云的矢量数据库,可让您有效地存储和搜索矢量。spring-doc.cadn.net.cn

前提条件

  1. Pinecone账户:在开始之前,请注册一个Pinecone账户spring-doc.cadn.net.cn

  2. Pinecone项目:注册后,生成 API 密钥并创建和索引。您需要这些详细信息进行配置。spring-doc.cadn.net.cn

  3. EmbeddingModel实例来计算文档嵌入。有几个选项可用:spring-doc.cadn.net.cn

设置PineconeVectorStore,从您的 Pinecone 帐户中收集以下详细信息:spring-doc.cadn.net.cn

您可以在 Pinecone UI 门户中获取此信息。 命名空间支持在 Pinecone 免费层中不可用。spring-doc.cadn.net.cn

自动配置

Spring AI 自动配置、入门模块的工件名称发生了重大变化。 有关更多信息,请参阅升级说明spring-doc.cadn.net.cn

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

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

或 Gradlebuild.gradle构建文件。spring-doc.cadn.net.cn

dependencies {
    implementation 'org.springframework.ai:spring-ai-starter-vector-store-pinecone'
}
请参阅依赖项管理部分,将 Spring AI BOM 添加到构建文件中。
请参阅 Artifact Repositories 部分,将 Maven Central 和/或 Snapshot Repositories 添加到构建文件中。

此外,您还需要配置一个EmbeddingModel豆。有关详细信息,请参阅 EmbeddingModel 部分。spring-doc.cadn.net.cn

下面是所需 bean 的示例:spring-doc.cadn.net.cn

@Bean
public EmbeddingModel embeddingModel() {
    // Can be any other EmbeddingModel implementation.
    return new OpenAiEmbeddingModel(new OpenAiApi(System.getenv("OPENAI_API_KEY")));
}

要连接到 Pinecone,您需要提供实例的访问详细信息。 可以通过 Spring Boot 的 application.properties 提供简单的配置,spring-doc.cadn.net.cn

spring.ai.vectorstore.pinecone.apiKey=<your api key>
spring.ai.vectorstore.pinecone.index-name=<your index name>

# API key if needed, e.g. OpenAI
spring.ai.openai.api.key=<api-key>

请查看矢量存储的配置参数列表,了解默认值和配置选项。spring-doc.cadn.net.cn

现在,您可以在应用程序中自动连接 Pinecone 矢量存储并使用它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
vectorStore.add(documents);

// Retrieve documents similar to a query
List<Document> results = this.vectorStore.similaritySearch(SearchRequest.builder().query("Spring").topK(5).build());

配置属性

您可以在 Spring Boot 配置中使用以下属性来自定义 Pinecone 矢量存储。spring-doc.cadn.net.cn

属性 描述 默认值

spring.ai.vectorstore.pinecone.api-keyspring-doc.cadn.net.cn

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

-spring-doc.cadn.net.cn

spring.ai.vectorstore.pinecone.index-namespring-doc.cadn.net.cn

Pinecone索引名称spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.vectorstore.pinecone.namespacespring-doc.cadn.net.cn

Pinecone 命名空间spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.vectorstore.pinecone.content-field-namespring-doc.cadn.net.cn

Pinecone 元数据字段名称,用于存储原始文本内容。spring-doc.cadn.net.cn

document_contentspring-doc.cadn.net.cn

spring.ai.vectorstore.pinecone.distance-metadata-field-namespring-doc.cadn.net.cn

Pinecone 元数据字段名称,用于存储计算的距离。spring-doc.cadn.net.cn

distancespring-doc.cadn.net.cn

spring.ai.vectorstore.pinecone.server-side-timeoutspring-doc.cadn.net.cn

20 秒spring-doc.cadn.net.cn

元数据过滤

您可以利用 Pinecone 存储的通用可移植元数据过滤器spring-doc.cadn.net.cn

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

vectorStore.similaritySearch(
    SearchRequest.builder()
    .query("The World")
    .topK(TOP_K)
    .similarityThreshold(SIMILARITY_THRESHOLD)
    .filterExpression("author in ['john', 'jill'] && article_type == 'blog'").build());

或以编程方式使用Filter.ExpressionDSL: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("author","john", "jill"),
        b.eq("article_type", "blog")).build()).build());
这些过滤器表达式将转换为等效的 Pinecone 过滤器。

手动配置

如果您更喜欢配置PineconeVectorStore手动,您可以使用PineconeVectorStore#Builder.spring-doc.cadn.net.cn

将以下依赖项添加到项目中:spring-doc.cadn.net.cn

<dependency>
	<groupId>org.springframework.ai</groupId>
	<artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-pinecone-store</artifactId>
</dependency>
请参阅依赖项管理部分,将 Spring AI BOM 添加到构建文件中。

示例代码

要在应用程序中配置 Pinecone,您可以使用以下设置:spring-doc.cadn.net.cn

@Bean
public VectorStore pineconeVectorStore(EmbeddingModel embeddingModel) {
    return PineconeVectorStore.builder(embeddingModel)
            .apiKey(PINECONE_API_KEY)
            .indexName(PINECONE_INDEX_NAME)
            .namespace(PINECONE_NAMESPACE) // the free tier doesn't support namespaces.
            .contentFieldName(CUSTOM_CONTENT_FIELD_NAME) // optional field to store the original content. Defaults to `document_content`
            .build();
}

在主代码中,创建一些文档:spring-doc.cadn.net.cn

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")));

将文档添加到 Pinecone:spring-doc.cadn.net.cn

vectorStore.add(documents);

最后,检索类似于查询的文档:spring-doc.cadn.net.cn

List<Document> results = vectorStore.similaritySearch(SearchRequest.query("Spring").topK(5).build());

如果一切顺利,您应该检索包含文本“Spring AI rocks!!”的文档。spring-doc.cadn.net.cn

访问本机客户端

Pinecone 矢量存储实现提供对底层原生 Pinecone 客户端 (PineconeConnection) 通过getNativeClient()方法:spring-doc.cadn.net.cn

PineconeVectorStore vectorStore = context.getBean(PineconeVectorStore.class);
Optional<PineconeConnection> nativeClient = vectorStore.getNativeClient();

if (nativeClient.isPresent()) {
    PineconeConnection client = nativeClient.get();
    // Use the native client for Pinecone-specific operations
}

本机客户端允许您访问特定于 Pinecone 的功能和作,这些功能和作可能不会通过VectorStore接口。spring-doc.cadn.net.cn