Integrate custom machine learning models to your Bot using ML.NET

Do you want to recommend your latest products or services based upon your customer’s choice of shopping? Do you want to notify your customers with an estimated time of order delivery with respect to their location? Do you want to classify the complaints automatically as they’re reported?

All of above is now possible by just an integration of your customized machine learning models with your bots. This integration brings an achievement of business goals at a higher success rate.

Let’s assume you’re an owner of AMA Auto Services where you provide multiple automobile services to your customers. One of them is to evaluate the car whether it’s acceptable, good or unacceptable for trading purpose.

In a traditional way, when a customer calls on your help-line, one of your staff either invites him/her in-person to your workshop or assists on call. Either way, he/she has to evaluate the car by considering several parameters such as buying price, maintenance cost, doors, safety and so on.

Traditionally, it is time and cost consuming therefore, you, being a smart chap, thought to automate this task using Machine Learning. By this technique, you would be able to get the predicted value of car evaluation in faster with no additional cost, in fact you will reduce the cost by empowering your staff to do something more productive.

In order to achieve your goal, you will use ML.NET framework to build your model. Since this blog post talks about the integration of your ML models with bot, therefore, I will not be going in too much of detail for ML.NET as you can find one of the best explanations of this framework, here. However, I will talk in detail about the integration part after the creation of model till its consumption in our bot.

The approach I have chosen for the demo (or blog post) may not be ideal but the reason of my choice was just to show you that once the model is created, it can be exported anywhere and interestingly, with the ML.NET 0.3, you can actually do wonders with ONNX support.

So, after you’re done with creation of the model, you can navigate to the bin folder of project where you will see your model packed in as .zip file. Copy that model to your botframework SDK 4 project, add the nuget package of Microsoft.ML to your project along with the Car class you wrote for your model (you can refer to the code at github repository for the reference). In the same class file, you can write down the method to predict the results.

                // Load model 
                string appPath = Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);
                string modelPath = Path.Combine(appPath, "CarEvaluator.zip");

                // Read model
                var model = PredictionModel.ReadAsync<CarEval, CarEvalPredictor>(modelPath).Result;

                // Predict car value
                var prediction = model.Predict(carEval);

                // Return to the caller
                return ValueMapper(prediction.Result);

Then, write down your logic of conversational flow just like you want to ask customers about their cars. For the sake of simplicity, you can create a guided flow for the ease of customers using Dialogs Set. Upon each prompt’s response, your bot will save the information provided by customer to the conversational state.

By the last prompt’s response, you will have all the necessary parameters saved in your memory which are required to predict the value of car. Just pass those details to the method and it will predict the desired results which you can send to your customer as a result.

We have seen how easy it is to incorporate these models into our bot. I know I have not covered a lot of details from ML side. However, if you have any specific queries or you want me to write a detailed tutorial on ML.NET please let me know by either commenting or contacting me on twitter and I will definitely go for it.

If being a part of this learning paradigm excites you then you can always contribute to this repository.

Happy coding!

Related Posts

Leave a comment