Ejemplo n.º 1
0
    def on_response(self, response: ResponseDelegate, db_row: DatabaseRow):

        if response.request.rerank_cids:
            db_row.server_mrr = calculate_mrr(
                correct=response.request.rerank_cids.list,
                guesses=response.cids
            )

        start_time = time.perf_counter()

        ranks = self.rank(
            query=response.request.query,
            choices=response.cvalues,
            filter_results=response.request.filter_results
        )
        db_row.rerank_time = time.perf_counter() - start_time

        reranked_choices = [response.choices[rank] for rank in ranks]
        response.choices = reranked_choices

        if response.request.rerank_cids:
            db_row.model_mrr = calculate_mrr(
                correct=response.request.rerank_cids.list,
                guesses=response.cids
            )

        response.choices = response.choices[:db_row.topk]
Ejemplo n.º 2
0
    def on_response(self, response: ResponseDelegate, db_row: DatabaseRow):

        if response.request.rerank_cids:
            db_row.server_mrr = calculate_mrr(
                correct=response.request.rerank_cids.list,
                guesses=response.cids
            )

        start_time = time.perf_counter()

        ranks, scores = self.rank(
            query=response.request.query,
            choices=response.cvalues,
            filter_results=response.request.filter_results
        )
        db_row.rerank_time = time.perf_counter() - start_time

        # raise helpful error if choices is shorter than ranks
        reranked_choices = [response.choices[rank] for rank in ranks]

        response.choices = reranked_choices
        response.set_path('body.nboost.scores', list([float(score) for score in scores]))

        if response.request.rerank_cids:
            db_row.model_mrr = calculate_mrr(
                correct=response.request.rerank_cids.list,
                guesses=response.cids
            )

        response.choices = response.choices[:db_row.topk]
Ejemplo n.º 3
0
    def on_response(self, response: ResponseDelegate, db_row: DatabaseRow):
        query = response.request.query
        choices = response.cvalues

        corpus = [self.tokenize(choice) for choice in choices]
        bm25 = BM25Okapi(corpus)
        ranks = np.argsort(bm25.get_scores(self.tokenize(query)))[::-1]

        reranked_choices = [response.choices[rank] for rank in ranks]
        response.choices = reranked_choices

        response.choices = response.choices[:50]
Ejemplo n.º 4
0
        def proxy_through(path):
            # parse the client request
            dict_request = flask_request_to_dict_request(
                flask_request)  # takes the json
            """Search request."""
            db_row = db.new_row()

            # combine command line args and runtime args sent by request
            query_args = {}
            for key in list(dict_request['url']['query']):
                if key in defaults.__dict__:
                    query_args[key] = dict_request['url']['query'].pop(key)
            json_args = dict_request['body'].pop('nboost', {})
            args = {**cli_args, **json_args, **query_args}

            request = RequestDelegate(dict_request, **args)
            request.dict['headers'].pop('Host', '')
            request.set_path('url.headers.host',
                             '%s:%s' % (request.uhost, request.uport))
            request.set_path('url.netloc',
                             '%s:%s' % (request.uhost, request.uport))
            request.set_path('url.scheme', 'https' if request.ussl else 'http')

            for plugin in plugins:  # type: Plugin
                plugin.on_request(request, db_row)

            # get response from upstream server
            start_time = perf_counter()
            requests_response = dict_request_to_requests_response(dict_request)
            db_row.response_time = perf_counter() - start_time
            try:
                dict_response = requests_response_to_dict_response(
                    requests_response)
            except JSONDecodeError:
                print(requests_response.content)
                return requests_response.content
            response = ResponseDelegate(dict_response, request)
            response.set_path('body.nboost', {})
            db_row.choices = len(response.choices)

            for plugin in plugins:  # type: Plugin
                plugin.on_response(response, db_row)

            # save stats to sql lite
            db.insert(db_row)

            return dict_response_to_flask_response(dict_response)
Ejemplo n.º 5
0
        def search(**_) -> FlaskResponse:
            """Search request."""
            db_row = db.new_row()

            # parse the client request
            dict_request = flask_request_to_dict_request(flask_request)

            # combine command line args and runtime args sent by request
            query_args = {}
            for key in list(dict_request['url']['query']):
                if key in defaults.__dict__:
                    query_args[key] = dict_request['url']['query'].pop(key)
            json_args = dict_request['body'].pop('nboost', {})
            args = {**cli_args, **json_args, **query_args}

            request = RequestDelegate(dict_request, **args)
            request.dict['headers'].pop('Host', '')
            request.set_path('url.headers.host',
                             '%s:%s' % (request.uhost, request.uport))
            request.set_path('url.netloc',
                             '%s:%s' % (request.uhost, request.uport))

            for plugin in plugins:  # type: Plugin
                plugin.on_request(request, db_row)

            # get response from upstream server
            start_time = perf_counter()
            requests_response = dict_request_to_requests_response(dict_request)
            db_row.response_time = perf_counter() - start_time
            dict_response = requests_response_to_dict_response(
                requests_response)
            response = ResponseDelegate(dict_response, request)
            response.set_path('body.nboost', {})
            db_row.choices = len(response.choices)

            for plugin in plugins:  # type: Plugin
                plugin.on_response(response, db_row)

            # save stats to sql lite
            db.insert(db_row)

            return dict_response_to_flask_response(dict_response)
Ejemplo n.º 6
0
    def on_response(self, response: ResponseDelegate, db_row: DatabaseRow):
        if response.cvalues:
            start_time = time.perf_counter()
            responses = []
            for idx, cvalue in enumerate(response.cvalues):
                answer, start_pos, stop_pos, score = self.get_answer(
                    response.request.query, cvalue)

                self.logger.info(
                    f"{response.request.qa_threshold} \t {answer}, {start_pos}, {stop_pos}, {score}"
                )

                responses.append({
                    'answer_text': answer,
                    'answer_start_pos': start_pos,
                    'answer_stop_pos': stop_pos,
                    'answer_score': score,
                })

            db_row.qa_time = time.perf_counter() - start_time

            response.set_path(f'body.nboost.qa', responses)
Ejemplo n.º 7
0
    def on_response(self, response: ResponseDelegate, db_row: DatabaseRow):
        if response.cvalues:
            start_time = time.perf_counter()

            answer, start_pos, stop_pos, score = self.get_answer(
                query=response.request.query, cvalue=response.cvalues[0])

            db_row.qa_time = time.perf_counter() - start_time

            if score > response.request.qa_threshold:
                response.set_path('json.nboost.answer_text', answer)
                response.set_path('json.nboost.answer_start_pos', start_pos)
                response.set_path('json.nboost.answer_stop_pos', stop_pos)
Ejemplo n.º 8
0
 def on_response(self, response: ResponseDelegate, db_row: DatabaseRow):
     response.set_path('body.nboost.topk', db_row.topk)
     response.set_path('body.nboost.topn', response.request.topn)
     response.set_path('body.nboost.query', response.request.query)
     response.set_path('body.nboost.choices', response.choices)
     response.set_path('body.nboost.cids', response.cids)
     response.set_path('body.nboost.cvalues', response.cvalues)