Pinecone

このセクションでは、ドキュメントの埋め込みを保存し、類似性検索を実行するための Pinecone VectorStore のセットアップについて説明します。

Pinecone (英語) は人気のあるクラウドベースのベクトルデータベースで、ベクトルを効率的に保存および検索できます。

前提条件

  1. Pinecone アカウント: 始める前に、Pinecone アカウント (英語) にサインアップしてください。

  2. Pinecone プロジェクト: 登録したら、新しいプロジェクトとインデックスを作成し、API キーを生成します。構成にはこれらの詳細が必要になります。

  3. EmbeddingModel インスタンスを使用してドキュメントの埋め込みを計算します。いくつかのオプションが利用可能です:

    • 必要に応じて、EmbeddingModel が PineconeVectorStore によって保存される埋め込みを生成するための API キー。

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 をビルドファイルに追加するには、"依存関係管理" セクションを参照してください。
マイルストーンおよび / またはスナップショットリポジトリをビルドファイルに追加するには、リポジトリセクションを参照してください。

さらに、設定済みの 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(List.of(document));

// Retrieve documents similar to a query
List<Document> results = 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.namespace

Pinecone 名前空間

-

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

Pinecone metadata field name used to store the original text content.

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 スターターをプロジェクトに追加して、OpenAI の埋め込みと統合します。これにより、埋め込みクライアントの実装が提供されます。

@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(List.of(document));

最後に、クエリに似たドキュメントを取得します。

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

すべてがうまくいけば、"Spring AI rocks!!" というテキストを含むドキュメントを取得する必要があります。