Beispiel #1
0
    def test_questions_passages(self, discovery):
        question_count = 5000
        agg = 'term(question.title,count:%s)'
        expected_agg = agg % str(question_count)
        expected_query_opts = {
          'aggregation': expected_agg,
          'count': 0
        }
        expected_value = [
          {'question': 'Can you tell a cabbie which route to take?'}
        ]
        mock_response = json.loads(
          """
            {
              "matching_results": 37314,
              "aggregations": [
                {
                  "type": "term",
                  "field": "question.title",
                  "count": 1,
                  "results": [
                    {
                      "key": "Can you tell a cabbie which route to take?",
                      "matching_results": 24
                    }
                  ]
                }
              ]
            }
          """ # noqa
        )
        discovery.query = MagicMock(return_value=mock_response)

        actual_value = get_questions(
                        discovery=discovery,
                        constants=constants,
                        question_count=question_count,
                        feature_type="passages")
        discovery.query.assert_called_with(
          environment_id='my_environment_id',
          collection_id='my_passages_collection_id',
          query_options=expected_query_opts
        )
        self.assertEqual(actual_value, expected_value)
              discovery,
              regular_name=os.getenv(
                            'DISCOVERY_REGULAR_COLLECTION_NAME',
                            'knowledge_base_regular'
                          ),
              enriched_name=os.getenv(
                            'DISCOVERY_ENRICHED_COLLECTION_NAME',
                            'knowledge_base_enriched'
                          )
            )
try:
    total_questions = int(os.getenv('DISCOVERY_QUESTION_COUNT', 5000))
except ValueError:
    sys.exit('DISCOVERY_QUESTION_COUNT not an integer, terminating...')

question_cache = get_questions(discovery, constants, total_questions)


@app.route('/')
@limiter.exempt
def index():
    return render_template('index.html')


@app.route('/api/query/<collection_type>', methods=['POST'])
def query(collection_type):
    collection_id_key = 'collection_id_regular'
    query_options = json.loads(request.data)
    query_options['return'] = 'text'

    if collection_type == 'enriched':
from helpers import get_anagram_hub_id, get_questions

parser = argparse.ArgumentParser()
parser.add_argument("--name", help="Extension name", dest="name")
parser.add_argument("--apiUrl", help="API URL", dest="api_url")
parser.add_argument("--authToken", help="API session token", dest="auth_token")
parser.add_argument("--settingsPath", help="Setting directory", dest="settings_path")
parser.add_argument("--logPath", help="Log directory", dest="log_path")
parser.add_argument(
    "--debug", help="Enable debug mode", dest="debug", action="store_true"
)
parser.set_defaults(debug=False)

with open("word_dict.pickle", "br") as f:
    WORD_DICT = pickle.load(f)


if __name__ == "__main__":
    args = parser.parse_args()
    auth_header = {"Authorization": args.auth_token}
    hub_id = get_anagram_hub_id(args.api_url, auth_header)
    for question in get_questions(args.api_url, auth_header, hub_id):
        word = "".join(sorted(question))
        possible_answers = WORD_DICT[word]
        for answer in possible_answers:
            requests.post(
                f"http://{args.api_url}hubs/{hub_id}/chat_message",
                json={"text": answer},
                headers=auth_header,
            )
Beispiel #4
0
    def test_questions_trained(self, discovery):
        question_count = 5000
        agg = 'term(question.title,count:%s)'
        expected_agg = agg % str(question_count)
        expected_query_opts = {
          'aggregation': expected_agg,
          'count': 0
        }
        expected_value = [
          {'question': 'Can you tell a cabbie which route to take?',
           'is_training_query': True}
        ]
        mock_response = json.loads(
          """
            {
              "matching_results": 37314,
              "aggregations": [
                {
                  "type": "term",
                  "field": "question.title",
                  "count": 1,
                  "results": [
                    {
                      "key": "Can you tell a cabbie which route to take?",
                      "matching_results": 24
                    }
                  ]
                }
              ]
            }
          """ # noqa
        )
        mock_training_data_response = json.loads(
          """
            {
              "environment_id": "env_id",
              "collection_id": "coll_id",
              "queries": [
                {
                  "query_id": "q_id",
                  "natural_language_query": "Can you tell a cabbie which route to take?",
                  "filter": "",
                  "examples": [
                    {
                      "document_id": "doc_id",
                      "cross_reference": "cross_id",
                      "relevance": 0
                    }
                  ]
                }
              ]
            }
          """ # noqa
        )
        discovery.query = MagicMock(return_value=mock_response)
        discovery.list_training_data = MagicMock(
          return_value=mock_training_data_response)

        actual_value = get_questions(
                        discovery=discovery,
                        constants=constants,
                        question_count=question_count,
                        feature_type="trained")
        discovery.query.assert_called_with(
          environment_id='my_environment_id',
          collection_id='my_trained_collection_id',
          query_options=expected_query_opts
        )
        discovery.list_training_data.assert_called_with(
          environment_id='my_environment_id',
          collection_id='my_trained_collection_id',
        )
        self.assertEqual(actual_value, expected_value)
"""
constants = get_constants(
    discovery,
    passages_name=os.getenv('DISCOVERY_PASSAGES_COLLECTION_NAME',
                            'knowledge_base_regular'),
    regular_name=os.getenv('DISCOVERY_REGULAR_COLLECTION_NAME',
                           'knowledge_base_regular'),
    trained_name=os.getenv('DISCOVERY_TRAINED_COLLECTION_NAME',
                           'knowledge_base_trained'))
try:
    total_questions = int(os.getenv('DISCOVERY_QUESTION_COUNT', 5000))
except ValueError:
    sys.exit('DISCOVERY_QUESTION_COUNT not an integer, terminating...')

passages_question_cache = get_questions(discovery=discovery,
                                        constants=constants,
                                        question_count=total_questions,
                                        feature_type='passages')
trained_question_cache = get_questions(discovery=discovery,
                                       constants=constants,
                                       question_count=total_questions,
                                       feature_type='trained')


@app.route('/')
@limiter.exempt
def index():
    return render_template('index.html')


@app.route('/api/query/<collection_type>', methods=['POST'])
def query(collection_type):