MongoDB Java Sync Driver を使用して、 MongoDB ベクトル検索 をspring AIと統合し、生成系AIアプリケーションを構築できます。このチュートリアルでは、spring AIのベクトルストアとしてMongoDB ベクトル検索 の使用を開始する方法と、データに対してセマンティック検索を実行する方法を説明します。
具体的には、次のアクションを実行します。
環境を設定します。
MongoDB ベクトル検索インデックスを作成します。
MongoDBにベクトル埋め込みデータを保存します。
データに対してセマンティック検索クエリを実行します。
バックグラウンド
spring AI は、アプリケーションでさまざまなAIサービスとプラグインを組み合わせることができる Apple のアプリケーションフレームワークです。spring AI は、テキストベースのさまざまなAIユースケースに使用できます。
MongoDB をベクトルデータベースとして使用し、MongoDB ベクトル検索を使用して、データから意味的に類似したドキュメントを検索することで RAG を実装できます。RAG の詳細については、「MongoDB による検索拡張生成(RAG)」を参照してください。
前提条件
Atlas の サンプル データ セット からの映画データを含むコレクションを使用します。
次のいずれかのMongoDBクラスター タイプ
MongoDB バージョン 6.0.11 を実行している Atlas クラスター、7.0.2以降。IP アドレスが Atlas プロジェクトのアクセスリストに含まれていることを確認します。
Python と Docker を使用して作成されたローカル Atlas 配置。
atlas-local-lib-py(pip install atlas-local-lib-py) をインストールして、ローカル配置をプログラムで作成および管理します。詳細については、atlas-local-lib-py リポジトリを参照してください。検索とベクトル検索がインストールされた MongoDB Community クラスター。
OpenAI APIキー。APIリクエストに使用できるクレジットを持つ OpenAI アカウントが必要です。OpenAI アカウントの登録の詳細については、OpenAI APIウェブサイト を参照してください。
Java 開発キット(JDK) バージョン8 以降。
Javaアプリケーションを設定して実行する環境。Maven または Gradle を構成してプロジェクトを構築および実行するようにするには、IntelliJ IDEA や Eclipse IDE などの統合開発環境(IDE)を使用することをお勧めします。
環境を設定する
最初に、必要な依存関係の追加や構成プロパティの設定など、このチュートリアルの環境を設定する必要があります。
spring Java アプリケーションを作成します。
スキームを初期化するために移動し、プロジェクトを次の設定で構成します。
プロジェクト: Maven
言語: Java
スキームを使用することで、選択したデフォルトのバージョンを使用できます。
プロジェクト メタデータ:
Java: 21
他のすべてのフィールドにデフォルト値を使用できます。
書込み初期化の右側にある [ ADD DEPENDENCIES ] をクリックし、次の依存関係を検索して追加します。
MongoDB Atlas Vector Database
Spring Data MongoDB
[GENERATE] をクリックして、Spring プロジェクトの zip バージョンをダウンロードします。ファイルを解凍し、IDE で開きます。
依存関係を追加します。
書込みAI は、 MongoDB ベクトル検索のspring ブート自動構成を提供します。
プロジェクトの
pom.xmlファイルのdependencies配列に次の依存関係を追加します。 これらの依存関係により、spring AI と自動構成ライブラリがアプリケーションに追加されます。pom.xml
<dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-openai-spring-boot-starter</artifactId></dependency> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-spring-boot-autoconfigure</artifactId></dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency>最後に、spring AI スナップショットのリポジトリを
pom.xmlファイルのrepositoriesエントリに追加します。pom.xml
<repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <releases> <enabled>false</enabled> </releases></repository>これらのリポジトリの詳細については、spring AIドキュメントの「 マイルストーンとスナップショット リポジトリの追加 」セクションを参照してください。
pom.xmlファイルの編集が終了したら、プロジェクトを再読み込みして、依存関係がインストールされていることを確認します。
アプリケーション プロパティを定義します。
src/main/resources/application.properties ファイルを見つけ、そのファイルの内容を次のプロパティで置き換えます。 プレースホルダーを OpenAI APIキーとAtlas 接続文字列で置き換えます。
src/main/resources/application.properties
spring.application.name=springai-mongodbspring.ai.openai.api-key=<OpenAI API Key>spring.ai.openai.embedding.options.model=text-embedding-ada-002
spring.data.mongodb.uri=<connection string>spring.data.mongodb.database=springai_testspring.ai.vectorstore.mongodb.indexName=vector_indexspring.ai.vectorstore.mongodb.collection-name=vector_store注意
接続stringには、次の形式を使用する必要があります。
mongodb+srv://<db_username>:<db_password>@<clusterName>.<hostname>.mongodb.net/?<settings>接続文字列の取得の詳細については、接続文字列ガイドを参照してください。
MongoDB ベクトル検索インデックスの作成
ベクトルストアでベクトル検索クエリを有効にするには、springai_test.vector_storeコレクションにMongoDB ベクトル検索インデックスを作成する必要があります。
スキーマの初期化を有効にします。
アプリケーションで Atlas をベクトルストアとして構成すると、spring AI はバックエンドスキーマを自動的に初期化できます。この初期化には、ベクトル埋め込みを含むコレクションにMongoDB ベクトル検索インデックスを作成することが含まれます。
スキーマの初期化を有効にするため、次の設定を application.properties ファイルに追加します。
src/main/resources/application.properties
spring.ai.vectorstore.mongodb.initialize-schema=trueinitialize-schema=true を指定すると、spring AIはクラスターにプログラムによってMongoDB ベクトル検索インデックスを作成します。詳細については、 MongoDB ベクトル検索インデックスの作成 を参照してください。
注意
既知の問題: 既存のインデックス
springai_test.vector_storeコレクションに vector_index という既存のMongoDB ベクトル検索インデックスがある場合、spring AI は追加のインデックスを作成しません。このため、既存のインデックスが別の次元数など互換性のない設定で構成されていた場合は、チュートリアルの後半でエラーが発生する可能性があります。
インデックスが次の構成であることを確認します。
{ "fields": [ { "numDimensions": 1536, "path": "embedding", "similarity": "cosine", "type": "vector" } ]}MongoDB をベクトル ストアとして使用
このセクションでは、 MongoDB をベクトルデータベース(ベクトルストアとも呼ばれます)として構成し、カスタム データのベクトル埋め込みを保存する方法を説明します。
プロジェクトでsrc/main/java/com/example/demo/DemoApplication.javaファイルを見つけます。 このファイルと同じレベルで、 configというディレクトリを作成し、このディレクトリにConfig.javaというファイルを作成して、spring App 構成を設定します。
次の手順は、ベクトル ストアを準備するために必要なBeanオブジェクトを作成する方法を示しています。
インポート ステートメントを追加します。
次のコードをConfig.javaファイルに貼り付けて、必要なクラスをインポートします。
/config/Config.java
import org.springframework.ai.embedding.EmbeddingModel;import org.springframework.ai.openai.OpenAiEmbeddingModel;import org.springframework.ai.openai.api.OpenAiApi;import org.springframework.ai.vectorstore.MongoDBAtlasVectorStore;import org.springframework.ai.vectorstore.VectorStore;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.SpringBootConfiguration;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.mongodb.core.MongoTemplate;参照アプリケーションのプロパティ。
次のコードをConfig.javaファイルに貼り付けて、アプリケーション プロパティ ファイルに設定した値を参照します。
/config/Config.java
@Configuration@SpringBootConfiguration@EnableAutoConfigurationpublic class Config {
@Value("${spring.ai.openai.api-key}") private String openAiKey;
@Value("${spring.data.mongodb.database}") private String databaseName;
@Value("${spring.ai.vectorstore.mongodb.collection-name:vector_store}") private String collectionName;
@Value("${spring.ai.vectorstore.mongodb.indexName:vector_index}") private String indexName;
@Value("${spring.data.mongodb.uri}") private String mongoUri;
@Value("${spring.ai.vectorstore.mongodb.initialize-schema}") private Boolean initSchema;
// Add beans here...
}VectorStore Spring Bean を作成します。
最後に、次のコードを貼り付けて、 VectorStoreインスタンスを返す Bearer を作成します。 VectorStoreインスタンスは、配置に対応するMongoTemplateと、前のステップで作成されたOpenAiEmbeddingModelを使用します。
/config/Config.java
@Beanpublic VectorStore mongodbVectorStore(MongoTemplate mongoTemplate, EmbeddingModel embeddingModel) { return new MongoDBAtlasVectorStore(mongoTemplate, embeddingModel, MongoDBAtlasVectorStore.MongoDBVectorStoreConfig.builder().build(), initSchema);}カスタム データの保存とセマンティック検索クエリの実行
このセクションでは、 Javaアプリケーションでエンドポイントを作成してMongoDBにカスタム データのベクトル埋め込みを保存し、そのデータに対してセマンティック検索クエリーを実行する方法を学びます。
エンドポイントの作成
configフォルダーと同じレベルで、 controllerフォルダーを作成し、API エンドポイントを設定するためのController.javaファイルを作成します。 次の手順は、ベクトル ストアにデータを追加し、 similaritySearch()メソッドを使用してセマンティック検索クエリを実行するためのGETエンドポイントを作成する方法を示しています。
インポート ステートメントを追加します。
次のコードをController.javaファイルに貼り付けて、必要なクラスをインポートします。
/controller/Controller.java
import org.springframework.ai.document.Document;import org.springframework.ai.vectorstore.VectorStore;import org.springframework.ai.vectorstore.SearchRequest;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;
import java.util.List;import java.util.Map;import java.util.stream.Collectors;コントローラーを作成します。
次のコードを貼り付けて、次のタスクを実行します。
Controllerクラスに注釈を付けて、アプリケーション コントローラーとしてマークします。リクエストを
/tutorialパスにマッピングするためのマッピングを作成します。VectorStorebean を自動ワイヤ化します。
/controller/Controller.java
@RestController@RequestMapping("/tutorial")public class Controller { @Autowired private VectorStore vectorStore;
// Add endpoints here...}ベクトル ストアにドキュメントを追加するためのエンドポイントを作成します。
次のコードをコントローラーに貼り付けて、サンプル ドキュメントを作成し、それをベクトル埋め込みとしてベクトルストアに保存するGETエンドポイントを作成します。
/controller/Controller.java
@GetMapping("/add")public String addDocuments() { List<Document> docs = List.of( new Document("Proper tuber planting involves site selection, proper timing, and exceptional care. Choose spots with well-drained soil and adequate sun exposure. Tubers are generally planted in spring, but depending on the plant, timing varies. Always plant with the eyes facing upward at a depth two to three times the tuber's height. Ensure 4 inch spacing between small tubers, expand to 12 inches for large ones. Adequate moisture is needed, yet do not overwater. Mulching can help preserve moisture and prevent weed growth.", Map.of("author", "A", "type","post")), new Document("Successful oil painting necessitates patience, proper equipment, and technique. Begin with a carefully prepared, primed canvas. Sketch your composition lightly before applying paint. Use high-quality brushes and oils to create vibrant, long-lasting artworks. Remember to paint 'fat over lean,' meaning each subsequent layer should contain more oil to prevent cracking. Allow each layer to dry before applying another. Clean your brushes often and avoid solvents that might damage them. Finally, always work in a well-ventilated space.", Map.of("author", "A")), new Document("For a natural lawn, selection of the right grass type suitable for your climate is crucial. Balanced watering, generally 1 to 1.5 inches per week, is important; overwatering invites disease. Opt for organic fertilizers over synthetic versions to provide necessary nutrients and improve soil structure. Regular lawn aeration helps root growth and prevents soil compaction. Practice natural pest control and consider overseeding to maintain a dense sward, which naturally combats weeds and pest.", Map.of("author", "B", "type","post")) );
vectorStore.add(docs); return "Documents added successfully!\n";}セマンティック検索を実行するためのエンドポイントを作成します。
次のコードをコントローラーに貼り付けて、 "learn how to grow things"というフレーズのセマンティック検索クエリを実行し、最も関連性の高い結果を 2 つ返すGETエンドポイントを作成します。
/controller/Controller.java
@GetMapping("/search")public List<Map<String, Object>> searchDocuments() {
List<Document> results = vectorStore.similaritySearch( SearchRequest .query("learn how to grow things") .withTopK(2) );
return results.stream().map(doc -> Map.of( "content", doc.getContent(), "metadata", doc.getMetadata() )).collect(Collectors.toList());}(任意)メタデータ フィルタリングを使用してセマンティック検索を実行します。
メタデータ フィルタリングを使用して検索を実行するには、 Java Sync Driverの Filter.Expression ビルダ クラスを使用できます。
MQL マッチ式を使用して、ドキュメントを事前フィルターすることができます。この例では、author フィールドの値が "A" であるドキュメントをフィルターします。その後、"learn how to grow things" というフレーズに対してセマンティック検索クエリを実行します。
searchDocuments()前のステップで定義されている メソッドの本体で、similaritySearch() メソッドを呼び出すコード(前のブロックの4 -8 行)を次のコードに置き換えます。
/controller/Controller.java
FilterExpressionBuilder b = new FilterExpressionBuilder();
List<Document> results = vectorStore.similaritySearch( SearchRequest.defaults() .withQuery("learn how to grow things") .withTopK(2) .withSimilarityThreshold(0.5) .withFilterExpression(b.eq("author", "A").build()));注意
メタデータフィールドのパスをMongoDB ベクトル検索インデックスに追加する必要があります。「フィルター型について」の「ベクトル検索のフィールドにインデックスを作成する方法」セクションを参照してください。
メタデータの事前フィルターについてさらに学ぶには、MongoDB ベクトル検索のフィルターを参照してください。
アクセス エンドポイント
アプリケーションを実行すると、 エンドポイント にアクセスしてまずベクトル ストアにドキュメントを追加し、次にセマンティック検索クエリを実行できます。
ターミナル内からエンドポイントにアクセスします。
アプリケーションが実行されていることを確認したら、ターミナルで次のコマンドを実行してaddエンドポイントにアクセスします。これにより、サンプル データがベクトル埋め込みに変換され、Atlas に埋め込みが挿入されます。
curl -X GET http://localhost:8080/tutorial/addTip
Atlas を使用している場合は、エンドポイントにアクセスした後、Atlas UIで springai_test.vector_store名前空間に移動することでベクトル埋め込みを確認できます。
次に、ターミナルで次のコマンドを実行してsearchエンドポイントにアクセスし、セマンティック検索を実行します。
curl -X GET http://localhost:8080/tutorial/search次のステップ
Githubからこのアプリの完了したバージョンを表示してダウンロードできます。アプリを使用して、自分のアプリケーションのトラブルシューティングを行ったり、機能をすばやくテストしたりできます。
MongoDBは、次の開発者リソースも提供しています。