LayerX エンジニアブログ

LayerX の エンジニアブログです。

Azure AI SearchのSemantic Ranker #LayerXテックアドカレ

Privacy Tech事業部も合流し、先日新たに新設されたAI・LLM事業部でソフトウェアエンジニアをしている osukeです。

layerx.co.jp

この記事は LayerXテックアドカレ 15日目の記事です。前回は @yossylx による バクラクの帳票画像を用いたDALL-E dVAEの学習 でした。次回は @yu-ya4 が担当します。

ここではMicrosoft Ignite 2023中にGAとなった Azure AI Search(旧Azure Cognitive Search) のSemantic Rankerについて紹介します。*1

Semantic Rankerとは

文書検索において、クエリにマッチングした結果は何らかのロジックで順位付けされレスポンスされます。この順位付けをさらに並び替えることを「リランキング」と言います。

Azure AI SearchではこれまでSemantic Searchという名称でリランキング処理がアドオン機能としてプレビュー公開されていましたが、Semantic Rankerという名称でGAされました。リランキングの処理自体はMicrosoft Bingでも用いられている言語モデルをベースにしているようです。*2

前提として、Azure AI Searchでは主に以下の3種類の検索クエリをサポートしています。

  • キーワード検索:
    • Apache Lucene ベースのテキスト検索。BM25でスコアリング。
  • ベクトル検索
    • テキストをベクトル化したembeddingによる検索。
  • ハイブリッド検索

Semantic Rankerを有効化すると、これらの検索クエリのレスポンスをリランキングします。(執筆時点ではキーワード検索およびハイブリッド検索でのみSemantic Rankerは対応しています) 正確には、キャプションやハイライトなど検索結果として付随する情報もレスポンスされるようになりますがここでは省略します。

Semantinc Rankerを加えることで、一般的に以下のようにベクトル検索やハイブリッド検索のみより精度向上が見込めることが報告されています。

引用: Azure Cognitive Search: Outperforming vector search with hybrid retrieval and ranking capabilities

とはいえ、全ての場合でリランキングを入れると精度が良くなるわけではなく、データセットやリランカーによってはtext-embedding-ada-002 のベクトル検索で性能が低下する実験結果もあげられているので、導入前に検証は必要となります。*3

また、リランキングはベクトル検索と比べて精度は高い一方、実行時間が非常に長くなる傾向にあります。なので、文書検索で直接使うというよりもあくまで絞られた上位の検索結果のリランキングに対して使われます。*4

使い方

事前に、お使いのAzure AI SearchのサービスでSemantic Rankerを有効化しておきます。 そして、Azure AI Searchでインデックスを作成する際に、どのフィールドをリランキングとして考慮するかなどを指定するSemantic Configirationを設定します。pythonでは以下のように指定します。

client = SearchIndexClient(
    "endpoint", AzureKeyCredential("key")
)
index = SearchIndex(
    name="index_name",
    fields=[SearchableField(
            name="content",
            type=SearchFieldDataType.String,
    )],
        # SemanticSettingsを定義する
    semantic_settings=SemanticSettings(
        default_configuration="default",
        configurations=[
            SemanticConfiguration(
                name="default",
                prioritized_fields=PrioritizedFields(
                    prioritized_content_fields=[
                        SemanticField(
                            field_name="content",
                        ),
                    ]
                ),
            )
        ],
    ),   
)
client.create_index(index)

prioritized_fields ではドキュメントのフィールドを指定するcontentだけではなくtitleやkeywordを指定するプロパティも存在しますが、ここではcontentのみを指定しています。Semantic Rankerでは内部的に文書要約モデルが使われますがこのモデルへのインプットは前述のprioritized_fieldsを全てアセンブルした2,000トークンが上限となっています。

そして、検索クエリ実行時に、query_type=semantic を指定します。これにより、キーワード検索結果の上位50個がSemantic Rankerによりリランキングされます。

client.search(
    search_text=query,    
    top=num,
    query_type="semantic",
)

試す

ここでは、シマウマのwikipedia をPDF化しAzure Document IntelligenceでOCRし、パラグラフ単位で分けた際のハイブリッドサーチ + Semantic Rankerを検証します。例えば、検索クエリを「What kind of animal preys on zebra?」(あえてanimalを単数形にしています)とした際、Semantic Rankerあり・なしでそれぞれ上位2つの検索結果は以下のようになります。

Semantic Ranker 1位 2位
あり Zebras are preyed on mainly by lions. Leopards, cheetahs, spotted hyenas, brown hyenas and wild dogs pose less of a threat to adults. 186] Nile crocodiles also prey on zebras when they near water.187] Biting and kicking are a zebra's defense tactics. When threatened by lions, zebras flee, and when caught they are rarely effective in fighting off the big cats. 188] In one study, the maximum speed of a zebra was found to be 50 km/h (31 mph) while a The aposematic hypothesis suggests that the stripes serve as warning colouration. This hypothesis was first suggested by Wallace in 1867 and discussed in more detail by Edward Bagnall Poulton in 1890. As with known aposematic mammals, zebras are recognisable up close, live in more open environments, have a high risk of predation and do not hide or act inconspicuous. However they are frequently preyed on by lions, suggesting that stripes do not work on them but may on smaller predators, and are not slow-moving enough to need to ward off threats. They also do not possess adequate defenses to back up the warning pattern.[61]
なし Zebras' dazzling stripes make them among the most recognisable mammals. They have been featured in art and stories in Africa and beyond. Historically, they have been highly sought after by exotic animal collectors, but unlike horses and donkeys, zebras have never been truly domesticated. The International Union for Conservation of Nature (IUCN) lists the Grévy's zebra as endangered, the mountain zebra as vulnerable and the plains zebra as near-threatened. The quagga (E. quagga quagga), a type of plains zebra, was driven to extinction in the 19th century. Nevertheless, zebras can be found in numerous protected areas. Zebras are preyed on mainly by lions. Leopards, cheetahs, spotted hyenas, brown hyenas and wild dogs pose less of a threat to adults. 186] Nile crocodiles also prey on zebras when they near water.187] Biting and kicking are a zebra's defense tactics. When threatened by lions, zebras flee, and when caught they are rarely effective in fighting off the big cats. 188] In one study, the maximum speed of a zebra was found to be 50 km/h (31 mph) while a

Semantic Rankerあり・なしだと1位の取得パラグラフで違いが見られます。 Semantic Rankerありの1位ではパラグラフの冒頭で「Zebras are preyed on mainly by lions.」と欲しい情報が取得できてることが分かります。一方、Semantic Rankerなしの1位は、絶滅危惧種と指定されている内容となっており、欲しい情報ではありません。 Semantic Rankerありの2位は、「However they are frequently preyed on by lions,」などが記載されており、捕食に関するパラグラフが取得できています。Semantic Rankerなしの2位はSemantic Rankerありの1位と同じパラグラフを取得しています。 このように、リランカーの有無が検索結果の順位付けに影響を与えていることが分かります。

最後に

LLM・生成AIの分野では日々新しい技術が登場していますが、LayerX AI・LLM事業部ではチーム一丸となって徹底的に情報収集・技術検証をしながら、LLMを使った事業開発を進めています。

AI・LLM事業部では一緒に働く仲間を絶賛募集中です!

open.talentio.com