Hi!
I def a function in run.py like this :
model_path=‘/home/username/Documents/RasaW/models/20190821-011550.tar.gz’
agent = Agent.load(model_path)
async def parse(text: Text,sender_id_text: Text):
logger.info('Start') response = await agent.handle_text(text,sender_id=sender_id_text) logger.info("Text: '{}'".format(text)) logger.info("Response:") logger.info(response) logger.info('end') return response
and run2.py like this:
coroutine=run.parse('你好','wa')
loop.run_until_complete(coroutine)
coroutine=run.parse('帮我查话费','wa')
loop.run_until_complete(coroutine)
coroutine=run.parse('12月','wa')
loop.run_until_complete(coroutine)
coroutine=run.parse('恩','wa')
loop.run_until_complete(coroutine)
But,The response written in the actions.py file does not executed. The log file is,
> 2019-08-24 09:00:23,718 - run - INFO - Start
> 2019-08-24 09:00:24,333 - run - INFO - Text: '你好'
> 2019-08-24 09:00:24,333 - run - INFO - Response:
> 2019-08-24 09:00:24,333 - run - INFO - [{'recipient_id': 'wa', 'text': 'hi! 我是小贝,有什么可以帮您吗。'}]
> 2019-08-24 09:00:24,333 - run - INFO - end
> 2019-08-24 09:00:24,333 - run - INFO - Start
> 2019-08-24 09:00:24,338 - run - INFO - Text: '帮我查话费'
> 2019-08-24 09:00:24,338 - run - INFO - Response:
> 2019-08-24 09:00:24,339 - run - INFO - [{'recipient_id': 'wa', 'text': '你想查哪个时间段的?'}]
> 2019-08-24 09:00:24,339 - run - INFO - end
> 2019-08-24 09:00:24,339 - run - INFO - Start
> 2019-08-24 09:00:24,344 - run - INFO - Text: '12月'
> 2019-08-24 09:00:24,344 - run - INFO - Response:
> 2019-08-24 09:00:24,344 - run - INFO - [{'recipient_id': 'wa', 'text': '你确认吗?'}]
> 2019-08-24 09:00:24,344 - run - INFO - end
> 2019-08-24 09:00:24,344 - run - INFO - Start
> 2019-08-24 09:00:24,352 - run - INFO - Text: '恩'
> 2019-08-24 09:00:24,352 - run - INFO - Response:
> 2019-08-24 09:00:24,352 - run - INFO - [{'recipient_id': 'wa', 'text': '还有什么能帮您吗?'}]
> 2019-08-24 09:00:24,352 - run - INFO - end
The actions.py file is ,
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from rasa_core_sdk import Action
from rasa_core_sdk.events import SlotSet
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# create a filehandler
handler =logging.FileHandler('hello.log')
handler.setLevel(logging.INFO)
# create alogging format
formatter =logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
# add thehandlers to the logger
logger.addHandler(handler)
support_search = ["话费", "流量","月租"]
def extract_item(item):
if item is None:
return None
for name in support_search:
if name in item:
return name
return None
class ActionSearchConsume(Action):
def name(self):
return 'action_search_consume'
def run(self, dispatcher, tracker, domain):
logger.info('action actived')
item = tracker.get_slot("item")
item = extract_item(item)
if item is None:
dispatcher.utter_message("您好,我现在只会查话费和流量")
dispatcher.utter_message("你可以这样问我:“帮我查话费”")
return []
time = tracker.get_slot("time")
if time is None:
dispatcher.utter_message("您想查询哪个月的消费?")
return []
# query database here using item and time as key. but you may normalize time format first.
dispatcher.utter_message("好,请稍等")
if item == "流量":
dispatcher.utter_message(
"您好,您{}共使用{}二百八十兆,剩余三十兆。".format(time, item))
elif item=="月租":
dispatcher.utter_message("您好,您月租100元")
else:
dispatcher.utter_message("您好,您{}共消费二十八元。".format(time))
return []