1633767300
チャットボットは近年多くの人気を博しており、ビジネスでのチャットボットの使用への関心が高まるにつれ、研究者は会話型AIチャットボットの進歩にも素晴らしい仕事をしました。
このチュートリアルでは、Huggingfaceトランスフォーマーライブラリを使用して、事前にトレーニングされたDialoGPTモデルを使用して会話型応答を生成します。
DialoGPTは、Redditから抽出された1億4700万の会話でトレーニングされた、大規模で調整可能なニューラル会話応答生成モデルです。データセットで微調整して、最初からトレーニングするよりも優れたパフォーマンスを実現できるのは良いことです。
開始するには、トランスフォーマーをインストールしましょう:
$ pip3 install transformers
新しいPythonファイルまたはノートブックを開き、次の手順を実行します。
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
# model_name = "microsoft/DialoGPT-large"
model_name = "microsoft/DialoGPT-medium"
# model_name = "microsoft/DialoGPT-small"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
DialoGPTには3つのバージョンがあります。小、中、大。もちろん、大きいほど良いのですが、これをマシンで実行している場合は、中小規模でも問題なくメモリに収まると思います。また、GoogleColabを使用して大きなものを試すこともできます。
このセクションでは、欲張り検索アルゴリズムを使用して応答を生成します。つまり、各タイムステップで選択される可能性が最も高いチャットボット応答を選択します。
欲張り検索を使用してAIとチャットするためのコードを作成しましょう:
# chatting 5 times with greedy search
for step in range(5):
# take user input
text = input(">> You:")
# encode the input and add end of string token
input_ids = tokenizer.encode(text + tokenizer.eos_token, return_tensors="pt")
# concatenate new user input with chat history (if there is)
bot_input_ids = torch.cat([chat_history_ids, input_ids], dim=-1) if step > 0 else input_ids
# generate a bot response
chat_history_ids = model.generate(
bot_input_ids,
max_length=1000,
pad_token_id=tokenizer.eos_token_id,
)
#print the output
output = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
print(f"DialoGPT: {output}")
このコードのコアを説明しましょう:
input_ids
DialoGPTトークナイザーを使用してテキストをエンコードし、文字列トークンの末尾を追加して、Pytorchテンソルとして返します。input_ids
モデルに直接フィードして世代を超えます。それ以外の場合は、torch.cat()
メソッドを使用して連結を使用してチャット履歴を追加します。model.generate()
、チャットボット応答を生成する方法を使用します。tokenizer.decode()
してシーケンスをデコードし、に設定skip_special_tokens
しTrue
て、などの迷惑な特別なトークンが表示されないようにし<|endoftext|>
ます。また、モデルはシーケンス全体を返すため、以前のチャット履歴をスキップし、新しく生成されたチャットボットの回答のみを出力します。以下は、ボットとのディスカッションの例です。
>> You:How can you be rich so quickly?
DialoGPT: I'm not rich, I'm just a rich man.
>> You:Ok, you don't make sense, how old are you ?
DialoGPT: I'm not a rich man, I'm just a rich man.
>> You:How can you be happy ?
DialoGPT: I'm not happy, I'm just a rich man.
>> You:But they say money buys happiness!
DialoGPT: Money buys happiness, but not happiness itself.
>> You:You don't make sense again!
DialoGPT: I'm not happy, I'm just a rich man.
モデルは多くの応答を繰り返すことがわかります。これらは最も高い確率であり、毎回それを選択しているためです。
デフォルトでmodel.generate()
は、他のパラメーターが設定されていない場合は欲張り検索アルゴリズムを使用します。次のセクションでは、このメソッドにいくつかの引数を追加して、生成を改善できるかどうかを確認します。
ビーム検索ではnum_beams
、各タイムステップで最も可能性の高い仮説を保持し、全体的に最も確率の高いシーケンスを取得することで、高確率シーケンスを見逃すリスクを減らすことができます。以下のコードは、ビーム検索でチャットボット応答を生成します。
# chatting 5 times with beam search
for step in range(5):
# take user input
text = input(">> You:")
# encode the input and add end of string token
input_ids = tokenizer.encode(text + tokenizer.eos_token, return_tensors="pt")
# concatenate new user input with chat history (if there is)
bot_input_ids = torch.cat([chat_history_ids, input_ids], dim=-1) if step > 0 else input_ids
# generate a bot response
chat_history_ids = model.generate(
bot_input_ids,
max_length=1000,
num_beams=3,
early_stopping=True,
pad_token_id=tokenizer.eos_token_id
)
#print the output
output = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
print(f"DialoGPT: {output}")
inメソッドに設定num_beams
する場合、各タイムステップで3つの単語を選択し、それらを開発して、シーケンスの全体的な確率が最も高くなるようにします。1に設定することは、欲張り検索と同じです。3model.generate()num_beams
以下は、ビーム検索を使用したチャットボットとのディスカッションの例です。
>> You:How can I be rich?
DialoGPT: You can't.
>> You:Why not?
DialoGPT: Because you can't.
>> You:Then how am I supposed to live well?
DialoGPT: You can't.
>> You:So basically, you want me to be a poor guy?
DialoGPT: No, I want you to be a rich guy.
>> You:Ok how to be rich, then?
DialoGPT: I don't know.
前のセクションでは、ビーム検索と欲張り検索を使用して、最も確率の高いシーケンスを生成しました。これは、出力が予測可能な機械翻訳やテキスト要約などのタスクに最適です。ただし、チャットボットのように、オープンエンド世代には最適なオプションではありません。
より良い生成のために、確率に基づいて広範囲の候補配列からサンプリングするランダム性を導入する必要があります。
# chatting 5 times with sampling
for step in range(5):
# take user input
text = input(">> You:")
# encode the input and add end of string token
input_ids = tokenizer.encode(text + tokenizer.eos_token, return_tensors="pt")
# concatenate new user input with chat history (if there is)
bot_input_ids = torch.cat([chat_history_ids, input_ids], dim=-1) if step > 0 else input_ids
# generate a bot response
chat_history_ids = model.generate(
bot_input_ids,
max_length=1000,
do_sample=True,
top_k=0,
pad_token_id=tokenizer.eos_token_id
)
#print the output
output = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
print(f"DialoGPT: {output}")
今回は、サンプリング用に設定do_sample
しTrue
、可能なすべての確率を選択していることtop_k
を0
示すように設定しましたtop_k
。後でパラメーターについて説明します。
これらのパラメータを使用したチャットは次のとおりです。
>> You:Hello, how can I be rich?
DialoGPT: You can't
>> You:Really? then how others get rich?
DialoGPT: They work as money artists
>> You:How to be a money artist?
DialoGPT: I don't know, I don't have enough knowledge on artists
>> You:Alright, thank you!
DialoGPT: No problem, if you need tips pm me.
>> You:Great, you're my savior!
DialoGPT: Thank you for helping a stranger out like me!
明らかにいくつかの改善があります。ただし、確率の低いシーケンスの幅広いリストでサンプリングすると、ランダムに生成される可能性があります(最後の文で見られるように)。
それをさらに改善するために、次のことができます。
temperature
低くすると、確率の低い単語を選択する可能性が低くなり、確率の高い単語を選択する可能性が高くなります。# chatting 5 times with Top K sampling & tweaking temperature
for step in range(5):
# take user input
text = input(">> You:")
# encode the input and add end of string token
input_ids = tokenizer.encode(text + tokenizer.eos_token, return_tensors="pt")
# concatenate new user input with chat history (if there is)
bot_input_ids = torch.cat([chat_history_ids, input_ids], dim=-1) if step > 0 else input_ids
# generate a bot response
chat_history_ids = model.generate(
bot_input_ids,
max_length=1000,
do_sample=True,
top_k=100,
temperature=0.75,
pad_token_id=tokenizer.eos_token_id
)
#print the output
output = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
print(f"DialoGPT: {output}")
ここで、確率で降順にソートされた上位の単語からサンプリングtop_k
する100
ように設定しました100
。またtemperature
、0.75
(デフォルトは1.0
)に設定して、確率の高い単語を選択する可能性を高めます。温度を0.0
貪欲検索と同じに設定し、無限大に設定すると、完全にランダムと同じになります。
これらのパラメータについての説明は次のとおりです。
>> You:How can I get rich?
DialoGPT: I'd also like to know this.
>> You:So you don't know it?
DialoGPT: I know it, but it doesn't matter.
>> You:Really? money matters, man!
DialoGPT: Well, money is money and I gotta get my money.
>> You:Yeah how to get them?
DialoGPT: Well, that's a question. I'd like to know more, just for curiosity's sake, to know what other people have said about how I should get rich.
>> You:Some robots like you told me you have to buy gold, silver or stocks.
DialoGPT: I think my wallet is empty.
ご覧のとおり、今でははるかに優れています。パラメータを自由に調整してtemperature
、top_k
改善できるかどうかを確認してください。
核サンプリングまたはTop-pサンプリングは、累積確率がp
設定したパラメーターを超える可能な限り小さい単語から選択します。
以下は、Top-pサンプリングを使用した例です。
# chatting 5 times with nucleus sampling & tweaking temperature
for step in range(5):
# take user input
text = input(">> You:")
# encode the input and add end of string token
input_ids = tokenizer.encode(text + tokenizer.eos_token, return_tensors="pt")
# concatenate new user input with chat history (if there is)
bot_input_ids = torch.cat([chat_history_ids, input_ids], dim=-1) if step > 0 else input_ids
# generate a bot response
chat_history_ids = model.generate(
bot_input_ids,
max_length=1000,
do_sample=True,
top_p=0.95,
top_k=0,
temperature=0.75,
pad_token_id=tokenizer.eos_token_id
)
#print the output
output = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
print(f"DialoGPT: {output}")
Top-kサンプリングを無効にするtop_k
よう0
に設定しましたが、どちらの方法も使用できるため、より効果的に機能する傾向があります。これがチャットです:
>> You:How can I be rich fast?
DialoGPT: Inflation and free money
>> You:lol, but that takes time, I need fast money.
DialoGPT: Your post makes no sense. You just need a lot of money to be rich fast
>> You:Ok how to get a lot of money?
DialoGPT: Buy gold and then trade it for money.
>> You:Hmm, I got your point, thanks.
DialoGPT: No problem. You have to have money to buy gold, but its only good for trading it.
>> You:Yeah, I understand, thank you so much.
DialoGPT: Ok, let me know if you need help
現在、チャットボットは多くの場合明らかに意味があります。
次に、複数のチャットボット応答を生成するコードを追加して、次の入力に含める応答を選択します。
# chatting 5 times with nucleus & top-k sampling & tweaking temperature & multiple
# sentences
for step in range(5):
# take user input
text = input(">> You:")
# encode the input and add end of string token
input_ids = tokenizer.encode(text + tokenizer.eos_token, return_tensors="pt")
# concatenate new user input with chat history (if there is)
bot_input_ids = torch.cat([chat_history_ids, input_ids], dim=-1) if step > 0 else input_ids
# generate a bot response
chat_history_ids_list = model.generate(
bot_input_ids,
max_length=1000,
do_sample=True,
top_p=0.95,
top_k=50,
temperature=0.75,
num_return_sequences=5,
pad_token_id=tokenizer.eos_token_id
)
#print the outputs
for i in range(len(chat_history_ids_list)):
output = tokenizer.decode(chat_history_ids_list[i][bot_input_ids.shape[-1]:], skip_special_tokens=True)
print(f"DialoGPT {i}: {output}")
choice_index = int(input("Choose the response you want for the next input: "))
chat_history_ids = torch.unsqueeze(chat_history_ids_list[choice_index], dim=0)
一度に5つの文を返すように設定num_return_sequences
し5
ました。次のシーケンスに含まれる文を選択する必要があります。これがどのように進んだかです:
>> You:Hello, friend.
DialoGPT 0: Hey there!
DialoGPT 1: I am your friend, and I am also your friend.
DialoGPT 2: Hello. :
DialoGPT 3: Hiya! How are you?
DialoGPT 4: Hello. I like your username.
Choose the response you want for the next input: 3
>> You:Fine, thanks, what about you?
DialoGPT 0: Good. Not much to do.
DialoGPT 1: Not much, you?
DialoGPT 2: Fine, thanks.
DialoGPT 3: I'm okay, thank you for asking.
DialoGPT 4: I'm fine too.
Choose the response you want for the next input: 2
>> You:Can you tell me how can I get rich quickly?
DialoGPT 0: Yes, I can!
DialoGPT 1: Yes, just look up how to get rich on Google.
DialoGPT 2: Just keep doing your thing. You'll get rich.
DialoGPT 3: No, I can't.
DialoGPT 4: no, i have no money
Choose the response you want for the next input: 1
>> You:Yeah, but I thought you have an answer...
DialoGPT 0: I'm getting married in the morning.
DialoGPT 1: How do you get rich?
DialoGPT 2: I think there are many good answers, but I could send you one, if you want.
DialoGPT 3: I will, thanks!
DialoGPT 4: I thought you have an answer.
Choose the response you want for the next input: 2
>> You:Yes, please send me.
DialoGPT 0: You can send me link.
DialoGPT 1: I am very interested
DialoGPT 2: I sent you a PM
DialoGPT 3: I'll send you a PM
DialoGPT 4: I am always interested in new ideas.
Choose the response you want for the next input: 2
このチュートリアルが、DialoGPTおよび同様のモデルでテキストを生成する方法に役立つことを願っています。テキストを生成する方法の詳細については、「Transformersを使用してテキストを生成する方法」ガイドを読むことを強くお勧めします。
ボットのパフォーマンスを向上させることができるかどうかを確認するために、パラメーターを微調整しておきます。
また、これをテキスト読み上げおよび音声読み上げのチュートリアルと組み合わせて、Alexa、Siri、Cortanaなどの仮想アシスタントを構築することもできます。
1633767300
チャットボットは近年多くの人気を博しており、ビジネスでのチャットボットの使用への関心が高まるにつれ、研究者は会話型AIチャットボットの進歩にも素晴らしい仕事をしました。
このチュートリアルでは、Huggingfaceトランスフォーマーライブラリを使用して、事前にトレーニングされたDialoGPTモデルを使用して会話型応答を生成します。
DialoGPTは、Redditから抽出された1億4700万の会話でトレーニングされた、大規模で調整可能なニューラル会話応答生成モデルです。データセットで微調整して、最初からトレーニングするよりも優れたパフォーマンスを実現できるのは良いことです。
開始するには、トランスフォーマーをインストールしましょう:
$ pip3 install transformers
新しいPythonファイルまたはノートブックを開き、次の手順を実行します。
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
# model_name = "microsoft/DialoGPT-large"
model_name = "microsoft/DialoGPT-medium"
# model_name = "microsoft/DialoGPT-small"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
DialoGPTには3つのバージョンがあります。小、中、大。もちろん、大きいほど良いのですが、これをマシンで実行している場合は、中小規模でも問題なくメモリに収まると思います。また、GoogleColabを使用して大きなものを試すこともできます。
このセクションでは、欲張り検索アルゴリズムを使用して応答を生成します。つまり、各タイムステップで選択される可能性が最も高いチャットボット応答を選択します。
欲張り検索を使用してAIとチャットするためのコードを作成しましょう:
# chatting 5 times with greedy search
for step in range(5):
# take user input
text = input(">> You:")
# encode the input and add end of string token
input_ids = tokenizer.encode(text + tokenizer.eos_token, return_tensors="pt")
# concatenate new user input with chat history (if there is)
bot_input_ids = torch.cat([chat_history_ids, input_ids], dim=-1) if step > 0 else input_ids
# generate a bot response
chat_history_ids = model.generate(
bot_input_ids,
max_length=1000,
pad_token_id=tokenizer.eos_token_id,
)
#print the output
output = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
print(f"DialoGPT: {output}")
このコードのコアを説明しましょう:
input_ids
DialoGPTトークナイザーを使用してテキストをエンコードし、文字列トークンの末尾を追加して、Pytorchテンソルとして返します。input_ids
モデルに直接フィードして世代を超えます。それ以外の場合は、torch.cat()
メソッドを使用して連結を使用してチャット履歴を追加します。model.generate()
、チャットボット応答を生成する方法を使用します。tokenizer.decode()
してシーケンスをデコードし、に設定skip_special_tokens
しTrue
て、などの迷惑な特別なトークンが表示されないようにし<|endoftext|>
ます。また、モデルはシーケンス全体を返すため、以前のチャット履歴をスキップし、新しく生成されたチャットボットの回答のみを出力します。以下は、ボットとのディスカッションの例です。
>> You:How can you be rich so quickly?
DialoGPT: I'm not rich, I'm just a rich man.
>> You:Ok, you don't make sense, how old are you ?
DialoGPT: I'm not a rich man, I'm just a rich man.
>> You:How can you be happy ?
DialoGPT: I'm not happy, I'm just a rich man.
>> You:But they say money buys happiness!
DialoGPT: Money buys happiness, but not happiness itself.
>> You:You don't make sense again!
DialoGPT: I'm not happy, I'm just a rich man.
モデルは多くの応答を繰り返すことがわかります。これらは最も高い確率であり、毎回それを選択しているためです。
デフォルトでmodel.generate()
は、他のパラメーターが設定されていない場合は欲張り検索アルゴリズムを使用します。次のセクションでは、このメソッドにいくつかの引数を追加して、生成を改善できるかどうかを確認します。
ビーム検索ではnum_beams
、各タイムステップで最も可能性の高い仮説を保持し、全体的に最も確率の高いシーケンスを取得することで、高確率シーケンスを見逃すリスクを減らすことができます。以下のコードは、ビーム検索でチャットボット応答を生成します。
# chatting 5 times with beam search
for step in range(5):
# take user input
text = input(">> You:")
# encode the input and add end of string token
input_ids = tokenizer.encode(text + tokenizer.eos_token, return_tensors="pt")
# concatenate new user input with chat history (if there is)
bot_input_ids = torch.cat([chat_history_ids, input_ids], dim=-1) if step > 0 else input_ids
# generate a bot response
chat_history_ids = model.generate(
bot_input_ids,
max_length=1000,
num_beams=3,
early_stopping=True,
pad_token_id=tokenizer.eos_token_id
)
#print the output
output = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
print(f"DialoGPT: {output}")
inメソッドに設定num_beams
する場合、各タイムステップで3つの単語を選択し、それらを開発して、シーケンスの全体的な確率が最も高くなるようにします。1に設定することは、欲張り検索と同じです。3model.generate()num_beams
以下は、ビーム検索を使用したチャットボットとのディスカッションの例です。
>> You:How can I be rich?
DialoGPT: You can't.
>> You:Why not?
DialoGPT: Because you can't.
>> You:Then how am I supposed to live well?
DialoGPT: You can't.
>> You:So basically, you want me to be a poor guy?
DialoGPT: No, I want you to be a rich guy.
>> You:Ok how to be rich, then?
DialoGPT: I don't know.
前のセクションでは、ビーム検索と欲張り検索を使用して、最も確率の高いシーケンスを生成しました。これは、出力が予測可能な機械翻訳やテキスト要約などのタスクに最適です。ただし、チャットボットのように、オープンエンド世代には最適なオプションではありません。
より良い生成のために、確率に基づいて広範囲の候補配列からサンプリングするランダム性を導入する必要があります。
# chatting 5 times with sampling
for step in range(5):
# take user input
text = input(">> You:")
# encode the input and add end of string token
input_ids = tokenizer.encode(text + tokenizer.eos_token, return_tensors="pt")
# concatenate new user input with chat history (if there is)
bot_input_ids = torch.cat([chat_history_ids, input_ids], dim=-1) if step > 0 else input_ids
# generate a bot response
chat_history_ids = model.generate(
bot_input_ids,
max_length=1000,
do_sample=True,
top_k=0,
pad_token_id=tokenizer.eos_token_id
)
#print the output
output = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
print(f"DialoGPT: {output}")
今回は、サンプリング用に設定do_sample
しTrue
、可能なすべての確率を選択していることtop_k
を0
示すように設定しましたtop_k
。後でパラメーターについて説明します。
これらのパラメータを使用したチャットは次のとおりです。
>> You:Hello, how can I be rich?
DialoGPT: You can't
>> You:Really? then how others get rich?
DialoGPT: They work as money artists
>> You:How to be a money artist?
DialoGPT: I don't know, I don't have enough knowledge on artists
>> You:Alright, thank you!
DialoGPT: No problem, if you need tips pm me.
>> You:Great, you're my savior!
DialoGPT: Thank you for helping a stranger out like me!
明らかにいくつかの改善があります。ただし、確率の低いシーケンスの幅広いリストでサンプリングすると、ランダムに生成される可能性があります(最後の文で見られるように)。
それをさらに改善するために、次のことができます。
temperature
低くすると、確率の低い単語を選択する可能性が低くなり、確率の高い単語を選択する可能性が高くなります。# chatting 5 times with Top K sampling & tweaking temperature
for step in range(5):
# take user input
text = input(">> You:")
# encode the input and add end of string token
input_ids = tokenizer.encode(text + tokenizer.eos_token, return_tensors="pt")
# concatenate new user input with chat history (if there is)
bot_input_ids = torch.cat([chat_history_ids, input_ids], dim=-1) if step > 0 else input_ids
# generate a bot response
chat_history_ids = model.generate(
bot_input_ids,
max_length=1000,
do_sample=True,
top_k=100,
temperature=0.75,
pad_token_id=tokenizer.eos_token_id
)
#print the output
output = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
print(f"DialoGPT: {output}")
ここで、確率で降順にソートされた上位の単語からサンプリングtop_k
する100
ように設定しました100
。またtemperature
、0.75
(デフォルトは1.0
)に設定して、確率の高い単語を選択する可能性を高めます。温度を0.0
貪欲検索と同じに設定し、無限大に設定すると、完全にランダムと同じになります。
これらのパラメータについての説明は次のとおりです。
>> You:How can I get rich?
DialoGPT: I'd also like to know this.
>> You:So you don't know it?
DialoGPT: I know it, but it doesn't matter.
>> You:Really? money matters, man!
DialoGPT: Well, money is money and I gotta get my money.
>> You:Yeah how to get them?
DialoGPT: Well, that's a question. I'd like to know more, just for curiosity's sake, to know what other people have said about how I should get rich.
>> You:Some robots like you told me you have to buy gold, silver or stocks.
DialoGPT: I think my wallet is empty.
ご覧のとおり、今でははるかに優れています。パラメータを自由に調整してtemperature
、top_k
改善できるかどうかを確認してください。
核サンプリングまたはTop-pサンプリングは、累積確率がp
設定したパラメーターを超える可能な限り小さい単語から選択します。
以下は、Top-pサンプリングを使用した例です。
# chatting 5 times with nucleus sampling & tweaking temperature
for step in range(5):
# take user input
text = input(">> You:")
# encode the input and add end of string token
input_ids = tokenizer.encode(text + tokenizer.eos_token, return_tensors="pt")
# concatenate new user input with chat history (if there is)
bot_input_ids = torch.cat([chat_history_ids, input_ids], dim=-1) if step > 0 else input_ids
# generate a bot response
chat_history_ids = model.generate(
bot_input_ids,
max_length=1000,
do_sample=True,
top_p=0.95,
top_k=0,
temperature=0.75,
pad_token_id=tokenizer.eos_token_id
)
#print the output
output = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
print(f"DialoGPT: {output}")
Top-kサンプリングを無効にするtop_k
よう0
に設定しましたが、どちらの方法も使用できるため、より効果的に機能する傾向があります。これがチャットです:
>> You:How can I be rich fast?
DialoGPT: Inflation and free money
>> You:lol, but that takes time, I need fast money.
DialoGPT: Your post makes no sense. You just need a lot of money to be rich fast
>> You:Ok how to get a lot of money?
DialoGPT: Buy gold and then trade it for money.
>> You:Hmm, I got your point, thanks.
DialoGPT: No problem. You have to have money to buy gold, but its only good for trading it.
>> You:Yeah, I understand, thank you so much.
DialoGPT: Ok, let me know if you need help
現在、チャットボットは多くの場合明らかに意味があります。
次に、複数のチャットボット応答を生成するコードを追加して、次の入力に含める応答を選択します。
# chatting 5 times with nucleus & top-k sampling & tweaking temperature & multiple
# sentences
for step in range(5):
# take user input
text = input(">> You:")
# encode the input and add end of string token
input_ids = tokenizer.encode(text + tokenizer.eos_token, return_tensors="pt")
# concatenate new user input with chat history (if there is)
bot_input_ids = torch.cat([chat_history_ids, input_ids], dim=-1) if step > 0 else input_ids
# generate a bot response
chat_history_ids_list = model.generate(
bot_input_ids,
max_length=1000,
do_sample=True,
top_p=0.95,
top_k=50,
temperature=0.75,
num_return_sequences=5,
pad_token_id=tokenizer.eos_token_id
)
#print the outputs
for i in range(len(chat_history_ids_list)):
output = tokenizer.decode(chat_history_ids_list[i][bot_input_ids.shape[-1]:], skip_special_tokens=True)
print(f"DialoGPT {i}: {output}")
choice_index = int(input("Choose the response you want for the next input: "))
chat_history_ids = torch.unsqueeze(chat_history_ids_list[choice_index], dim=0)
一度に5つの文を返すように設定num_return_sequences
し5
ました。次のシーケンスに含まれる文を選択する必要があります。これがどのように進んだかです:
>> You:Hello, friend.
DialoGPT 0: Hey there!
DialoGPT 1: I am your friend, and I am also your friend.
DialoGPT 2: Hello. :
DialoGPT 3: Hiya! How are you?
DialoGPT 4: Hello. I like your username.
Choose the response you want for the next input: 3
>> You:Fine, thanks, what about you?
DialoGPT 0: Good. Not much to do.
DialoGPT 1: Not much, you?
DialoGPT 2: Fine, thanks.
DialoGPT 3: I'm okay, thank you for asking.
DialoGPT 4: I'm fine too.
Choose the response you want for the next input: 2
>> You:Can you tell me how can I get rich quickly?
DialoGPT 0: Yes, I can!
DialoGPT 1: Yes, just look up how to get rich on Google.
DialoGPT 2: Just keep doing your thing. You'll get rich.
DialoGPT 3: No, I can't.
DialoGPT 4: no, i have no money
Choose the response you want for the next input: 1
>> You:Yeah, but I thought you have an answer...
DialoGPT 0: I'm getting married in the morning.
DialoGPT 1: How do you get rich?
DialoGPT 2: I think there are many good answers, but I could send you one, if you want.
DialoGPT 3: I will, thanks!
DialoGPT 4: I thought you have an answer.
Choose the response you want for the next input: 2
>> You:Yes, please send me.
DialoGPT 0: You can send me link.
DialoGPT 1: I am very interested
DialoGPT 2: I sent you a PM
DialoGPT 3: I'll send you a PM
DialoGPT 4: I am always interested in new ideas.
Choose the response you want for the next input: 2
このチュートリアルが、DialoGPTおよび同様のモデルでテキストを生成する方法に役立つことを願っています。テキストを生成する方法の詳細については、「Transformersを使用してテキストを生成する方法」ガイドを読むことを強くお勧めします。
ボットのパフォーマンスを向上させることができるかどうかを確認するために、パラメーターを微調整しておきます。
また、これをテキスト読み上げおよび音声読み上げのチュートリアルと組み合わせて、Alexa、Siri、Cortanaなどの仮想アシスタントを構築することもできます。