How to debugg (policies, default actions, classifiers,...) in VS Code

Hey,

I always missed a way in Rasa to debug the code with a debugger form for example VS Code or other IDEs. There are some tutorials how to start rasa from the VS Code terminal but the most of them install Rasa in the terminal and initiate their Rasa project there. Because I changed something in the policies and default actions in my project, I was not able to do this because I was afraid of deleting my progress with this. I also never found an answer or blog post in the way I stetted up Rasa for debugging. So here is my solution.

  1. Write program which starts Rasa in it. (It is important that Rasa gets started in the program. Previously I just wrote a program that writes the Rasa commands to start Rasa in the terminal. But then the debugging ends in the moment Rasa starts.)
    Create this file in the folder where you have all the files to customize your bot. (This is the folder where also your config.yml, credentials.yml, domain.yml, … is in)

    import os  
    import sys
    import time
    
    
    os.chdir('<Path to this file>')
    os.system("conda activate")
    time.sleep(1)
    
    #do not do os.system("rasa run-...") the debugger will not have access to the program. Do this with sys.argv.append...
    #os.system("rasa run -m models --enable-api --cors ‘*’ --debug  --endpoints endpoints.yml")
    #
    # The following is like using the command:
    #  $ rasa run -m models --enable-api --debug
    
    sys.argv.append('run')
    sys.argv.append('-m models')
    sys.argv.append('--enable-api')
    #sys.argv.append('--cors ‘*’') #I don't know why this is not working. If somebody know who to make it work contact me
    sys.argv.append('--debug')
    #sys.argv.append('--endpoints endpoints.yml') #I don't know why this is not working. If somebody know who to make it work contact me
    
    from rasa.__main__ import main
    main()
    
  2. Add in the 6th line the path where to start your bot. (This is again the folder where also your config.yml, credentials.yml, domain.yml,… is in).

  3. Klick on VS Code on the debug symbol or press Ctrl+Shift+D. If you have not configured running and debugging klick on “create a launch.json file”. Copy into the launch.json the settings I made in my launch.json file.

    {
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
     {
         "name": "Python: Current File",
         "type": "python",
         "request": "launch",
         "program": "<Path to this file>/my_rasa_run.py",
         "console": "integratedTerminal",
         "justMyCode": false,
         "stopOnEntry": true,
     }
    ]
    }
    
  4. Now add wherever you need the breakpoints (the points where you want the program to stopp to watch the variables).

  5. Klick in VS Code in the top bar on “Run” and then on “Start Debugging” or press F5.

VS Code sometimes changes in the updates the way you configure your debugger. So if the launch.json file causes trouble or if you didn’t understand something check out the VS Code page for python debugging. Debugging configurations for Python apps in Visual Studio Code

I also uploaded the file to run Rasa and my launch.json to GitHub.

I hope this helps.