Exemplo n.º 1
0
 def handle_message(self, message):
     self._parse_if_needed(message)
     uttered = UserUttered(message)
     tracker = self.tracker_store.get_tracker(message.sender_id)
     tracker.update(uttered)
     self.listen(tracker)
     skill_name = self.policy.pick_skill(tracker)
     responses = []
     triggered_event = None
     if skill_name is not None:
         while skill_name is not None:
             skill_name, parameters = self._parse_skill_name(skill_name)
             skill = self.skills[skill_name]()
             for event in skill.perform(tracker, **parameters):
                 tracker.update(event)
                 if isinstance(event, BotUttered):
                     bot_message = event.bot_message
                     responses.append(bot_message)
                 elif isinstance(event, AgentTriggered):
                     triggered_event = event
             skill_name = self.policy.pick_skill(tracker)
     self.tracker_store.update_tracker(tracker)
     if triggered_event is not None:
         new_message = UserMessage(
             message.sender_id, 
             triggered_event.text,
             external_intent=triggered_event.intent,
             external_entities=triggered_event.entities
         )
         responses.extend(self.dialogue_engine.handel_message_with_engines(new_message))
     return responses
Exemplo n.º 2
0
def run_cmd():
    channel = CollectingOutputChannel()
    while True:
        msg = input("user:"******"", msg, channel=channel)
        engine.handle_message(message)
        for res in channel.messages:
            if res.text:
                print(f"bot:{res.text}")
            elif res.data:
                print(f"bot:{res.data}")
        channel.messages.clear()
Exemplo n.º 3
0
if __name__ == "__main__":
    # skill = GreetSkill()
    # events = skill.perform(None)
    # assert(len(events)) == 3
    # assert isinstance(events[1], BotUttered)
    # print(skill.generate_script())

    agent = Agent.from_config_file("tests/data/agent_config.json")
    from .agent.script import script
    agent.set_script(script)

    tracker = DialogueStateTracker("", agent)
    agent.add_skill(GreetFormSkill)

    message = UserMessage("", "嘿")
    agent.parser.parse(message)
    event = UserUttered(message)
    tracker.update(event)

    skill = GreetFormSkill()
    events = skill.perform(tracker)
    for event in events:
        tracker.update(event)
    for event in events:
        if isinstance(event, BotUttered):
            print(event.bot_message.text)

    message = UserMessage("", "我是罗西")
    agent.parser.parse(message)
    event = UserUttered(message)
Exemplo n.º 4
0
 def from_parameters(cls, parameter_string=None):
     message = UserMessage.from_pattern_string(parameter_string)
     return cls(message)
Exemplo n.º 5
0

class Sub2(Base):
    def __init__(self) -> None:
        self.cache["b"] = 2


s1 = Sub1()
s2 = Sub2()
print(s1.cache)
print(s2.cache)

if __name__ == "__main__":
    parser = RENaturalLanguageParser.from_config(
        {"intent_file": "tests/data/intent_patterns.json"})
    message1 = UserMessage("sender", "意图一实体")
    message2 = UserMessage("sender", "意图二")

    parser.parse(message1)
    assert "intent" in message1.parsed_data
    intent = message1.get("intent")
    assert intent["name"] == "intent1"
    assert "entities" in message1.parsed_data
    entities = message1.get("entities")
    assert len(entities) == 1
    entity = entities["entity"]
    assert (len(entity)) == 1
    entity = entity[0]
    assert entity["type"] == "entity"
    assert entity["value"] == "实体"
Exemplo n.º 6
0
from Broca.faq_engine.engine import FAQEngine
from Broca.message import UserMessage

if __name__ == "__main__":
    engine = FAQEngine.from_config_file("tests/data/faq_engine_config.json")
    engine.load_agents("tests")
    result = engine.handle_message(UserMessage("sender", "你来自哪里"))
    print(result["response"].text)
Exemplo n.º 7
0
from Broca.task_engine.engine import Engine
from Broca.channel import CollectingOutputChannel
from Broca.message import UserMessage
from .test_agent import read_scenes

if __name__ == "__main__":
    engine = Engine.from_config_file("tests/data/engine_config.json")
    engine.load_agents("tests")
    assert len(engine.agents) == 1
    agent = engine.agents[0]
    # assert len(agent.skills) == 11
    engine.collect_intent_patterns()

    user_msg = UserMessage("", "我要买电影票")
    responses = engine.handle_message(user_msg)
    print([response.text for response in responses])
    user_msg = UserMessage("", "第一个")
    responses = engine.handle_message(user_msg)
    print([response.text for response in responses])

    scenes = read_scenes("tests/data/dialogues.txt")
    for scene in scenes:
        engine = Engine.from_config_file("tests/data/engine_config.json")
        engine.load_agents("tests")
        engine.collect_intent_patterns()
        for turn in scene:
            user_message = UserMessage("", turn[0])
            messages = engine.handle_message(user_message)

            if turn[1][0] == "null":
                assert messages == []