Build Sentiment Classifier, Translator and a Flight Tracker with the Generative AI
Tags: beginners, azure, machinelearning, ai
Published: 2023-07-24 00:07:49

Last week I received an email from the Microsoft Reactor team informing me that I had 360 RSVPs on the day of <a href="https://arafattehsin.com/thegem_news/generative-ai-for-developers/">my talk</a>. While I was super excited, I was a bit nervous because I wanted to ensure that I deliver the content responsibly. If you value your audience's time, ensure your content is engaging, informative and well-organized. During my talk, I answered around 25 questions therefore I thought that I shouldn't just deliver the session and upload my slide deck and demos. Instead, I should add some more value to it and make it more inclusive for most of the developers. As a result, I created not just one, but two Polyglot Notebooks (fully interoperable with Jupyter Notebooks) and decided to write about it.

At first, I considered developing simple console applications for the demos. Although these console apps would have sufficed, the getting started guide would have been somewhat cluttered, requiring multiple apps. Therefore, I decided to use a Polyglot Notebook, allowing us to consolidate everything into a single file.
<blockquote>The biggest benefit of Polyglot Notebooks is its interoperability with Jupyter Notebooks &amp; multi-language .NET support.</blockquote>
<h1>Introducing Generative AI Notebooks</h1>
As a long time .NET developer, I always try my best to bring tech like <a href="https://arafattehsin.com/reinforcement-learning-in-apps-bots-websites-with-azure-personalizer-part-2/">Reinforcement Learning</a>, <a href="https://arafattehsin.com/beyond-sentiment-analysis-object-detection-with-ml-net/">Computer Vision</a> and Generative AI to the developer community using the similar languages and frameworks.  Despite my personal interest in .NET, I also did not want to ignore the fact that many developers are also (and rightly so) using Python within this space. Therefore, I decided to create <a href="https://github.com/arafattehsin/generative-ai/tree/main/notebooks/azure%20openai">two notebooks</a>, one for <a href="https://github.com/arafattehsin/generative-ai/tree/main/notebooks/azure%20openai/dotnet">C# with .NET Interactive</a> and the other one for <a href="https://github.com/arafattehsin/generative-ai/tree/main/notebooks/azure%20openai/python">Python</a>. Both of the notebooks are identical in terms of the topics covered, so it depends on you which one you'd like to use.

<img class="aligncenter size-full wp-image-31758" src="https://arafattehsin.com/wp-content/uploads/2023/07/genai-notebook.png" alt="Generative AI Notebook" width="993" height="213" />
<div>
<div>I've created somewhat boilerplate code for a few use-cases, which is the topic of our blog post. Let's dive into each of them.</div>
&nbsp;
<h2>Sentiment Classifier</h2>
</div>
<h4><strong>Potential Use-cases: Customer Feedback Analysis, Client Satisfaction Index, Call Centre Real-time Insights</strong></h4>
Traditionally, machine learning models are always trained on a bigger dataset for better accuracy along with the right choice of algorithm with some hyperparameters. For example, if you want to build an engine that'd classify a sentiment out of a sentence, you may want to train it on at least 1000 records with a mixture of negative and positive sentiments. This way, the model may achieve as 70-80% of accuracy. I've done it in the past with my <a href="https://www.nuget.org/packages/SentimentAnalyzer/">SentimentAnalyzer</a> package which has already been used 24k times by developers. However, this was a bit of a task for me to train and then integrate it within my applications.

Large language models have changed this all. Now, with the magic of <a href="https://youtu.be/xVYAUwAOSQg?t=1709">few shot learning,</a> you can easily build a sentiment classifier. This can easily be done using the <a href="https://learn.microsoft.com/en-us/azure/cognitive-services/openai/how-to/completions?WT.mc_id=AI-MVP-5003464">Completion API with GPT-3</a>.

Below is the syntax on how I did it. You can try it using the notebook.

<a href="https://vscode.dev/github/arafattehsin/generative-ai/blob/main/notebooks/azure%20openai/dotnet/gen-ai-for-devs.ipynb#C11:L4"><img class="aligncenter size-full wp-image-31760" src="https://arafattehsin.com/wp-content/uploads/2023/07/sentiment-classifier.png" alt="Sentiment Classifier using Azure OpenAI Service" width="1112" height="600" /></a>

<code>Remember to initialise a .NET or Python kernel before you run the subsequent steps of the notebook(s)</code>
<h2>Summarisation and Translation</h2>
<h4><strong>Potential Use-cases: </strong><strong>Summary of the long emails, translating it from non-English to English and vice versa</strong></h4>
Imagine you work in a hospitality services business, and you request your customers to fill a survey at the end of their service. Your customers are both native English speakers and those who speak other languages. You often receive around 100-200 words of feedback and due to the volume, it gets tricky for you to go through each of them. This is where you want to summarise the feedback with their appropriate sentiments and also get the proper translation of it.

Below example is a glimpse of how you'd like to do it.

<a href="https://vscode.dev/github/arafattehsin/generative-ai/blob/main/notebooks/azure%20openai/dotnet/gen-ai-for-devs.ipynb#C15:L4"><img class="aligncenter wp-image-31761 size-full" src="https://arafattehsin.com/wp-content/uploads/2023/07/summary-translation.png" alt="Summary and Translation with GPT-3.5" width="921" height="832" /></a>
<h2>Flight Tracker</h2>
<h4><strong>Potential Use-cases: Flight tracking, postage tracking, food delivery </strong></h4>
<span style="font-size: 14.4px;">In 2020, I wrote two posts about how to build a Flight Tracker using Language Understanding Intelligent Service (aka LUIS), which is now upgraded as </span>Language<span style="font-size: 14.4px;"> Understanding in the Azure Language Studio. The reason why I brought the similar use-case here is to show you how effective it is to use the power of generative AI to build a much better version of it with less code. 💯</span>

Before we get there, imagine how you'd build it yourself? For this, you may need a source, destination or a flight number. If you do not have either of the locations or just the flight number, then you may not be able to track down the flight. This is <strong>at least</strong> required information for any flight for a simplest hypothetical use-case.

I have been working with Conversational AI since 2017 and pushed some useful solutions to production since then, I can confirm and you will also appreciate that this is not an easy problem. Your customer (who is an actual user of this bot) is a human. Humans are not naturally accustomed to following a pattern or sequence of the questions, all the time. That's why there's a good chance that humans may either provide a flight number, a destination or a source.

In order to solve this problem with Generative AI (Azure OpenAI Service), we just have to provide a good prompt, few shot examples and that's it. You will be able to see that it works quite well.

<strong><em>Protip: You may get a response returned in JSON or any format you'd like for your use-case. </em></strong>

Below is the syntax on how I did it. You can try it using the notebook.

<a href="https://vscode.dev/github/arafattehsin/generative-ai/blob/main/notebooks/azure%20openai/dotnet/gen-ai-for-devs.ipynb#C13:L3"><img class="aligncenter size-full wp-image-31762" src="https://arafattehsin.com/wp-content/uploads/2023/07/flight-tracker.png" alt="Flight Tracker with the Generative AI" width="1097" height="623" /></a>
<h2>DALL-E (Bonus 🎁)</h2>
If you look at the last section of the notebook, you will see that I have also created a boilerplate code for you to work with the DALL-E model which is still in preview. This creates an image out of the natural language utterance. Try it out yourself.

<img class="aligncenter size-full wp-image-31763" src="https://arafattehsin.com/wp-content/uploads/2023/07/batman-dall-e.png" alt="Batman working from home. Image generated by DALL-E" width="533" height="562" />

The power of generative AI has revolutionised the way we approach problem-solving in various domains. By leveraging these Generative AI notebooks and the advancements in AI technology such as Azure OpenAI Service (GPT-3, GPT-4 and so on), developers can now create more efficient and effective solutions with less code and effort. This blog post highlights the potential of generative AI in use-cases such as sentiment classification, summarization and translation, and flight tracking. As AI continues to evolve, we can expect to see even more innovative applications and streamlined development processes, making it easier for developers to deliver cutting-edge solutions to their users. Embracing these advancements will not only enhance our ability to solve complex problems but also foster a more inclusive and accessible environment for developers across various programming languages and platforms.

Until next time.