milvus-logo
LFAI
フロントページへ
  • モデル

リランカーの概要

情報検索や生成AIの領域において、リランカーは最初の検索結果の順序を最適化するために不可欠なツールである。リランカーは、従来の埋め込みモデルとは異なり、クエリと文書を入力とし、埋め込み値の代わりに類似度スコアを直接返す。このスコアは、入力されたクエリと文書の関連性を示す。

再ランカーは、通常ベクトル近似最近傍(ANN)技術によって行われる第一段階の検索の後に採用されることが多い。ANN検索は、関連する可能性のある結果を幅広く取得するのに効率的であるが、クエリとの実際の意味的な近さという点で、必ずしも結果に優先順位をつけるとは限らない。ここでrerankersは、BERTや他のTransformerベースのモデルのような高度な機械学習モデルを活用し、より深い文脈分析を用いて結果の順序を最適化するために使用される。こうすることで、リランカーはユーザーに提示される最終結果の精度と関連性を劇的に高めることができる。

PyMilvusモデルライブラリは、最初の検索から返される結果の順序を最適化するためのリランク関数を統合しています。Milvusから最近傍の埋め込みを取得した後、検索結果の精度を高めるためにこれらの再ランクツールを活用して検索結果を絞り込むことができます。

再ランク機能APIまたはオープンソース
BGEオープンソース
クロスエンコーダオープンソース
ボヤージュAPI
CohereAPI
ジーナAIAPI
  • オープンソースのリランカーを使用する前に、必要な依存関係とモデルをすべてダウンロードし、インストールしてください。

  • APIベースのリランカーの場合は、プロバイダーからAPIキーを取得し、適切な環境変数または引数に設定する。

例1: BGE rerank関数を使ってクエリに従ってドキュメントをランク付けする

この例では、特定のクエリに基づいてBGEリランカーを使って検索結果をランク付けする方法を示します。

PyMilvusモデルライブラリでリランカーを使うには、まずPyMilvusモデルライブラリと、必要なリランキングユーティリティをすべて含むモデルサブパッケージをインストールします:

pip install pymilvus[model]
# or pip install "pymilvus[model]" for zsh.

BGE のリランカーを使うには、まずBGERerankFunction クラスをインポートします:

from pymilvus.model.reranker import BGERerankFunction

次に、BGERerankFunction インスタンスを作成します:

bge_rf = BGERerankFunction(
    model_name="BAAI/bge-reranker-v2-m3",  # Specify the model name. Defaults to `BAAI/bge-reranker-v2-m3`.
    device="cpu" # Specify the device to use, e.g., 'cpu' or 'cuda:0'
)

クエリに基づいて文書をランキングし直すには、次のコードを使います:

query = "What event in 1956 marked the official birth of artificial intelligence as a discipline?"

documents = [
    "In 1950, Alan Turing published his seminal paper, 'Computing Machinery and Intelligence,' proposing the Turing Test as a criterion of intelligence, a foundational concept in the philosophy and development of artificial intelligence.",
    "The Dartmouth Conference in 1956 is considered the birthplace of artificial intelligence as a field; here, John McCarthy and others coined the term 'artificial intelligence' and laid out its basic goals.",
    "In 1951, British mathematician and computer scientist Alan Turing also developed the first program designed to play chess, demonstrating an early example of AI in game strategy.",
    "The invention of the Logic Theorist by Allen Newell, Herbert A. Simon, and Cliff Shaw in 1955 marked the creation of the first true AI program, which was capable of solving logic problems, akin to proving mathematical theorems."
]

bge_rf(query, documents)

期待される出力は以下のようなものです:

[RerankResult(text="The Dartmouth Conference in 1956 is considered the birthplace of artificial intelligence as a field; here, John McCarthy and others coined the term 'artificial intelligence' and laid out its basic goals.", score=0.9911615761470803, index=1),
 RerankResult(text="In 1950, Alan Turing published his seminal paper, 'Computing Machinery and Intelligence,' proposing the Turing Test as a criterion of intelligence, a foundational concept in the philosophy and development of artificial intelligence.", score=0.0326971950177779, index=0),
 RerankResult(text='The invention of the Logic Theorist by Allen Newell, Herbert A. Simon, and Cliff Shaw in 1955 marked the creation of the first true AI program, which was capable of solving logic problems, akin to proving mathematical theorems.', score=0.006514905766152258, index=3),
 RerankResult(text='In 1951, British mathematician and computer scientist Alan Turing also developed the first program designed to play chess, demonstrating an early example of AI in game strategy.', score=0.0042116724917325935, index=2)]

例2:リランカーを使って検索結果の関連性を高める

このガイドでは、PyMilvusのsearch() メソッドを利用して類似検索を行う方法と、リランカーを使って検索結果の関連性を高める方法について説明します。デモでは以下のデータセットを使用します:

entities = [
    {'doc_id': 0, 'doc_vector': [-0.0372721,0.0101959,...,-0.114994], 'doc_text': "In 1950, Alan Turing published his seminal paper, 'Computing Machinery and Intelligence,' proposing the Turing Test as a criterion of intelligence, a foundational concept in the philosophy and development of artificial intelligence."}, 
    {'doc_id': 1, 'doc_vector': [-0.00308882,-0.0219905,...,-0.00795811], 'doc_text': "The Dartmouth Conference in 1956 is considered the birthplace of artificial intelligence as a field; here, John McCarthy and others coined the term 'artificial intelligence' and laid out its basic goals."}, 
    {'doc_id': 2, 'doc_vector': [0.00945078,0.00397605,...,-0.0286199], 'doc_text': 'In 1951, British mathematician and computer scientist Alan Turing also developed the first program designed to play chess, demonstrating an early example of AI in game strategy.'}, 
    {'doc_id': 3, 'doc_vector': [-0.0391119,-0.00880096,...,-0.0109257], 'doc_text': 'The invention of the Logic Theorist by Allen Newell, Herbert A. Simon, and Cliff Shaw in 1955 marked the creation of the first true AI program, which was capable of solving logic problems, akin to proving mathematical theorems.'}
]

データセットの構成要素

  • doc_id:各文書の一意な識別子。
  • doc_vector:文書を表す埋め込みベクトル。埋め込みベクトル生成の手引きはEmbeddings を参照。
  • doc_text:文書のテキスト内容。

準備

類似検索を開始する前に、Milvusとの接続を確立し、コレクションを作成し、データを準備し、そのコレクションに挿入する必要があります。以下のコード・スニペットは、これらの準備手順を示しています。

from pymilvus import MilvusClient, DataType

client = MilvusClient(
    uri="http://10.102.6.214:19530" # replace with your own Milvus server address
)

client.drop_collection('test_collection')

# define schema

schema = client.create_schema(auto_id=False, enabel_dynamic_field=True)

schema.add_field(field_name="doc_id", datatype=DataType.INT64, is_primary=True, description="document id")
schema.add_field(field_name="doc_vector", datatype=DataType.FLOAT_VECTOR, dim=384, description="document vector")
schema.add_field(field_name="doc_text", datatype=DataType.VARCHAR, max_length=65535, description="document text")

# define index params

index_params = client.prepare_index_params()

index_params.add_index(field_name="doc_vector", index_type="IVF_FLAT", metric_type="IP", params={"nlist": 128})

# create collection

client.create_collection(collection_name="test_collection", schema=schema, index_params=index_params)

# insert data into collection

client.insert(collection_name="test_collection", data=entities)

# Output:
# {'insert_count': 4, 'ids': [0, 1, 2, 3]}

データ挿入後、search メソッドを使用して類似検索を実行します。

# search results based on our query

res = client.search(
    collection_name="test_collection",
    data=[[-0.045217834, 0.035171617, ..., -0.025117004]], # replace with your query vector
    limit=3,
    output_fields=["doc_id", "doc_text"]
)

for i in res[0]:
    print(f'distance: {i["distance"]}')
    print(f'doc_text: {i["entity"]["doc_text"]}')

期待される出力は以下のようなものである:

distance: 0.7235960960388184
doc_text: The Dartmouth Conference in 1956 is considered the birthplace of artificial intelligence as a field; here, John McCarthy and others coined the term 'artificial intelligence' and laid out its basic goals.
distance: 0.6269873976707458
doc_text: In 1950, Alan Turing published his seminal paper, 'Computing Machinery and Intelligence,' proposing the Turing Test as a criterion of intelligence, a foundational concept in the philosophy and development of artificial intelligence.
distance: 0.5340118408203125
doc_text: The invention of the Logic Theorist by Allen Newell, Herbert A. Simon, and Cliff Shaw in 1955 marked the creation of the first true AI program, which was capable of solving logic problems, akin to proving mathematical theorems.

リランカーを使って検索結果を改善する

次に、リランカーのステップで検索結果の関連性を向上させる。この例では、PyMilvusに組み込まれているCrossEncoderRerankFunction

# use reranker to rerank search results

from pymilvus.model.reranker import CrossEncoderRerankFunction

ce_rf = CrossEncoderRerankFunction(
    model_name="cross-encoder/ms-marco-MiniLM-L-6-v2",  # Specify the model name.
    device="cpu" # Specify the device to use, e.g., 'cpu' or 'cuda:0'
)

reranked_results = ce_rf(
    query='What event in 1956 marked the official birth of artificial intelligence as a discipline?',
    documents=[
        "In 1950, Alan Turing published his seminal paper, 'Computing Machinery and Intelligence,' proposing the Turing Test as a criterion of intelligence, a foundational concept in the philosophy and development of artificial intelligence.",
        "The Dartmouth Conference in 1956 is considered the birthplace of artificial intelligence as a field; here, John McCarthy and others coined the term 'artificial intelligence' and laid out its basic goals.",
        "In 1951, British mathematician and computer scientist Alan Turing also developed the first program designed to play chess, demonstrating an early example of AI in game strategy.",
        "The invention of the Logic Theorist by Allen Newell, Herbert A. Simon, and Cliff Shaw in 1955 marked the creation of the first true AI program, which was capable of solving logic problems, akin to proving mathematical theorems."
    ],
    top_k=3
)

# print the reranked results
for result in reranked_results:
    print(f'score: {result.score}')
    print(f'doc_text: {result.text}')

期待される出力は以下のようなものです:

score: 6.250532627105713
doc_text: The Dartmouth Conference in 1956 is considered the birthplace of artificial intelligence as a field; here, John McCarthy and others coined the term 'artificial intelligence' and laid out its basic goals.
score: -2.9546022415161133
doc_text: In 1950, Alan Turing published his seminal paper, 'Computing Machinery and Intelligence,' proposing the Turing Test as a criterion of intelligence, a foundational concept in the philosophy and development of artificial intelligence.
score: -4.771512031555176
doc_text: The invention of the Logic Theorist by Allen Newell, Herbert A. Simon, and Cliff Shaw in 1955 marked the creation of the first true AI program, which was capable of solving logic problems, akin to proving mathematical theorems.

翻訳DeepL

Try Managed Milvus for Free

Zilliz Cloud is hassle-free, powered by Milvus and 10x faster.

Get Started
フィードバック

このページは役に立ちましたか ?