diff options
-rw-r--r-- | Content/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.md | 68 | ||||
-rw-r--r-- | docs/feed.rss | 78 | ||||
-rw-r--r-- | docs/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.html | 74 |
3 files changed, 192 insertions, 28 deletions
diff --git a/Content/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.md b/Content/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.md index 0dcabec..7701c09 100644 --- a/Content/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.md +++ b/Content/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.md @@ -17,6 +17,32 @@ Disclaimer: This is a very crude proof-of-concept, but it does work. I opened a CSV file and added some sample entries, with a corresponding label. ![Screenshot of Sample Dataset](/assets/posts/swift-chatbot/intent-csv.png) + +```csv +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 +``` + ![Screenshot of Create ML Text Classifier](/assets/posts/swift-chatbot/create-intent.png) ## Word Tagging @@ -26,6 +52,24 @@ This model will be only called if the intent from the classifier is a custom act I created a sample JSON with only 3 examples (I know, very less, but works for a crude PoC). ![Screenshot of Sample Dataset](/assets/posts/swift-chatbot/drugs-json.png) + +```json +[ + { + "tokens": ["Tell","me","about","the","drug","Aspirin","."], + "labels": ["NONE","NONE","NONE","NONE","NONE","COMPOUND","NONE"] + }, + { + "tokens": ["Please","tell","me","information","about","the","compound","salicylic","acid","."], + "labels": ["NONE","NONE","NONE","NONE","NONE","NONE","NONE","COMPOUND","COMPOUND","NONE"] + }, + { + "tokens": ["Information","about","the","compound","Ibuprofen","please","."], + "labels": ["NONE","NONE","NONE","NONE","COMPOUND","NONE","NONE"] + } +] +``` + ![Screenshot of Create ML Text Classifier](/assets/posts/swift-chatbot/create-tagger.png) ## Time to Get Swift-y @@ -105,14 +149,26 @@ Otherwise, it calls the custom action. ![Screenshot](/assets/posts/swift-chatbot/carbon-4.png) ```swift -let defaultResponses = [ - "greetings": "Hello", - "banter": "no, plix no" +let sampleMessages = [ + "Hey there, how is it going", + "hello, there", + "Who let the dogs out", + "can you tell me about the compound Geraniin", + "what do you know about the compound Ibuprofen", + "please, tell me more about the compound", + "please, tell me more about the molecule dihydrogen-monoxide" ] -let customActions = [ - "deez-drug": customAction -] +for sampleMessage in sampleMessages { + User.message = sampleMessage + let prediction = intentPredictor.predictedLabel(for: sampleMessage) + + if (defaultResponses[prediction!] != nil) { + print(defaultResponses[prediction!]!) + } else if (customActions[prediction!] != nil) { + print(customActions[prediction!]!()) + } +} ``` ![Output](/assets/posts/swift-chatbot/output.png) diff --git a/docs/feed.rss b/docs/feed.rss index 9a44450..51f5890 100644 --- a/docs/feed.rss +++ b/docs/feed.rss @@ -4,8 +4,8 @@ <title>Navan's Archive</title> <description>Rare Tips, Tricks and Posts</description> <link>https://web.navan.dev/</link><language>en</language> - <lastBuildDate>Mon, 28 Jun 2021 00:47:49 -0000</lastBuildDate> - <pubDate>Mon, 28 Jun 2021 00:47:49 -0000</pubDate> + <lastBuildDate>Mon, 28 Jun 2021 00:52:32 -0000</lastBuildDate> + <pubDate>Mon, 28 Jun 2021 00:52:32 -0000</pubDate> <ttl>250</ttl> <atom:link href="https://web.navan.dev/feed.rss" rel="self" type="application/rss+xml"/> @@ -2045,8 +2045,33 @@ Disclaimer: This is a very crude proof-of-concept, but it does work.</p> <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" /> -<img src="/assets/posts/swift-chatbot/create-intent.png" alt="Screenshot of Create ML Text Classifier" /></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>Word Tagging</h2> @@ -2054,8 +2079,25 @@ Disclaimer: This is a very crude proof-of-concept, but it does work.</p> 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" /> -<img src="/assets/posts/swift-chatbot/create-tagger.png" alt="Screenshot of Create ML Text Classifier" /></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="p">{</span> + <span class="nt">"tokens"</span><span class="p">:</span> <span class="p">[</span><span class="s2">"Tell"</span><span class="p">,</span><span class="s2">"me"</span><span class="p">,</span><span class="s2">"about"</span><span class="p">,</span><span class="s2">"the"</span><span class="p">,</span><span class="s2">"drug"</span><span class="p">,</span><span class="s2">"Aspirin"</span><span class="p">,</span><span class="s2">"."</span><span class="p">],</span> + <span class="nt">"labels"</span><span class="p">:</span> <span class="p">[</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"COMPOUND"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">]</span> + <span class="p">},</span> + <span class="p">{</span> + <span class="nt">"tokens"</span><span class="p">:</span> <span class="p">[</span><span class="s2">"Please"</span><span class="p">,</span><span class="s2">"tell"</span><span class="p">,</span><span class="s2">"me"</span><span class="p">,</span><span class="s2">"information"</span><span class="p">,</span><span class="s2">"about"</span><span class="p">,</span><span class="s2">"the"</span><span class="p">,</span><span class="s2">"compound"</span><span class="p">,</span><span class="s2">"salicylic"</span><span class="p">,</span><span class="s2">"acid"</span><span class="p">,</span><span class="s2">"."</span><span class="p">],</span> + <span class="nt">"labels"</span><span class="p">:</span> <span class="p">[</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"COMPOUND"</span><span class="p">,</span><span class="s2">"COMPOUND"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">]</span> + <span class="p">},</span> + <span class="p">{</span> + <span class="nt">"tokens"</span><span class="p">:</span> <span class="p">[</span><span class="s2">"Information"</span><span class="p">,</span><span class="s2">"about"</span><span class="p">,</span><span class="s2">"the"</span><span class="p">,</span><span class="s2">"compound"</span><span class="p">,</span><span class="s2">"Ibuprofen"</span><span class="p">,</span><span class="s2">"please"</span><span class="p">,</span><span class="s2">"."</span><span class="p">],</span> + <span class="nt">"labels"</span><span class="p">:</span> <span class="p">[</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"COMPOUND"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">]</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>Time to Get Swift-y</h2> @@ -2130,14 +2172,26 @@ 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">defaultResponses</span> <span class="p">=</span> <span class="p">[</span> - <span class="s">"greetings"</span><span class="p">:</span> <span class="s">"Hello"</span><span class="p">,</span> - <span class="s">"banter"</span><span class="p">:</span> <span class="s">"no, plix no"</span> +<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">"Hey there, how is it going"</span><span class="p">,</span> + <span class="s">"hello, there"</span><span class="p">,</span> + <span class="s">"Who let the dogs out"</span><span class="p">,</span> + <span class="s">"can you tell me about the compound Geraniin"</span><span class="p">,</span> + <span class="s">"what do you know about the compound Ibuprofen"</span><span class="p">,</span> + <span class="s">"please, tell me more about the compound"</span><span class="p">,</span> + <span class="s">"please, tell me more about the molecule dihydrogen-monoxide"</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">"deez-drug"</span><span class="p">:</span> <span class="n">customAction</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> diff --git a/docs/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.html b/docs/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.html index e4e4d1d..50c74a2 100644 --- a/docs/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.html +++ b/docs/posts/2021-06-27-Crude-ML-AI-Powered-Chatbot-Swift.html @@ -51,8 +51,33 @@ Disclaimer: This is a very crude proof-of-concept, but it does work.</p> <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" /> -<img src="/assets/posts/swift-chatbot/create-intent.png" alt="Screenshot of Create ML Text Classifier" /></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>Word Tagging</h2> @@ -60,8 +85,25 @@ Disclaimer: This is a very crude proof-of-concept, but it does work.</p> 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" /> -<img src="/assets/posts/swift-chatbot/create-tagger.png" alt="Screenshot of Create ML Text Classifier" /></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="p">{</span> + <span class="nt">"tokens"</span><span class="p">:</span> <span class="p">[</span><span class="s2">"Tell"</span><span class="p">,</span><span class="s2">"me"</span><span class="p">,</span><span class="s2">"about"</span><span class="p">,</span><span class="s2">"the"</span><span class="p">,</span><span class="s2">"drug"</span><span class="p">,</span><span class="s2">"Aspirin"</span><span class="p">,</span><span class="s2">"."</span><span class="p">],</span> + <span class="nt">"labels"</span><span class="p">:</span> <span class="p">[</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"COMPOUND"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">]</span> + <span class="p">},</span> + <span class="p">{</span> + <span class="nt">"tokens"</span><span class="p">:</span> <span class="p">[</span><span class="s2">"Please"</span><span class="p">,</span><span class="s2">"tell"</span><span class="p">,</span><span class="s2">"me"</span><span class="p">,</span><span class="s2">"information"</span><span class="p">,</span><span class="s2">"about"</span><span class="p">,</span><span class="s2">"the"</span><span class="p">,</span><span class="s2">"compound"</span><span class="p">,</span><span class="s2">"salicylic"</span><span class="p">,</span><span class="s2">"acid"</span><span class="p">,</span><span class="s2">"."</span><span class="p">],</span> + <span class="nt">"labels"</span><span class="p">:</span> <span class="p">[</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"COMPOUND"</span><span class="p">,</span><span class="s2">"COMPOUND"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">]</span> + <span class="p">},</span> + <span class="p">{</span> + <span class="nt">"tokens"</span><span class="p">:</span> <span class="p">[</span><span class="s2">"Information"</span><span class="p">,</span><span class="s2">"about"</span><span class="p">,</span><span class="s2">"the"</span><span class="p">,</span><span class="s2">"compound"</span><span class="p">,</span><span class="s2">"Ibuprofen"</span><span class="p">,</span><span class="s2">"please"</span><span class="p">,</span><span class="s2">"."</span><span class="p">],</span> + <span class="nt">"labels"</span><span class="p">:</span> <span class="p">[</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"COMPOUND"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">,</span><span class="s2">"NONE"</span><span class="p">]</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>Time to Get Swift-y</h2> @@ -136,14 +178,26 @@ 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">defaultResponses</span> <span class="p">=</span> <span class="p">[</span> - <span class="s">"greetings"</span><span class="p">:</span> <span class="s">"Hello"</span><span class="p">,</span> - <span class="s">"banter"</span><span class="p">:</span> <span class="s">"no, plix no"</span> +<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">"Hey there, how is it going"</span><span class="p">,</span> + <span class="s">"hello, there"</span><span class="p">,</span> + <span class="s">"Who let the dogs out"</span><span class="p">,</span> + <span class="s">"can you tell me about the compound Geraniin"</span><span class="p">,</span> + <span class="s">"what do you know about the compound Ibuprofen"</span><span class="p">,</span> + <span class="s">"please, tell me more about the compound"</span><span class="p">,</span> + <span class="s">"please, tell me more about the molecule dihydrogen-monoxide"</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">"deez-drug"</span><span class="p">:</span> <span class="n">customAction</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> |