Pinecone

本節將引導您設定 Pinecone VectorStore,以儲存文件嵌入並執行相似性搜尋。

Pinecone 是一個熱門的雲端向量資料庫,可讓您有效率地儲存和搜尋向量。

先決條件

  1. Pinecone 帳戶:在開始之前,請註冊一個 Pinecone 帳戶

  2. Pinecone 專案:註冊後,建立一個新專案、索引,並產生 API 金鑰。您將需要這些詳細資訊進行設定。

  3. 用於計算文件嵌入的 EmbeddingModel 實例。有多種選項可供選擇

    • 如果需要,用於 EmbeddingModel 的 API 金鑰,以產生由 PineconeVectorStore 儲存的嵌入。

若要設定 PineconeVectorStore,請從您的 Pinecone 帳戶收集以下詳細資訊

  • Pinecone API 金鑰

  • Pinecone 環境

  • Pinecone 專案 ID

  • Pinecone 索引名稱

  • Pinecone 命名空間

此資訊可在 Pinecone UI 入口網站中找到。

自動設定

Spring AI 為 Pinecone 向量儲存庫提供 Spring Boot 自動設定。若要啟用它,請將以下依賴項新增至您專案的 Maven pom.xml 檔案

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

或您的 Gradle build.gradle 建置檔案。

dependencies {
    implementation 'org.springframework.ai:spring-ai-pinecone-store-spring-boot-starter'
}
請參閱依賴項管理章節,將 Spring AI BOM 新增至您的建置檔案。
請參閱儲存庫章節,將 Milestone 和/或 Snapshot 儲存庫新增至您的建置檔案。

此外,您還需要一個已設定的 EmbeddingModel Bean。請參閱EmbeddingModel 章節以取得更多資訊。

以下是所需 Bean 的範例

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

若要連線到 Pinecone,您需要提供您實例的存取詳細資訊。可以透過 Spring Boot 的 application.properties 提供簡單的設定,

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>

請查看組態參數列表,以了解向量儲存庫的預設值和組態選項。

現在您可以在您的應用程式中自動注入 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.query("Spring").withTopK(5));

組態屬性

您可以使用 Spring Boot 組態中的以下屬性來自訂 Pinecone 向量儲存庫。

屬性 描述 預設值

spring.ai.vectorstore.pinecone.api-key

Pinecone API 金鑰

-

spring.ai.vectorstore.pinecone.environment

Pinecone 環境

gcp-starter

spring.ai.vectorstore.pinecone.project-id

Pinecone 專案 ID

-

spring.ai.vectorstore.pinecone.index-name

Pinecone 索引名稱

-

spring.ai.vectorstore.pinecone.namespace

Pinecone 命名空間

-

spring.ai.vectorstore.pinecone.content-field-name

用於儲存原始文字內容的 Pinecone 元數據欄位名稱。

document_content

spring.ai.vectorstore.pinecone.distance-metadata-field-name

用於儲存計算距離的 Pinecone 元數據欄位名稱。

distance

spring.ai.vectorstore.pinecone.server-side-timeout

20 秒。

元數據過濾

您可以將通用的、可移植的元數據篩選器與 Pinecone 儲存庫一起使用。

例如,您可以使用文字表達式語言

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

或以程式方式使用 Filter.Expression DSL

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()));
這些篩選器表達式會轉換為等效的 Pinecone 篩選器。

手動設定

如果您希望手動設定 PineconeVectorStore,您可以透過建立 PineconeVectorStoreConfig Bean 並將其傳遞給 PineconeVectorStore 建構函式來完成。

將這些依賴項新增至您的專案

  • OpenAI:計算嵌入時需要。

<dependency>
	<groupId>org.springframework.ai</groupId>
	<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
  • Pinecone

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-pinecone-store</artifactId>
</dependency>
請參閱依賴項管理章節,將 Spring AI BOM 新增至您的建置檔案。

範例程式碼

若要在您的應用程式中設定 Pinecone,您可以使用以下設定

@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();
}

透過將 Spring Boot OpenAI Starter 新增至您的專案,與 OpenAI 的嵌入整合。這為您提供了 Embeddings 用戶端的實作

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

在您的主要程式碼中,建立一些文件

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").withTopK(5));

如果一切順利,您應該會檢索到包含文字 "Spring AI rocks!!" 的文件。