How to get the predicted intent with confidence using agent rasa sdk chatbot

Solution for How to get the predicted intent with confidence using agent rasa sdk chatbot
is Given Below:

I am loading the trained rasa models manually by using this

agent = Agent.load(
                model,
                action_endpoint=EndpointConfig(ACTION_ENDPOINT)
            )

And i am predicting the result like this

botResponse = await agent.handle_text(query)

but this just returns the response as text, but i need the confidence and intent name as well

I tried the handle_message but still it does not give confidence.

you can retrieve this information from the tracker_store instance of the Agent. To do so, firstly make sure that you pass a sender id while calling agent.handle_text(query, sender_id="some sender id"). Then retrieve the tracker with:

current_tracker = agent.get_or_create_tracker(sender_id="some sender id")

Once you have the tracker, you can retrieve the NLU parsed data of the last sent message with :

user_event = tracker.get_last_event_for(UserUttered)
if user_event:
    nlu_parse_data = user_event.parse_data

nlu_parse_data should look something like this:

"text": "Hi MoodBot.",
        "parse_data": {
          "intent": {
            "id": 3068390702409455462,
            "name": "greet",
            "confidence": 0.9968197345733643
          },
          "entities": [],
          "text": "Hi MoodBot.",
          "message_id": "47efa155fc234abea554242883f0a74e",
          "metadata": {},
          "intent_ranking": [
            {
              "id": 3068390702409455462,
              "name": "greet",
              "confidence": 0.9968197345733643
            },
            {
              "id": -7997748339392136471,
              "name": "bot_challenge",
              "confidence": 0.0019184695556759834
            },
            {
              "id": -3856210704443307570,
              "name": "mood_unhappy",
              "confidence": 0.0010514792520552874
            },

I used two different apis of rasa to achieve the same

To get the intent and confidence of the query parse_message_using_nlu_interpreter and to get the response handle_text

queryResponseList = await agent.handle_text(query)
intentInfo = await agent.parse_message_using_nlu_interpreter(query)
intent = Intent(**{
            "name": intentInfo["intent"]["name"],
            "confidence": intentInfo["intent"]["confidence"]
        })