Google Cloud Vertex AI ランキングAPI
紹介
Google Cloud Vertex AI ランキングAPIは、特定のクエリに対して取得されたドキュメントの関連性を洗練することで検索結果を強化する強力なツールです。 従来の検索方法とは異なり、高度な機械学習アルゴリズムを活用してクエリとドキュメントの両方の意味的コンテキストを理解し、より正確で関連性の高い結果を提供します。 クエリと各ドキュメント間の意味的関係を分析することで、APIは計算された関連性スコアに基づいて候補ドキュメントを並べ替え、最も関連性の高い結果が検索結果ページの上部に表示されるようにします。
Maven依存関係
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-vertex-ai</artifactId>
<version>1.23.0-beta33</version>
</dependency>
使用方法
モデルを設定するには、以下を指定する必要があります:
- Google CloudプロジェクトのプロジェクトID
- プロジェクト番号
- ロケーション(例:
us-central1、europe-west1) - 使用したいモデル
注:プロジェクト番号はGoogle Cloudコンソールで確認するか、
gcloud projects describe your-project-idを実行して確認できます。
score(text, query)およびscore(segment, query)メソッドを使用して、
単一の文字列またはTextSegmentをクエリに対してスコアリングできます。
scoreAll(segments, query)メソッドを使用して、
複数の文字列またはTextSegmentをクエリに対してスコアリングすることも可能です:
VertexAiScoringModel scoringModel = VertexAiScoringModel.builder()
.projectId(System.getenv("GCP_PROJECT_ID"))
.projectNumber(System.getenv("GCP_PROJECT_NUM"))
.projectLocation(System.getenv("GCP_LOCATION"))
.model("semantic-ranker-512")
.build();
Response<List<Double>> score = scoringModel.scoreAll(Stream.of(
"The sky appears blue due to a phenomenon called Rayleigh scattering. " +
"Sunlight is comprised of all the colors of the rainbow. Blue light has shorter " +
"wavelengths than other colors, and is thus scattered more easily.",
"A canvas stretched across the day,\n" +
"Where sunlight learns to dance and play.\n" +
"Blue, a hue of scattered light,\n" +
"A gentle whisper, soft and bright."
).map(TextSegment::from).collect(Collectors.toList()),
"Why is the sky blue?");
// [0.8199999928474426, 0.4300000071525574]
特定のtitleキーを持つTextSegmentを渡すと、ランカーモデルはこのメタデータを計算に考慮することができます。
カスタムタイトルキーを指定するには、titleMetadataKey()ビルダーメソッドを使用できます。
AiServicesとそのcontentAgregator()メソッドでスコアリングモデルを使用できます。
このメソッドはスコアリングモデルを指定できるContentAggregatorクラスを取ります:
VertexAiScoringModel scoringModel = VertexAiScoringModel.builder()
.projectId(System.getenv("GCP_PROJECT_ID"))
.projectNumber(System.getenv("GCP_PROJECT_NUM"))
.projectLocation(System.getenv("GCP_LOCATION"))
.model("semantic-ranker-512")
.build();
ContentAggregator contentAggregator = ReRankingContentAggregator.builder()
.scoringModel(scoringModel)
...
.build();
RetrievalAugmentor retrievalAugmentor = DefaultRetrievalAugmentor.builder()
...
.contentAggregator(contentAggregator)
.build();
return AiServices.builder(Assistant.class)
.chatModel(...)
.retrievalAugmentor(retrievalAugmentor)
.build();