0
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?

音韻検索ベンチマークのLLM性能評価関数の整備

0
Posted at

はじめに

空耳歌詞に基づく音韻検索ベンチマーク soramimi-phonetic-search-dataset でLLMの音韻検索性能を評価する機会が増えたため、専用の関数を整備しました。(v0.0.20以降)

使い方

soramimi-phonetic-search-dataset をinstallしてください。

uv add soramimi-phonetic-search-dataset

最低限のコードは以下です。

"""LLMリランク (gpt-4o-mini) による評価を実行するスクリプト"""

import json
from pathlib import Path

from soramimi_phonetic_search_dataset import (
    RankingFunctionOutput,
    evaluate_ranking_function,
    load_default_dataset_for_llm,
    rank_by_llm,
)


def main():

    dataset = load_default_dataset_for_llm(query_limit=10)

    def ranking_func(
        query_texts: list[str], wordlists: list[list[str]]
    ) -> RankingFunctionOutput:
        return rank_by_llm(
            query_texts,
            wordlists,
            topn=10,
            model_name="gpt-4o-mini",
            batch_size=2,
            rerank_interval=1,
        )

    results = evaluate_ranking_function(
        ranking_func=ranking_func,
        topn=10,
        dataset=dataset,
    )

    print("Recall: ", results.metrics.recall)
    print("Execution time: ", results.metrics.execution_time)


if __name__ == "__main__":
    main()

実行すると以下のような出力が得られます。

Recall:  0.55
Execution time:  5.95770001411438

解説

rank_by_llm

モデル名を指定することで、指定されたllmによるリランクを実行できる関数を作りました。

内部的にはlitellmを用いており、litellmが対応する非推論モデルであれば、モデル名の変更だけで評価可能です。

gpt-5.4など推論モデル(reasoning_effortが指定可能なモデル)を使う場合は、rank_by_reasoning_llmの使用を推奨します。rank_by_llmではreasoning_effortを受け取ることができず、デフォルト値で実行されてしまうためです。

リランク用のプロンプトはシステムプロンプト、exapmle、ユーザプロンプトテンプレートのそれぞれを引数として与えることができます。未指定の場合は、下記の初期値が用いられます。

PROMPT_INSTRUCTIONS = """
You are a phonetic search assistant.
You are given a query and a list of words.
You need to rerank the words based on phonetic similarity to the query.
When estimating phonetic similarity, please consider the following:
1. Prioritize matching vowels
2. Substitution, insertion, or deletion of nasal sounds, geminate consonants, and long vowels is acceptable
3. For other cases, words with similar mora counts are preferred
You need to return only the reranked list of index numbers of the words, no other text.
You need to return only topn index numbers.
"""
PROMPT_EXAMPLE_SUFFIX = """
Example:
Query: タロウ
Wordlist:
0. アオ
1. アオウヅ
2. アノウ
3. タキョウ
4. タド
5. タノ
6. タロウ
7. タンノ
Top N: 5
Reranked: 6, 4, 5, 7, 2
"""
USER_PROMPT_TEMPLATE = """
Query: {query}
Wordlist:
{wordlist}
Top N: {topn}
Reranked:
"""

load_default_dataset_for_llm

LLM評価用のデータセット読み込み関数を作成しました。
この関数では、事前に算出済みの、ハードネガティブ100件を読み込み、LLMのリランクの対象とできます。
ハードネガティブは母音同士、子音同士の編集距離の和に基づき取得した100件(100件に含まれないpositiveが存在する場合は下位のものとそれらを入れ替える)をあいうえお順に並べたものです。

従来のload_default_datasetではすべての候補語が読まれるため、ハードネガティブを算出する前処理を書く必要がありましたが、これを書かずに住むようにしました。

またload_default_dataset_for_llmの引数query_limitによりdatasetの件数を指定できるようにしました。LLM評価では、時間、コストの関係から、少数データセットを何回も回したくなることが多かったためです。

evaluate_ranking_function

llmには直接関係ありませんが、評価対象のデータセットを引数として受け取れるようにすることで、load_default_dataset_for_llmと組み合わせて使いやすくしました。

おわりに

LLMの音韻検索性能の評価に活用いただけたら幸いです。

0
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
0
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?