Pinecone

This section walks you through setting up the Pinecone VectorStore to store document embeddings and perform similarity searches.spring-doc.cn

Pinecone is a popular cloud-based vector database, which allows you to store and search vectors efficiently.spring-doc.cn

Prerequisites

  1. Pinecone Account: Before you start, sign up for a Pinecone account.spring-doc.cn

  2. Pinecone Project: Once registered, create a new project, an index, and generate an API key. You’ll need these details for configuration.spring-doc.cn

  3. EmbeddingModel instance to compute the document embeddings. Several options are available:spring-doc.cn

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

This information is available to you in the Pinecone UI portal.spring-doc.cn

Auto-configuration

Spring AI provides Spring Boot auto-configuration for the Pinecone Vector Store. To enable it, add the following dependency to your project’s Maven pom.xml file:spring-doc.cn

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

or to your Gradle build.gradle build file.spring-doc.cn

dependencies {
    implementation 'org.springframework.ai:spring-ai-pinecone-store-spring-boot-starter'
}
Refer to the Dependency Management section to add the Spring AI BOM to your build file.
Refer to the Repositories section to add Milestone and/or Snapshot Repositories to your build file.

Additionally, you will need a configured EmbeddingModel bean. Refer to the EmbeddingModel section for more information.spring-doc.cn

Here is an example of the needed bean:spring-doc.cn

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

To connect to Pinecone you need to provide access details for your instance. A simple configuration can either be provided via Spring Boot’s application.properties,spring-doc.cn

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>

Please have a look at the list of configuration parameters for the vector store to learn about the default values and configuration options.spring-doc.cn

Now you can Auto-wire the Pinecone Vector Store in your application and use itspring-doc.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.query("Spring").withTopK(5));

Configuration properties

You can use the following properties in your Spring Boot configuration to customize the Pinecone vector store.spring-doc.cn

Property Description Default value

spring.ai.vectorstore.pinecone.api-keyspring-doc.cn

Pinecone API Keyspring-doc.cn

-spring-doc.cn

spring.ai.vectorstore.pinecone.environmentspring-doc.cn

Pinecone environmentspring-doc.cn

gcp-starterspring-doc.cn

spring.ai.vectorstore.pinecone.project-idspring-doc.cn

Pinecone project IDspring-doc.cn

-spring-doc.cn

spring.ai.vectorstore.pinecone.index-namespring-doc.cn

Pinecone index namespring-doc.cn

-spring-doc.cn

spring.ai.vectorstore.pinecone.namespacespring-doc.cn

Pinecone namespacespring-doc.cn

-spring-doc.cn

spring.ai.vectorstore.pinecone.content-field-namespring-doc.cn

Pinecone metadata field name used to store the original text content.spring-doc.cn

document_contentspring-doc.cn

spring.ai.vectorstore.pinecone.distance-metadata-field-namespring-doc.cn

Pinecone metadata field name used to store the computed distance.spring-doc.cn

distancespring-doc.cn

spring.ai.vectorstore.pinecone.server-side-timeoutspring-doc.cn

20 sec.spring-doc.cn

Metadata filtering

You can leverage the generic, portable metadata filters with the Pinecone store.spring-doc.cn

For example, you can use either the text expression language:spring-doc.cn

vectorStore.similaritySearch(
    SearchRequest.defaults()
    .withQuery("The World")
    .withTopK(TOP_K)
    .withSimilarityThreshold(SIMILARITY_THRESHOLD)
    .withFilterExpression("author in ['john', 'jill'] && article_type == 'blog'"));

or programmatically using the Filter.Expression DSL:spring-doc.cn

FilterExpressionBuilder b = new FilterExpressionBuilder();

vectorStore.similaritySearch(SearchRequest.defaults()
    .withQuery("The World")
    .withTopK(TOP_K)
    .withSimilarityThreshold(SIMILARITY_THRESHOLD)
    .withFilterExpression(b.and(
        b.in("author","john", "jill"),
        b.eq("article_type", "blog")).build()));
These filter expressions are converted into the equivalent Pinecone filters.

Manual Configuration

If you prefer to configure the PineconeVectorStore manually, you can do so by creating a PineconeVectorStoreConfig bean and passing it to the PineconeVectorStore constructor.spring-doc.cn

Add these dependencies to your project:spring-doc.cn

<dependency>
	<groupId>org.springframework.ai</groupId>
	<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-pinecone-store</artifactId>
</dependency>
Refer to the Dependency Management section to add the Spring AI BOM to your build file.

Sample Code

To configure Pinecone in your application, you can use the following setup:spring-doc.cn

@Bean
public PineconeVectorStoreConfig pineconeVectorStoreConfig() {

    return PineconeVectorStoreConfig.builder()
        .withApiKey(<PINECONE_API_KEY>)
        .withEnvironment("gcp-starter")
        .withProjectId("89309e6")
        .withIndexName("spring-ai-test-index")
        .withNamespace("") // the free tier doesn't support namespaces.
        .withContentFieldName("my_content") // optional field to store the original content. Defaults to `document_content`
        .build();
}

Integrate with OpenAI’s embeddings by adding the Spring Boot OpenAI starter to your project. This provides you with an implementation of the Embeddings client:spring-doc.cn

@Bean
public VectorStore vectorStore(PineconeVectorStoreConfig config, EmbeddingModel embeddingModel) {
    return new PineconeVectorStore(config, embeddingModel);
}

In your main code, create some documents:spring-doc.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")));

Add the documents to Pinecone:spring-doc.cn

vectorStore.add(documents);

And finally, retrieve documents similar to a query:spring-doc.cn

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

If all goes well, you should retrieve the document containing the text "Spring AI rocks!!".spring-doc.cn