Search code examples
pythonsdkbotframeworkazure-bot-serviceazure-qna-maker

How can I get top 5 answers from QnA maker using Bot Framewoek SDK python


Basically, I have this code from the official documentation. What I'm trying to achieve is to get the list of question-answer pairs that are the closest to the user's input.

async def on_message_activity(self, turn_context: TurnContext):
    # The actual call to the QnA Maker service.
    response = await self.qna_maker.get_answers(turn_context)
    if response:
        await turn_context.send_activity(MessageFactory.text(response[0]))
    else:
        await turn_context.send_activity("No QnA Maker answers were found.")

Solution

  • I found a solution after 3 hours. You just need to add QnAMakerOptions(top=10) in the __init__ method.

        def __init__(self, config: DefaultConfig):
            self.qna_maker = QnAMaker(
                QnAMakerEndpoint(
                    knowledge_base_id=config.QNA_KNOWLEDGEBASE_ID,
                    endpoint_key=config.QNA_ENDPOINT_KEY,
                    host=config.QNA_ENDPOINT_HOST,
                ), QnAMakerOptions(
                    top=10
                )
            )