跳到主要内容
版本:2.0.1

Pinecone

QWen 3.8 中英对照 Pinecone

本节将指导你配置 Pinecone VectorStore,以存储文档嵌入并执行相似度搜索。

Pinecone 是一款流行的基于云端的向量数据库,允许您高效地存储和搜索向量。

前提条件​

  1. Pinecone 账户:在开始之前,请注册一个 Pinecone 账户。

  2. Pinecone 项目:注册后,生成 API 密钥并创建索引。配置时需要这些详细信息。

  3. 用于计算文档嵌入的 EmbeddingModel 实例。有几种可用的选项:

    • 如有必要,生成并存储在 PineconeVectorStore 中嵌入所需的 EmbeddingModel 的 API 密钥。

要配置 PineconeVectorStore,请从你的 Pinecone 账户中获取以下详细信息:

  • Pinecone API Key

  • Pinecone Index 名称

  • Pinecone Namespace

备注

此信息可在 Pinecone UI 门户中查看。所有当前 Pinecone 计划均支持命名空间,包括免费的 Starter 计划;每个无服务器索引的最大命名空间数量取决于您的计划。

自动配置​

备注

Spring AI 的自动配置(auto-configuration)以及 Starter 模块的构件(artifact)名称发生了重大变更。详情请参阅升级说明。

Spring AI 为 Pinecone Vector Store 提供了 Spring Boot 自动配置功能。若要启用该功能,请向项目的 Maven pom.xml 文件中添加以下依赖项:

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

或添加到你的 Gradle build.gradle 构建文件中。

dependencies {
implementation 'org.springframework.ai:spring-ai-starter-vector-store-pinecone'
}
提示

请参阅 依赖管理 部分,以在您的构建文件中添加 Spring AI BOM。

提示

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

此外,你还需要一个已配置的 EmbeddingModel bean。更多信息请参考 EmbeddingModel 部分。

以下是所需的 Bean 示例:

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

要连接到 Pinecone,您需要提供实例的访问信息。可以通过 Spring Boot 的 application.properties 提供简单的配置,

spring.ai.vectorstore.pinecone.api-key=<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>

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

现在你可以自动注入(Auto-wire)Pinecone 向量存储(Vector Store)到你的应用中并使用它

@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.ai.vectorstore.pinecone.api-keyPinecone API Key-
spring.ai.vectorstore.pinecone.index-namePinecone 索引名称-
spring.ai.vectorstore.pinecone.namespacePinecone 命名空间-
spring.ai.vectorstore.pinecone.content-field-name用于存储原始文本内容的 Pinecone 元数据字段名。document_content
spring.ai.vectorstore.pinecone.distance-metadata-field-name用于存储计算距离的 Pinecone 元数据字段名。distance
spring.ai.vectorstore.pinecone.server-side-timeout20 秒

元数据过滤​

您可以借助通用的、可移植的 元数据过滤器 来使用 Pinecone 存储。

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

vectorStore.similaritySearch(
SearchRequest.builder()
.query("The World")
.topK(TOP_K)
.similarityThreshold(SIMILARITY_THRESHOLD)
.filterExpression("author in ['john', 'jill'] && article_type == 'blog'").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("author","john", "jill"),
b.eq("article_type", "blog")).build()).build());
备注

这些过滤表达式将被转换为等效的 Pinecone 过滤器。

手动配置​

如果你希望手动配置 PineconeVectorStore,可以通过使用 PineconeVectorStore#Builder 来实现。

在你的项目中添加以下依赖项:

  • OpenAI:用于计算嵌入,必需。
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>
  • Pinecone
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-pinecone-store</artifactId>
</dependency>
提示

请参阅 Dependency Management 章节,以在您的构建文件中添加 Spring AI BOM。

示例代码​

要在您的应用中配置 Pinecone,可以使用以下设置:

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

在主代码中,创建一些文档:

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:

vectorStore.add(documents);

最后,检索与查询相似的文档:

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

如果一切顺利,您应该能检索到包含文本 "Spring AI rocks!!" 的文档。

访问 Native Client​

Pinecone Vector Store 实现通过 getNativeClient() 方法提供对底层原生 Pinecone 客户端(Pinecone)的访问:

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

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

原生客户端提供了对 Pinecone 特定功能和操作的访问,这些功能和操作可能未通过 VectorStore 接口暴露。