Invoke Chatgpt in action_default_fallback

Hi! I am trying to get chatgpt to answer on behalf of my Rasa bot in the event of all fallback, but this code results in no recognised ‘action_default_fallback’. I have registered this action in domains.yml

Can anyone please help?

Config.yml

pipeline:
  - name: WhitespaceTokenizer
  - name: RegexFeaturizer
  - name: LexicalSyntacticFeaturizer
  - name: CountVectorsFeaturizer
    analyzer: char_wb
    min_ngram: 1
    max_ngram: 4
  - name: DIETClassifier
    epochs: 100
    constrain_similarities: true
  - name: EntitySynonymMapper
  - name: ResponseSelector
    epochs: 100
    constrain_similarities: true
  - name: FallbackClassifier
    threshold: 0.7
    ambiguity_threshold: 0.1

policies:
  - name: RulePolicy
    core_fallback_threshold: 0.4
    core_fallback_action_name: "action_default_fallback"
    enable_fallback_prediction: True

Actions.py

class ActionDefaultFallback(Action):
    def name(self) -> Text:
        return "action_default_fallback"

    def run(
        self,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any],
    ) -> List[Dict[Text, Any]]:
    
    # Get user message from Rasa tracker
        user_message = tracker.latest_message.get('text')

    # def get_chatgpt_response(self, message):
        url = 'https://api.openai.com/v1/chat/completions'
        headers = {
            'Authorization': 'Bearer sk-xxxxxxxxxxxxxxxxxxxxxxXD',
            'Content-Type': 'application/json'
        }
        data = {
            'model': "gpt-3.5-turbo",
            'messages': [{'role': 'user', 'content': 'You: ' + user_message}],
            'max_tokens': 100
        }
        response = requests.post(url, headers=headers, json=data)
                # response = requests.post(api_url, headers=headers, json=data)

        if response.status_code == 200:
            ai = response.json()['choices'][0]['message']['content']
            dispatcher.utter_message(ai)
        else:
            # Handle error
            return "Sorry, I couldn't generate a response at the moment. Please try again later."

Did you list action_default_fallback in the actions section of your default.yml?

Thank you for your reply @stephens

My mistake was to use this:

ai = response.json()['choices'][0]['message']['content']

instead of using this:

assistant_response = choices[0]["message"]["content"]

hi @karan did you make some changes in the rules.yml as well

Hi @akshayagrawal927,

If you are still facing any issue, You can check this blog: How to use ChatGPT with Rasa - Python Warriors It shows how you can use ChatGPT in case of nlu_fallback

Thanks for the help @anoopshrma .

But my bot is misclassifying sentences like "Who is the president of USA " with the intent inform. and also in some case, if it is able to classify it with nlu_fallback, it is not running the last action that was run before the user intent was classified as a fallback.