ConversationAgent
Project description
Using
購票系統範例
bot_json = {
"__MAIN_STAGES__": [
{
"stage_type": "__RE_STAGE__",
"question": {
"sys_reply_q1": "請問是要做哪種票種呢?",
"sys_reply_q2": "請說『月票』或是『單程票』",
"sys_reply_complete": "好的,將開始訂購 %%set_level%% "
},
"is_fits": [
[
"(月票|1280|長期票|定期票)+",
"set_level"
],
[
"(單程票|單程|一次)+",
"set_level"
]
]
},
{
"stage_type": "Switch",
"stages_filter": [
[
"set_level",
"月票",
"_月票_"
],
[
"set_level",
"1280",
"_月票_"
],
[
"set_level",
"長期票",
"_月票_"
],
[
"set_level",
"定期票",
"_月票_"
],
[
"set_level",
"單程票",
"_單程票_"
],
[
"set_level",
"單程",
"_單程票_"
],
[
"set_level",
"一次",
"_單程票_"
]
]
}
],
"_月票_": [
{
"stage_type": "__RE_STAGE__",
"question": {
"sys_reply_q1": "月票的價格為 1280元,是否確認?",
"sys_reply_q2": "月票的價格為 1280元,是否確認?請回答『是』或『否』",
"sys_reply_complete": "好的,確認您使用 %%set_level%% 車廂的意願為 『 %%user_status%% 』,\n 感謝您的使用。\n "
},
"is_fits": [
[
"(是|好的|好|沒問題)+$",
"user_status"
],
[
"(否|不|不行|不要|不好)+$",
"user_status"
]
]
}
],
"_單程票_": [
{
"stage_type": "__RE_STAGE__",
"question": {
"sys_reply_q1": "",
"sys_reply_q2": "",
"sys_reply_complete": "如果要訂購單程票,請使用票卷機,感謝您的使用。\n "
},
"is_fits": [],
"__DISSABLE_Q1__": True
}
]
}
data = {}
agent = gen_multi_agent(bot_json)
#
text = "hi"
reply_text, data = ConversationAgent.mock_client_once(agent, text, data)
print(f"data: {data}")
print(f"reply_text: {reply_text}")
#
text = ""
reply_text, data = ConversationAgent.mock_client_once(agent, text, data)
print(f"data: {data}")
print(f"reply_text: {reply_text}")
#
text = "哪裡有廁所"
reply_text, data = ConversationAgent.mock_client_once(agent, text, data)
print(f"data: {data}")
print(f"reply_text: {reply_text}")
購票+問答系統範例
bot_json = {
"__MAIN_STAGES__": [
{
"stage_type": "__RE_STAGE__",
"question": {
"sys_reply_q1": "哈囉請問要做什麼? 目前提供『問答』和『訂票』服務",
"sys_reply_q2": "目前只提供『問答』和『訂票』服務喔",
"sys_reply_complete": "好的,將開始 『 %%selected_service%% 』 "
},
"is_fits": [
[
"(問答|問題|詢問)+",
"selected_service"
],
[
"(訂票|票價|買票)+",
"selected_service"
]
]
},
{
"stage_type": "Switch",
"stages_filter": [
[
"selected_service",
"訂票",
"_訂票_"
],
[
"selected_service",
"買票",
"_訂票_"
],
[
"selected_service",
"票價",
"_訂票_"
],
[
"selected_service",
"問答",
"_問答_"
],
[
"selected_service",
"問題",
"_問答_"
],
[
"selected_service",
"詢問",
"_問答_"
]
]
}
],
"_問答_": [
{
"stage_type": "__QA_STAGE__",
"corpus": {
"廁所在哪裡": "這裡沒有廁所",
"詢問處在哪裡": "這裡沒有詢問處",
"診所在哪裡": "這裡沒有診所",
},
"question": {
"sys_reply_q1": "請問有什麼問題呢?",
"sys_reply_q2": "",
"sys_reply_complete": "我有 %%__QA_RESPOND_SCORE__%% 的信心覺得您要問:<br> \n %%__QA_RESPOND_QUESTION__%% <br> \n 答案是 %%__QA_RESPOND__%% "
},
"is_fits": []
}
],
"_訂票_": [
{
"stage_type": "__RE_STAGE__",
"question": {
"sys_reply_q1": "請問是要做哪種票種呢?",
"sys_reply_q2": "請說『月票』或是『單程票』",
"sys_reply_complete": "好的,將開始訂購 %%set_level%% "
},
"is_fits": [
[
"(月票|1280|長期票|定期票)+",
"set_level"
],
[
"(單程票|單程|一次)+",
"set_level"
]
]
},
{
"stage_type": "Switch",
"stages_filter": [
[
"set_level",
"月票",
"_月票_"
],
[
"set_level",
"1280",
"_月票_"
],
[
"set_level",
"長期票",
"_月票_"
],
[
"set_level",
"定期票",
"_月票_"
],
[
"set_level",
"單程票",
"_單程票_"
],
[
"set_level",
"單程",
"_單程票_"
],
[
"set_level",
"一次",
"_單程票_"
]
]
}
],
"_月票_": [
{
"stage_type": "__RE_STAGE__",
"question": {
"sys_reply_q1": "月票的價格為 1280元,是否確認?",
"sys_reply_q2": "月票的價格為 1280元,是否確認?請回答『是』或『否』",
"sys_reply_complete": "好的,確認您使用 %%set_level%% 車廂的意願為 『 %%user_status%% 』,\n 感謝您的使用。\n "
},
"is_fits": [
[
"(是|好的|好|沒問題)+$",
"user_status"
],
[
"(否|不|不行|不要|不好)+$",
"user_status"
]
]
}
],
"_單程票_": [
{
"stage_type": "__RE_STAGE__",
"question": {
"sys_reply_q1": "",
"sys_reply_q2": "",
"sys_reply_complete": "如果要訂購單程票,請使用票卷機,感謝您的使用。\n "
},
"is_fits": [],
"__DISSABLE_Q1__": True
}
]
}
data = {}
agent = gen_multi_agent(bot_json)
#
text = "hi"
reply_text, data = ConversationAgent.mock_client_once(agent, text, data)
print(f"data: {data}")
print(f"reply_text: {reply_text}")
#
text = "我要月票"
reply_text, data = ConversationAgent.mock_client_once(agent, text, data)
print(f"data: {data}")
print(f"reply_text: {reply_text}")
#
text = "好"
reply_text, data = ConversationAgent.mock_client_once(agent, text, data)
print(f"data: {data}")
print(f"reply_text: {reply_text}")
如何客製化類別:以購票+問答系統為例子
from ConversationAgent.LibStage import __LIB_STAGES__
from ConversationAgent.LibStage import QAStage
import requests
"""
##########
__NEW QAWorkerSTAGE__
##########
"""
__NEW_QUESTIONANSWER__ = "NEW_QUESTIONANSWER"
class QAWorkerSTAGE(QAStage):
def __init__(self,data):
super(QAWorkerSTAGE, self).__init__(data)
self.similar_method = data.get(self.__SIMILAR_METHOD__, "worker_api")
self.__NLPCORESERVER__ = "http://52.147.71.0:8000"
def __request_similar_api__(self,text,corpus):
res = requests.post(url=f"{self.__NLPCORESERVER__}/jobs/{self.similar_method}", json={
"sentence": [
text
],
"corpus": corpus})
return res.json()
# 增加自訂義的類別
__LIB_STAGES__[__NEW_QUESTIONANSWER__] = QAWorkerSTAGE
"""
##########
Bot
##########
"""
bot_json = {
"__MAIN_STAGES__": [
{
"stage_type": "__RE_STAGE__",
"question": {
"sys_reply_q1": "哈囉請問要做什麼? 目前提供『問答』和『訂票』服務",
"sys_reply_q2": "目前只提供『問答』和『訂票』服務喔",
"sys_reply_complete": "好的,將開始 『 %%selected_service%% 』 "
},
"is_fits": [
[
"(問答|問題|詢問)+",
"selected_service"
],
[
"(訂票|票價|買票)+",
"selected_service"
]
]
},
{
"stage_type": "Switch",
"stages_filter": [
[
"selected_service",
"訂票",
"_訂票_"
],
[
"selected_service",
"買票",
"_訂票_"
],
[
"selected_service",
"票價",
"_訂票_"
],
[
"selected_service",
"問答",
"_問答_"
],
[
"selected_service",
"問題",
"_問答_"
],
[
"selected_service",
"詢問",
"_問答_"
]
]
}
],
"_問答_": [
{
"stage_type": __NEW_QUESTIONANSWER__,
"corpus": {
"廁所在哪裡": "這裡沒有廁所",
"詢問處在哪裡": "這裡沒有詢問處",
"診所在哪裡": "這裡沒有診所",
},
"question": {
"sys_reply_q1": "請問有什麼問題呢?",
"sys_reply_q2": "",
"sys_reply_complete": "我有 %%__QA_RESPOND_SCORE__%% 的信心覺得您要問:<br> \n %%__QA_RESPOND_QUESTION__%% <br> \n 答案是 %%__QA_RESPOND__%% "
},
"is_fits": []
}
],
"_訂票_": [
{
"stage_type": "__RE_STAGE__",
"question": {
"sys_reply_q1": "請問是要做哪種票種呢?",
"sys_reply_q2": "請說『月票』或是『單程票』",
"sys_reply_complete": "好的,將開始訂購 %%set_level%% "
},
"is_fits": [
[
"(月票|1280|長期票|定期票)+",
"set_level"
],
[
"(單程票|單程|一次)+",
"set_level"
]
]
},
{
"stage_type": "Switch",
"stages_filter": [
[
"set_level",
"月票",
"_月票_"
],
[
"set_level",
"1280",
"_月票_"
],
[
"set_level",
"長期票",
"_月票_"
],
[
"set_level",
"定期票",
"_月票_"
],
[
"set_level",
"單程票",
"_單程票_"
],
[
"set_level",
"單程",
"_單程票_"
],
[
"set_level",
"一次",
"_單程票_"
]
]
}
],
"_月票_": [
{
"stage_type": "__RE_STAGE__",
"question": {
"sys_reply_q1": "月票的價格為 1280元,是否確認?",
"sys_reply_q2": "月票的價格為 1280元,是否確認?請回答『是』或『否』",
"sys_reply_complete": "好的,確認您使用 %%set_level%% 車廂的意願為 『 %%user_status%% 』,\n 感謝您的使用。\n "
},
"is_fits": [
[
"(是|好的|好|沒問題)+$",
"user_status"
],
[
"(否|不|不行|不要|不好)+$",
"user_status"
]
]
}
],
"_單程票_": [
{
"stage_type": "__RE_STAGE__",
"question": {
"sys_reply_q1": "",
"sys_reply_q2": "",
"sys_reply_complete": "如果要訂購單程票,請使用票卷機,感謝您的使用。\n "
},
"is_fits": [],
"__DISSABLE_Q1__": True
}
]
}
data = {}
agent = gen_multi_agent(bot_json)
#
text = "hi"
reply_text, data = ConversationAgent.mock_client_once(agent, text, data)
print(f"data: {data}")
print(f"reply_text: {reply_text}")
#
text = "我有點問題"
reply_text, data = ConversationAgent.mock_client_once(agent, text, data)
print(f"data: {data}")
print(f"reply_text: {reply_text}")
#
text = "附近有廁所嗎"
reply_text, data = ConversationAgent.mock_client_once(agent, text, data)
print(f"data: {data}")
print(f"reply_text: {reply_text}")
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.