Pinecone

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

Pinecone 是一個流行的雲原生向量資料庫,它允許您高效地儲存和搜尋向量。

前提條件

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

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

  3. EmbeddingModel 例項,用於計算文件嵌入。有幾種選項可用

    • 如果需要,為 EmbeddingModel 提供 API 金鑰,以便生成由 PineconeVectorStore 儲存的嵌入。

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

  • Pinecone API 金鑰

  • Pinecone 索引名稱

  • Pinecone 名稱空間

這些資訊可在 Pinecone UI 門戶中獲取。Pinecone 免費套餐不支援名稱空間。

自動配置

Spring AI 自動配置、starter 模組的 artifact 名稱發生了重大變化。請參閱升級說明瞭解更多資訊。

Spring AI 為 Pinecone Vector Store 提供了 Spring Boot 自動配置。要啟用它,請將以下依賴項新增到專案的 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'
}
請參閱依賴項管理部分,將 Spring AI BOM 新增到您的構建檔案中。
請參閱倉庫部分,將 Maven Central 和/或 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.index-name=<your index name>

# API key if needed, e.g. OpenAI
spring.ai.openai.api.key=<api-key>

請檢視向量儲存的配置引數列表,瞭解預設值和配置選項。

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

// 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.ai.vectorstore.pinecone.api-key

Pinecone API 金鑰

-

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.builder()
    .query("The World")
    .topK(TOP_K)
    .similarityThreshold(SIMILARITY_THRESHOLD)
    .filterExpression("author in ['john', 'jill'] && article_type == 'blog'").build());

或使用 Filter.Expression DSL 進行程式設計

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#Builder 進行。

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

  • OpenAI:計算嵌入所需。

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

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-pinecone-store</artifactId>
</dependency>
請參閱依賴項管理部分,將 Spring AI BOM 新增到您的構建檔案中。

示例程式碼

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

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

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

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

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

訪問原生客戶端

Pinecone Vector Store 實現透過 getNativeClient() 方法提供對底層原生 Pinecone 客戶端(PineconeConnection)的訪問

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 介面暴露。