跳到主要内容

Pinecone

DeepSeek V3 中英对照 Pinecone

本节将引导您如何设置 Pinecone VectorStore 来存储文档嵌入并执行相似性搜索。

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

前提条件

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

  2. Pinecone 项目:注册成功后,创建一个新项目、一个索引,并生成一个 API 密钥。你将需要这些详细信息进行配置。

  3. EmbeddingModel 实例用于计算文档嵌入。有以下几种可选方案:

    • 如果需要,可以为 EmbeddingModel 提供一个 API 密钥,以生成由 PineconeVectorStore 存储的嵌入。

要设置 PineconeVectorStore,请从您的 Pinecone 账户中收集以下详细信息:

  • Pinecone API 密钥

  • Pinecone 环境

  • Pinecone 项目 ID

  • Pinecone 索引名称

  • Pinecone 命名空间

备注

此信息可在 Pinecone UI 门户中获取。Pinecone 免费版不支持命名空间功能。

自动配置

Spring AI 提供了对 Pinecone 向量存储的 Spring Boot 自动配置。要启用它,请将以下依赖项添加到项目的 Maven pom.xml 文件中:

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

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

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

请参考 依赖管理 部分,将 Spring AI BOM 添加到你的构建文件中。

提示

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

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

以下是一个所需的 bean 示例:

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

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

spring.ai.vectorstore.pinecone.apiKey=<your api key>
spring.ai.vectorstore.pinecone.environment=<your environment>
spring.ai.vectorstore.pinecone.projectId=<your project id>
spring.ai.vectorstore.pinecone.index-name=<your index name>

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

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

现在你可以在应用程序中自动连接 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());
java

配置属性

你可以在 Spring Boot 配置中使用以下属性来自定义 Pinecone 向量存储。

属性描述默认值
spring.ai.vectorstore.pinecone.api-keyPinecone API 密钥-
spring.ai.vectorstore.pinecone.environmentPinecone 环境gcp-starter
spring.ai.vectorstore.pinecone.project-idPinecone 项目 ID-
spring.ai.vectorstore.pinecone.index-namePinecone 索引名称-
spring.ai.vectorstore.pinecone.namespacePinecone 命名空间-
spring.ai.vectorstore.pinecone.content-field-namePinecone 元数据字段名称,用于存储原始文本内容。document_content
spring.ai.vectorstore.pinecone.distance-metadata-field-namePinecone 元数据字段名称,用于存储计算的距离。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());
java

或通过编程方式使用 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());
java
备注

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

手动配置

如果你更倾向于手动配置 PineconeVectorStore,你可以使用 PineconeVectorStore#Builder 来实现。

将这些依赖项添加到你的项目中:

  • OpenAI:用于计算嵌入(embeddings)。
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
xml
  • Pinecone
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-pinecone-store</artifactId>
</dependency>
xml
提示

请参考依赖管理部分,将 Spring AI BOM 添加到你的构建文件中。

示例代码

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

@Bean
public VectorStore pineconeVectorStore(EmbeddingModel embeddingModel) {
return PineconeVectorStore.builder(embeddingModel)
.apiKey(PINECONE_API_KEY)
.projectId(PINECONE_PROJECT_ID)
.environment(PINECONE_ENVIRONMENT)
.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();
}
java

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

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

将文档添加到 Pinecone:

index.upsert(vectors)
python
vectorStore.add(documents);
java

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

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

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

访问 Native Client

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

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
}
java

原生客户端让您能够访问 Pinecone 特有的功能和操作,这些功能和操作可能无法通过 VectorStore 接口暴露出来。