Pinecone
本节将指导您设置 Pinecone VectorStore 来存储文档嵌入并执行相似性搜索。
Pinecone 是一个流行的基于云的向量数据库,允许您高效地 存储和搜索向量。
Prerequisites
- Pinecone 账户:在开始之前,请注册一个 Pinecone 账户。
- Pinecone 项目:注册后,生成 API key 并创建索引。您需要这些详细信息进行配置。
- 用于计算文档嵌入的
EmbeddingModel实例。有多个选项可用:
- 如果需要,为 EmbeddingModel 提供一个 API key,用于生成
PineconeVectorStore存储的嵌入。
要设置 PineconeVectorStore,请从您的 Pinecone 账户收集以下详细信息:
- Pinecone API Key
- Pinecone Index Name
- Pinecone Namespace
注意: 此信息在 Pinecone UI 门户中可用。 Pinecone 免费版不支持 namespace。
Auto-configuration
注意: Spring AI auto-configuration、starter modules 的 artifact 名称发生了重大变化。 请参阅 upgrade notes 了解更多信息。
Spring AI 为 Pinecone Vector Store 提供 Spring Boot auto-configuration。
要启用它,请将以下依赖项添加到您项目的 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'
}
提示: 请参阅 Dependency Management 部分,将 Spring AI BOM 添加到您的构建文件中。
提示: 请参阅 Artifact Repositories 部分,将 Maven Central 和/或 Snapshot Repositories 添加到您的构建文件中。
此外,您需要一个配置的 EmbeddingModel bean。请参阅 EmbeddingModel 部分了解更多信息。
以下是所需 bean 的示例:
@Bean
public EmbeddingModel embeddingModel() {
// 可以是任何其他 EmbeddingModel 实现。
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,例如 OpenAI
spring.ai.openai.api.key=<api-key>
请查看 configuration parameters 列表以了解向量存储的默认值和配置选项。
现在您可以在应用程序中自动装配 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")));
// 添加文档
vectorStore.add(documents);
// 检索与查询相似的文档
List<Document> results = this.vectorStore.similaritySearch(SearchRequest.builder().query("Spring").topK(5).build());