Skip to content

Instantly share code, notes, and snippets.

@nige123
Created November 21, 2024 10:58
Show Gist options
  • Save nige123/ea12b525ff2af3f2fc4fd63ceeff2a24 to your computer and use it in GitHub Desktop.
Save nige123/ea12b525ff2af3f2fc4fd63ceeff2a24 to your computer and use it in GitHub Desktop.
<h1 id="wat-will-llms-give-you-for-christmas-">WAT will LLMs give you for Christmas? |</h1>
<p>Programming for a living used to be an active conversation between yourself, the computer, and your colleagues.
This Christmas, a new guest is joining the programming party: the LLM. Large Language Models (LLMs) can talk a lot
and just like your crazy Uncle occasionally blurt out something bonkers. But do we want to invite LLMs to the
programming party, and where should they sit? How can they help things flow?</p>
<p>Finding flow while programming is the art of staying in the Goldilocks zone - working on challenges that are not
too hard and not too easy. Raku and Perl are both super-expressive languages with rich operators. Their low and
long learning curves enable programmers to pick a place that matches their current skill level and stay in flow.</p>
<p>There is a potential downside, however, for a language like Raku that is <a href="https://perl6advent.wordpress.com/2015/12/20/perl-6-christmas-have-an-appropriate-amount-of-fun/">optimized for (O)fun</a>. Sometimes new
programmers encounter code too far along their learning curve, way outside their Goldilocks zone. For them, fear
is a flow stopper. Worse than that, as Master Yoda put it, &quot;Fear leads to anger. Anger leads to hate. Hate leads
to suffering.&quot; The <a href="https://raku-advent.blog/2020/12/02/day-1-perl-is-dead-long-live-perl-and-raku/">mindshare of expressive languages</a> has suffered enough.</p>
<p>Fortunately, Santa is smiling more than ever this Christmas! LLMs are a gift that can help lift more programmers up the learning curve and into the Goldilocks zone: less fear, more flow. </p>
<p>Expressive languages, like Raku, can offer LLM-powered utilities as part of their tooling to help new
programmers up the learning curve. Here&#39;s a simple example.</p>
<h3 id="remember-your-first-regular-expression-encounter-">Remember Your First Regular Expression Encounter?</h3>
<pre><code class="lang-regex">[12]<span class="hljs-tag">\<span class="hljs-name">d</span><span class="hljs-string">{3}</span></span>-[01]<span class="hljs-tag">\<span class="hljs-name">d</span></span>-[0-3]<span class="hljs-tag">\<span class="hljs-name">d</span></span> ([^ <span class="hljs-tag">\<span class="hljs-name">[</span></span>]*?)<span class="hljs-tag">\<span class="hljs-name">[</span></span>([^<span class="hljs-tag">\<span class="hljs-name">]</span></span>]*?)<span class="hljs-tag">\<span class="hljs-name">]</span></span>:.*
</code></pre>
<p>Now instead of wailing &quot;WAT?!&quot; Just use the Raku &quot;wat&quot; utility to help explain it. </p>
<pre><code class="lang-bash">shell&gt; wat "[12]<span class="hljs-tag">\<span class="hljs-name">d</span><span class="hljs-string">{3}</span></span>-[01]<span class="hljs-tag">\<span class="hljs-name">d</span></span>-[0-3]<span class="hljs-tag">\<span class="hljs-name">d</span></span> ([^ <span class="hljs-tag">\<span class="hljs-name">[</span></span>]*?)<span class="hljs-tag">\<span class="hljs-name">[</span></span>([^<span class="hljs-tag">\<span class="hljs-name">]</span></span>]*?)<span class="hljs-tag">\<span class="hljs-name">]</span></span>:.*"
</code></pre>
<p>Output:</p>
<blockquote>
<p>This code extracts log entries in the format &quot;timestamp [source]: message&quot;. Use it to parse log files and extract specific information. </p>
</blockquote>
<p><code>wat</code> works with code snippets or can explain entire programs for you.</p>
<pre><code class="lang-bash"><span class="hljs-keyword">shell</span>&gt; wat WAT/<span class="hljs-keyword">CLI</span>.rakumod
</code></pre>
<p>Output:</p>
<blockquote>
<p>This code summarizes code using the LLM::DWIM module.<br><strong>To use:</strong> </p>
<ul>
<li>Run <code>wat &lt;filename&gt;</code> to summarize a program </li>
<li>Run <code>wat &quot;$some-code.here();&quot;</code> to explain code </li>
<li>Run <code>wat config</code> to change settings </li>
<li>Run <code>wat help</code> to show help </li>
</ul>
</blockquote>
<p>The <code>wat</code> module uses the following prompt:</p>
<pre><code class="lang-raku"><span class="hljs-comment"># keeping the prompt short and sweet</span>
<span class="hljs-keyword">my</span> $prompt = q:<span class="hljs-keyword">to</span><span class="hljs-string">"PROMPT"</span>;
You are an expert programmer.
Produce friendly, simple, structured output.
Summarize <span class="hljs-keyword">the</span> following code <span class="hljs-keyword">for</span> a new programmer.
Limit <span class="hljs-keyword">the</span> response <span class="hljs-keyword">to</span> <span class="hljs-keyword">less than</span> $response-char-limit <span class="hljs-built_in">characters</span>.
Highlight how <span class="hljs-keyword">to</span> use this code:
PROMPT
</code></pre>
<p>One programmer&#39;s WAT is another&#39;s DWIM (Do What I Mean). Under the hood, the <code>WAT::CLI</code> module relies on
the <a href=""><code>LLM::DWIM</code></a> module&#39;s <code>dwim</code> method to do all the LLM work.</p>
<pre><code class="lang-raku"><span class="hljs-attribute">say</span> dwim <span class="hljs-variable">$prompt</span> <span class="hljs-regexp">~ <span class="hljs-variable">$program</span>-content</span>;
</code></pre>
<h3 id="context-matters">Context Matters</h3>
<p>Current LLMs work best with sufficient context - so they can associate problems and solutions. Without context,
LLMs can stumble. Just try giving <code>wat</code> an empty context (<code>&quot; &quot;</code>) and it&#39;s like that crazy Uncle at Christmas after
one too many!</p>
<h3 id="getting-started">Getting Started</h3>
<p>To have a play with <code>wat</code> this Christmas, install it with Raku&#39;s package manager <code>zef</code>:</p>
<pre><code class="lang-bash"><span class="hljs-keyword">shell&gt; </span>zef <span class="hljs-keyword">install </span>wat
</code></pre>
<hr>
<h2 id="build-your-own-llm-powered-raku-tool">Build Your Own LLM-Powered Raku Tool</h2>
<p>Here are some ideas for helping code to flow.</p>
<h3 id="given-the-source-code-of-a-program-">Given the Source Code of a Program:</h3>
<ul>
<li><strong>Comment Injector</strong>: Inject useful comments </li>
<li><strong>Comment Translator</strong>: Translate comments into your spoken language </li>
<li><strong>Reviewer</strong>: Suggest potential improvements </li>
<li><strong>Module Documentor</strong>: Document a module with RakuDoc </li>
<li><strong>Unit Test Maker</strong>: Generate unit tests </li>
<li><strong>CLI Maker</strong>: Generate a command-line interface to a program </li>
<li><strong>API Scaffolder</strong>: Translate between OpenAPI spec ↔ program </li>
<li><strong>Code Translator</strong>: Translate code from another language to Raku (e.g., Python β†’ Raku) </li>
<li><strong>SEO Docs</strong>: Document the problems the program solves and generate HTML for SEO and ingestion by LLMs </li>
</ul>
<h3 id="given-the-output-of-a-shell-command-">Given the Output of a Shell Command:</h3>
<ul>
<li><strong>Git Log Summary</strong>: Generate a stand-up report for your next Scrum meeting </li>
<li><strong>Test Error Summary</strong>: Summarize the output of many tests with next steps to fix </li>
<li><strong>Ticket Subtasks</strong>: Generate a set of subtasks required to close a ticket </li>
</ul>
<hr>
<p>Merry Christmas, and happy coding with your new guest at the programming table!</p>
<p>πŸŽ…πŸ’»βœ¨</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment