# Invoke Chatgpt in action\_default\_fallback

**URL:** <https://forum.rasa.com/t/invoke-chatgpt-in-action-default-fallback/58009>\
**Category:** Rasa Open Source\
**Created:** [April 17, 2023, 2:38am UTC](https://forum.rasa.com/t/invoke-chatgpt-in-action-default-fallback/58009 "2023-04-17T02:38:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![karan](https://avatars.discourse-cdn.com/v4/letter/k/58f4c7/32.png) [@karan](https://forum.rasa.com/u/karan)\
**Post date:** [April 17, 2023, 2:38am UTC](https://forum.rasa.com/t/invoke-chatgpt-in-action-default-fallback/58009/1 "2023-04-17T02:38:52Z")

</div>

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

```auto
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

```auto
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."

```

---

<div class="post-metadata">

**Author:** ![stephens](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/stephens/32/1632_2.png) [@stephens](https://forum.rasa.com/u/stephens)\
**Post date:** [April 18, 2023, 3:25am UTC](https://forum.rasa.com/t/invoke-chatgpt-in-action-default-fallback/58009/2 "2023-04-18T03:25:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![karan](https://avatars.discourse-cdn.com/v4/letter/k/58f4c7/32.png) [@karan](https://forum.rasa.com/u/karan)\
**Post date:** [April 29, 2023, 2:18am UTC](https://forum.rasa.com/t/invoke-chatgpt-in-action-default-fallback/58009/3 "2023-04-29T02:18:35Z")

</div>

Thank you for your reply @stephens …

My mistake was to use this:

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

```

instead of using this:

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

---

<div class="post-metadata">

**Author:** ![akshayagrawal927](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/akshayagrawal927/32/20034_2.png) [@akshayagrawal927](https://forum.rasa.com/u/akshayagrawal927)\
**Post date:** [August 2, 2023, 8:30am UTC](https://forum.rasa.com/t/invoke-chatgpt-in-action-default-fallback/58009/4 "2023-08-02T08:30:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anoopshrma](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/anoopshrma/32/13349_2.png) [@anoopshrma](https://forum.rasa.com/u/anoopshrma)\
**Post date:** [August 2, 2023, 10:25am UTC](https://forum.rasa.com/t/invoke-chatgpt-in-action-default-fallback/58009/5 "2023-08-02T10:25:30Z")

</div>

Hi @akshayagrawal927,

If you are still facing any issue, You can check this blog: [How to use ChatGPT with Rasa - Python Warriors](https://pythonwarriors.com/how-to-use-chatgpt-with-rasa/) It shows how you can use ChatGPT in case of ` nlu_fallback`

---

<div class="post-metadata">

**Author:** ![akshayagrawal927](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/akshayagrawal927/32/20034_2.png) [@akshayagrawal927](https://forum.rasa.com/u/akshayagrawal927)\
**Post date:** [August 3, 2023, 7:08am UTC](https://forum.rasa.com/t/invoke-chatgpt-in-action-default-fallback/58009/6 "2023-08-03T07:08:25Z")

</div>

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.
