Esempio n. 1
0
    async def __chat_internal(self, chat_input, topic, history, entities,
                              chat_reload_training):
        if self.last_chat_time is None:
            await self.controller.start_chat_for_ai(self)

        if self.__chat_process_pool is None:
            process_name = 'Chat_{0}'.format(self.ai_id)
            chat_pool = a_pool.AsyncProcessPool(
                self.controller.multiprocessing_manager,
                process_name,
                num_processes=1)
            chat_process_worker, process_kwargs = \
                self.create_chat_process_worker()
            if process_kwargs is None:
                process_kwargs = {}

            await chat_pool.initialize_processes(chat_process_worker,
                                                 **process_kwargs)
            self.__chat_process_pool = chat_pool
            msg = self.create_wake_chat_message(str(self.ai_data_directory),
                                                self.ai_id)
            await self.__chat_process_pool.do_work(msg)

        self.last_chat_time = datetime.datetime.utcnow()
        msg = ait_c.ChatRequestMessage(chat_input, topic, history,
                                       chat_reload_training, entities)
        result = await self.__chat_process_pool.do_work(msg)
        return result
async def test_custom_entity_match(mocker, mocked_chat):
    score = 1.0
    msg = ait_c.ChatRequestMessage("This is a custom entity match",
                                   None, None, update_state=True,
                                   entities={"custom entity": ["custom_ent", "fake_tag"],
                                             "fake entity value": ["fake_tag2"]})
    response = await mocked_chat.chat_request(msg)
    assert response.answer == "custom entity match"
    assert response.score == score
    assert response.topic_out is None
    assert response.history is None
async def test_chat_request_no_match(mocker, mocked_chat):
    score = 0.0
    mocker.spy(mocked_chat.entity_wrapper, "match_entities")

    msg = ait_c.ChatRequestMessage("How are you?", None, None, update_state=True, entities=None)
    response = await mocked_chat.chat_request(msg)
    assert response.answer == ""
    assert response.score == score
    assert response.topic_out is None
    assert response.history is None
    assert mocked_chat.entity_wrapper.match_entities.call_count == 2
async def test_chat_request_entity_match(mocker, mocked_chat):
    score = float(embedding.chat_process.ENTITY_MATCH_PROBA)
    mocker.spy(mocked_chat.entity_wrapper, "match_entities")

    msg = ait_c.ChatRequestMessage("This should give an entity match for London and today",
                                   None, None, update_state=True, entities=None)
    response = await mocked_chat.chat_request(msg)
    assert response.answer == "entity wins with London today"
    assert response.score == score
    assert response.topic_out is None
    assert response.history is None
    assert mocked_chat.entity_wrapper.match_entities.call_count == 2
async def test_casing_match(mocker, mocked_chat):
    score = 1.0
    msg = ait_c.ChatRequestMessage("week 1",
                                   None, None, update_state=True,
                                   entities={"Week 1": ["week"]})
    response = await mocked_chat.chat_request(msg)
    assert response.answer == "week"
    assert response.score == score

    msg = ait_c.ChatRequestMessage("Week 2",
                                   None, None, update_state=True,
                                   entities={"Week 2": ["week"]})
    response = await mocked_chat.chat_request(msg)
    assert response.answer == "week"
    assert response.score == score

    msg = ait_c.ChatRequestMessage("wEEk 3",
                                   None, None, update_state=True,
                                   entities={"wEEk 3": ["week"]})
    response = await mocked_chat.chat_request(msg)
    assert response.answer == "week"
    assert response.score == score
async def test_chat_request_string_match(mocker, mocked_chat):
    score = 1.00
    mocker.spy(mocked_chat.entity_wrapper, "match_entities")

    msg = ait_c.ChatRequestMessage("This is a perfect string match",
                                   None, None, update_state=True, entities=None)
    response = await mocked_chat.chat_request(msg)
    assert response.answer == "string wins"
    print(response.score)
    assert round(response.score, 2) == score
    assert response.topic_out is None
    assert response.history is None
    assert mocked_chat.entity_wrapper.match_entities.call_count == 1
async def test_chat_request_embedding_match(mocker, mocked_chat):
    score = 0.85
    mocker.spy(mocked_chat.entity_wrapper, "match_entities")
    mocker.patch("embedding.text_classifier_class.EmbeddingComparison.predict")
    embedding.text_classifier_class.EmbeddingComparison.predict.return_value = (
        ["embedding wins"], [1.0])

    msg = ait_c.ChatRequestMessage("This should match with word1 word2",
                                   None, None, update_state=True, entities=None)
    response = await mocked_chat.chat_request(msg)
    assert response.answer == "embedding wins"
    assert response.score == score
    assert response.topic_out is None
    assert response.history is None
    assert mocked_chat.entity_wrapper.match_entities.call_count == 1