Exemple #1
0
def ask_faq(model_id: int, request: Query):
    finder = FINDERS.get(model_id, None)
    if not finder:
        raise HTTPException(
            status_code=404, detail=f"Couldn't get Finder with ID {model_id}. Available IDs: {list(FINDERS.keys())}"
        )

    results = []
    for question in request.questions:
        if request.filters:
            # put filter values into a list and remove filters with null value
            request.filters = {key: [value] for key, value in request.filters.items() if value is not None}
            logger.info(f" [{datetime.now()}] Request: {request}")

        # temporary routing of requests by language
        result = finder.get_answers_via_similar_questions(
            question=question, top_k_retriever=request.top_k_retriever, filters=request.filters,
        )
        results.append(result)

        elasticapm.set_custom_context({"results": results})
        logger.info({"request": request.json(), "results": results})

        # remember questions with result in the autocomplete
        if len(results) > 0:
            addQuestionToAutocomplete(question)

        return {"results": results}
Exemple #2
0
def ask(model_id: int, request: Query):
    t1 = time.time()
    finder = FINDERS.get(model_id, None)
    if not finder:
        raise HTTPException(
            status_code=404,
            detail=
            f"Couldn't get Finder with ID {model_id}. Available IDs: {list(FINDERS.keys())}"
        )

    results = []
    for question in request.questions:
        if request.filters:
            # put filter values into a list and remove filters with null value
            request.filters = {
                key: [value]
                for key, value in request.filters.items() if value is not None
            }
            logger.info(f" [{datetime.now()}] Request: {request}")

        result = finder.get_answers(
            question=question,
            top_k_retriever=request.top_k_retriever,
            top_k_reader=request.top_k_reader,
            filters=request.filters,
        )
        results.append(result)

        resp_time = round(time.time() - t1, 2)
        logger.info({
            "time": resp_time,
            "request": request.json(),
            "results": results
        })

        # remember questions with result in the autocomplete
        if len(results) > 0:
            addQuestionToAutocomplete(question)

        return {"results": results}