I Created an Alexa Skill to Settle an Argument With My Wife
By: Logan Yang
Tags: Side Project, Artificial Intelligence, Conversational Ai, Alexa, Programming

Then it comes the fun part:

Design what the skill should do if it’s a standalone service Find out how to implement it with Alexa

In my design, the skill should be able to give an “outdoor score” in the range of 0–100 for a city within a time frame. The score should reflect how comfortable it was, so I came up with a formula that takes temperature, humidity, cloudiness, precipitation into account. There’s a range for perfect temperature and humidity. Once it’s out of the range, the formula gives a linear penalty depending on how far it deviates. The percentage of cloudiness and precipitation also plays a big role. A place such as San Diego should consistently score very high.

The skill should also be able to summarize the weather using these properties so that the users get an idea of the overall picture and the reason why a certain score was given.

There’s some nitty-gritty in timezone handling and data munging, but the logic isn’t too complicated to implement. Before I built the actual skill, I wrote the logic and tests in Python, then used FastAPI to make it a service and deployed it on Heroku so that perhaps the Alexa skill could directly call it later.

How to Build a Custom Alexa Skill

This part is where the learning is. I had no prior experience developing Alexa skills, so I searched for tutorials and followed their documentation. The most helpful example was the City Guide Skill Sample. You can follow its step-by-step setup instruction to build your own skill.

In a nutshell, one can register as an Alexa developer, create a skill in the dashboard, and build up each component of the skill all in their web UI. The components include

The skill’s invocation name, as in “Alexa, ask <skill_name> to …”

An interaction model that has intents, slots, and utterances

Optional multi-modal responses, such as audio and video.

An endpoint that serves the requests. It can be an Alexa-hosted Lambda function (recommended) or a third-party service

During the process, I found out that using a third-party web service as the backend is quite a hassle. Going serverless and using the recommended Alexa-hosted Lambda function is easier and more secure. It also handles scaling if it ever gets too many requests. Since I wrote my logic as an independent module in the FastAPI case, I can easily put it here in the Lambda function. The only hoop I had to jump through was the IAM role access so that this Alexa hosted Lambda can query my DynamoDB.

Once I got familiar with the Alexa Developer Console, I was able to create the Interaction Model right there with a few clicks.

If you know some natural language processing, this interaction model is basically named-entity recognition (NER) and intent classification. NER is for finding slots. They are the variables you can extract from an utterance (the sentence that the user says to Alexa). For example, if you say “Alexa, what’s the weather in {New York}?” New York is a {city} variable that you can extract and use in your code. If you provide enough utterances with labeled slots, the NER model can generalize and extract similar words or phrases as the slot from utterances it has never seen before.

Intent classification is for identifying the user’s intent. In the case of Weather Score, I defined 3 intents at the moment: score, summary, and detailed summary. If the user asks “what’s the score for Seattle last week”, the intent should be classified as score, and it should trigger the score intent handler in my Lambda function which then queries and calculates the score for the city and timePeriod slots mentioned in the utterance. If the user says “give me a detailed summary of Honolulu last month”, it should trigger the detailed summary intent handler and output an hourly summary I defined.

The training of these models is completely encapsulated into a few clicks. This is quite nice. Even if the developer doesn’t know machine learning at all, its benefits are tangible with just a few lines of code.

VS Code Extension: Alexa Skills Toolkit

I like VS Code a lot, one reason being its powerful extensions. This Alexa Skills Toolkit extension makes me like VS Code even more. I can test and deploy the skill without leaving my editor! If you would like to learn how to set it up, just follow this guide here.

Screenshot of my VS Code Extension: Alexa Skills Kit

Local Debugging

One thing I particularly like is being able to step through my code line by line with breakpoints. It requires some extra steps to set up for an Alexa app, but the result was worth it. I just type into the test dialog box powered by the Alexa extension, and the execution stops at breakpoints I set for easy debugging. If you are interested in setting this up for local development, I have written a separate article for the details.

Deploy and Submit to Alexa Skill Store

Once my code was ready, I could deploy and promote it to live by clicking some buttons and filling in a form in the Alexa Developer Console. In the form, I described what this skill did and gave some sample utterances. After that, the console runs a validation and tells you what needs to be updated. With everything validated, you can submit your skill for review. It usually takes less than 24 hours. My first review took only ~2 hours.

Here are some sample utterances and responses.

Utterance: Alexa, ask weather score the weather in Seattle last week Response: For the last 7 days, Seattle had a high of 52 degrees, low of 37 degrees, average temperature 46 degrees. 7 out of the 7 days had precipitation. There were a total 58 hours of precipitation.

This is the “summary” intent. It gives the highest and the lowest temperature at hourly granularity during the time period asked, along with the number of days/hours with precipitation. And wow, Seattle really does have a lot of rain!

Utterance: Alexa, ask weather score the score of New York yesterday Response: The outdoor score for new york is 62 out of 100 yesterday. There was precipitation.

This is the “score” intent. The response is an outdoor score calculated based on my formula and a brief mention of whether or not it had precipitation.

Utterance: Alexa, ask weather score for a detailed summary of Honolulu for last month Response: Here is a detailed hourly summary for Honolulu for the last 30 days: the temperature was comfortable most of the time, 68% of the time was humid, 21% of the time was cloudy, 3% of the time had precipitation.

This is the “detailed summary” intent. It summarizes the temperature, humidity, cloudiness, and precipitation over all the hours. It’s a more direct look under the hood for the outdoor score calculation.

To try it out yourself, go to the skill page to enable it if you have Alexa at home. Or you can use the Alexa app on your phone, navigate to the skill store, search for Weather Score, and enable it there. If you find it useful, please give it a 5-star! Any suggestions or ideas are appreciated.

Limitations and Future Work

The most obvious limitation is the range of time and space it can query for now using the free data source.

At this time, the supported time periods are yesterday (default if there’s no time period provided), last week, and last month.

The supported cities include Los Angeles, New York, Chicago, Dallas, San Jose, Denver, Honolulu, San Francisco, Seattle, San Diego, Beijing, Hong Kong, Tokyo, Singapore, Shanghai. This is just the initial list and can be expanded if there’s demand.

With more data, I’m thinking of several potential features to add

Query a specific month, e.g.

“What’s the weather like in {August} in New York?”

Compare two cities over a time period, e.g.

“Compare the weather in San Francisco and Seattle last month”

The response can include the outdoor scores for both cities, a comparison of the percentage of cloudiness and precipitation, and more.

If you have any suggestions, please let me know!

The End

Weather Score is a fun project for me to learn how to build an Alexa skill, and it serves a small niche that I personally wanted. The gist of this article is to let you know that it is quite easy to develop an Alexa skill. If you would like to build one yourself, a good place to start is to think of something you are too lazy to search online but repeatedly need. Find an API that has the data you are interested in, write a Lambda function, click some buttons in the Alexa Developer Console, and that’s it!

After building this project, I realized how cumbersome it is to say “Alex ask <skill_name> to …” every time I need to invoke a skill. In my opinion, for a more natural conversational experience, the user shouldn’t have to explicitly say the skill name every time. Since the average user won’t have too many skills enabled, one idea is for Alexa to add a layer that infers the skill the user wants based on the list of enabled skills they have. Though it may introduce conflicting intents, it is not impossible to solve. If anybody who works at Alexa reads this, let me know if it makes sense.

Thanks for reading!