Mycroft Skill Creation Lifecycle Using MSK

by | Jun 6, 2018 | Mycroft

Introduction

In today’s post we will be going over the Mycroft AI skill lifecycle and show you how to use the new Mycroft Skills Kit to create a new skill, create tests for it, and also what you need to know in order to get your skill submitted to the new branch on the official skills respository.

So let me introduce myself, I am known as btotharye in the Mycroft AI community.  I would like to think I’m a pretty active contributor to the community and I try to stay ahead of the curve on the new things coming out in Mycroft.  Recently I updated my Home Assistant skill for Mycroft AI located Here.

The below article will go over the process of creating a new skill layout and we will in our case be creating a skill that will give you answers to Star Wars questions via the https://swapi.co/

Creating A Skill

So in our first part we are going to create our first skill.  We will be using the Mycroft Skills Kit to create our skill and fill everything out.  In the GIF below here you can see the overall process which we are going to follow and outline below.

So the first step is to install MSK via pip which is currently only tested on Linux atm:

pip install msk – This will install the Mycroft Skills Kit, when I did it I also had to do pip install future because it said I was missing it.

Now we have the kit installed and can run the below command to create our skill:

msk create – This will start giving us the prompts to fill out to make our skill.

Here is the sample output from the Star Wars Answer skill I’m making as a part of this post:

Enter a short unique skill name (ie. “siren alarm” or “pizza orderer”): star wars answers

Class name: StarWarsAnswersSkill
Repo name: star-wars-answers-skill

Looks good? (Y/n) y
Enter a one line description for your skill (ie. Orders fresh pizzas from the store): Answers questions about Star Wars franchise
Enter a long description:
> Uses the https://swapi.co/ to answer questions about Star Wars
>
Enter some example phrases to trigger your skill:
– when was luke skywalker born
– what color is luke skywalkers hair
– how tall was luke skywalker

Enter author: btotharye
Would you like to create a GitHub repo for it? (Y/n) y
=== GitHub Credentials ===
Username: btotharye
Password:
Counting objects: 12, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (12/12), 1.37 KiB | 701.00 KiB/s, done.
Total 12 (delta 0), reused 0 (delta 0)
To https://github.com/btotharye/star-wars-answers-skill
* [new branch] master -> master
Branch ‘master’ set up to track remote branch ‘master’ from ‘origin’.
Created GitHub repo: https://github.com/btotharye/star-wars-answers-skill
Created skill at: /opt/mycroft/skills/star-wars-answers-skill

So you can see it asks us for a name of the skill, then a description along with some phrases to trigger.  We then tell it our name and if we want to use github we give it the credentials and it makes the repo for us as well.  So now you should have a repo similiar to mine located at https://github.com/btotharye/star-wars-answers-skill

Connecting Skill to api

So now that we have our source code and our skill setup we can open up the __init__.py file and can modify it to our liking.  For more in depth information on creating a Mycroft skill please see https://mycroft.ai/documentation/skills/introduction-developing-skills/

 

Now if we have a instance of mycroft running where we just ran MSK it seems to auto load the skill for us from my testing so we can test it.  In my case its not going to give us any answers yet because we haven’t told it to.  We have to modify the code for that.

MSK will ask for your phrases to trigger the skill as well as the response.  You can use the {{variable}} in your response and it will build the variable for you and make it empty and setup the dialog files and code as well, very slick stuff.

 

So now our skill has been created in the /opt/mycroft/skills folder and we can interact with it via the Mycroft CLI.  In my case I’m running this on source so I will activate my venv and then startup the cli:

 

source mycroft-core/.venv/bin/activate – This is ran against the git folder and it should have a .venv folder if Mycroft is installed, this is how we activate it.

 

./start-mycroft.sh cli – This is ran from the mycroft-core folder and this is how we start up the cli.

 

We should see something like this if done correctly and you can see where I asked it Luke Skywalker’s height:

So now we have a working skill.  You can look at the source code located at https://github.com/btotharye/star-wars-answers-skill to see how I made the connection to the API and converted the centimeters I received into feet and then responded with that value.

Why Do We Test

We create tests for our code to save ourselves a lot of time and frustration later.  You want to build tests for code while you write your code if possible this way you have something that can verify its going to work the way it is expected to, thus preventing bugs in the future.  http://docs.python-guide.org/en/latest/writing/tests/ has some great reasons why and was also great for me when I was learning testing.

Thanks to Ake from the Mycroft Team we now have a test runner that we can use to test our utterances and the overall skill to ensure it will work correctly.  We can actually use MSK to create our test for us and we will show that in the next section.

Creating Our First Test

So we can use the MSK program to create our tests as well.  Also for a more in depth guide on tests and mocking them for Mycroft please see https://github.com/MycroftAI/mycroft-skills/wiki/Writing-intent-tests-for-skills

Using `msk create-test /opt/mycroft/skills/star-wars-answers-skill.btotharye` I’m able to step through the menu creating my test for the skill.  Below is the example output.

Which intent would you like to test?
1. luke.height.intent

> 1

=== Intent Examples ===
How tall is luke skywalker

Enter an example query: how tall is luke skywalker

Choose expected dialog (leave empty to skip).
1. luke.height

> 1
Generated test file: /opt/mycroft/skills/star-wars-answers-skill.btotharye/test/intent/luke.height.intent.0.intent.json

So now I have my test file created and we can verify if it works correctly.

One thing to note I found a bug that when it creates the test.json file for you it leaves off the .intent part in the intent_type section, so please modify the test/intent/*.intent.json file and make sure your intent_type section has .intent at the end of it.

Verifying Our Test

To verify our test is pretty easy.  We just want to activate the virtual env and run the below commands to kick off the test runner and let it verify there aren’t any issues with our skill or the others.

source mycroft-core/.venv/bin/activate – This is ran against the git folder and it should have a .venv folder if Mycroft is installed, this is how we activate it.

cp test/integrationtests/skills/skill_developers_testrunner.py /opt/mycroft/skills/star-wars-answers-skill.btotharye/ – This will copy the test runner file to our local skill so we can run just the test against our skill to make sure it works ok.  You cant ignore this file in git since we don’t want to add this to version control.

python test/integrationtests/skills/single_test.py /opt/mycroft/skills/star-wars-answers-skill.btotharye/ – This is an alternative to copying the test runner over and we can use this to run our single test.

python /opt/mycroft/skills/star-wars-answers-skill.btotharye/skill_developers_testrunner.py – This will run our test fill and it should show something similiar to below stating Ok

07:41:04.716 – star-wars-answers-skill_btotharye – DEBUG – Feet is: 6
07:41:04.716 – star-wars-answers-skill_btotharye – DEBUG – Here is the conversion response: 6
07:41:04.716 – star-wars-answers-skill_btotharye – DEBUG – <Response [200]>
07:41:04.723 – mycroft.skills.intent_service:send_metrics:247 – DEBUG – Sending metric if opt_in is enabled

Evaluating message: {‘utterance’: ‘how tall is luke skywalker’, ‘lang’: ‘en-us’, ‘__type__’: ‘intent_failure’}

Evaluating message: {‘handler’: ‘fallback’, ‘__type__’: ‘mycroft.skill.handler.start’}

Evaluating message: {‘utterance’: ‘how tall is luke skywalker’, ‘__type__’: ‘luke.height.intent’}
.
———————————————————————-
Ran 1 test in 2.798s

OK

Now we have verified our skill has successfully passed tests and can be submitted to the skills repository.

Running Tests in Pycharm

So in order to run our testing in Pycharm it is actually relatively easy.  You basically want to setup the Environment section of the project to use our mycroft-core virtualenv.

In pycharm while you have the project open go to Run then select Edit Configurations.  From there you will want to select the Envrionment section and ensure that the Project’s Python interpreter is the one inside our mycroft-core .venv like my image below.  Once we have this we can now inside Pycharm right click on the skill_developers_testrunner.py file and select run Unittests and it will now run our Mycroft tests.

More information on how to add this interpreter to Pycharm can be found at https://www.jetbrains.com/help/pycharm-edu/configuring-python-interpreter-for-a-project.html

Submitting Your skill

There are 2 Mycroft documentation resources for this process you are going to want to familiarize yourself with.  The first is the actual https://mycroft.ai/documentation/skills/skill-submission/ submission process and what you have to do with git submodules to add it to the repository.

The other is https://mycroft.ai/documentation/skills/skills-acceptance-process/ which is a much larger document that outlines the policies and procedures that the skill team uses for approving skills.  It seems like a new process now is that for skills that require special hardware, or other requirements there is a need for another community member to vouch for the skill.