聊天機器人是一種人工智能,它通過應(yīng)用程序或消息來模擬與用戶的對話。本文我們將使用Pytho的chatterbot庫來實現(xiàn)聊天機器人。該庫生成對用戶輸入的自動響應(yīng)。響應(yīng)基于庫中實現(xiàn)的機器學(xué)習(xí)算法。
機器學(xué)習(xí)算法使聊天機器人在收集用戶響應(yīng)時更容易隨著時間的推移改進和優(yōu)化響應(yīng)。
這些功能使聊天機器人更容易通過不同的移動應(yīng)用程序和網(wǎng)站進行對話。它會保存來自用戶的數(shù)據(jù)并隨著時間的推移,聊天機器人響應(yīng)的準確性會提高。
創(chuàng)建功能聊天機器人的步驟:
1、創(chuàng)建一個聊天機器人:這是使用create_bot函數(shù)完成的。該函數(shù)將名稱bot作為輸入?yún)?shù)。此函數(shù)返回一個對象,該對象bo在程序中進一步使用。在例子中,我們將其設(shè)置為Jordan。
2、訓(xùn)練聊天機器人:這是使用train_all_data函數(shù)完成的。我們正在訓(xùn)練聊天機器人的數(shù)據(jù)顯示在這里。此函數(shù)的輸入?yún)?shù)bot.
3、使用自定義數(shù)據(jù)訓(xùn)練:我們使用custom_train函數(shù)使用自定義數(shù)據(jù)訓(xùn)練聊天機器人。
這個函數(shù)的第一個輸入?yún)?shù)是它bot本身。
第二個參數(shù)是我們要訓(xùn)練的自定義數(shù)據(jù)。此自定義數(shù)據(jù)采用Python的形式list。列表的第一個元素是問題,第二個元素是答案。您可以根據(jù)需要使用盡可能多的特定自定義數(shù)據(jù)來訓(xùn)練聊天機器人。
4、啟動聊天機器人:使用start_chatbot函數(shù)啟動聊天機器人。這個函數(shù)的輸入?yún)?shù)是bot我們要啟動的。
Ai聊天機器人代碼部分
def create_bot(name):
from chatterbot import ChatBot
Bot=ChatBot(name=name,
read_only=False,
logic_adapters=["chatterbot.logic.BestMatch"],
storage_adapter="chatterbot.storage.SQLStorageAdapter")
return Bot
def train_all_data(Bot):
from chatterbot.trainers import ChatterBotCorpusTrainer
corpus_trainer=ChatterBotCorpusTrainer(Bot)
corpus_trainer.train("chatterbot.corpus.english")
def custom_train(Bot,conversation):
from chatterbot.trainers import ListTrainer
trainer=ListTrainer(Bot)
trainer.train(conversation)
def start_chatbot(Bot):
print('\033c')
print("Hello,I am Jordan.How can I help you")
bye_list=["bye jordan","bye","good bye"]
while(True):
user_input=input("me:")
if user_input.lower()in bye_list:
print("Jordan:Good bye and have a blessed day!")
break
response=Bot.get_response(user_input)
print("Jordan:",response)
登錄后復(fù)制






