<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>Ibis Birdbrain</title>
<link>https://ibis-project.github.io/posts.html</link>
<atom:link href="https://ibis-project.github.io/posts.xml" rel="self" type="application/rss+xml"/>
<description>the portable Python ML-powered data bot</description>
<generator>quarto-1.4.398</generator>
<lastBuildDate>Mon, 16 Oct 2023 00:00:00 GMT</lastBuildDate>
<item>
  <title>Auxiliary tools</title>
  <dc:creator>Cody Peterson</dc:creator>
  <link>https://ibis-project.github.io/posts/llms-and-data-pt4/</link>
  <description><![CDATA[ 





<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>As a product manager, I don’t spend most of my time managing products. I suspect most data developers (analysts, engineers, scientists, etc.) don’t spend most of their time writing data code. There are many auxiliary tasks that are required in doing many technical jobs. These include:</p>
<ul>
<li>searching the Internet for information</li>
<li>reading, summarizing, and synthesizing information</li>
<li>performing boring computer tasks</li>
<li>translating between different languages (e.g.&nbsp;SQL and Python; English and Spanish)</li>
<li>copying and modifying existing code</li>
<li>querying some basic informatin from data platforms</li>
</ul>
<p>What if we could, through natural language, have a bot perform many of these tasks (in addition to basic data analysis) on our behalf?</p>
</section>
<section id="were-using-python-lets-use-python" class="level2">
<h2 class="anchored" data-anchor-id="were-using-python-lets-use-python">We’re using Python, let’s use Python</h2>
<p>We’re already using Python for Ibis and Marvin. Let’s use it for auxillary tools. We’ll setup our data and AI platform connections and some simple example data to work with.</p>
<div id="c9d5a817" class="cell" data-execution_count="1">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="annotated-cell-1" style="background: #f1f3f5;"><pre class="sourceCode python code-annotation-code code-with-copy code-annotated"><code class="sourceCode python"><button class="code-annotation-anchor" data-target-cell="annotated-cell-1" data-target-annotation="1">1</button><span id="annotated-cell-1-1" class="code-annotation-target"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> ibis</span>
<span id="annotated-cell-1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> marvin</span>
<span id="annotated-cell-1-3"></span>
<span id="annotated-cell-1-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> dotenv <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> load_dotenv</span>
<span id="annotated-cell-1-5"></span>
<button class="code-annotation-anchor" data-target-cell="annotated-cell-1" data-target-annotation="2">2</button><span id="annotated-cell-1-6" class="code-annotation-target">load_dotenv()</span>
<span id="annotated-cell-1-7"></span>
<button class="code-annotation-anchor" data-target-cell="annotated-cell-1" data-target-annotation="3">3</button><span id="annotated-cell-1-8" class="code-annotation-target">con <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ibis.<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">connect</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"duckdb://penguins.ddb"</span>)</span>
<span id="annotated-cell-1-9">t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ibis.examples.penguins.fetch()</span>
<span id="annotated-cell-1-10">t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> con.create_table(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"penguins"</span>, t.to_pyarrow(), overwrite<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div>
</details>
<div class="cell-annotation">
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-1" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-1" data-code-lines="1,2,4" data-code-annotation="1">Import the libraries we need.</span>
</dd>
<dt data-target-cell="annotated-cell-1" data-target-annotation="2">2</dt>
<dd>
<span data-code-cell="annotated-cell-1" data-code-lines="6" data-code-annotation="2">Load the environment variable to setup Marvin to call our OpenAI account.</span>
</dd>
<dt data-target-cell="annotated-cell-1" data-target-annotation="3">3</dt>
<dd>
<span data-code-cell="annotated-cell-1" data-code-lines="8,9,10" data-code-annotation="3">Setup the demo datain an Ibis backend.</span>
</dd>
</dl>
</div>
</div>
<div id="22b11433" class="cell" data-execution_count="2">
<div class="sourceCode cell-code" id="annotated-cell-2" style="background: #f1f3f5;"><pre class="sourceCode python code-annotation-code code-with-copy code-annotated"><code class="sourceCode python"><button class="code-annotation-anchor" data-target-cell="annotated-cell-2" data-target-annotation="1">1</button><span id="annotated-cell-2-1" class="code-annotation-target"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> ibis</span>
<span id="annotated-cell-2-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> marvin</span>
<span id="annotated-cell-2-3"></span>
<span id="annotated-cell-2-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> ibis.expr.schema <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Schema</span>
<span id="annotated-cell-2-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> ibis.expr.types.relations <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Table</span>
<span id="annotated-cell-2-6"></span>
<button class="code-annotation-anchor" data-target-cell="annotated-cell-2" data-target-annotation="2">2</button><span id="annotated-cell-2-7" class="code-annotation-target">ibis.options.interactive <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="annotated-cell-2-8">marvin.settings.llm_model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"openai/gpt-4"</span></span>
<span id="annotated-cell-2-9"></span>
<button class="code-annotation-anchor" data-target-cell="annotated-cell-2" data-target-annotation="3">3</button><span id="annotated-cell-2-10" class="code-annotation-target">con <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ibis.<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">connect</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"duckdb://penguins.ddb"</span>)</span>
<span id="annotated-cell-2-11">t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> con.table(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"penguins"</span>)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div>
<div class="cell-annotation">
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-2" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-2" data-code-lines="1,2,4,5" data-code-annotation="1">Import Ibis and Marvin.</span>
</dd>
<dt data-target-cell="annotated-cell-2" data-target-annotation="2">2</dt>
<dd>
<span data-code-cell="annotated-cell-2" data-code-lines="7,8" data-code-annotation="2">Configure Ibis (interactive) and Marvin (GPT-4).</span>
</dd>
<dt data-target-cell="annotated-cell-2" data-target-annotation="3">3</dt>
<dd>
<span data-code-cell="annotated-cell-2" data-code-lines="10,11" data-code-annotation="3">Connect to the data and load a table into a variable.</span>
</dd>
</dl>
</div>
</div>
</section>
<section id="filesystem-tools" class="level2">
<h2 class="anchored" data-anchor-id="filesystem-tools">Filesystem tools</h2>
</section>
<section id="internet-tools" class="level2">
<h2 class="anchored" data-anchor-id="internet-tools">Internet tools</h2>
</section>
<section id="ai-powered-tools" class="level2">
<h2 class="anchored" data-anchor-id="ai-powered-tools">AI-powered tools</h2>
</section>
<section id="introducing-ibis-birdbrain" class="level2">
<h2 class="anchored" data-anchor-id="introducing-ibis-birdbrain">Introducing Ibis Birdbrain</h2>
<p>Introduce the bot. Need to overview the tools here I think, but should probably skip most details.</p>
</section>
<section id="a-comparison-with-mlops" class="level2">
<h2 class="anchored" data-anchor-id="a-comparison-with-mlops">A comparison with MLOps</h2>
<p>TODO: point on how most of the work is not ML</p>
<p>Before “MLOps” was a standard term, the <a href="https://proceedings.neurips.cc/paper_files/paper/2015/file/86df7dcfd896fcaf2674f757a2463eba-Paper.pdf">Sculley et al paper</a> from 2015 described the key issues with building real-world ML systems.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://ibis-project.github.io/posts/llms-and-data-pt4/images/figure1.png" class="img-fluid figure-img"></p>
<figcaption>Figure 1: Only a small fraction of real-world ML systems is composed of the ML code, as shown by the small black box in the middle. The required surround infrastructure is vast and complex.</figcaption>
</figure>
</div>
<p>You can substitute “LLM” for “ML” in the above figure.</p>
<section id="the-toy-problem-problem" class="level3">
<h3 class="anchored" data-anchor-id="the-toy-problem-problem">The “toy problem” problem</h3>
<p>ML and LLMs are cool! They’re fun to play with and it’s easy to get distracted with fun applications. Often, ML is learned through solving toy problems, and …</p>
</section>
<section id="the-application-landscape-is-vast" class="level3">
<h3 class="anchored" data-anchor-id="the-application-landscape-is-vast">The application landscape is vast</h3>
<p>…and thus requires modular, interoperable, customizable, and extensible tools. TODO: more comparison to MLOps.</p>
</section>
</section>
<section id="next-steps" class="level2">
<h2 class="anchored" data-anchor-id="next-steps">Next steps</h2>
<p>You can get involved with <a href="https://github.com/ibis-project/ibis-birdbrain">Ibis Birdbrain</a>, our open-source data &amp; AI project for building next-generation natural language interfaces to data.</p>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>LLMs and data</category>
  <guid>https://ibis-project.github.io/posts/llms-and-data-pt4/</guid>
  <pubDate>Mon, 16 Oct 2023 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Composable data + AI systems</title>
  <dc:creator>Cody Peterson</dc:creator>
  <link>https://ibis-project.github.io/posts/llms-and-data-pt3/</link>
  <description><![CDATA[ 





<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>When writing (data and AI) code, it’s important to use the right abstraction for your job to be done. A modular and composable data and AI system is easier than ever with the new generation of LLM-powered tools, but lacks a standard, modular, interoperable, customizable, and extensible framework. Can Ibis Birdbrain be that standard?</p>
</section>
<section id="standards" class="level2">
<h2 class="anchored" data-anchor-id="standards">Standards</h2>
<blockquote class="blockquote">
<blockquote class="blockquote">
<p>When things don’t work as they should, it often means that standards are absent.</p>
<p>- <a href="https://www.iso.org/standards.html">The International Organization for Standardization (ISO)</a></p>
</blockquote>
<p>- <a href="https://voltrondata.com/codex/standards-over-silos">The Composable Codex</a></p>
</blockquote>
<p>The standard that has emerged from the recent LLM applications is the power of a conversation in interacting with a computer. If we can give that interaction tht ability to interact with data platforms, we can achieve a lot through natural language.</p>
</section>
<section id="composable-data-and-ai-systems-are-mice" class="level2">
<h2 class="anchored" data-anchor-id="composable-data-and-ai-systems-are-mice">Composable data (and AI) systems are MICE</h2>
<p><a href="https://voltrondata.com/codex/standards-over-silos">Composable data systems are MICE: modular, interoperable, customizable, and extensible</a>.</p>
<p>With any new tech, there’s a lot of duplication of effort and reinvention of wheels. Ibis Birdbrain aims to be an open-source standard for building data and AI systems in Python that adheres to MICE best practices and works with any data and AI platform. It achieves this by building on top of <a href="https://ibis-project.org/">Ibis</a> and <a href="https://www.askmarvin.ai/components/overview/">Marvin</a>, handling calls to the data and AI platforms respectively. Ibis and Marvin are themselves built on many other open-source projects, but provide the right level of abstraction for building composable data and AI systems, independently. Ibis Birdbrain glues them together enables a natural language interface that’s interoperable between 18+ data platforms and any AI platform Marvin supports.</p>
<p>glue that enables working via natural language with any data platform Ibis supports and any AI platform Marvin supports.</p>
<div class="callout callout-style-default callout-important callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Important
</div>
</div>
<div class="callout-body-container callout-body">
<p>Ibis Birdbrain is very early in development and looking for community input and contributors. We believe it’s important for there to be a standard, and I can see Ibis Birdbrain going in several directions:</p>
<ol type="1">
<li>A useful template for building a language user interface with data tools</li>
<li>A standard for building data and AI systems with MICE components</li>
</ol>
<p>If you’re interested in either of these directions, please get involved with the project!</p>
<p>Ibis Birdbrain is under <a href="https://github.com/ibis-project/governance/blob/main/governance.md">Ibis project governance</a>.</p>
</div>
</div>
</section>
<section id="looking-ahead" class="level2">
<h2 class="anchored" data-anchor-id="looking-ahead">Looking ahead</h2>
<p>I am not a software engineer, I just saw a cool application of Ibis and Marvin. The objective of releasing Ibis Birdbrain as an open-source project under Ibis governance is to enable the community to build a standard for composable data and AI systems. My hope is for the Ibis, Marvin, and broader data/AI Python communities to build the standard in the open, and properietary applications on top of those standards. This is important for a composable data ecosystem that can be modular, interoperable, customizable, and extensible.</p>
</section>
<section id="how-is-ibis-birdbrain-mice-now-whats-next" class="level2">
<h2 class="anchored" data-anchor-id="how-is-ibis-birdbrain-mice-now-whats-next">How is Ibis Birdbrain MICE now? What’s next?</h2>
</section>
<section id="next-steps" class="level2">
<h2 class="anchored" data-anchor-id="next-steps">Next steps</h2>
<p>You can get involved with <a href="https://github.com/ibis-project/ibis-birdbrain">Ibis Birdbrain</a>, our open-source data &amp; AI project for building next-generation natural language interfaces to data.</p>
<p><a href="../llms-and-data-pt4">Read the next post in this series, building Ibis Birdbrain</a>.</p>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>LLMs and data</category>
  <guid>https://ibis-project.github.io/posts/llms-and-data-pt3/</guid>
  <pubDate>Sun, 15 Oct 2023 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Computations and control flow: it’s just programming</title>
  <dc:creator>Cody Peterson</dc:creator>
  <link>https://ibis-project.github.io/posts/llms-and-data-pt2/</link>
  <description><![CDATA[ 





<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>The recent Generative AI hype cycle has led to a lot of new terminology to understand. In this post, we’ll cover some key concepts from the groud up and explain the basics of working with LLMs in the context of data.</p>
<p>This post assumes <a href="../llms-and-data-pt0">basic familiarity with Marvin and Ibis</a> and <a href="../llms-and-data-pt1">three approaches to applying LLMs to data</a>.</p>
<div id="675d651b" class="cell" data-execution_count="1">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="annotated-cell-1" style="background: #f1f3f5;"><pre class="sourceCode python code-annotation-code code-with-copy code-annotated"><code class="sourceCode python"><button class="code-annotation-anchor" data-target-cell="annotated-cell-1" data-target-annotation="1">1</button><span id="annotated-cell-1-1" class="code-annotation-target"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> ibis</span>
<span id="annotated-cell-1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> marvin</span>
<span id="annotated-cell-1-3"></span>
<span id="annotated-cell-1-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> dotenv <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> load_dotenv</span>
<span id="annotated-cell-1-5"></span>
<button class="code-annotation-anchor" data-target-cell="annotated-cell-1" data-target-annotation="2">2</button><span id="annotated-cell-1-6" class="code-annotation-target">load_dotenv()</span>
<span id="annotated-cell-1-7"></span>
<button class="code-annotation-anchor" data-target-cell="annotated-cell-1" data-target-annotation="3">3</button><span id="annotated-cell-1-8" class="code-annotation-target">con <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ibis.<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">connect</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"duckdb://penguins.ddb"</span>)</span>
<span id="annotated-cell-1-9">t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ibis.examples.penguins.fetch()</span>
<span id="annotated-cell-1-10">t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> con.create_table(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"penguins"</span>, t.to_pyarrow(), overwrite<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div>
</details>
<div class="cell-annotation">
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-1" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-1" data-code-lines="1,2,4" data-code-annotation="1">Import the libraries we need.</span>
</dd>
<dt data-target-cell="annotated-cell-1" data-target-annotation="2">2</dt>
<dd>
<span data-code-cell="annotated-cell-1" data-code-lines="6" data-code-annotation="2">Load the environment variable to setup Marvin to call our OpenAI account.</span>
</dd>
<dt data-target-cell="annotated-cell-1" data-target-annotation="3">3</dt>
<dd>
<span data-code-cell="annotated-cell-1" data-code-lines="8,9,10" data-code-annotation="3">Setup the demo datain an Ibis backend.</span>
</dd>
</dl>
</div>
</div>
<p>First, we’ll setup Ibis and Marvin with some simple example data:</p>
<div id="297c21b2" class="cell" data-execution_count="2">
<div class="sourceCode cell-code" id="annotated-cell-2" style="background: #f1f3f5;"><pre class="sourceCode python code-annotation-code code-with-copy code-annotated"><code class="sourceCode python"><button class="code-annotation-anchor" data-target-cell="annotated-cell-2" data-target-annotation="1">1</button><span id="annotated-cell-2-1" class="code-annotation-target"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> ibis</span>
<span id="annotated-cell-2-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> marvin</span>
<span id="annotated-cell-2-3"></span>
<span id="annotated-cell-2-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> ibis.expr.schema <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Schema</span>
<span id="annotated-cell-2-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> ibis.expr.types.relations <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Table</span>
<span id="annotated-cell-2-6"></span>
<button class="code-annotation-anchor" data-target-cell="annotated-cell-2" data-target-annotation="2">2</button><span id="annotated-cell-2-7" class="code-annotation-target">ibis.options.interactive <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="annotated-cell-2-8">marvin.settings.llm_model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"openai/gpt-4"</span></span>
<span id="annotated-cell-2-9"></span>
<button class="code-annotation-anchor" data-target-cell="annotated-cell-2" data-target-annotation="3">3</button><span id="annotated-cell-2-10" class="code-annotation-target">con <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ibis.<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">connect</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"duckdb://penguins.ddb"</span>)</span>
<span id="annotated-cell-2-11">t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> con.table(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"penguins"</span>)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div>
<div class="cell-annotation">
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-2" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-2" data-code-lines="1,2,4,5" data-code-annotation="1">Import Ibis and Marvin.</span>
</dd>
<dt data-target-cell="annotated-cell-2" data-target-annotation="2">2</dt>
<dd>
<span data-code-cell="annotated-cell-2" data-code-lines="7,8" data-code-annotation="2">Configure Ibis (interactive) and Marvin (GPT-4).</span>
</dd>
<dt data-target-cell="annotated-cell-2" data-target-annotation="3">3</dt>
<dd>
<span data-code-cell="annotated-cell-2" data-code-lines="10,11" data-code-annotation="3">Connect to the data and load a table into a variable.</span>
</dd>
</dl>
</div>
</div>
</section>
<section id="context" class="level2">
<h2 class="anchored" data-anchor-id="context">Context</h2>
<p>Context is a fancy way of talking about the input to a LLM.</p>
</section>
<section id="calls" class="level2">
<h2 class="anchored" data-anchor-id="calls">Calls</h2>
<p>We make calls with inputs to functions or systems and get outputs. We can think of calling the LLM with our input (context) and getting an output (text).</p>
</section>
<section id="computations" class="level2">
<h2 class="anchored" data-anchor-id="computations">Computations</h2>
<p>A function or system often computes something. We can be pedantic about calls versus computations, but in general the connotation around computations is more time and resource intensive than a call. At the end of the day, they will both take some computer cycles.</p>
</section>
<section id="retrieval-augmented-generation-rag" class="level2">
<h2 class="anchored" data-anchor-id="retrieval-augmented-generation-rag">Retrieval augmented generation (RAG)</h2>
<p>Instead of you typing out context for the bot, we can <strong>retrieve</strong> context from somewhere, <strong>augment</strong> our strings sent to the bot with this context, and then <strong>generate</strong> a response from the bot.</p>
<p>As a contrived example, instead of saying “The capitol of foo is bar”, we can retrieve the capitol of foo from a database, augment it with our context, and then generate a response from the bot. You may notice that <a href="../llms-and-data-pt0">we already did this in the firt post in the series – let’s review that code again</a>:</p>
<div id="1e60f7bd" class="cell" data-execution_count="3">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> ibis.expr.schema <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Schema</span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> ibis.expr.types.relations <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Table</span>
<span id="cb1-3"></span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@marvin.ai_fn</span></span>
<span id="cb1-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">def</span> sql_select(</span>
<span id="cb1-7">    text: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, table_name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> t.get_name(), schema: Schema <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> t.schema()</span>
<span id="cb1-8">) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="cb1-9">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""writes the SQL SELECT statement to query the table according to the text"""</span></span>
<span id="cb1-10"></span>
<span id="cb1-11"></span>
<span id="cb1-12">query <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"the unique combination of species and islands"</span></span>
<span id="cb1-13">sql <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sql_select(query).strip(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">";"</span>)</span>
<span id="cb1-14">sql</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="3">
<pre><code>'SELECT DISTINCT species, island FROM penguins'</code></pre>
</div>
</div>
<p>Notice that we <strong>retrieved</strong> the table name and schema with calls to the Ibis table (<code>t.get_name()</code> and <code>t.schema()</code>). We then <strong>augment</strong> our context (the query in natural language) with this information and <strong>generate</strong> a response from the bot.</p>
<p>This works reasonably well for simple SQL queries:</p>
<div id="07c59dc8" class="cell" data-execution_count="4">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1">t.sql(sql)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="4">
<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace">┏━━━━━━━━━━━┳━━━━━━━━━━━┓
┃<span style="font-weight: bold"> species   </span>┃<span style="font-weight: bold"> island    </span>┃
┡━━━━━━━━━━━╇━━━━━━━━━━━┩
│ <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">string</span>    │ <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">string</span>    │
├───────────┼───────────┤
│ <span style="color: #008000; text-decoration-color: #008000">Adelie   </span> │ <span style="color: #008000; text-decoration-color: #008000">Torgersen</span> │
│ <span style="color: #008000; text-decoration-color: #008000">Adelie   </span> │ <span style="color: #008000; text-decoration-color: #008000">Biscoe   </span> │
│ <span style="color: #008000; text-decoration-color: #008000">Adelie   </span> │ <span style="color: #008000; text-decoration-color: #008000">Dream    </span> │
│ <span style="color: #008000; text-decoration-color: #008000">Gentoo   </span> │ <span style="color: #008000; text-decoration-color: #008000">Biscoe   </span> │
│ <span style="color: #008000; text-decoration-color: #008000">Chinstrap</span> │ <span style="color: #008000; text-decoration-color: #008000">Dream    </span> │
└───────────┴───────────┘
</pre>
</div>
</div>
<p>I would argue in this case there wasn’t any real <strong>computation</strong> done by our <strong>calls</strong> to the Ibis table – we were just retrieving some relatively static metadata – but we could have done some more complex computations (on any of 18+ data platforms).</p>
</section>
<section id="thought-leadership" class="level2">
<h2 class="anchored" data-anchor-id="thought-leadership">Thought leadership</h2>
<p>TODO: human rewrite</p>
<p>In the realm of Generative AI, particularly when working with Language Learning Models (LLMs), understanding the concept of ‘context’ is crucial. Context, in this domain, refers to the inputs that are fed into an LLM, and the corresponding outputs they generate. This post breaks down the complexities of this process into understandable fragments, including retrieval of context, its augmentation, and, thereafter, the generation of a response.</p>
<p>An illustrative example is provided, showcasing a database interaction. It demonstrates how the data retrieved can be used to augment the context before the bot generates a response. This valuable insight underlines the practical application of the theory, reinforcing the understanding of the readers.</p>
<p>We also venture into the difference between simple static metadata retrieval and the more intricate computations. This distinction echoes the breadth and depth of the processes involved in Generative AI.</p>
<p>As we continue to explore and unravel the potential of Generative AI and LLMs, this post serves as a fundamental building block. It creates a pathway for enthusiasts and professionals alike to delve deeper into this exciting field. By breaking down complex concepts into comprehensible segments, it fosters an environment of learning and growth.</p>
<p>This marks just the beginning of our journey into the world of Generative AI. As we dig deeper, we will continue to explore, learn and share with our readers. Stay tuned for more insightful content in this series. [1]</p>
<p>[1] https://github.com/ibis-project/ibis-birdbrain</p>
</section>
<section id="next-steps" class="level2">
<h2 class="anchored" data-anchor-id="next-steps">Next steps</h2>
<p>You can get involved with <a href="https://github.com/ibis-project/ibis-birdbrain">Ibis Birdbrain</a>, our open-source data &amp; AI project for building next-generation natural language interfaces to data.</p>
<p><a href="../llms-and-data-pt3">Read the next post in this series</a>.</p>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>LLMs and data</category>
  <guid>https://ibis-project.github.io/posts/llms-and-data-pt2/</guid>
  <pubDate>Sat, 14 Oct 2023 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Three approaches</title>
  <dc:creator>Cody Peterson</dc:creator>
  <link>https://ibis-project.github.io/posts/llms-and-data-pt1/</link>
  <description><![CDATA[ 





<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>The thought of using natural language to transform and analyze data is appealing. This post assumes familiarity with Marvin and Ibis – <a href="../llms-and-data-pt0">read the previous post in the series for a quick overview</a>.</p>
</section>
<section id="approaches" class="level2">
<h2 class="anchored" data-anchor-id="approaches">Approaches</h2>
<p>When discussed at Voltron Data, we identified three distinct approaches to applying LLMs to data analytics that can be implemented today:</p>
<ol type="1">
<li>LLM writes an analytic code</li>
<li>LLM writes an analytic subroutine</li>
<li>Use LLM in an analytic subroutine</li>
</ol>
<p>While these three approaches are not an exhaustive list of how LLMs can be applied to data, they can be easily understood and implemented with Ibis and Marvin in a few lines of code. Together with these two open-source tools, we can build a natural language interface for data analytics that supports 18+ backends.</p>
<p>But first, let’s demonstrate the three approaches.</p>
<section id="approach-1-llm-writes-analytic-code" class="level3">
<h3 class="anchored" data-anchor-id="approach-1-llm-writes-analytic-code">Approach 1: LLM writes analytic code</h3>
<p>State of the art (SoTA) LLMs are decent at generating SQL out of the box. We can be clever to handle errors, retries, and more, but in its simplest form:</p>
<div id="0f74b507" class="cell" data-execution_count="1">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="annotated-cell-1" style="background: #f1f3f5;"><pre class="sourceCode python code-annotation-code code-with-copy code-annotated"><code class="sourceCode python"><button class="code-annotation-anchor" data-target-cell="annotated-cell-1" data-target-annotation="1">1</button><span id="annotated-cell-1-1" class="code-annotation-target"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> ibis</span>
<span id="annotated-cell-1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> marvin</span>
<span id="annotated-cell-1-3"></span>
<span id="annotated-cell-1-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> rich <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span></span>
<span id="annotated-cell-1-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> time <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> sleep</span>
<span id="annotated-cell-1-6"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> dotenv <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> load_dotenv</span>
<span id="annotated-cell-1-7"></span>
<button class="code-annotation-anchor" data-target-cell="annotated-cell-1" data-target-annotation="2">2</button><span id="annotated-cell-1-8" class="code-annotation-target">load_dotenv()</span>
<span id="annotated-cell-1-9"></span>
<button class="code-annotation-anchor" data-target-cell="annotated-cell-1" data-target-annotation="3">3</button><span id="annotated-cell-1-10" class="code-annotation-target">con <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ibis.<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">connect</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"duckdb://penguins.ddb"</span>)</span>
<span id="annotated-cell-1-11">t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ibis.examples.penguins.fetch()</span>
<span id="annotated-cell-1-12">t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> con.create_table(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"penguins"</span>, t.to_pyarrow(), overwrite<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div>
</details>
<div class="cell-annotation">
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-1" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-1" data-code-lines="1,2,4,5,6" data-code-annotation="1">Import the libraries we need.</span>
</dd>
<dt data-target-cell="annotated-cell-1" data-target-annotation="2">2</dt>
<dd>
<span data-code-cell="annotated-cell-1" data-code-lines="8" data-code-annotation="2">Load the environment variable to setup Marvin to call our OpenAI account.</span>
</dd>
<dt data-target-cell="annotated-cell-1" data-target-annotation="3">3</dt>
<dd>
<span data-code-cell="annotated-cell-1" data-code-lines="10,11,12" data-code-annotation="3">Setup the demo datain an Ibis backend.</span>
</dd>
</dl>
</div>
</div>
<div id="ab13587b" class="cell" data-execution_count="2">
<div class="sourceCode cell-code" id="annotated-cell-2" style="background: #f1f3f5;"><pre class="sourceCode python code-annotation-code code-with-copy code-annotated"><code class="sourceCode python"><button class="code-annotation-anchor" data-target-cell="annotated-cell-2" data-target-annotation="1">1</button><span id="annotated-cell-2-1" class="code-annotation-target"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> ibis</span>
<span id="annotated-cell-2-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> marvin</span>
<span id="annotated-cell-2-3"></span>
<span id="annotated-cell-2-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> ibis.expr.schema <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Schema</span>
<span id="annotated-cell-2-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> ibis.expr.types.relations <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Table</span>
<span id="annotated-cell-2-6"></span>
<span id="annotated-cell-2-7"></span>
<button class="code-annotation-anchor" data-target-cell="annotated-cell-2" data-target-annotation="2">2</button><span id="annotated-cell-2-8" class="code-annotation-target">ibis.options.interactive <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="annotated-cell-2-9">marvin.settings.llm_model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"openai/gpt-4"</span></span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div>
<div class="cell-annotation">
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-2" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-2" data-code-lines="1,2,4,5" data-code-annotation="1">Import Ibis and Marvin.</span>
</dd>
<dt data-target-cell="annotated-cell-2" data-target-annotation="2">2</dt>
<dd>
<span data-code-cell="annotated-cell-2" data-code-lines="8,9" data-code-annotation="2">Configure Ibis and Marvin</span>
</dd>
</dl>
</div>
</div>
<div id="52b31d4e" class="cell" data-execution_count="3">
<div class="sourceCode cell-code" id="annotated-cell-3" style="background: #f1f3f5;"><pre class="sourceCode python code-annotation-code code-with-copy code-annotated"><code class="sourceCode python"><button class="code-annotation-anchor" data-target-cell="annotated-cell-3" data-target-annotation="1">1</button><span id="annotated-cell-3-1" class="code-annotation-target"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@marvin.ai_fn</span></span>
<span id="annotated-cell-3-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">def</span> _generate_sql_select(</span>
<span id="annotated-cell-3-3">    text: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, table_name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, table_schema: Schema</span>
<span id="annotated-cell-3-4">) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="annotated-cell-3-5">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Generate SQL SELECT from text."""</span></span>
<span id="annotated-cell-3-6"></span>
<span id="annotated-cell-3-7"></span>
<button class="code-annotation-anchor" data-target-cell="annotated-cell-3" data-target-annotation="2">2</button><span id="annotated-cell-3-8" class="code-annotation-target"><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">def</span> sql_from_text(text: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, t: Table) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> Table:</span>
<span id="annotated-cell-3-9">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Run SQL from text."""</span></span>
<span id="annotated-cell-3-10">    <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">return</span> t.sql(_generate_sql_select(text, t.get_name(), t.schema()).strip(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">";"</span>))</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div>
<div class="cell-annotation">
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-3" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-3" data-code-lines="1,4,5" data-code-annotation="1">A non-deterministic, LLM-powered AI function.</span>
</dd>
<dt data-target-cell="annotated-cell-3" data-target-annotation="2">2</dt>
<dd>
<span data-code-cell="annotated-cell-3" data-code-lines="8,9,10" data-code-annotation="2">A deterministic, human-authored function that calls the AI function.</span>
</dd>
</dl>
</div>
</div>
<div id="97498020" class="cell" data-execution_count="4">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1">t2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sql_from_text(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"the unique combination of species and islands"</span>, t)</span>
<span id="cb1-2">t2</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="58">
<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace">┏━━━━━━━━━━━┳━━━━━━━━━━━┓
┃<span style="font-weight: bold"> species   </span>┃<span style="font-weight: bold"> island    </span>┃
┡━━━━━━━━━━━╇━━━━━━━━━━━┩
│ <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">string</span>    │ <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">string</span>    │
├───────────┼───────────┤
│ <span style="color: #008000; text-decoration-color: #008000">Adelie   </span> │ <span style="color: #008000; text-decoration-color: #008000">Torgersen</span> │
│ <span style="color: #008000; text-decoration-color: #008000">Adelie   </span> │ <span style="color: #008000; text-decoration-color: #008000">Biscoe   </span> │
│ <span style="color: #008000; text-decoration-color: #008000">Adelie   </span> │ <span style="color: #008000; text-decoration-color: #008000">Dream    </span> │
│ <span style="color: #008000; text-decoration-color: #008000">Gentoo   </span> │ <span style="color: #008000; text-decoration-color: #008000">Biscoe   </span> │
│ <span style="color: #008000; text-decoration-color: #008000">Chinstrap</span> │ <span style="color: #008000; text-decoration-color: #008000">Dream    </span> │
└───────────┴───────────┘
</pre>
</div>
</div>
<div id="742cc41e" class="cell" data-execution_count="5">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="annotated-cell-5" style="background: #f1f3f5;"><pre class="sourceCode python code-annotation-code code-with-copy code-annotated"><code class="sourceCode python"><button class="code-annotation-anchor" data-target-cell="annotated-cell-5" data-target-annotation="1">1</button><span id="annotated-cell-5-1" class="code-annotation-target">sleep(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div>
</details>
<div class="cell-annotation">
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-5" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-5" data-code-lines="1" data-code-annotation="1">Avoid rate-limiting by waiting.</span>
</dd>
</dl>
</div>
</div>
<div id="01f39f85" class="cell" data-execution_count="6">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1">t3 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sql_from_text(</span>
<span id="cb2-2">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"the unique combination of species and islands, with their counts, ordered from highest to lowest, and name that column just 'count'"</span>,</span>
<span id="cb2-3">    t,</span>
<span id="cb2-4">)</span>
<span id="cb2-5">t3</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="60">
<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace">┏━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━┓
┃<span style="font-weight: bold"> species   </span>┃<span style="font-weight: bold"> island    </span>┃<span style="font-weight: bold"> count </span>┃
┡━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━┩
│ <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">string</span>    │ <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">string</span>    │ <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">int64</span> │
├───────────┼───────────┼───────┤
│ <span style="color: #008000; text-decoration-color: #008000">Gentoo   </span> │ <span style="color: #008000; text-decoration-color: #008000">Biscoe   </span> │   <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">124</span> │
│ <span style="color: #008000; text-decoration-color: #008000">Chinstrap</span> │ <span style="color: #008000; text-decoration-color: #008000">Dream    </span> │    <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">68</span> │
│ <span style="color: #008000; text-decoration-color: #008000">Adelie   </span> │ <span style="color: #008000; text-decoration-color: #008000">Dream    </span> │    <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">56</span> │
│ <span style="color: #008000; text-decoration-color: #008000">Adelie   </span> │ <span style="color: #008000; text-decoration-color: #008000">Torgersen</span> │    <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">52</span> │
│ <span style="color: #008000; text-decoration-color: #008000">Adelie   </span> │ <span style="color: #008000; text-decoration-color: #008000">Biscoe   </span> │    <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">44</span> │
└───────────┴───────────┴───────┘
</pre>
</div>
</div>
<div id="d6ed7efe" class="cell" data-execution_count="7">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="annotated-cell-7" style="background: #f1f3f5;"><pre class="sourceCode python code-annotation-code code-with-copy code-annotated"><code class="sourceCode python"><button class="code-annotation-anchor" data-target-cell="annotated-cell-7" data-target-annotation="1">1</button><span id="annotated-cell-7-1" class="code-annotation-target">sleep(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div>
</details>
<div class="cell-annotation">
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-7" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-7" data-code-lines="1" data-code-annotation="1">Avoid rate-limiting by waiting.</span>
</dd>
</dl>
</div>
</div>
<p>This works well-enough for simple cases and can be expanded to handle complex ones. In many scenarios, it may be easier to express a query in English or another language than to write it in SQL, especially if working across multiple SQL dialects.</p>
<p>SQL isn’t standard, with many dialects across data platforms. Ibis works around this by providing a standard Python API for analytic code but must make compromises to support many data platforms, often via SQL in their native dialect. <a href="https://substrait.io">Substrait</a> is a newer project that aims to solve this problem by providing a standard, portable, and extensible intermediary representation (IR) for data transformation code that Ibis and data platforms could all standardize on. Substrait is still in the early stages of development, but it’s worth keeping an eye on and will be adopted in Ibis once supported across many data platforms.</p>
<p>For now, we’ll focus on generating SQL and Python analytical code with LLMs.</p>
</section>
<section id="approach-2-llm-writes-an-analytical-subroutine" class="level3">
<h3 class="anchored" data-anchor-id="approach-2-llm-writes-an-analytical-subroutine">Approach 2: LLM writes an analytical subroutine</h3>
<p>If more complex logic needs to be expressed, SoTA LLMs are also decent at writing Python and a number of other programming languages that are used in analytical subroutines. Many data platforms support user-defined functions (UDFs) in Python or some other language. We’ll stick to scalar Python UDFs via DuckDB to demonstrate the concept:</p>
<div id="2ad7357f" class="cell" data-execution_count="8">
<div class="sourceCode cell-code" id="annotated-cell-8" style="background: #f1f3f5;"><pre class="sourceCode python code-annotation-code code-with-copy code-annotated"><code class="sourceCode python"><button class="code-annotation-anchor" data-target-cell="annotated-cell-8" data-target-annotation="1">1</button><span id="annotated-cell-8-1" class="code-annotation-target"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@marvin.ai_fn</span></span>
<span id="annotated-cell-8-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">def</span> _generate_python_function(text: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="annotated-cell-8-3">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Generate a simple, typed, correct Python function from text."""</span></span>
<span id="annotated-cell-8-4"></span>
<span id="annotated-cell-8-5"></span>
<button class="code-annotation-anchor" data-target-cell="annotated-cell-8" data-target-annotation="2">2</button><span id="annotated-cell-8-6" class="code-annotation-target"><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">def</span> create_udf_from_text(text: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="annotated-cell-8-7">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Create a UDF from text."""</span></span>
<span id="annotated-cell-8-8">    <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">return</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"""</span></span>
<span id="annotated-cell-8-9"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">import ibis</span></span>
<span id="annotated-cell-8-10"></span>
<span id="annotated-cell-8-11"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">@ibis.udf.scalar.python</span></span>
<span id="annotated-cell-8-12"><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>_generate_python_function(text)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span>
<span id="annotated-cell-8-13"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"""</span>.strip()</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div>
<div class="cell-annotation">
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-8" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-8" data-code-lines="1,2,3" data-code-annotation="1">A non-deterministic, LLM-powered AI function.</span>
</dd>
<dt data-target-cell="annotated-cell-8" data-target-annotation="2">2</dt>
<dd>
<span data-code-cell="annotated-cell-8" data-code-lines="6,7,13" data-code-annotation="2">A deterministic, human-authored function that calls the AI function.</span>
</dd>
</dl>
</div>
</div>
<div id="600a8b1e" class="cell" data-execution_count="9">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1">udf <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> create_udf_from_text(</span>
<span id="cb3-2">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"a function named count_vowels that given an input string, returns an int w/ the number of vowels (y_included as a boolean option defaulted to False)"</span></span>
<span id="cb3-3">)</span>
<span id="cb3-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(udf)</span>
<span id="cb3-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">exec</span>(udf)</span></code></pre></div>
<div class="cell-output cell-output-display">
<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace">import ibis

@ibis.udf.scalar.python
def <span style="color: #800080; text-decoration-color: #800080; font-weight: bold">count_vowels</span><span style="font-weight: bold">(</span>input_string: str, y_included: bool = <span style="color: #ff0000; text-decoration-color: #ff0000; font-style: italic">False</span><span style="font-weight: bold">)</span> -&gt; int:
    <span style="color: #008000; text-decoration-color: #008000">"""Given an input string, it returns the number of vowels. y can be included as a vowel."""</span>
    vowels = <span style="color: #008000; text-decoration-color: #008000">'aeiou'</span>
    if y_included:
        vowels += <span style="color: #008000; text-decoration-color: #008000">'y'</span>
    return <span style="color: #800080; text-decoration-color: #800080; font-weight: bold">sum</span><span style="font-weight: bold">(</span><span style="color: #008080; text-decoration-color: #008080; font-weight: bold">1</span> for char in input_string if char in vowels<span style="font-weight: bold">)</span>
</pre>
</div>
</div>
<div id="0190650b" class="cell" data-execution_count="10">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="annotated-cell-10" style="background: #f1f3f5;"><pre class="sourceCode python code-annotation-code code-with-copy code-annotated"><code class="sourceCode python"><button class="code-annotation-anchor" data-target-cell="annotated-cell-10" data-target-annotation="1">1</button><span id="annotated-cell-10-1" class="code-annotation-target">sleep(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div>
</details>
<div class="cell-annotation">
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-10" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-10" data-code-lines="1" data-code-annotation="1">Avoid rate-limiting by waiting.</span>
</dd>
</dl>
</div>
</div>
<div id="db1f79e4" class="cell" data-execution_count="11">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1">t4 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> t3.mutate(</span>
<span id="cb4-2">    species_vowel_count<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>count_vowels(t3.species),</span>
<span id="cb4-3">    island_vowel_count<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>count_vowels(t3.island),</span>
<span id="cb4-4">)</span>
<span id="cb4-5">t4</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="65">
<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace">┏━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━┓
┃<span style="font-weight: bold"> species   </span>┃<span style="font-weight: bold"> island    </span>┃<span style="font-weight: bold"> count </span>┃<span style="font-weight: bold"> species_vowel_count </span>┃<span style="font-weight: bold"> island_vowel_count </span>┃
┡━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━┩
│ <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">string</span>    │ <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">string</span>    │ <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">int64</span> │ <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">int64</span>               │ <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">int64</span>              │
├───────────┼───────────┼───────┼─────────────────────┼────────────────────┤
│ <span style="color: #008000; text-decoration-color: #008000">Gentoo   </span> │ <span style="color: #008000; text-decoration-color: #008000">Biscoe   </span> │   <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">124</span> │                   <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">3</span> │                  <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">3</span> │
│ <span style="color: #008000; text-decoration-color: #008000">Chinstrap</span> │ <span style="color: #008000; text-decoration-color: #008000">Dream    </span> │    <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">68</span> │                   <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">2</span> │                  <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">2</span> │
│ <span style="color: #008000; text-decoration-color: #008000">Adelie   </span> │ <span style="color: #008000; text-decoration-color: #008000">Dream    </span> │    <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">56</span> │                   <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">3</span> │                  <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">2</span> │
│ <span style="color: #008000; text-decoration-color: #008000">Adelie   </span> │ <span style="color: #008000; text-decoration-color: #008000">Torgersen</span> │    <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">52</span> │                   <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">3</span> │                  <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">3</span> │
│ <span style="color: #008000; text-decoration-color: #008000">Adelie   </span> │ <span style="color: #008000; text-decoration-color: #008000">Biscoe   </span> │    <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">44</span> │                   <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">3</span> │                  <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">3</span> │
└───────────┴───────────┴───────┴─────────────────────┴────────────────────┘
</pre>
</div>
</div>
<div id="a229f402" class="cell" data-execution_count="12">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="annotated-cell-12" style="background: #f1f3f5;"><pre class="sourceCode python code-annotation-code code-with-copy code-annotated"><code class="sourceCode python"><button class="code-annotation-anchor" data-target-cell="annotated-cell-12" data-target-annotation="1">1</button><span id="annotated-cell-12-1" class="code-annotation-target">sleep(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div>
</details>
<div class="cell-annotation">
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-12" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-12" data-code-lines="1" data-code-annotation="1">Avoid rate-limiting by waiting.</span>
</dd>
</dl>
</div>
</div>
<p>In this case, there’s no reason not to have a human in the loop reviewing the output code and committing it for production use. This could be useful for quick prototyping or, given a box of tools in the form of UDFs, working through a natural language interface.</p>
</section>
<section id="approach-3-use-llm-in-an-analytical-subroutine" class="level3">
<h3 class="anchored" data-anchor-id="approach-3-use-llm-in-an-analytical-subroutine">Approach 3: Use LLM in an analytical subroutine</h3>
<p>We can also call the LLM once-per-row in the table via a subroutine. For variety, we’ll use an <a href="https://www.askmarvin.ai/components/ai_model/">AI model</a> instead of an <a href="https://www.askmarvin.ai/components/ai_function/">AI function</a>:</p>
<div id="2909462e" class="cell" data-execution_count="13">
<div class="sourceCode cell-code" id="annotated-cell-13" style="background: #f1f3f5;"><pre class="sourceCode python code-annotation-code code-with-copy code-annotated"><code class="sourceCode python"><button class="code-annotation-anchor" data-target-cell="annotated-cell-13" data-target-annotation="1">1</button><span id="annotated-cell-13-1" class="code-annotation-target"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> pydantic <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> BaseModel, Field</span>
<span id="annotated-cell-13-2"></span>
<span id="annotated-cell-13-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># decrease cost</span></span>
<button class="code-annotation-anchor" data-target-cell="annotated-cell-13" data-target-annotation="2">2</button><span id="annotated-cell-13-4" class="code-annotation-target">marvin.settings.llm_model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"openai/gpt-3.5-turbo-16k"</span></span>
<span id="annotated-cell-13-5"></span>
<span id="annotated-cell-13-6"></span>
<button class="code-annotation-anchor" data-target-cell="annotated-cell-13" data-target-annotation="3">3</button><span id="annotated-cell-13-7" class="code-annotation-target"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@marvin.ai_model</span></span>
<span id="annotated-cell-13-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">class</span> VowelCounter(BaseModel):</span>
<span id="annotated-cell-13-9">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Count vowels in a string."""</span></span>
<span id="annotated-cell-13-10"></span>
<span id="annotated-cell-13-11">    include_y: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">bool</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Field(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>, description<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Include 'y' as a vowel."</span>)</span>
<span id="annotated-cell-13-12">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># num_a: int = Field(..., description="The number of 'a' vowels.")</span></span>
<span id="annotated-cell-13-13">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># num_e: int = Field(..., description="The number of 'e' vowels.")</span></span>
<span id="annotated-cell-13-14">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># num_i: int = Field(..., description="The number of 'i' vowels.")</span></span>
<span id="annotated-cell-13-15">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># num_o: int = Field(..., description="The number of 'o' vowels.")</span></span>
<span id="annotated-cell-13-16">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># num_u: int = Field(..., description="The number of 'u' vowels.")</span></span>
<span id="annotated-cell-13-17">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># num_y: int = Field(..., description="The number of 'y' vowels.")</span></span>
<span id="annotated-cell-13-18">    num_total: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Field(..., description<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The total number of vowels."</span>)</span>
<span id="annotated-cell-13-19"></span>
<span id="annotated-cell-13-20"></span>
<button class="code-annotation-anchor" data-target-cell="annotated-cell-13" data-target-annotation="4">4</button><span id="annotated-cell-13-21" class="code-annotation-target">VowelCounter(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hello world"</span>)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div>
<div class="cell-annotation">
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-13" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-13" data-code-lines="1" data-code-annotation="1">Additional imports for Pydantic.</span>
</dd>
<dt data-target-cell="annotated-cell-13" data-target-annotation="2">2</dt>
<dd>
<span data-code-cell="annotated-cell-13" data-code-lines="4" data-code-annotation="2">Configure Marvin to use a cheaper model.</span>
</dd>
<dt data-target-cell="annotated-cell-13" data-target-annotation="3">3</dt>
<dd>
<span data-code-cell="annotated-cell-13" data-code-lines="7,8,9,11,12,13,14,15,16,17,18" data-code-annotation="3">A non-deterministic, LLM-powered AI model.</span>
</dd>
<dt data-target-cell="annotated-cell-13" data-target-annotation="4">4</dt>
<dd>
<span data-code-cell="annotated-cell-13" data-code-lines="21" data-code-annotation="4">Call the AI model on some text.</span>
</dd>
</dl>
</div>
<div class="cell-output cell-output-display" data-execution_count="67">
<pre><code>VowelCounter(include_y=False, num_total=3)</code></pre>
</div>
</div>
<p>Then we’ll have the LLM write the UDF that calls the LLM, just to be fancy:</p>
<div id="7261ca98" class="cell" data-execution_count="14">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1">udf <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> create_udf_from_text(</span>
<span id="cb6-2">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"a function named count_vowels_ai that given an input string, calls VowelCounter on it and returns the num_total attribute of that result"</span></span>
<span id="cb6-3">)</span>
<span id="cb6-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(udf)</span>
<span id="cb6-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">exec</span>(udf)</span></code></pre></div>
<div class="cell-output cell-output-display">
<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace">import ibis

@ibis.udf.scalar.python
def <span style="color: #800080; text-decoration-color: #800080; font-weight: bold">count_vowels_ai</span><span style="font-weight: bold">(</span>input_string: str<span style="font-weight: bold">)</span> -&gt; int:
    result = <span style="color: #800080; text-decoration-color: #800080; font-weight: bold">VowelCounter</span><span style="font-weight: bold">(</span>input_string<span style="font-weight: bold">)</span>
    return result.num_total
</pre>
</div>
</div>
<div id="8f30fb74" class="cell" data-execution_count="15">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="annotated-cell-15" style="background: #f1f3f5;"><pre class="sourceCode python code-annotation-code code-with-copy code-annotated"><code class="sourceCode python"><button class="code-annotation-anchor" data-target-cell="annotated-cell-15" data-target-annotation="1">1</button><span id="annotated-cell-15-1" class="code-annotation-target">sleep(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div>
</details>
<div class="cell-annotation">
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-15" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-15" data-code-lines="1" data-code-annotation="1">Avoid rate-limiting by waiting.</span>
</dd>
</dl>
</div>
</div>
<div id="faa9c777" class="cell" data-execution_count="16">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1">t5 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> t3.mutate(</span>
<span id="cb7-2">    species_vowel_count<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>count_vowels_ai(t3.species),</span>
<span id="cb7-3">    island_vowel_count<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>count_vowels_ai(t3.island),</span>
<span id="cb7-4">)</span>
<span id="cb7-5">t5</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="70">
<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace">┏━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━┓
┃<span style="font-weight: bold"> species   </span>┃<span style="font-weight: bold"> island    </span>┃<span style="font-weight: bold"> count </span>┃<span style="font-weight: bold"> species_vowel_count </span>┃<span style="font-weight: bold"> island_vowel_count </span>┃
┡━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━┩
│ <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">string</span>    │ <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">string</span>    │ <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">int64</span> │ <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">int64</span>               │ <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">int64</span>              │
├───────────┼───────────┼───────┼─────────────────────┼────────────────────┤
│ <span style="color: #008000; text-decoration-color: #008000">Gentoo   </span> │ <span style="color: #008000; text-decoration-color: #008000">Biscoe   </span> │   <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">124</span> │                   <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">3</span> │                  <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">3</span> │
│ <span style="color: #008000; text-decoration-color: #008000">Chinstrap</span> │ <span style="color: #008000; text-decoration-color: #008000">Dream    </span> │    <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">68</span> │                   <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">2</span> │                  <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">1</span> │
│ <span style="color: #008000; text-decoration-color: #008000">Adelie   </span> │ <span style="color: #008000; text-decoration-color: #008000">Dream    </span> │    <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">56</span> │                   <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">3</span> │                  <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">2</span> │
│ <span style="color: #008000; text-decoration-color: #008000">Adelie   </span> │ <span style="color: #008000; text-decoration-color: #008000">Torgersen</span> │    <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">52</span> │                   <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">3</span> │                  <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">3</span> │
│ <span style="color: #008000; text-decoration-color: #008000">Adelie   </span> │ <span style="color: #008000; text-decoration-color: #008000">Biscoe   </span> │    <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">44</span> │                   <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">3</span> │                  <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">3</span> │
└───────────┴───────────┴───────┴─────────────────────┴────────────────────┘
</pre>
</div>
</div>
<p>Notice that in this UDF, unlike in the previous example, a LLM is being called (possibly several times) for each row in the table. This is a very expensive operation and we’ll need to be careful about how we use it in practice.</p>
<div id="c40711f7" class="cell" data-execution_count="17">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="annotated-cell-17" style="background: #f1f3f5;"><pre class="sourceCode python code-annotation-code code-with-copy code-annotated"><code class="sourceCode python"><button class="code-annotation-anchor" data-target-cell="annotated-cell-17" data-target-annotation="1">1</button><span id="annotated-cell-17-1" class="code-annotation-target">sleep(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div>
</details>
<div class="cell-annotation">
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-17" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-17" data-code-lines="1" data-code-annotation="1">Avoid rate-limiting by waiting.</span>
</dd>
</dl>
</div>
</div>
</section>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<p>To summarize this post:</p>
<div id="3d6313ea" class="cell" data-execution_count="18">
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> rich <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span></span>
<span id="cb8-2"></span>
<span id="cb8-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">with</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"index.qmd"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r"</span>) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> f:</span>
<span id="cb8-4">    self_text <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> f.read()</span>
<span id="cb8-5"></span>
<span id="cb8-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># increease accuracy</span></span>
<span id="cb8-7">marvin.settings.llm_model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"openai/gpt-4"</span></span>
<span id="cb8-8"></span>
<span id="cb8-9"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@marvin.ai_model</span></span>
<span id="cb8-10"><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">class</span> Summary(BaseModel):</span>
<span id="cb8-11">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Summary of text."""</span></span>
<span id="cb8-12"></span>
<span id="cb8-13">    summary_line: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Field(..., description<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The one-line summary of the text."</span>)</span>
<span id="cb8-14">    summary_paragraph: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Field(</span>
<span id="cb8-15">        ..., description<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The one-paragraph summary of the text."</span></span>
<span id="cb8-16">    )</span>
<span id="cb8-17">    conclusion: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Field(</span>
<span id="cb8-18">        ..., description<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The conclusion the reader should draw from the text."</span></span>
<span id="cb8-19">    )</span>
<span id="cb8-20">    key_points: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Field(..., description<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The key points of the text."</span>)</span>
<span id="cb8-21">    critiques: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Field(</span>
<span id="cb8-22">        ..., description<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Professional, fair critiques of the text."</span></span>
<span id="cb8-23">    )</span>
<span id="cb8-24">    suggested_improvements: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Field(</span>
<span id="cb8-25">        ..., description<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Suggested improvements for the text."</span></span>
<span id="cb8-26">    )</span>
<span id="cb8-27">    sentiment: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Field(..., description<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The sentiment of the text."</span>)</span>
<span id="cb8-28">    sentiment_label: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Field(..., description<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The sentiment label of the text."</span>)</span>
<span id="cb8-29">    author_bias: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Field(..., description<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The author bias of the text."</span>)</span>
<span id="cb8-30"></span>
<span id="cb8-31"></span>
<span id="cb8-32"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(Summary(self_text))</span></code></pre></div>
<div class="cell-output cell-output-display">
<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace"><span style="color: #800080; text-decoration-color: #800080; font-weight: bold">Summary</span><span style="font-weight: bold">(</span>
    <span style="color: #808000; text-decoration-color: #808000">summary_line</span>=<span style="color: #008000; text-decoration-color: #008000">"The blog post titled 'Three approaches' by Cody Peterson, dated 2023-10-13, discusses three </span>
<span style="color: #008000; text-decoration-color: #008000">distinct approaches to applying Language Learning Models (LLMs) to data analytics using open-source tools Ibis and </span>
<span style="color: #008000; text-decoration-color: #008000">Marvin."</span>,
    <span style="color: #808000; text-decoration-color: #808000">summary_paragraph</span>=<span style="color: #008000; text-decoration-color: #008000">'The author explains three approaches to applying LLMs to data analytics. The first approach </span>
<span style="color: #008000; text-decoration-color: #008000">is letting the LLM write an analytic code, specifically generating SQL. The second approach involves letting LLM </span>
<span style="color: #008000; text-decoration-color: #008000">write an analytic subroutine, demonstrated by writing Python code for user-defined functions. The third approach </span>
<span style="color: #008000; text-decoration-color: #008000">uses LLM in an analytic subroutine, illustrated by calling the LLM once-per-row in a table via a subroutine. Each </span>
<span style="color: #008000; text-decoration-color: #008000">method has potential applications and limitations, and the choice of approach will depend on the specific </span>
<span style="color: #008000; text-decoration-color: #008000">requirements of the data analytics task.'</span>,
    <span style="color: #808000; text-decoration-color: #808000">conclusion</span>=<span style="color: #008000; text-decoration-color: #008000">'LLMs can be used to transform and analyze data in various ways. By using these approaches in </span>
<span style="color: #008000; text-decoration-color: #008000">combination with open-source tools like Ibis and Marvin, it is possible to build a natural language interface for </span>
<span style="color: #008000; text-decoration-color: #008000">data analytics that supports multiple backends. The author concludes by introducing an open-source data &amp; AI </span>
<span style="color: #008000; text-decoration-color: #008000">project for building next-generation natural language interfaces to data, Ibis Birdbrain.'</span>,
    <span style="color: #808000; text-decoration-color: #808000">key_points</span>=<span style="font-weight: bold">[</span>
        <span style="color: #008000; text-decoration-color: #008000">'The LLM can write an analytic code, particularly generating SQL.'</span>,
        <span style="color: #008000; text-decoration-color: #008000">'The LLM can also write an analytic subroutine, exemplified by writing Python code for user-defined </span>
<span style="color: #008000; text-decoration-color: #008000">functions.'</span>,
        <span style="color: #008000; text-decoration-color: #008000">'The LLM can be used in an analytic subroutine, shown by calling the LLM once-per-row in a table via a </span>
<span style="color: #008000; text-decoration-color: #008000">subroutine.'</span>,
        <span style="color: #008000; text-decoration-color: #008000">'The author suggests that each approach has its potential applications and limitations.'</span>,
        <span style="color: #008000; text-decoration-color: #008000">'The choice of approach will depend on the specific requirements of the data analytics task.'</span>
    <span style="font-weight: bold">]</span>,
    <span style="color: #808000; text-decoration-color: #808000">critiques</span>=<span style="font-weight: bold">[</span>
        <span style="color: #008000; text-decoration-color: #008000">'The author could have provided more concrete examples or case studies to illustrate the application of </span>
<span style="color: #008000; text-decoration-color: #008000">these approaches.'</span>,
        <span style="color: #008000; text-decoration-color: #008000">'A comparison of the advantages and disadvantages of each approach would have been helpful.'</span>
    <span style="font-weight: bold">]</span>,
    <span style="color: #808000; text-decoration-color: #808000">suggested_improvements</span>=<span style="font-weight: bold">[</span>
        <span style="color: #008000; text-decoration-color: #008000">'The author could include real-world applications or examples of each approach.'</span>,
        <span style="color: #008000; text-decoration-color: #008000">'A detailed comparison of the pros and cons of each approach would provide better guidance for readers.'</span>
    <span style="font-weight: bold">]</span>,
    <span style="color: #808000; text-decoration-color: #808000">sentiment</span>=<span style="color: #008080; text-decoration-color: #008080; font-weight: bold">0.25</span>,
    <span style="color: #808000; text-decoration-color: #808000">sentiment_label</span>=<span style="color: #008000; text-decoration-color: #008000">'Neutral'</span>,
    <span style="color: #808000; text-decoration-color: #808000">author_bias</span>=<span style="color: #008000; text-decoration-color: #008000">'The author shows a positive bias towards the use of open-source tools, Ibis and Marvin, for data </span>
<span style="color: #008000; text-decoration-color: #008000">analytics.'</span>
<span style="font-weight: bold">)</span>
</pre>
</div>
</div>
</section>
<section id="next-steps" class="level2">
<h2 class="anchored" data-anchor-id="next-steps">Next steps</h2>
<p>You can get involved with <a href="https://github.com/ibis-project/ibis-birdbrain">Ibis Birdbrain</a>, our open-source data &amp; AI project for building next-generation natural language interfaces to data.</p>
<p><a href="../llms-and-data-pt2">Read the next post in this series</a>.</p>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>LLMs and data</category>
  <guid>https://ibis-project.github.io/posts/llms-and-data-pt1/</guid>
  <pubDate>Fri, 13 Oct 2023 00:00:00 GMT</pubDate>
</item>
<item>
  <title>An introduction to Marvin and Ibis</title>
  <dc:creator>Cody Peterson</dc:creator>
  <link>https://ibis-project.github.io/posts/llms-and-data-pt0/</link>
  <description><![CDATA[ 





<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>In this “LLMs and data” series, we’ll explore how to apply large-language models (LLMs) to data analytics. We’ll walk through the steps to build Ibis Birdbrain.</p>
<p>Throughout the series, we’ll be using <a href="https://www.askmarvin.ai/welcome/overview/">Marvin</a> and <a href="https://ibis-project.org">Ibis</a>. A brief introduction to each is provided below.</p>
</section>
<section id="marvin" class="level2">
<h2 class="anchored" data-anchor-id="marvin">Marvin</h2>
<p><a href="https://www.askmarvin.ai/welcome/overview/">Marvin</a> is an AI engineering framework that makes it easy to build up to an interactive conversational application.</p>
<p>Marvin makes calls to an AI platform. You typically use an API key set as an environment variable – in this case, we’ll load a <code>.env</code> file that contians secrets for the AI platform that Marvin will use. We also set the large language model model.</p>
<div id="8ce530f0" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="annotated-cell-1" style="background: #f1f3f5;"><pre class="sourceCode python code-annotation-code code-with-copy code-annotated"><code class="sourceCode python"><button class="code-annotation-anchor" data-target-cell="annotated-cell-1" data-target-annotation="1">1</button><span id="annotated-cell-1-1" class="code-annotation-target"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> marvin</span>
<span id="annotated-cell-1-2"></span>
<span id="annotated-cell-1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> rich <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span></span>
<span id="annotated-cell-1-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> time <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> sleep</span>
<span id="annotated-cell-1-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> dotenv <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> load_dotenv</span>
<span id="annotated-cell-1-6"></span>
<button class="code-annotation-anchor" data-target-cell="annotated-cell-1" data-target-annotation="2">2</button><span id="annotated-cell-1-7" class="code-annotation-target">load_dotenv()</span>
<span id="annotated-cell-1-8"></span>
<span id="annotated-cell-1-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># increase accuracy</span></span>
<button class="code-annotation-anchor" data-target-cell="annotated-cell-1" data-target-annotation="3">3</button><span id="annotated-cell-1-10" class="code-annotation-target">marvin.settings.llm_model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"openai/gpt-4"</span></span>
<span id="annotated-cell-1-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># decrease cost</span></span>
<span id="annotated-cell-1-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># marvin.settings.llm_model = "openai/gpt-3.5-turbo"</span></span>
<span id="annotated-cell-1-13"></span>
<button class="code-annotation-anchor" data-target-cell="annotated-cell-1" data-target-annotation="4">4</button><span id="annotated-cell-1-14" class="code-annotation-target">test_str <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"working with data and LLMs on 18+ data platforms is easy!"</span></span>
<span id="annotated-cell-1-15">test_str</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div>
<div class="cell-annotation">
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-1" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-1" data-code-lines="1,3,4,5" data-code-annotation="1">Import the libraries we need.</span>
</dd>
<dt data-target-cell="annotated-cell-1" data-target-annotation="2">2</dt>
<dd>
<span data-code-cell="annotated-cell-1" data-code-lines="7" data-code-annotation="2">Load the environment variable to setup Marvin to call our OpenAI account.</span>
</dd>
<dt data-target-cell="annotated-cell-1" data-target-annotation="3">3</dt>
<dd>
<span data-code-cell="annotated-cell-1" data-code-lines="10,12" data-code-annotation="3">Configure the LLM model to use.</span>
</dd>
<dt data-target-cell="annotated-cell-1" data-target-annotation="4">4</dt>
<dd>
<span data-code-cell="annotated-cell-1" data-code-lines="14" data-code-annotation="4">Some text to test on</span>
</dd>
</dl>
</div>
<div class="cell-output cell-output-display" data-execution_count="45">
<pre><code>'working with data and LLMs on 18+ data platforms is easy!'</code></pre>
</div>
</div>
<section id="functions" class="level3">
<h3 class="anchored" data-anchor-id="functions">Functions</h3>
<p>AI functions are one of the building blocks in Marvin and allow yout to specify a typed python function with no code – only a docstring – to achieve a wide variety of tasks.</p>
<p>We’ll demonstrate this with an AI function that trnaslates text:</p>
<div id="b34d1977" class="cell" data-execution_count="2">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@marvin.ai_fn</span></span>
<span id="cb2-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">def</span> translate(text: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, from_: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"English"</span>, to: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Spanish"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="cb2-3">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""translates the text"""</span></span>
<span id="cb2-4"></span>
<span id="cb2-5">translate(test_str)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="46">
<pre><code>'trabajar con datos y LLMs en más de 18 plataformas de datos es fácil!'</code></pre>
</div>
</div>
<div id="980eaf72" class="cell" data-execution_count="3">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="annotated-cell-3" style="background: #f1f3f5;"><pre class="sourceCode python code-annotation-code code-with-copy code-annotated"><code class="sourceCode python"><button class="code-annotation-anchor" data-target-cell="annotated-cell-3" data-target-annotation="1">1</button><span id="annotated-cell-3-1" class="code-annotation-target">sleep(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div>
</details>
<div class="cell-annotation">
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-3" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-3" data-code-lines="1" data-code-annotation="1">Avoid rate-limiting by waiting.</span>
</dd>
</dl>
</div>
</div>
<div id="d7405aa6" class="cell" data-execution_count="4">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1">translate(translate(test_str), from_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Spanish"</span>, to<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"English"</span>)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="48">
<pre><code>'Working with data and LLMs on more than 18 data platforms is easy!'</code></pre>
</div>
</div>
<div id="a5a9b927" class="cell" data-execution_count="5">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="annotated-cell-5" style="background: #f1f3f5;"><pre class="sourceCode python code-annotation-code code-with-copy code-annotated"><code class="sourceCode python"><button class="code-annotation-anchor" data-target-cell="annotated-cell-5" data-target-annotation="1">1</button><span id="annotated-cell-5-1" class="code-annotation-target">sleep(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div>
</details>
<div class="cell-annotation">
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-5" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-5" data-code-lines="1" data-code-annotation="1">Avoid rate-limiting by waiting.</span>
</dd>
</dl>
</div>
</div>
</section>
<section id="models" class="level3">
<h3 class="anchored" data-anchor-id="models">Models</h3>
<p>AI models are another building block for generating python classes from input text. It’s a great way to build structured data from unstructured data that can be customized for your needs.</p>
<p>We’ll demosntrate this with an AI model that translates text:</p>
<div id="f5796802" class="cell" data-execution_count="6">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> pydantic <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> BaseModel, Field</span>
<span id="cb6-2"></span>
<span id="cb6-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># decrease cost</span></span>
<span id="cb6-4">marvin.settings.llm_model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"openai/gpt-3.5-turbo"</span></span>
<span id="cb6-5"></span>
<span id="cb6-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@marvin.ai_model</span></span>
<span id="cb6-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">class</span> ExtractParts(BaseModel):</span>
<span id="cb6-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Extracts parts of a sentence"""</span></span>
<span id="cb6-9">    subject: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Field(..., description<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The subject of the sentence."</span>)</span>
<span id="cb6-10">    objects: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Field(..., description<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The objects of the sentence."</span>)</span>
<span id="cb6-11">    predicate: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Field(..., description<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The predicate of the sentence."</span>)</span>
<span id="cb6-12">    modifiers: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>[<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Field(..., description<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The modifiers of the sentence."</span>)</span>
<span id="cb6-13"></span>
<span id="cb6-14">ExtractParts(test_str)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="50">
<pre><code>ExtractParts(subject='working', objects=['data', 'LLMs'], predicate='is', modifiers=['on 18+ data platforms', 'easy'])</code></pre>
</div>
</div>
<div id="43107868" class="cell" data-execution_count="7">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="annotated-cell-7" style="background: #f1f3f5;"><pre class="sourceCode python code-annotation-code code-with-copy code-annotated"><code class="sourceCode python"><button class="code-annotation-anchor" data-target-cell="annotated-cell-7" data-target-annotation="1">1</button><span id="annotated-cell-7-1" class="code-annotation-target">sleep(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div>
</details>
<div class="cell-annotation">
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-7" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-7" data-code-lines="1" data-code-annotation="1">Avoid rate-limiting by waiting.</span>
</dd>
</dl>
</div>
</div>
</section>
<section id="classifiers" class="level3">
<h3 class="anchored" data-anchor-id="classifiers">Classifiers</h3>
<p>AI classifiers are another building block for generating python classes from input text. It’s the most efficient (time and cost) method for applying LLMs as it only results in a single output token, selecting an output in a specified Enum.</p>
<p>We’ll demonstrate this by classifying the language of some text:</p>
<div id="51061ea8" class="cell" data-execution_count="8">
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> enum <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Enum</span>
<span id="cb8-2"></span>
<span id="cb8-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># increase accuracy</span></span>
<span id="cb8-4">marvin.settings.llm_model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"openai/gpt-4"</span></span>
<span id="cb8-5"></span>
<span id="cb8-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@marvin.ai_classifier</span></span>
<span id="cb8-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">class</span> IdentifyLanguage(Enum):</span>
<span id="cb8-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Identifies the language of the text"""</span></span>
<span id="cb8-9"></span>
<span id="cb8-10">    english <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"English"</span></span>
<span id="cb8-11">    spanish <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Spanish"</span></span>
<span id="cb8-12"></span>
<span id="cb8-13"></span>
<span id="cb8-14">IdentifyLanguage(test_str).value</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="52">
<pre><code>'English'</code></pre>
</div>
</div>
<div id="ac081563" class="cell" data-execution_count="9">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="annotated-cell-9" style="background: #f1f3f5;"><pre class="sourceCode python code-annotation-code code-with-copy code-annotated"><code class="sourceCode python"><button class="code-annotation-anchor" data-target-cell="annotated-cell-9" data-target-annotation="1">1</button><span id="annotated-cell-9-1" class="code-annotation-target">sleep(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div>
</details>
<div class="cell-annotation">
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-9" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-9" data-code-lines="1" data-code-annotation="1">Avoid rate-limiting by waiting.</span>
</dd>
</dl>
</div>
</div>
<div id="5f0c8ea7" class="cell" data-execution_count="10">
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1">IdentifyLanguage(translate(test_str)).value</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="54">
<pre><code>'Spanish'</code></pre>
</div>
</div>
<div id="badd93ea" class="cell" data-execution_count="11">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="annotated-cell-11" style="background: #f1f3f5;"><pre class="sourceCode python code-annotation-code code-with-copy code-annotated"><code class="sourceCode python"><button class="code-annotation-anchor" data-target-cell="annotated-cell-11" data-target-annotation="1">1</button><span id="annotated-cell-11-1" class="code-annotation-target">sleep(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div>
</details>
<div class="cell-annotation">
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-11" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-11" data-code-lines="1" data-code-annotation="1">Avoid rate-limiting by waiting.</span>
</dd>
</dl>
</div>
</div>
</section>
</section>
<section id="ibis" class="level2">
<h2 class="anchored" data-anchor-id="ibis">Ibis</h2>
<p><a href="https://ibis-project.org">Ibis</a> is the portable Python dataframe library that enables Ibis Birdbrain to work on many data platforms at native scale.</p>
<p>Ibis makes calls to a data platform, providing an API but pushing the compute to (local or remote) query engines and storage. DuckDB is the default and we’ll typically use it for demo puroses. You can work with an in-memory instance, but we’ll often create a database file from example data:</p>
<div id="98caa12e" class="cell" data-execution_count="12">
<div class="sourceCode cell-code" id="annotated-cell-12" style="background: #f1f3f5;"><pre class="sourceCode python code-annotation-code code-with-copy code-annotated"><code class="sourceCode python"><button class="code-annotation-anchor" data-target-cell="annotated-cell-12" data-target-annotation="1">1</button><span id="annotated-cell-12-1" class="code-annotation-target"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> ibis</span>
<span id="annotated-cell-12-2"></span>
<button class="code-annotation-anchor" data-target-cell="annotated-cell-12" data-target-annotation="2">2</button><span id="annotated-cell-12-3" class="code-annotation-target">con <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ibis.<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">connect</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"duckdb://penguins.ddb"</span>)</span>
<span id="annotated-cell-12-4">t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ibis.examples.penguins.fetch()</span>
<span id="annotated-cell-12-5">t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> con.create_table(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"penguins"</span>, t.to_pyarrow(), overwrite<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div>
<div class="cell-annotation">
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-12" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-12" data-code-lines="1" data-code-annotation="1">Import the libraries we need.</span>
</dd>
<dt data-target-cell="annotated-cell-12" data-target-annotation="2">2</dt>
<dd>
<span data-code-cell="annotated-cell-12" data-code-lines="3,4,5" data-code-annotation="2">Setup the demo datain an Ibis backend.</span>
</dd>
</dl>
</div>
</div>
<p>You will typically connect to an existing data platform via your corresponding Ibis backend and have access to a number of tables:</p>
<div id="b203ed48" class="cell" data-execution_count="13">
<div class="sourceCode cell-code" id="annotated-cell-13" style="background: #f1f3f5;"><pre class="sourceCode python code-annotation-code code-with-copy code-annotated"><code class="sourceCode python"><button class="code-annotation-anchor" data-target-cell="annotated-cell-13" data-target-annotation="1">1</button><span id="annotated-cell-13-1" class="code-annotation-target"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> ibis</span>
<span id="annotated-cell-13-2"></span>
<button class="code-annotation-anchor" data-target-cell="annotated-cell-13" data-target-annotation="2">2</button><span id="annotated-cell-13-3" class="code-annotation-target">ibis.options.interactive <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="annotated-cell-13-4"></span>
<button class="code-annotation-anchor" data-target-cell="annotated-cell-13" data-target-annotation="3">3</button><span id="annotated-cell-13-5" class="code-annotation-target">con <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ibis.<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">connect</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"duckdb://penguins.ddb"</span>)</span>
<span id="annotated-cell-13-6">t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> con.table(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"penguins"</span>)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div>
<div class="cell-annotation">
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-13" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-13" data-code-lines="1" data-code-annotation="1">Import Ibis.</span>
</dd>
<dt data-target-cell="annotated-cell-13" data-target-annotation="2">2</dt>
<dd>
<span data-code-cell="annotated-cell-13" data-code-lines="3" data-code-annotation="2">Configure Ibis (interactive).</span>
</dd>
<dt data-target-cell="annotated-cell-13" data-target-annotation="3">3</dt>
<dd>
<span data-code-cell="annotated-cell-13" data-code-lines="5,6" data-code-annotation="3">Connect to the data and load a table into a variable.</span>
</dd>
</dl>
</div>
</div>
<section id="backend" class="level3">
<h3 class="anchored" data-anchor-id="backend">Backend</h3>
<p>A backend provides the connection and basic management of the data platform. Above, we created the <code>con</code> variable that is an instance of a DuckDB backend:</p>
<div id="dbdab3d4" class="cell" data-execution_count="14">
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1">con</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="58">
<pre><code>&lt;ibis.backends.duckdb.Backend at 0x16a17af10&gt;</code></pre>
</div>
</div>
<p>It usually contains some tables:</p>
<div id="38db1276" class="cell" data-execution_count="15">
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb14-1">con.list_tables()</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="59">
<pre><code>['penguins']</code></pre>
</div>
</div>
<p>We can access some internals of Ibis to see what backends are available:</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p>Don’t rely on accessing internals of Ibis in production.</p>
</div>
</div>
<div id="cc9730d9" class="cell" data-execution_count="16">
<div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb16-1">backends <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [entrypoint.name <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">for</span> entrypoint <span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">in</span> ibis.util.backend_entry_points()]</span>
<span id="cb16-2">backends</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="60">
<pre><code>['bigquery',
 'clickhouse',
 'dask',
 'datafusion',
 'druid',
 'duckdb',
 'flink',
 'impala',
 'mssql',
 'mysql',
 'oracle',
 'pandas',
 'polars',
 'postgres',
 'pyspark',
 'snowflake',
 'sqlite',
 'trino']</code></pre>
</div>
</div>
</section>
<section id="table" class="level3">
<h3 class="anchored" data-anchor-id="table">Table</h3>
<p>You typically work with a table, conventionally named <code>t</code> for demo or exploratory purposes:</p>
<div id="4b48beba" class="cell" data-execution_count="17">
<div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb18-1">t</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="61">
<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace">┏━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━┓
┃<span style="font-weight: bold"> species </span>┃<span style="font-weight: bold"> island    </span>┃<span style="font-weight: bold"> bill_length_mm </span>┃<span style="font-weight: bold"> bill_depth_mm </span>┃<span style="font-weight: bold"> flipper_length_mm </span>┃<span style="font-weight: bold"> body_mass_g </span>┃<span style="font-weight: bold"> sex    </span>┃<span style="font-weight: bold"> year  </span>┃
┡━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━┩
│ <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">string</span>  │ <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">string</span>    │ <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">float64</span>        │ <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">float64</span>       │ <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">int64</span>             │ <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">int64</span>       │ <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">string</span> │ <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">int64</span> │
├─────────┼───────────┼────────────────┼───────────────┼───────────────────┼─────────────┼────────┼───────┤
│ <span style="color: #008000; text-decoration-color: #008000">Adelie </span> │ <span style="color: #008000; text-decoration-color: #008000">Torgersen</span> │           <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">39.1</span> │          <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">18.7</span> │               <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">181</span> │        <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">3750</span> │ <span style="color: #008000; text-decoration-color: #008000">male  </span> │  <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">2007</span> │
│ <span style="color: #008000; text-decoration-color: #008000">Adelie </span> │ <span style="color: #008000; text-decoration-color: #008000">Torgersen</span> │           <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">39.5</span> │          <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">17.4</span> │               <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">186</span> │        <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">3800</span> │ <span style="color: #008000; text-decoration-color: #008000">female</span> │  <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">2007</span> │
│ <span style="color: #008000; text-decoration-color: #008000">Adelie </span> │ <span style="color: #008000; text-decoration-color: #008000">Torgersen</span> │           <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">40.3</span> │          <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">18.0</span> │               <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">195</span> │        <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">3250</span> │ <span style="color: #008000; text-decoration-color: #008000">female</span> │  <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">2007</span> │
│ <span style="color: #008000; text-decoration-color: #008000">Adelie </span> │ <span style="color: #008000; text-decoration-color: #008000">Torgersen</span> │            <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">nan</span> │           <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">nan</span> │              <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">NULL</span> │        <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">NULL</span> │ <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">NULL</span>   │  <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">2007</span> │
│ <span style="color: #008000; text-decoration-color: #008000">Adelie </span> │ <span style="color: #008000; text-decoration-color: #008000">Torgersen</span> │           <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">36.7</span> │          <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">19.3</span> │               <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">193</span> │        <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">3450</span> │ <span style="color: #008000; text-decoration-color: #008000">female</span> │  <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">2007</span> │
│ <span style="color: #008000; text-decoration-color: #008000">Adelie </span> │ <span style="color: #008000; text-decoration-color: #008000">Torgersen</span> │           <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">39.3</span> │          <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">20.6</span> │               <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">190</span> │        <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">3650</span> │ <span style="color: #008000; text-decoration-color: #008000">male  </span> │  <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">2007</span> │
│ <span style="color: #008000; text-decoration-color: #008000">Adelie </span> │ <span style="color: #008000; text-decoration-color: #008000">Torgersen</span> │           <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">38.9</span> │          <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">17.8</span> │               <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">181</span> │        <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">3625</span> │ <span style="color: #008000; text-decoration-color: #008000">female</span> │  <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">2007</span> │
│ <span style="color: #008000; text-decoration-color: #008000">Adelie </span> │ <span style="color: #008000; text-decoration-color: #008000">Torgersen</span> │           <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">39.2</span> │          <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">19.6</span> │               <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">195</span> │        <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">4675</span> │ <span style="color: #008000; text-decoration-color: #008000">male  </span> │  <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">2007</span> │
│ <span style="color: #008000; text-decoration-color: #008000">Adelie </span> │ <span style="color: #008000; text-decoration-color: #008000">Torgersen</span> │           <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">34.1</span> │          <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">18.1</span> │               <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">193</span> │        <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">3475</span> │ <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">NULL</span>   │  <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">2007</span> │
│ <span style="color: #008000; text-decoration-color: #008000">Adelie </span> │ <span style="color: #008000; text-decoration-color: #008000">Torgersen</span> │           <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">42.0</span> │          <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">20.2</span> │               <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">190</span> │        <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">4250</span> │ <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">NULL</span>   │  <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">2007</span> │
│ <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">…</span>       │ <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">…</span>         │              <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">…</span> │             <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">…</span> │                 <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">…</span> │           <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">…</span> │ <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">…</span>      │     <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">…</span> │
└─────────┴───────────┴────────────────┴───────────────┴───────────────────┴─────────────┴────────┴───────┘
</pre>
</div>
</div>
<p>When working with many tables, you should name them descriptively.</p>
</section>
<section id="schema" class="level3">
<h3 class="anchored" data-anchor-id="schema">Schema</h3>
<p>A table has a schema that Ibis maps to the data platform’s data types:</p>
<div id="30328fce" class="cell" data-execution_count="18">
<div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb19-1">t.schema()</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="62">
<pre><code>ibis.Schema {
  species            string
  island             string
  bill_length_mm     float64
  bill_depth_mm      float64
  flipper_length_mm  int64
  body_mass_g        int64
  sex                string
  year               int64
}</code></pre>
</div>
</div>
</section>
</section>
<section id="llms-and-data-marvin-and-ibis" class="level2">
<h2 class="anchored" data-anchor-id="llms-and-data-marvin-and-ibis">LLMs and data: Marvin and Ibis</h2>
<p>You can use Marvin and Ibis together to easily apply LLMs to data.</p>
<div id="547cc28d" class="cell" data-execution_count="19">
<div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb21-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> ibis.expr.schema <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Schema</span>
<span id="cb21-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> ibis.expr.types.relations <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Table</span>
<span id="cb21-3"></span>
<span id="cb21-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@marvin.ai_fn</span></span>
<span id="cb21-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">def</span> sql_select(</span>
<span id="cb21-6">    text: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, table_name: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> t.get_name(), schema: Schema <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> t.schema()</span>
<span id="cb21-7">) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>:</span>
<span id="cb21-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""writes the SQL SELECT statement to query the table according to the text"""</span></span>
<span id="cb21-9"></span>
<span id="cb21-10"></span>
<span id="cb21-11">query <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"the unique combination of species and islands"</span></span>
<span id="cb21-12">sql <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sql_select(query).strip(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">";"</span>)</span>
<span id="cb21-13">sql</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="63">
<pre><code>'SELECT DISTINCT species, island FROM penguins'</code></pre>
</div>
</div>
<div id="03d96043" class="cell" data-execution_count="20">
<div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb23-1">t.sql(sql)</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="64">
<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace">┏━━━━━━━━━━━┳━━━━━━━━━━━┓
┃<span style="font-weight: bold"> species   </span>┃<span style="font-weight: bold"> island    </span>┃
┡━━━━━━━━━━━╇━━━━━━━━━━━┩
│ <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">string</span>    │ <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">string</span>    │
├───────────┼───────────┤
│ <span style="color: #008000; text-decoration-color: #008000">Adelie   </span> │ <span style="color: #008000; text-decoration-color: #008000">Torgersen</span> │
│ <span style="color: #008000; text-decoration-color: #008000">Adelie   </span> │ <span style="color: #008000; text-decoration-color: #008000">Biscoe   </span> │
│ <span style="color: #008000; text-decoration-color: #008000">Adelie   </span> │ <span style="color: #008000; text-decoration-color: #008000">Dream    </span> │
│ <span style="color: #008000; text-decoration-color: #008000">Gentoo   </span> │ <span style="color: #008000; text-decoration-color: #008000">Biscoe   </span> │
│ <span style="color: #008000; text-decoration-color: #008000">Chinstrap</span> │ <span style="color: #008000; text-decoration-color: #008000">Dream    </span> │
└───────────┴───────────┘
</pre>
</div>
</div>
<div id="402d960f" class="cell" data-execution_count="21">
<details>
<summary>Code</summary>
<div class="sourceCode cell-code" id="annotated-cell-21" style="background: #f1f3f5;"><pre class="sourceCode python code-annotation-code code-with-copy code-annotated"><code class="sourceCode python"><button class="code-annotation-anchor" data-target-cell="annotated-cell-21" data-target-annotation="1">1</button><span id="annotated-cell-21-1" class="code-annotation-target">sleep(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span><div class="code-annotation-gutter-bg"></div><div class="code-annotation-gutter"></div></code></pre></div>
</details>
<div class="cell-annotation">
<dl class="code-annotation-container-hidden code-annotation-container-grid">
<dt data-target-cell="annotated-cell-21" data-target-annotation="1">1</dt>
<dd>
<span data-code-cell="annotated-cell-21" data-code-lines="1" data-code-annotation="1">Avoid rate-limiting by waiting.</span>
</dd>
</dl>
</div>
</div>
<div id="a12d71b3" class="cell" data-execution_count="22">
<div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb24-1">t.sql(sql_select(query <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" and include their counts in from highest to lowest"</span>).strip(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">";"</span>))</span></code></pre></div>
<div class="cell-output cell-output-display" data-execution_count="66">
<pre style="white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace">┏━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━┓
┃<span style="font-weight: bold"> species   </span>┃<span style="font-weight: bold"> island    </span>┃<span style="font-weight: bold"> count </span>┃
┡━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━┩
│ <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">string</span>    │ <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">string</span>    │ <span style="color: #7f7f7f; text-decoration-color: #7f7f7f">int64</span> │
├───────────┼───────────┼───────┤
│ <span style="color: #008000; text-decoration-color: #008000">Gentoo   </span> │ <span style="color: #008000; text-decoration-color: #008000">Biscoe   </span> │   <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">124</span> │
│ <span style="color: #008000; text-decoration-color: #008000">Chinstrap</span> │ <span style="color: #008000; text-decoration-color: #008000">Dream    </span> │    <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">68</span> │
│ <span style="color: #008000; text-decoration-color: #008000">Adelie   </span> │ <span style="color: #008000; text-decoration-color: #008000">Dream    </span> │    <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">56</span> │
│ <span style="color: #008000; text-decoration-color: #008000">Adelie   </span> │ <span style="color: #008000; text-decoration-color: #008000">Torgersen</span> │    <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">52</span> │
│ <span style="color: #008000; text-decoration-color: #008000">Adelie   </span> │ <span style="color: #008000; text-decoration-color: #008000">Biscoe   </span> │    <span style="color: #008080; text-decoration-color: #008080; font-weight: bold">44</span> │
└───────────┴───────────┴───────┘
</pre>
</div>
</div>
</section>
<section id="next-steps" class="level2">
<h2 class="anchored" data-anchor-id="next-steps">Next steps</h2>
<p>You can get involved with <a href="https://github.com/ibis-project/ibis-birdbrain">Ibis Birdbrain</a>, our open-source data &amp; AI project for building next-generation natural language interfaces to data.</p>
<p><a href="../llms-and-data-pt1">Read the next post in this series</a>.</p>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>LLMs and data</category>
  <guid>https://ibis-project.github.io/posts/llms-and-data-pt0/</guid>
  <pubDate>Thu, 12 Oct 2023 00:00:00 GMT</pubDate>
</item>
</channel>
</rss>
