図1:3つの異なるQAGアプローチ。
lmqg 、言語モデル(LMS)を備えた質問と回答生成(QAG)のPythonライブラリです。ここでは、ユーザーがコンテキスト(パラグラフまたはドキュメント)を提供する段落レベルのQAGを検討し、モデルはコンテキストの質問と回答ペアのリストを生成します。 lmqgを使用すると、次のことを行うことができます。
2023年11月の更新: lmqgとAutoQGの両方で中国のQAGモデルが利用できるようになりました!
2023年5月の更新: 2つの論文がACL 2023によって受け入れられました(FindingのQAG、システムデモンストレーションでLMQG)。
2022年10月の更新: QGペーパーはEMNLP Main 2022に受け入れられました。
QAGモデルは、パイプライン、マルチタスク、 End2Endの3つのタイプにグループ化できます(図1を参照)。パイプラインは、質問生成(QG)と回答抽出(AE)モデルで構成されており、AEはコンテキスト内のすべての文を解析して回答を抽出し、QGは回答に関する質問を生成します。マルチタスクはパイプラインと同じアーキテクチャに従いますが、QGモデルとAEモデルは共同で微調整されたモデルを共有しています。最後に、 End2Endモデルは、エンドツーエンドの方法で質問と回答ペアのリストを生成します。実際には、パイプラインとマルチタスクはより多くの質問と回答ペアを生成しますが、 End2Endは数倍速く生成しますが、生成された質問と回答のペアの品質は言語に依存します。すべてのタイプは、 lmqgを介して9つの多様な言語(EN/FR/JA/KO/RU/IT/DE/ZH)で利用でき、モデルはすべてHuggingfaceで共有されています(モデルカードを参照)。 QAGの詳細については、QAGモデルについて説明し、すべての言語の各QAGモデルの完全なパフォーマンス比較を報告するACL 2023ペーパーを確認してください。
図2:QAG(a)およびQg(b)の例。
すべての機能は質問生成もサポートしています。 QGモデルは、ユーザーがコンテキストに加えて回答を指定することを想定しており、QGモデルはコンテキストを考慮して回答によって答えられる質問を生成します(QAGとQGの比較については図2を参照)。 QGの詳細については、QGモデルを詳細に説明するEMNLP 2022ペーパーを確認してください。
最初にPIP経由でlmqgをインストールしましょう。
pip install lmqg数行で質問と回答のペアを生成します。
from lmqg import TransformersQG
model = TransformersQG ( language = "en" )
context = "William Turner was an English painter who specialised in watercolour landscapes. He is often known "
"as William Turner of Oxford or just Turner of Oxford to distinguish him from his contemporary, "
"J. M. W. Turner. Many of Turner's paintings depicted the countryside around Oxford. One of his "
"best known pictures is a view of the city of Oxford from Hinksey Hill."
qa = model . generate_qa ( context )
print ( qa )
[
( 'Who was an English painter who specialised in watercolour landscapes?' , 'William Turner' ),
( 'What is William Turner often known as?' , 'William Turner of Oxford or just Turner of Oxford' ),
( "What did many of Turner's paintings depict?" , 'the countryside around Oxford' ),
( "What is one of Turner's best known pictures?" , 'a view of the city of Oxford from Hinksey Hill' )
]英語enに加えて、イタリア語it 、スペイン語es 、ロシアのru 、韓国ko 、日本ja 、ドイツde 、フランスfr 、および中国のzhをサポートしています。ロードモデル(スペイン語のTransformersQG(language="es")などの読み込みモデルで言語IDを指定することで言語を切り替えることができます。より詳細な使用については、次のセクションをお読みください。
lmqgの主な機能は、便利なAPIを使用して、特定のコンテキストで質問と回答ペアを生成することです。各QAGクラスで利用可能なモデルは、モデルカードで見つけることができます。
from pprint import pprint
from lmqg import TransformersQG
# initialize model
model = TransformersQG ( 'lmqg/t5-base-squad-qag' ) # or TransformersQG(model='lmqg/t5-base-squad-qg-ae')
# paragraph to generate pairs of question and answer
context = "William Turner was an English painter who specialised in watercolour landscapes. He is often known "
"as William Turner of Oxford or just Turner of Oxford to distinguish him from his contemporary, "
"J. M. W. Turner. Many of Turner's paintings depicted the countryside around Oxford. One of his "
"best known pictures is a view of the city of Oxford from Hinksey Hill."
# model prediction
question_answer = model . generate_qa ( context )
# the output is a list of tuple (question, answer)
pprint ( question_answer )
[
( 'Who was an English painter who specialised in watercolour landscapes?' , 'William Turner' ),
( 'What is William Turner often known as?' , 'William Turner of Oxford or just Turner of Oxford' ),
( "What did many of Turner's paintings depict?" , 'the countryside around Oxford' ),
( "What is one of Turner's best known pictures?" , 'a view of the city of Oxford from Hinksey Hill' )
]modelとmodel_aeは、それぞれQGとAEモデルです。 from pprint import pprint
from lmqg import TransformersQG
# initialize model
model = TransformersQG ( model = 'lmqg/t5-base-squad-qg' , model_ae = 'lmqg/t5-base-squad-ae' )
# paragraph to generate pairs of question and answer
context = "William Turner was an English painter who specialised in watercolour landscapes. He is often known "
"as William Turner of Oxford or just Turner of Oxford to distinguish him from his contemporary, "
"J. M. W. Turner. Many of Turner's paintings depicted the countryside around Oxford. One of his "
"best known pictures is a view of the city of Oxford from Hinksey Hill."
# model prediction
question_answer = model . generate_qa ( context )
# the output is a list of tuple (question, answer)
pprint ( question_answer )
[
( 'Who was an English painter who specialised in watercolour landscapes?' , 'William Turner' ),
( 'What is another name for William Turner?' , 'William Turner of Oxford' ),
( "What did many of William Turner's paintings depict around Oxford?" , 'the countryside' ),
( 'From what hill is a view of the city of Oxford taken?' , 'Hinksey Hill.' )
]modelはQGモデルです。利用可能なQGモデルのリストについては、多言語QGベンチマークであるQGベンチを参照してください。 from pprint import pprint
from lmqg import TransformersQG
# initialize model
model = TransformersQG ( model = 'lmqg/t5-base-squad-qg' )
# a list of paragraph
context = [
"William Turner was an English painter who specialised in watercolour landscapes" ,
"William Turner was an English painter who specialised in watercolour landscapes"
]
# a list of answer (same size as the context)
answer = [
"William Turner" ,
"English"
]
# model prediction
question = model . generate_q ( list_context = context , list_answer = answer )
pprint ( question )
[
'Who was an English painter who specialised in watercolour landscapes?' ,
'What nationality was William Turner?'
]modelはQGモデルです。 from pprint import pprint
from lmqg import TransformersQG
# initialize model
model = TransformersQG ( model = 'lmqg/t5-base-squad-ae' )
# model prediction
answer = model . generate_a ( "William Turner was an English painter who specialised in watercolour landscapes" )
pprint ( answer )
[ 'William Turner' ]AutoQG(https://autoqg.net)は、QAGモデルをホストする無料のWebアプリケーションです。
lmqg 、QG、AE、およびQAGモデルを微調整および評価するためのコマンドラインインターフェイスも提供します。
QG(またはAE、QAG)モデルを微調整するには、上記の図として説明されている2段階のハイパーパラメーターの最適化を採用しています。次のコマンドは、パラメーター最適化で微調整を実行することです。
lmqg-train-search -c " tmp_ckpt " -d " lmqg/qg_squad " -m " t5-small " -b 64 --epoch-partial 5 -e 15 --language " en " --n-max-config 1
-g 2 4 --lr 1e-04 5e-04 1e-03 --label-smoothing 0 0.15すべてのオプションを表示するにはlmqg-train-search -hを確認してください。
Pythonの微調整モデルは以下を続けます。
from lmqg import GridSearcher
trainer = GridSearcher (
checkpoint_dir = 'tmp_ckpt' ,
dataset_path = 'lmqg/qg_squad' ,
model = 't5-small' ,
epoch = 15 ,
epoch_partial = 5 ,
batch = 64 ,
n_max_config = 5 ,
gradient_accumulation_steps = [ 2 , 4 ],
lr = [ 1e-04 , 5e-04 , 1e-03 ],
label_smoothing = [ 0 , 0.15 ]
)
trainer . run ()評価ツールは、QGベンチに続くBLEU4 、 ROUGE-L 、 METEOR 、 BERTScore 、およびMoverScore報告しています。コマンドラインから、次のコマンドを実行します
lmqg-eval -m " lmqg/t5-large-squad-qg " -e " ./eval_metrics " -d " lmqg/qg_squad " -l " en "ここで、 -mはHuggingfaceまたはローカルチェックポイントへのパスのモデルエイリアスであり、 -eメトリックファイルを直接エクスポートし、 -dは評価するデータセット、 -lはテストセットの言語です。モデルの予測を実行する代わりに、代わりに予測ファイルを提供して、毎回計算を避けることができます。
lmqg-eval --hyp-test ' {your prediction file} ' -e " ./eval_metrics " -d " lmqg/qg_squad " -l " en "予測ファイルは、ターゲットデータセット(サンプル)でtest分割の順に、各行のモデル生成のテキストファイルである必要があります。 lmqg-eval -hをチェックして、すべてのオプションを表示します。
最後に、 lmqg 、Huggingface Inference APIを介してモデル推論をホストするREST APIを提供します。以下のように、独自のAPIを実行し、依存関係をインストールするには、Huggingface APIトークンが必要です。
pip install lmqg[api] Swagger UIは、アプリをローカルに実行するときにhttp://127.0.0.1:8088/docsで入手できます(アドレスをサーバーアドレスで置き換えます)。
export API_TOKEN={Your Huggingface API Token}
uvicorn app:app --host 0.0.0.0 --port 8088docker build -t lmqg/app:latest . --build-arg api_token={Your Huggingface API Token}
docker run -p 8080:8080 lmqg/app:latestuvicorn app_local:app --host 0.0.0.0 --port 8088環境変数API_TOKENを介してHuggingface APIトークンを渡す必要があります。メインエンドポイントはquestion_generationで、次のパラメーターがあります。
| パラメーター | 説明 |
|---|---|
| input_text | 入力テキスト、段落、または質問を生成するための文 |
| 言語 | 言語 |
| qg_model | 質問生成モデル |
| Answer_Model | 回答抽出モデル |
辞書のリストをquestionとanswerで返します。
{
" qa " : [
{ " question " : " Who founded Nintendo Karuta? " , " answer " : " Fusajiro Yamauchi " },
{ " question " : " When did Nintendo distribute its first video game console, the Color TV-Game? " , " answer " : " 1977 " }
]
}リソースを使用し、必要に応じてモデルを再現するためにコードを表示し、コードを表示する場合は、次の論文を引用してください。
@inproceedings{ushio-etal-2022-generative,
title = "{G}enerative {L}anguage {M}odels for {P}aragraph-{L}evel {Q}uestion {G}eneration",
author = "Ushio, Asahi and
Alva-Manchego, Fernando and
Camacho-Collados, Jose",
booktitle = "Proceedings of the 2022 Conference on Empirical Methods in Natural Language Processing",
month = dec,
year = "2022",
address = "Abu Dhabi, U.A.E.",
publisher = "Association for Computational Linguistics",
}
@inproceedings{ushio-etal-2023-an-empirical,
title = "An Empirical Comparison of LM-based Question and Answer Generation Methods",
author = "Ushio, Asahi and
Alva-Manchego, Fernando and
Camacho-Collados, Jose",
booktitle = "Proceedings of the 61th Annual Meeting of the Association for Computational Linguistics: Findings",
month = Jul,
year = "2023",
address = "Toronto, Canada",
publisher = "Association for Computational Linguistics",
}
@inproceedings{ushio-etal-2023-a-practical-toolkit,
title = "A Practical Toolkit for Multilingual Question and Answer Generation",
author = "Ushio, Asahi and
Alva-Manchego, Fernando and
Camacho-Collados, Jose",
booktitle = "Proceedings of the 61th Annual Meeting of the Association for Computational Linguistics: System Demonstrations",
month = Jul,
year = "2023",
address = "Toronto, Canada",
publisher = "Association for Computational Linguistics",
}