UNPKG

snips-sam

Version:

The Snips Assistant Manager

133 lines (89 loc) 5.5 kB
--- layout: article title: "Skills 101: Building a simple calculator" subtitle: A tutorial on building a skill performing basic arithmetic operations using the Snipsfile. permalink: /tutorials/calculator/ toc: Prerequisites|Preparing your device|Creating the assistant|Creating the skill|Deploying to your device --- # Prerequisites For this tutorial, we assume you have the following: - A Raspberry Pi Model 3 - A speaker (such as the JBL Go) - A microphone (see for instance our recommendations in the [Microphone Array Benchmark]({{ site.baseurl }}/articles/micarrays)) - A Snips account for access to the [Snips Console](https://console.snips.ai) # Preparing your device This tutorial is using the Snips Assistant Manager (`sam`) for setting up and deploying the assistant to your Pi. Make sure it is installed on your computer. If not, check out the <a href="{{ site.baseurl }}/getting-started/introduction/">Getting Started Guide</a>. Once installed, connect to your Pi: ```sh $ sam connect ``` <div class="notification light-notification"> Your Pi must be on the same network as your computer. For help on setting up a Pi on a Wi-Fi network, see our <a href="{{ site.baseurl }}/getting-started/introduction/">Raspberry Pi Network Connection</a> guide. </div> If you haven't done so already, install the Snips toolbelt on the Pi: ```sh $ sam init ``` This will take a little moment, which you may use to start the next step, and create an assistant in the Console. # Creating the assistant Head over to the Snips Console: <a class="button is-primary" href="https://console.snips.ai" target="_blank"> <span>Open Snips Console</span> <span class="icon"> <i class="fa fa-arrow-circle-right"></i> </span> </a> Create an account if you don't have one yet, and create a new assistant with your name of choice. For this tutorial, we will call it "Von Neumann": <img src="{{ site.baseurl }}/images/console-create-assistant.png" srcset="{{ site.baseurl }}/images/console-create-assistant@2x.png 2x"/> Add the Calculator bundle, which add intents that detect queries for sums, products, differences and quotients: <img src="{{ site.baseurl }}/images/console-add-bundle.png" srcset="{{ site.baseurl }}/images/console-add-bundle@2x.png 2x"/> If you want to add more intents, such as computing the square root of a number, you can do so by adding a new intent to the assistant. # Adding the skill Once the assistant has been created in the Console, you can create the skill that will bind intents with the actual action of performing the arithmetic operations and speak the answer. First, create a new project locally on your computer: ```sh $ sam create ``` This will generate a [Snipsfile]({{ site.baseurl }}/articles/snipsfile) in the current working directory. ## Pointing to your Console assistant Open the Snipsfile, find the line starting with `assistant_url`, and replace it with `assistant_id: YOUR_ASSISTANT_ID`, where `YOUR_ASSISTANT_ID` is the ID of your assistant: ```yaml assistant_id: YOUR_ASSISTANT_ID ``` The ID can be found in the URL of your assistant in the Console: <img src="{{ site.baseurl }}/images/console-assistant-id.png" srcset="{{ site.baseurl }}/images/console-assistant-id@2x.png 2x"/> ## Adding the handler code When your created the project above, Sam automatically added a sample skill under the `skills` section in the Snipsfile. Remove this section, and replace it with the following: ```yaml skills: - name: calculator intents: - intent: GetSum action: | {% raw %}{%{% endraw %} sum = int(intent.firstTerm) + int(intent.secondTerm) tts_service.speak(str(sum)) {% raw %}%}{% endraw %} - intent: GetProduct action: | {% raw %}{%{% endraw %} product = int(intent.firstTerm) * int(intent.secondTerm) tts_service.speak(str(product)) {% raw %}%}{% endraw %} ``` What is happening here? One skill, named `calculator`, has been added. It reacts to two intents, `GetSum` and `GetProduct`, which incidentally match those produced by your assistant. The `action` section contains a code block with some handler code. The code here is written in Python. It must be delimited by the `{% raw %}{%{% endraw %}` and `{% raw %}%}{% endraw %}` characters. In this code, two objects are accessible: `intent` and `tts_service`. The `intent` object gives you access to slot values: `firstTerm` and `secondTerm`. The `tts_service` can be used to speak a phrase, using the `speak()` method. We leave it to you to add the remaining `GetDifference` and `GetQuotion` intents. ## Testing the skill locally Before deploying the assistant to your Pi, it is wise to check that the skill works as expected. You can simulate intent messages using the `sam test` command. For instance, to test that the skill properly reacts to a `GetSum` intent, enter the following command: ```sh $ sam test skill calculator intent=GetSum firstTerm=3 secondTerm=5 ``` You should see the output `TTS says: 8`. # Deploying to your device Once you are satisfied that your skill is working, you can deploy to your device: ```sh $ sam deploy ``` After a short time, your assistant should be ready to take your voice commands. Try out: > Hey Snips, what is 10 times 15 You should hear the answer being spoken out! If you have any issues, check out our in-depth [Assistant Troubleshooting Guide]({{ site.baseurl }}/articles/troubleshooting-guide/).