最新快照版本请使用Spring AI 1.1.0spring-doc.cadn.net.cn

Pinecone

本节将带你了解如何搭建PineconeVectorStore用于存储文档嵌入并进行相似性搜索。spring-doc.cadn.net.cn

Pinecone是一个流行的云端向量数据库,可以高效地存储和搜索向量。spring-doc.cadn.net.cn

前提条件

  1. Pinecone账户:开始之前,先注册松果账户spring-doc.cadn.net.cn

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

  3. 嵌入模型实例用于计算文档嵌入。有几种选择:spring-doc.cadn.net.cn

准备Pinecone矢量商店请从您的Pinecone账户中收集以下信息:spring-doc.cadn.net.cn

这些信息可在Pinecone界面门户中获取。 Pinecone免费版中不支持命名空间。spring-doc.cadn.net.cn

自动配置

春季AI自动配置、起始模块的工件名称发生了重大变化。 更多信息请参阅升级说明spring-doc.cadn.net.cn

Spring AI 为Pinecone向量商店提供 Spring Boot 自动配置。 要启用它,请在项目的 Maven 中添加以下依赖pom.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的物料清单添加到你的构建文件中。
请参阅“遗物仓库”部分,将Maven Central和/或快照仓库添加到你的构建文件中。

此外,你还需要一个配置嵌入模型豆。更多信息请参阅嵌入模型部分。spring-doc.cadn.net.cn

这里是所需豆子的一个例子: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

距离spring-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());

或者程序化地使用滤波。表达DSL: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());
这些滤波表达式被转换为等效的PineconeFilter。

手动配置

如果你喜欢配置Pinecone矢量商店手动作时,你可以使用以下作Pinecone向量商店#建造者.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的物料清单添加到你的构建文件中。

示例代码

要在你的应用中配置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());

如果一切顺利,你应该取回包含“春季AI超棒!!”文字的文档。spring-doc.cadn.net.cn

访问本地客户端

Pinecone 向量存储实现提供了对底层原生 Pinecone 客户端的访问(Pinecone连接)通过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