2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

DatabricksのVector Search Indexにおけるリランカーのサポート

Posted at

こちらのアップデートです。

Mosaic AI Vector Searchリランカーのパブリックプレビュー
収集の精度改善の助けとなるリランキングがMosaic AI Vector Searchでサポートされました。詳細はクエリーでリランカーを使用するをご覧ください。

公式ブログはこちら。

リランキングとは

リランキング機能は、RAGエージェントの検索品質を向上させる2段階の検索アプローチです。

従来の検索フローとリランキング導入後の違い:

項目 従来のベクトル検索のみ リランキング機能導入後
検索段階 1段階(ベクトル類似度検索のみ) 2段階(ベクトル検索→リランキング)
精度(Recall@10) 74% 89%(15ポイント向上)
処理内容 高速な類似度マッチング 高速検索後、深い文脈理解で再順位付け
実装の複雑さ シンプル パラメータ1つ追加でシンプル
レイテンシ 高速 50件で最短1.5秒

具体的な処理の流れは以下のとおりです:

  1. 第1段階: ベクトル検索 - 数百万件のドキュメントから高速に関連候補を抽出(例: 上位50件)
  2. 第2段階: リランキング - 抽出された候補に対して、より深い意味理解を適用し、真に関連性の高い順に並び替え

準備

プレビューVector Search Rerankeがオンになっていることを確認してください。

Screenshot 2025-08-19 at 10.03.50.png

ウォークスルー

こちらのサンプルノートブックをベースに実行してきます。

%pip install --upgrade --force-reinstall databricks-vectorsearch
dbutils.library.restartPython()
from databricks.vector_search.client import VectorSearchClient

vsc = VectorSearchClient()

サンプルデータセットをDeltaテーブルにロード

# 利用するカタログとスキーマを指定します。カタログにはUSE_CATALOG権限、スキーマにはUSE_SCHEMAとCREATE_TABLE権限が必要です。
# 必要に応じてカタログ名やスキーマ名を変更してください。

catalog_name = "takaakiyayoi_catalog"
schema_name = "vector_search"
# Deltaテーブルの完全なテーブル名を作成します。
source_table_name = "en_wiki"
source_table_fullname = f"{catalog_name}.{schema_name}.{source_table_name}"

Screenshot 2025-08-19 at 10.18.54.png

# Deltaテーブルとして保存します。Change Data Feedを有効化しています。
source_df.write.format("delta").option("delta.enableChangeDataFeed", "true").saveAsTable(source_table_fullname)

ベクトル検索エンドポイントの作成

# ベクトル検索エンドポイント名を指定します。
vector_search_endpoint_name = "one-env-shared-endpoint-1"
# ベクトル検索エンドポイントを作成します。
vsc.create_endpoint(
    name=vector_search_endpoint_name,
    endpoint_type="STANDARD" # または "STORAGE_OPTIMIZED"
)

ベクトルインデックスの作成

# ベクトルインデックス
vs_index = "en_wiki_index"
vs_index_fullname = f"{catalog_name}.{schema_name}.{vs_index}"

embedding_model_endpoint = "databricks-gte-large-en"
# Deltaテーブル同期型のベクトルインデックスを作成します。
index = vsc.create_delta_sync_index(
  endpoint_name=vector_search_endpoint_name,
  source_table_name=source_table_fullname,
  index_name=vs_index_fullname,
  pipeline_type='TRIGGERED',
  primary_key="id",
  embedding_source_column="text",
  embedding_model_endpoint_name=embedding_model_endpoint
)
index.describe()

ベクトルインデックスの取得

get_index()でインデックスオブジェクトを取得できます。describe()でインデックスの設定情報を確認できます。

# インデックスオブジェクトを取得し、設定情報を表示します。
index = vsc.get_index(endpoint_name=vector_search_endpoint_name, index_name=vs_index_fullname)

index.describe()
{'name': 'takaakiyayoi_catalog.vector_search.en_wiki_index',
 'endpoint_name': 'one-env-shared-endpoint-1',
 'primary_key': 'id',
 'index_type': 'DELTA_SYNC',
 'delta_sync_index_spec': {'source_table': 'takaakiyayoi_catalog.vector_search.en_wiki',
  'embedding_source_columns': [{'name': 'text',
    'embedding_model_endpoint_name': 'databricks-gte-large-en'}],
  'pipeline_type': 'TRIGGERED',
  'pipeline_id': '2d24c1d5-6148-4f13-a112-5e04efc3a2fb'},
 'status': {'detailed_state': 'PROVISIONING_PIPELINE_RESOURCES',
  'message': 'Index is currently pending setup of pipeline resources. Check latest status: https://xxxxx.cloud.databricks.com/explore/data/takaakiyayoi_catalog/vector_search/en_wiki_index',
  'indexed_row_count': 0,
  'provisioning_status': {'provisioning_pipeline_time_spent_seconds': 3.0},
  'ready': False,
  'index_url': 'e2-demo-tokyo.cloud.databricks.com/api/2.0/vector-search/indexes/takaakiyayoi_catalog.vector_search.en_wiki_index'},
 'creator': 'takaaki.yayoi@databricks.com',
 'endpoint_type': 'STANDARD'}
# インデックスがONLINEになるまで待機します。数分かかる場合があります。
import time
while not index.describe().get('status').get('detailed_state').startswith('ONLINE'):
    print("インデックスがONLINEになるまで待機中...")
    time.sleep(5)
print("インデックスがONLINEになりました")
index.describe()
インデックスがONLINEになりました
{'name': 'takaakiyayoi_catalog.vector_search.en_wiki_index',
 'endpoint_name': 'one-env-shared-endpoint-1',
 'primary_key': 'id',
 'index_type': 'DELTA_SYNC',
 'delta_sync_index_spec': {'source_table': 'takaakiyayoi_catalog.vector_search.en_wiki',
  'embedding_source_columns': [{'name': 'text',
    'embedding_model_endpoint_name': 'databricks-gte-large-en'}],
  'pipeline_type': 'TRIGGERED',
  'pipeline_id': '2d24c1d5-6148-4f13-a112-5e04efc3a2fb'},
 'status': {'detailed_state': 'ONLINE_NO_PENDING_UPDATE',
  'message': 'Index creation succeeded. Check latest status: https://e2-demo-tokyo.cloud.databricks.com/explore/data/takaakiyayoi_catalog/vector_search/en_wiki_index',
  'indexed_row_count': 10,
  'triggered_update_status': {'last_processed_commit_version': 0,
   'last_processed_commit_timestamp': '2025-08-19T00:23:50Z'},
  'ready': True,
  'index_url': 'xxxxx.cloud.databricks.com/api/2.0/vector-search/indexes/takaakiyayoi_catalog.vector_search.en_wiki_index'},
 'creator': 'takaaki.yayoi@databricks.com',
 'endpoint_type': 'STANDARD'}

類似検索

ベクトルインデックスにクエリを投げて、類似するドキュメントを検索します。

from databricks.vector_search.reranker import DatabricksReranker

results = index.similarity_search(
    query_text="Greek myths",
    columns=["title"],
  num_results=10)
results

この時点ではエンべディングの類似度にのみ基づいてランキングされています。

[NOTICE] Using a notebook authentication token. Recommended for development only. For improved performance, please use Service Principal based authentication. To disable this message, pass disable_notice=True.
{'manifest': {'column_count': 2,
  'columns': [{'name': 'title'}, {'name': 'score'}]},
 'result': {'row_count': 10,
  'data_array': [['Hercules', 0.003353535],
   ['Hydrus', 0.002281647],
   ['History of physics', 0.0017764077],
   ['Hydrofoil', 0.0017075192],
   ['Hassium', 0.0015639906],
   ['Hard disk drive', 0.0015528739],
   ['History of Poland', 0.0015004252],
   ['Henri Chopin', 0.0014630749],
   ['Houston', 0.0014603797],
   ['Hradčany', 0.0012722029]]},
 'debug_info': {'response_time': 621.0,
  'ann_time': 14.0,
  'embedding_gen_time': 602.0}}

リランカーの使用

columns_to_rerankにリランキングに使用するカラムを指定します。

from databricks.vector_search.reranker import DatabricksReranker

results = index.similarity_search(
    query_text = "Greek myths",
    columns = ["title"],
    num_results = 10,
    query_type = "hybrid",
    reranker=DatabricksReranker(columns_to_rerank=["title"])
    )
results

スコアや順序が変化していることがわかります。

[NOTICE] Using a notebook authentication token. Recommended for development only. For improved performance, please use Service Principal based authentication. To disable this message, pass disable_notice=True.
{'manifest': {'column_count': 2,
  'columns': [{'name': 'title'}, {'name': 'score'}]},
 'result': {'row_count': 10,
  'data_array': [['Hercules', 1.0],
   ['Hydrus', 0.5],
   ['Hydrofoil', 0.3333333333333333],
   ['History of Poland', 0.25],
   ['History of physics', 0.2],
   ['Hassium', 0.16666666666666666],
   ['Hard disk drive', 0.14285714285714285],
   ['Houston', 0.125],
   ['Henri Chopin', 0.1111111111111111],
   ['Hradčany', 0.1]]},
 'debug_info': {'response_time': 1185.0,
  'ann_time': 11.0,
  'embedding_gen_time': 245.0,
  'reranker_time': 912.0}}

RAGの精度改善にご活用ください!

はじめてのDatabricks

はじめてのDatabricks

Databricks無料トライアル

Databricks無料トライアル

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?