summaryrefslogtreecommitdiff
path: root/docs/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.html
blob: ca49edd0837e6a391e7167cbee48bb4920c5eafb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<!DOCTYPE html>
<html lang="en">
<head>
    
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="theme-color" content="#6a9fb5">

    <title>Making a Crude ML Powered Chatbot in Swift using CoreML</title>

    <!--
    <link rel="stylesheet" href="https://unpkg.com/latex.css/style.min.css" /> 
    -->

    <link rel="stylesheet" href="/assets/c-hyde.css">

    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Abril+Fatface">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=PT+Sans:400,400italic,700">

    <link rel="stylesheet" href="/assets/main.css">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="og:site_name" content="Navan Chauhan">
    <link rel="canonical" href="https://web.navan.dev/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.html">
    <meta name="twitter:url" content="https://web.navan.dev/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.html">
    <meta name="og:url" content="https://web.navan.dev/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.html">
    <meta name="twitter:title" content="Making a Crude ML Powered Chatbot in Swift using CoreML">
    <meta name="og:title" content="Making a Crude ML Powered Chatbot in Swift using CoreML">
    <meta name="description" content="Writing a simple Machine-Learning powered Chatbot (or, daresay virtual personal assistant ) in Swift using CoreML.">
    <meta name="twitter:description" content="Writing a simple Machine-Learning powered Chatbot (or, daresay virtual personal assistant ) in Swift using CoreML.">
    <meta name="og:description" content="Writing a simple Machine-Learning powered Chatbot (or, daresay virtual personal assistant ) in Swift using CoreML.">
    <meta name="twitter:card" content="summary_large_image">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="shortcut icon" href="/images/favicon.png" type="image/png">
    <link href="/feed.rss" type="application/atom+xml" rel="alternate" title="Sitewide Atom feed">
    <meta name="twitter:image" content="https://web.navan.dev/images/opengraph/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.png">
    <meta name="og:image" content="https://web.navan.dev/images/opengraph/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.png">
    <meta name="google-site-verification" content="LVeSZxz-QskhbEjHxOi7-BM5dDxTg53x2TwrjFxfL0k">
    <script data-goatcounter="https://navanchauhan.goatcounter.com/count"
        async src="//gc.zgo.at/count.js"></script>
    <script defer data-domain="web.navan.dev" src="https://plausible.io/js/plausible.js"></script>
    <link rel="manifest" href="/manifest.json">
    
</head>
<body class="theme-base-0d">
    <div class="sidebar">
    <div class="container sidebar-sticky">
        <div class="sidebar-about">
            <h1><a href="/">Navan</a></h1>
            <p class="lead" id="random-lead">Alea iacta est.</p>
        </div>

        <ul class="sidebar-nav">
            <li><a class="sidebar-nav-item" href="/about/">about/links</a></li>
            <li><a class="sidebar-nav-item" href="/posts/">posts</a></li>
            <li><a class="sidebar-nav-item" href="/3D-Designs/">3D designs</a></li>
            <li><a class="sidebar-nav-item" href="/feed.rss">RSS Feed</a></li>
            <li><a class="sidebar-nav-item" href="/colophon/">colophon</a></li>
        </ul>
        <div class="copyright"><p>&copy; 2019-2024. Navan Chauhan <br> <a href="/feed.rss">RSS</a></p></div>
    </div>
</div>

<script>
let phrases = [
    "Something Funny", "Veni, vidi, vici", "Alea iacta est", "In vino veritas", "Acta, non verba", "Castigat ridendo mores",
    "Cui bono?", "Memento vivere", "अहम् ब्रह्मास्मि", "अनुगच्छतु प्रवाहं", "चरन्मार्गान्विजानाति", "coq de cheval", "我愛啤酒"
    ];

let new_phrase = phrases[Math.floor(Math.random()*phrases.length)];

let lead = document.getElementById("random-lead");
lead.innerText = new_phrase;
</script>
    <div class="content container">
    
	<div class="post">
	<h1 id="making-a-crude-ml-powered-chatbot-in-swift-using-coreml">Making a Crude ML Powered Chatbot in Swift using CoreML</h1>

<p>A chatbot/virtual assistant, on paper, looks easy to build.
The user says something, the programs finds the best action, checks if additional input is required and sends back the output.
To do this in Swift, I used two separate ML Models created using Apple's Create ML App.
First is a Text Classifier to classify intent, and the other a word tagger for extracting input from the input message. 
Disclaimer: This is a very crude proof-of-concept, but it does work.</p>

<h2 id="text-classifier">Text Classifier</h2>

<p>I opened a CSV file and added some sample entries, with a corresponding label.</p>

<p><img src="/assets/posts/swift-chatbot/intent-csv.png" alt="Screenshot of Sample Dataset" /></p>

<pre><code>text,label
hey there,greetings
hello,greetings
good morning,greetings
good evening,greetings
hi,greetings
open the pod bay doors,banter
who let the dogs out,banter
ahh that's hot,banter
bruh that's rad,banter
nothing,banter
da fuq,banter
can you tell me details about the compound aspirin,deez-drug
i want to know about some compounds,deez-drug
search about the compound,deez-drug
tell me about the molecule,deez-drug
tell me about something,banter
tell me something cool,banter
tell a joke,banter
make me a sandwich,banter
whatcha doing,greetings
i love you,banter
</code></pre>

<p><img src="/assets/posts/swift-chatbot/create-intent.png" alt="Screenshot of Create ML Text Classifier" /></p>

<h2 id="word-tagging">Word Tagging</h2>

<p>This is useful to extract the required variables directly from the user's input.
This model will be only called if the intent from the classifier is a custom action.
I created a sample JSON with only 3 examples (I know, very less, but works for a crude PoC).</p>

<p><img src="/assets/posts/swift-chatbot/drugs-json.png" alt="Screenshot of Sample Dataset" /></p>

<div class="codehilite">
<pre><span></span><code><span class="p">[</span>
<span class="w">    </span><span class="p">{</span>
<span class="w">        </span><span class="nt">&quot;tokens&quot;</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="s2">&quot;Tell&quot;</span><span class="p">,</span><span class="s2">&quot;me&quot;</span><span class="p">,</span><span class="s2">&quot;about&quot;</span><span class="p">,</span><span class="s2">&quot;the&quot;</span><span class="p">,</span><span class="s2">&quot;drug&quot;</span><span class="p">,</span><span class="s2">&quot;Aspirin&quot;</span><span class="p">,</span><span class="s2">&quot;.&quot;</span><span class="p">],</span>
<span class="w">        </span><span class="nt">&quot;labels&quot;</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="s2">&quot;NONE&quot;</span><span class="p">,</span><span class="s2">&quot;NONE&quot;</span><span class="p">,</span><span class="s2">&quot;NONE&quot;</span><span class="p">,</span><span class="s2">&quot;NONE&quot;</span><span class="p">,</span><span class="s2">&quot;NONE&quot;</span><span class="p">,</span><span class="s2">&quot;COMPOUND&quot;</span><span class="p">,</span><span class="s2">&quot;NONE&quot;</span><span class="p">]</span>
<span class="w">    </span><span class="p">},</span>
<span class="w">    </span><span class="p">{</span>
<span class="w">        </span><span class="nt">&quot;tokens&quot;</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="s2">&quot;Please&quot;</span><span class="p">,</span><span class="s2">&quot;tell&quot;</span><span class="p">,</span><span class="s2">&quot;me&quot;</span><span class="p">,</span><span class="s2">&quot;information&quot;</span><span class="p">,</span><span class="s2">&quot;about&quot;</span><span class="p">,</span><span class="s2">&quot;the&quot;</span><span class="p">,</span><span class="s2">&quot;compound&quot;</span><span class="p">,</span><span class="s2">&quot;salicylic&quot;</span><span class="p">,</span><span class="s2">&quot;acid&quot;</span><span class="p">,</span><span class="s2">&quot;.&quot;</span><span class="p">],</span>
<span class="w">        </span><span class="nt">&quot;labels&quot;</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="s2">&quot;NONE&quot;</span><span class="p">,</span><span class="s2">&quot;NONE&quot;</span><span class="p">,</span><span class="s2">&quot;NONE&quot;</span><span class="p">,</span><span class="s2">&quot;NONE&quot;</span><span class="p">,</span><span class="s2">&quot;NONE&quot;</span><span class="p">,</span><span class="s2">&quot;NONE&quot;</span><span class="p">,</span><span class="s2">&quot;NONE&quot;</span><span class="p">,</span><span class="s2">&quot;COMPOUND&quot;</span><span class="p">,</span><span class="s2">&quot;COMPOUND&quot;</span><span class="p">,</span><span class="s2">&quot;NONE&quot;</span><span class="p">]</span>
<span class="w">    </span><span class="p">},</span>
<span class="w">    </span><span class="p">{</span>
<span class="w">        </span><span class="nt">&quot;tokens&quot;</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="s2">&quot;Information&quot;</span><span class="p">,</span><span class="s2">&quot;about&quot;</span><span class="p">,</span><span class="s2">&quot;the&quot;</span><span class="p">,</span><span class="s2">&quot;compound&quot;</span><span class="p">,</span><span class="s2">&quot;Ibuprofen&quot;</span><span class="p">,</span><span class="s2">&quot;please&quot;</span><span class="p">,</span><span class="s2">&quot;.&quot;</span><span class="p">],</span>
<span class="w">        </span><span class="nt">&quot;labels&quot;</span><span class="p">:</span><span class="w"> </span><span class="p">[</span><span class="s2">&quot;NONE&quot;</span><span class="p">,</span><span class="s2">&quot;NONE&quot;</span><span class="p">,</span><span class="s2">&quot;NONE&quot;</span><span class="p">,</span><span class="s2">&quot;NONE&quot;</span><span class="p">,</span><span class="s2">&quot;COMPOUND&quot;</span><span class="p">,</span><span class="s2">&quot;NONE&quot;</span><span class="p">,</span><span class="s2">&quot;NONE&quot;</span><span class="p">]</span>
<span class="w">    </span><span class="p">}</span>
<span class="p">]</span>
</code></pre>
</div>

<p><img src="/assets/posts/swift-chatbot/create-tagger.png" alt="Screenshot of Create ML Text Classifier" /></p>

<h2 id="time-to-get-swift-y">Time to Get Swift-y</h2>

<p>The initial part is easy, importing CoreML and NaturalLanguage and then initializing the models and the tagger.</p>

<p><img src="/assets/posts/swift-chatbot/carbon.png" alt="Screenshot" /></p>

<div class="codehilite">
<pre><span></span><code><span class="kd">import</span> <span class="nc">CoreML</span>
<span class="kd">import</span> <span class="nc">NaturalLanguage</span>

<span class="kd">let</span> <span class="nv">mlModelClassifier</span> <span class="p">=</span> <span class="k">try</span> <span class="n">IntentDetection_1</span><span class="p">(</span><span class="n">configuration</span><span class="p">:</span>  <span class="bp">MLModelConfiguration</span><span class="p">()).</span><span class="n">model</span>
<span class="kd">let</span> <span class="nv">mlModelTagger</span> <span class="p">=</span> <span class="k">try</span> <span class="n">CompoundTagger</span><span class="p">(</span><span class="n">configuration</span><span class="p">:</span> <span class="bp">MLModelConfiguration</span><span class="p">()).</span><span class="n">model</span>

<span class="kd">let</span> <span class="nv">intentPredictor</span> <span class="p">=</span> <span class="k">try</span> <span class="bp">NLModel</span><span class="p">(</span><span class="n">mlModel</span><span class="p">:</span> <span class="n">mlModelClassifier</span><span class="p">)</span>
<span class="kd">let</span> <span class="nv">tagPredictor</span> <span class="p">=</span> <span class="k">try</span> <span class="bp">NLModel</span><span class="p">(</span><span class="n">mlModel</span><span class="p">:</span> <span class="n">mlModelTagger</span><span class="p">)</span>

<span class="kd">let</span> <span class="nv">tagger</span> <span class="p">=</span> <span class="bp">NLTagger</span><span class="p">(</span><span class="n">tagSchemes</span><span class="p">:</span> <span class="p">[.</span><span class="n">nameType</span><span class="p">,</span> <span class="n">NLTagScheme</span><span class="p">(</span><span class="s">&quot;Apple&quot;</span><span class="p">)])</span>
<span class="n">tagger</span><span class="p">.</span><span class="n">setModels</span><span class="p">([</span><span class="n">tagPredictor</span><span class="p">],</span> <span class="n">forTagScheme</span><span class="p">:</span> <span class="n">NLTagScheme</span><span class="p">(</span><span class="s">&quot;Apple&quot;</span><span class="p">))</span>
</code></pre>
</div>

<p>Now, we define a simple structure which the custom function(s) can use to access the provided input. 
It can also be used to hold additional variables.
This custom action for our third label, uses the Word Tagger model to check for the compound in the user's message.
If it is present then it displays the name, otherwise it tells the user that they have not provided the input.
The latter can be replaced with a function which asks the user for the input. </p>

<p><img src="/assets/posts/swift-chatbot/carbon-2.png" alt="Screenshot" /></p>

<div class="codehilite">
<pre><span></span><code><span class="kd">struct</span> <span class="nc">User</span> <span class="p">{</span>
    <span class="kd">static</span> <span class="kd">var</span> <span class="nv">message</span> <span class="p">=</span> <span class="s">&quot;&quot;</span>
<span class="p">}</span>

<span class="kd">func</span> <span class="nf">customAction</span><span class="p">()</span> <span class="p">-&gt;</span> <span class="nb">String</span> <span class="p">{</span>
    <span class="kd">let</span> <span class="nv">sampleMessage</span> <span class="p">=</span> <span class="n">User</span><span class="p">.</span><span class="n">message</span>
    <span class="kd">var</span> <span class="nv">actionable_item</span> <span class="p">=</span> <span class="s">&quot;&quot;</span>
    <span class="n">tagger</span><span class="p">.</span><span class="n">string</span> <span class="p">=</span> <span class="n">sampleMessage</span>
    <span class="n">tagger</span><span class="p">.</span><span class="n">enumerateTags</span><span class="p">(</span><span class="k">in</span><span class="p">:</span> <span class="n">sampleMessage</span><span class="p">.</span><span class="n">startIndex</span><span class="p">..&lt;</span><span class="n">sampleMessage</span><span class="p">.</span><span class="n">endIndex</span><span class="p">,</span> <span class="n">unit</span><span class="p">:</span> <span class="p">.</span><span class="n">word</span><span class="p">,</span>
                             <span class="n">scheme</span><span class="p">:</span> <span class="n">NLTagScheme</span><span class="p">(</span><span class="s">&quot;Apple&quot;</span><span class="p">),</span> <span class="n">options</span><span class="p">:</span> <span class="p">.</span><span class="n">omitWhitespace</span><span class="p">)</span> <span class="p">{</span> <span class="n">tag</span><span class="p">,</span> <span class="n">tokenRange</span>  <span class="k">in</span>
            <span class="k">if</span> <span class="kd">let</span> <span class="nv">tag</span> <span class="p">=</span> <span class="n">tag</span> <span class="p">{</span>
                <span class="k">if</span> <span class="n">tag</span><span class="p">.</span><span class="n">rawValue</span> <span class="p">==</span> <span class="s">&quot;COMPOUND&quot;</span> <span class="p">{</span>
                    <span class="n">actionable_item</span> <span class="o">+=</span> <span class="n">sampleMessage</span><span class="p">[</span><span class="n">tokenRange</span><span class="p">]</span>
                <span class="p">}</span>
            <span class="p">}</span>
        <span class="k">return</span> <span class="kc">true</span>
    <span class="p">}</span>
    <span class="k">if</span> <span class="n">actionable_item</span> <span class="p">==</span> <span class="s">&quot;&quot;</span> <span class="p">{</span>
        <span class="k">return</span> <span class="s">&quot;You did not provide any input&quot;</span>
    <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
        <span class="k">return</span> <span class="s">&quot;You provided input </span><span class="si">\(</span><span class="n">actionable_item</span><span class="si">)</span><span class="s"> for performing custom action&quot;</span>
    <span class="p">}</span>

<span class="p">}</span>
</code></pre>
</div>

<p>Sometimes, no action needs to be performed, and the bot can use a predefined set of responses. 
Otherwise, if an action is required, it can call the custom action.</p>

<p><img src="/assets/posts/swift-chatbot/carbon-3.png" alt="Screenshot" /></p>

<div class="codehilite">
<pre><span></span><code><span class="kd">let</span> <span class="nv">defaultResponses</span> <span class="p">=</span> <span class="p">[</span>
    <span class="s">&quot;greetings&quot;</span><span class="p">:</span> <span class="s">&quot;Hello&quot;</span><span class="p">,</span>
    <span class="s">&quot;banter&quot;</span><span class="p">:</span> <span class="s">&quot;no, plix no&quot;</span>
<span class="p">]</span>

<span class="kd">let</span> <span class="nv">customActions</span> <span class="p">=</span> <span class="p">[</span>
    <span class="s">&quot;deez-drug&quot;</span><span class="p">:</span> <span class="n">customAction</span>
<span class="p">]</span>
</code></pre>
</div>

<p>In the sample input, the program is updating the User.message and checking if it has a default response.
Otherwise, it calls the custom action.</p>

<p><img src="/assets/posts/swift-chatbot/carbon-4.png" alt="Screenshot" /></p>

<div class="codehilite">
<pre><span></span><code><span class="kd">let</span> <span class="nv">sampleMessages</span> <span class="p">=</span> <span class="p">[</span>
    <span class="s">&quot;Hey there, how is it going&quot;</span><span class="p">,</span>
    <span class="s">&quot;hello, there&quot;</span><span class="p">,</span>
    <span class="s">&quot;Who let the dogs out&quot;</span><span class="p">,</span>
    <span class="s">&quot;can you tell me about the compound Geraniin&quot;</span><span class="p">,</span>
    <span class="s">&quot;what do you know about the compound Ibuprofen&quot;</span><span class="p">,</span>
    <span class="s">&quot;please, tell me more about the compound&quot;</span><span class="p">,</span>
    <span class="s">&quot;please, tell me more about the molecule dihydrogen-monoxide&quot;</span>
<span class="p">]</span>

<span class="k">for</span> <span class="n">sampleMessage</span> <span class="k">in</span> <span class="n">sampleMessages</span> <span class="p">{</span>
    <span class="n">User</span><span class="p">.</span><span class="n">message</span> <span class="p">=</span> <span class="n">sampleMessage</span>
    <span class="kd">let</span> <span class="nv">prediction</span> <span class="p">=</span> <span class="n">intentPredictor</span><span class="p">.</span><span class="n">predictedLabel</span><span class="p">(</span><span class="k">for</span><span class="p">:</span> <span class="n">sampleMessage</span><span class="p">)</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">defaultResponses</span><span class="p">[</span><span class="n">prediction</span><span class="p">!]</span> <span class="o">!=</span> <span class="kc">nil</span><span class="p">)</span> <span class="p">{</span>
        <span class="bp">print</span><span class="p">(</span><span class="n">defaultResponses</span><span class="p">[</span><span class="n">prediction</span><span class="p">!]</span><span class="o">!</span><span class="p">)</span>
    <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">customActions</span><span class="p">[</span><span class="n">prediction</span><span class="p">!]</span> <span class="o">!=</span> <span class="kc">nil</span><span class="p">)</span> <span class="p">{</span>
        <span class="bp">print</span><span class="p">(</span><span class="n">customActions</span><span class="p">[</span><span class="n">prediction</span><span class="p">!]</span><span class="o">!</span><span class="p">())</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre>
</div>

<p><img src="/assets/posts/swift-chatbot/output.png" alt="Output" /></p>

<p>So easy.</p>

<p>If I ever release a part-2, it will either be about implementing this in Tensorflow.JS or an iOS app using SwiftUI ;)</p>

	</div>
	<blockquote>If you have scrolled this far, consider subscribing to my mailing list <a href="https://listmonk.navan.dev/subscription/form">here.</a> You can subscribe to either a specific type of post you are interested in, or subscribe to everything with the "Everything" list.</blockquote>
	<script data-isso="https://comments.navan.dev/"
        src="https://comments.navan.dev/js/embed.min.js"></script>
	<div id="isso-thread">
	    <noscript>Javascript needs to be activated to view comments.</noscript>
	</div>

    </div>
    <script src="assets/manup.min.js"></script>
    <script src="/pwabuilder-sw-register.js"></script>    
</body>
</html>