當前位置:
首頁 > 最新 > 智能聊天機器人-入門一

智能聊天機器人-入門一

當下,隨著人工智慧的火熱,作為人工智慧的一個重要分支-智能聊天機器人也跟隨著火熱了一把。目前各類巨頭、人工智慧創業公司,都在語音應用、聊天應用上做了相對應的布局,如百度的dueros、小米音箱、亞馬遜的alex、蘋果的siri等等。最近因為工作上需要,對智能聊天機器人做了一些調查和研究,基於工作需要,這裡記錄下學習過程。

經過這幾天的查閱和訪問周邊的同事,最後我們確定基於rasa開源工具來建設我們的智能對話聊天機器人。rasa是一款國外開源的專門用於處理自然語義理解(NLU)的一款開源工具,包括rasa-nlu、rasa-core,網上可能找到相關的介紹,官方幫助網址是:https://rasahq.github.io/rasa_nlu/index.html。

大家都知道,當我們學習新東西的時候,最好的辦法就是快速搭建好環境,麻利的跑起來才是王道。接下來,我快速帶大家入門。

環境:centos7.2

python 3.6

安裝:pip install rasa-core

pip install rasa-nlu

新建一個領域文件,yml格式,common_domain.yml內容如下:

intents:

- greet

- goodbye

- chat

entities:

- action

templates:

utter_greet:

-"hello!"

utter_goodbye:

-"byebye :("

utter_chat:

-"It seems like funny!"

actions:

- utter_greet

- utter_goodbye

- utter_chat

接下來,新建一個run.py文件,代碼如下:

from__future__importabsolute_import

from__future__importdivision

from__future__importprint_function

from__future__importunicode_literals

importlogging

importnumpyasnp

fromrasa_coreimportutils

fromrasa_core.actions.actionimportACTION_LISTEN_NAME

fromrasa_core.agentimportAgent

fromrasa_core.channels.consoleimportConsoleInputChannel

fromrasa_core.domainimportTemplateDomain

fromrasa_core.interpreterimportNaturalLanguageInterpreter

fromrasa_core.policiesimportPolicy

fromrasa_core.tracker_storeimportInMemoryTrackerStore

logger = logging.getLogger(__name__)

classCommonPolicy(Policy):

defpredict_action_probabilities(self, tracker, domain):

# type: (DialogueStateTracker, Domain) -> List[float]

#將對應的意圖與動作綁定

responses = {

"greet":2,

"goodbye":3,

"chat":4,

}

iftracker.latest_action_name == ACTION_LISTEN_NAME:

key = tracker.latest_message.intent["name"]

action = responses[key]ifkeyinresponseselse4

returnutils.one_hot(action, domain.num_actions)

else:

returnnp.zeros(domain.num_actions)

classHelloInterpreter(NaturalLanguageInterpreter):

defparse(self, message):

# intent = "greet" if "hello" in message else "default"

globalintent

#進行意圖識別

if"hello"inmessage:

intent="greet"

elif"goodbye"inmessage:

intent="goodbye"

else:

intent="chat"

return{

"text": message,

"intent": {"name": intent,"confidence":1.0},

"entities": []

}

defrun_hello_world(serve_forever=True):

default_domain = TemplateDomain.load("/Users/zhouqijun/Documents/self-python/pyframework/pynlp3/common_domain.yml")#載入多個domain怎麼辦

agent = Agent(default_domain,

policies=[CommonPolicy()],

interpreter=HelloInterpreter(),

tracker_store=InMemoryTrackerStore(default_domain))

ifserve_forever:

# Attach the commandline input to the controller to handle all

# incoming messages from that channel

agent.handle_channel(ConsoleInputChannel())

returnagent

if__name__ =="__main__":

run_hello_world()

接著就可以運行run.py了,效果如下:

能運行如上效果,說明你本地的環境已經完好,可以繼續往前探索了。


喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 全球大搜羅 的精彩文章:

渣男都是靠這樣東西騙你的!
做好今天的自己才是對明天最好的安排

TAG:全球大搜羅 |