Implements the `question_response`, `new_question_answer` and `new_context` methods

class DialogSystem[source]

DialogSystem(context_data_file=None, faq_data_file=None, configs_faq=None, download_models=True)

The DialogSystem class implements the main methods defined in the settings module.

INPUT:

  • context_data_file: csv file of contexts (default: None)

  • faq_data_file: csv file of FAQs (default: None)

  • configs_faq: json config file (default: None)

  • download_models: Indicates if download configuration files (default: True)

If the context or the faq files are not provided, a data directory with the missing files, will be created (in the same path where the module is running).

When an instance is created, the 'run_shell_installs', 'load_and_prepare_data' and 'load_qa_models' of the settings module are called. Also the data and qa_models attributes are created, they store the dataframes and models information, respectively.

If the dataframes are provided they must have the following columns:

  1. context dataframe columns: 'topic', 'context'
  2. faq dataframe columns: 'Question, 'Answer'

DialogSystem.new_context[source]

DialogSystem.new_context(topic, context)

Adds a new context.

INPUT:

  • topic (The title of the context)
  • context

The new context is stored in the path self.data['context']['path']

DialogSystem.new_question_answer[source]

DialogSystem.new_question_answer(question, answer)

Adds a new question-answer pair.

INPUT:

  • question

  • answer

The new question-answer pair is stored in the path self.data['faq']['path'] and the models in qa_models['faq'] get re-trained by calling the function deeppavlaov.train_model

DialogSystem.question_answer[source]

DialogSystem.question_answer(question)

Gets answers to a question.

INPUT:

  • question parameter

The method creates the following attributes:

  • 'self.question' -> the input parameter

  • 'self.responses' -> a dict of possible responses

  • 'self.formatted_responses' -> a formatted string of the possible responses

This method calls the functions settings.get_response and settings.format_responses

Test Example

#test
import tempfile
from os import path
with tempfile.TemporaryDirectory() as tmpdirname:

    ds = DialogSystem(
        faq_data_file=path.join(tmpdirname, 'faq_example.csv'),
        context_data_file=path.join(tmpdirname, 'context_example.csv'),
        download_models=False
    )

    ds.question_answer(question='What is Intekglobal?')

    assert ds.question == 'What is Intekglobal?'
    assert isinstance(ds.responses['squad'], dict)
    assert isinstance(ds.responses['faq'], dict)
    
    
    logging.info(f'{ds.question} \n\n {ds.formatted_responses}')

    assert 'This should not be in the current set of responses' not in  ds.formatted_responses 
    
    logging.info(f' dict of responses: {ds.responses}')
    logging.info(f'{ds.question} \n\n {ds.formatted_responses}')

    ds.new_question_answer(
        question='What day is today?', answer='Today is the day!'
    )
    ds.question_answer(question='What day is today?')

    assert 'Today is the day!' in ds.formatted_responses

    context = '''Space Exploration Technologies Corp., trading as SpaceX,
    is an American aerospace manufacturer and space transportation services company headquartered
    in Hawthorne, California. It was founded in 2002 by Elon Musk with the goal of reducing 
    space transportation costs to enable the colonization of Mars. SpaceX has developed several launch 
    vehicles,the Starlink satellite constellation, and the Dragon spacecraft.
    '''

    ds.new_context(topic='SpaceX', context=context)
    ds.question_answer(question='What are SpaceX initials stand for?')

    logging.info(f'{ds.question} \n\n {ds.formatted_responses}')

    assert 'Space Exploration Technologies Corp.' in ds.formatted_responses
[nltk_data] Downloading package punkt to /home/jovyan/nltk_data...
[nltk_data]   Package punkt is already up-to-date!
[nltk_data] Downloading package stopwords to /home/jovyan/nltk_data...
[nltk_data]   Package stopwords is already up-to-date!
[nltk_data] Downloading package perluniprops to
[nltk_data]     /home/jovyan/nltk_data...
[nltk_data]   Package perluniprops is already up-to-date!
[nltk_data] Downloading package nonbreaking_prefixes to
[nltk_data]     /home/jovyan/nltk_data...
[nltk_data]   Package nonbreaking_prefixes is already up-to-date!
/opt/conda/lib/python3.7/site-packages/sklearn/linear_model/logistic.py:432: FutureWarning: Default solver will be changed to 'lbfgs' in 0.22. Specify a solver to silence this warning.
  FutureWarning)
/opt/conda/lib/python3.7/site-packages/sklearn/linear_model/logistic.py:432: FutureWarning: Default solver will be changed to 'lbfgs' in 0.22. Specify a solver to silence this warning.
  FutureWarning)
/opt/conda/lib/python3.7/site-packages/sklearn/linear_model/logistic.py:469: FutureWarning: Default multi_class will be changed to 'auto' in 0.22. Specify the multi_class option to silence this warning.
  "this warning.", FutureWarning)
#hide