此版本仍在开发中,尚未被视为稳定版。为了获取最新的快照版本,请使用Spring AI 1.1.3spring-doc.cadn.net.cn

Pinecone

本部分将指导您设置Pinecone 0,用于存储文档嵌入和进行相似性搜索。spring-doc.cadn.net.cn

Pinecone 是一种流行的云向量数据库,允许您高效地存储和搜索向量。spring-doc.cadn.net.cn

前提条件

  1. Pinecone 账号:在开始之前,请您注册一个 Pinecone 账号spring-doc.cadn.net.cn

  2. Pinecone 项目: 一旦注册后,生成一个API密钥并创建索引。这些细节将用于配置。spring-doc.cadn.net.cn

  3. spring-doc.cadn.net.cn

    0个实例用于计算文档嵌入。几种选项都可用:spring-doc.cadn.net.cn

    spring-doc.cadn.net.cn

    • 如果需要,获取用于在存储于PineconeVectorStore中的嵌入模型的API密钥。spring-doc.cadn.net.cn

To set up PineconeVectorStore, collect the following details from your Pinecone account:spring-doc.cadn.net.cn

该信息可通过Pinecone用户界面门户访问。 Pinecone免费试用版不支持命名空间支持。spring-doc.cadn.net.cn

自动配置

There has been a significant change in the Spring AI auto-configuration, starter modules' artifact names. Please refer to the 升级说明以获取更多信息。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>

或者添加到您的Gradle 构建脚本文件中。spring-doc.cadn.net.cn

dependencies {
    implementation 'org.springframework.ai:spring-ai-starter-vector-store-pinecone'
}
参考以下依赖管理部分,添加Spring AI BOM到你的构建文件中。
引用 Artifact 仓库 部分,以在构建文件中添加 Maven Central 和/或 Snapshot 仓库。

此外,您还需要配置一个EmbeddingModel的豆。有关详细信息,请参阅EmbeddingModel部分。spring-doc.cadn.net.cn

以下是所需Bean的一个示例: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>

请查看以下向量存储的配置参数列表,了解默认值和配置选项。这些配置参数的类型为C0。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 配置密钥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

distancespring-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());

或通过编程方式使用 Filter.Expression 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());
这些过滤表达式被转换为等效的Pinecone过滤器。

手动配置

如果你愿意手动配置 PineconeVectorStore,你可以使用 PineconeVectorStore#Builderspring-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 BOM到你的构建文件中。

示例代码

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

如果一切顺利,你应该能获取到包含文本“Spring AI rocks!!”的文档。spring-doc.cadn.net.cn

访问原生客户端

pinecone 向量存储实现提供了通过底层 native pinecone 客户端(0)使用 1 方法访问的接口: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
}

The native client gives you access to Pinecone-specific features and operations that might not be exposed through the VectorStore interface.spring-doc.cadn.net.cn