그림 1 : 세 가지 뚜렷한 QAG 접근법.
lmqg 언어 모델 (LMS)이있는 질문 및 답변 생성 (QAG)을위한 파이썬 라이브러리입니다. 여기서는 사용자가 문맥 (단락 또는 문서)을 제공하는 단락 수준 QAG를 고려하고 모델은 컨텍스트에 대한 질문 및 답변 쌍의 목록을 생성합니다. lmqg 사용하면 다음을 수행 할 수 있습니다.
2023 년 11 월 업데이트 : 중국 QAG 모델은 이제 lmqg 및 AutoQG 모두에서 제공됩니다!
2023 년 5 월 업데이트 : ACL 2023에 의해 2 개의 논문이 수락되었습니다 (System 데모에서 LMQG를 찾을 때 QAG).
2022 년 10 월 업데이트 : QG 용지는 EMNLP Main 2022에 의해 받아 들여졌습니다.
당사의 QAG 모델은 파이프 라인 , 멀티 태스킹 및 end2end 의 세 가지 유형으로 그룹화 할 수 있습니다 (그림 1 참조). 파이프 라인은 질문 생성 (QG) 및 답변 추출 (AE) 모델로 구성되며, 여기서 AE는 컨텍스트에서 모든 문장을 정답을 추출하기 위해 구문 분석하고 QG는 답변에 대한 질문을 생성합니다. 멀티 태스킹은 파이프 라인 과 동일한 아키텍처를 따르지만 QG 및 AE 모델은 공동으로 공유 된 모델을 공유합니다. 마지막으로, End2end 모델은 끝에서 엔드 투 엔드 방식으로 질문과 답변 쌍의 목록을 생성합니다. 실제로, 파이프 라인 과 멀티 태스킹은 더 많은 질문과 답변 쌍을 생성하는 반면, end2end는 덜하지만 몇 배 더 빠르게 생성되며 생성 된 질문과 답변 쌍의 품질은 언어에 따라 다릅니다. 모든 유형은 lmqg 통해 9 가지 다양한 언어 (en/fr/ja/ko/ru/it/es/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 지원합니다. 로드 모델에서 언어 ID TransformersQG(language="es") 지정하여 언어를 전환 할 수 있습니다. 보다 자세한 사용은 다음 섹션을 읽으십시오.
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-Bench를 참조하십시오. 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 모델을 호스팅하는 무료 웹 응용 프로그램입니다.
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 확인하여 모든 옵션을 표시하십시오.
파이썬의 미세 조정 모델은 다음과 같습니다.
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 껴안는 표면의 모델 별칭 또는 로컬 체크 포인트로가는 경로인데, -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를 실행하고 아래와 같이 종속성을 설치하려면 API 토큰을 포옹해야합니다.
pip install lmqg[api] Swagger UI는 앱을 로컬로 실행할 때 (서버 주소로 주소를 교체) http://127.0.0.1:8088/docs 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 | 질문 세대 모델 |
| 답변 _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",
}