
Simple Transformers 入門 (8) - 会話型AI
「Simple Transformers」で「会話形AI」を行う方法をまとめました。
1. 会話型AI
HuggingFaceの「State-of-the-Art Conversational AI」の作成を行います
サポートモデルは、次のとおりです。
・GPT
・GPT-2
2. データ形式
データ形式は、「Facebook Persona-Chat」形式に従います。HuggingFaceによるJSON形式のバージョンはここにあります。JSONファイルはこのライブラリと直接互換性があります(データセットが指定されていない場合は自動的にダウンロードされて使用されます)。
personachatの各エントリは、キーpersonalityとutterancesを持つ辞書であり、データセットはエントリのリストです。
・personality : strのlist - エージェントのパーソナリティ。
・utterances : dicのlist - 辞書のリスト。各辞書には、2つのキーがある。
・candidates : strのlist - [next_utterance_candidate_1, ..., next_utterance_candidate_19] - 最後の候補は、会話データで観察されたgrand truthレスポンス。
・history : [dialog_turn_0、... dialog_turn N] - 他のユーザーが全ての会話を開始するため、Nは奇数。
前処理は、次のとおりです。
・文末のピリオドの前のスペース
・すべて小文字
学習データの例は、次のとおりです。
[
{
"personality": [
"i like computers .",
"i like reading books .",
"i like talking to chatbots .",
"i love listening to classical music ."
],
"utterances": [
{
"candidates": [
"i try to wear all black every day . it makes me feel comfortable .",
"well nursing stresses you out so i wish luck with sister",
"yeah just want to pick up nba nfl getting old",
"i really like celine dion . what about you ?",
"no . i live near farms .",
"mother taught me to cook ! we are looking for an exterminator .",
"i enjoy romantic movie . what is your favorite season ? mine is summer .",
"editing photos takes a lot of work .",
"you must be very fast . hunting is one of my favorite hobbies .",
"hi there . i'm feeling great! how about you ?"
],
"history": [
"hi , how are you ?"
]
},
{
"candidates": [
"i have trouble getting along with family .",
"i live in texas , what kind of stuff do you do in ",
"toronto ?",
"that's so unique ! veganism and line dancing usually don't mix !",
"no , it isn't that big . do you travel a lot",
"that's because they are real ; what do you do for work ?",
"i am lazy all day lol . my mom wants me to get a job and move out",
"i was born on arbor day , so plant a tree in my name",
"okay , i should not tell you , its against the rules ",
"i like to talk to chatbots too ! do you know why ? ."
],
"history": [
"hi , how are you ?",
"hi there . i'm feeling great! how about you ?",
"not bad ! i am trying out this chatbot ."
]
},
{
"candidates": [
"ll something like that . do you play games ?",
"does anything give you relief ? i hate taking medicine for mine .",
"i decorate cakes at a local bakery ! and you ?",
"do you eat lots of meat",
"i am so weird that i like to collect people and cats",
"how are your typing skills ?",
"yeah . i am headed to the gym in a bit to weight lift .",
"yeah you have plenty of time",
"metal is my favorite , but i can accept that people listen to country . haha",
"that's why you desire to be controlled . let me control you person one .",
"two dogs they are the best , how about you ?",
"you do art ? what kind of art do you do ?",
"i love watching baseball outdoors on sunny days .",
"oh i see . do you ever think about moving ? i do , it is what i want .",
"because i am a chatbot too, silly !"
],
"history": [
"hi , how are you ?",
"hi there . i'm feeling great! how about you ?",
"not bad ! i am trying out this chatbot .",
"i like to talk to chatbots too ! do you know why ? .",
"no clue, why don't you tell me ?"
]
}
]
}
]
3. 会話型AIの最小限のコード
Hugging Faceによってオープンソース化された事前学習済み(OpenAI GPTベース)の会話AIモデルはこちらからダウンロードできます。
以下に示す最小限の例では、モデルをダウンロードしてgpt_personachat_cacheに抽出できます。他の「GPT」または「GPT-2」はどれでも使用できますが、さらに学習が必要になることに注意してください。
また、「データ形式」で指定されたJSONファイルを作成し、data /minimal_train.jsonとして保存する必要があります。
「会話形AI」の最小限のコードは、次のとおりです。
from simpletransformers.conv_ai import ConvAIModel
# モデルの作成
train_args = {
"num_train_epochs": 50,
"save_model_every_epoch": False,
}
model = ConvAIModel("gpt", "gpt_personachat_cache", use_cuda=True, args=train_args)
# 学習
model.train_model("data/minimal_train.json")
# 評価
model.eval_model()
# 学習済みモデルと対話
model.interact()
Interaction()には、パーソナリティを構築するために使用される文字列のリストを指定できます。文字列のリストが指定されていない場合は、代わりにPERSONA-CHATからランダムなパーソナリティが選択されます。
4. 会話AIの学習のデータセット
主な「会話AI」のデータセットは、次のとおりです。
・Persona-Chat Conversational AI
気軽にクリエイターの支援と、記事のオススメができます!