Pinecone
本节将指导您如何设置 Pinecone
向量存储库,以存储文档嵌入
并执行相似性搜索
。
Pinecone 是什么?
Pinecone 是一款广受欢迎的云端向量数据库,它使您能够高效地存储和搜索向量。
先决条件
- Pinecone 帐户:开始之前,请注册一个Pinecone 帐户。
- Pinecone 项目:注册后,创建一个新项目、一个索引并生成一个 API 密钥。您将需要这些详细信息进行配置。
- OpenAI 帐户:如果你使用 OpenAI 来创建文档嵌入,你还需要在 OpenAI 注册处创建帐户并在 API Keys 处生成令牌。
配置
要设置 PineconeVectorStore
,请从您的 Pinecone 账户中收集以下详细信息:
- Pinecone API Key
- Pinecone Environment
- Pinecone Project ID
- Pinecone Index Name
- Pinecone Namespace
注意:您可以在 Pinecone UI 门户中获取此信息
当设置嵌入时,请选择 1536
向量维度。这与 OpenAI 的 text-embedding-ada-002
模型的维度相匹配,我们将在本指南中使用该模型。
此外,您还需要提供 OpenAI API 密钥。将其设置为环境变量,如下所示:
export SPRING_AI_OPENAI_API_KEY='Your_OpenAI_API_Key'
自动配置
Spring AI 为 Pinecone 向量存储提供了 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'
}
此外,您还需要一个配置好的 EmbeddingModel bean。更多信息请参考 EmbeddingModel 部分。
以下是所需 bean 的示例:
@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.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>
现在,您可以在应用程序中自动装配 Pinecone 向量存储并使用它。
@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 向量存储。
元数据过滤
您可以利用通用的、可移植的 元数据过滤器 与 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());
手动配置
若您倾向于手动配置 PineconeVectorStore,可通过使用 PineconeVectorStore# 构建器来实现。
将这些依赖项添加到您的项目中:
- 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>
示例代码
要在您的应用程序中配置 Pinecone,可以使用以下设置:
@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();
}
在你的主代码中,创建一些文档:
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.query("Spring").topK(5).build());
如果一切顺利,您应该能检索到包含 “Spring AI 太棒了!!” 文本的文档。
访问原生客户端
Pinecone 向量存储实现通过‘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
}
原生客户端为您提供访问 Pinecone 特有功能和操作的权限,这些功能可能未通过 VectorStore 接口公开。
最后编辑:Jeebiz 更新时间:2025-09-28 09:15