<?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>Travis Jakel: Python posts</title>
<link>https://travisjakel.github.io/blog/python.html</link>
<atom:link href="https://travisjakel.github.io/blog/python.xml" rel="self" type="application/rss+xml"/>
<description>Posts with Python code. This page&#39;s feed (python.xml) is the one given to Python-bloggers and Planet Python.</description>
<generator>quarto-1.11.9</generator>
<lastBuildDate>Mon, 30 Nov 2026 00:00:00 GMT</lastBuildDate>
<item>
  <title>Turn a whole book into a queryable knowledge graph in Python</title>
  <dc:creator>Travis Jakel</dc:creator>
  <link>https://travisjakel.github.io/blog/posts/iml-book-python/</link>
  <description><![CDATA[ 




<p>A long technical book is good to read and awkward to query. Which chapters cover local explanation methods? Where exactly is the Shapley value explanation? What links to what? This post turns a book into a small knowledge graph you can run SQL and semantic search against, deterministically, with no language model deciding what the structure is.</p>
<p>The book is Christoph Molnar’s <a href="https://github.com/christophM/interpretable-ml-book"><em>Interpretable Machine Learning</em></a>, whose Quarto sources are public under CC BY-NC-SA. The tool is <a href="https://github.com/travisjakel/okf-ingest"><strong>okf-ingest</strong></a>, which reads an <a href="https://github.com/open-knowledge-format/open-knowledge-format">Open Knowledge Format</a> bundle into a DuckDB catalog. Disclosure: I wrote okf-ingest.</p>
<p>OKF itself is a short convention. A bundle is a folder of markdown files, one concept per file, each with YAML frontmatter. The only required field is <code>type</code>. Ordinary markdown links are the graph. Two filenames are reserved: <code>index.md</code>, the map, and <code>log.md</code>, the history.</p>
<section id="step-1-install-and-get-the-book" class="level2">
<h2 class="anchored" data-anchor-id="step-1-install-and-get-the-book">Step 1: install, and get the book</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">pip</span> install okf-ingest</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> clone <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--depth</span> 1 https://github.com/christophM/interpretable-ml-book src</span></code></pre></div></div>
</section>
<section id="step-2-make-it-okf" class="level2">
<h2 class="anchored" data-anchor-id="step-2-make-it-okf">Step 2: make it OKF</h2>
<p>This is the only conversion step. The book is 47 Quarto chapters grouped into Parts. Each chapter becomes a concept whose <code>type</code> is its Part, with a title and a one-sentence description taken from its first line of prose. Each Part becomes a concept that links to its chapters, and <code>index.md</code> links to the Parts, so the folder has a real graph instead of a flat list.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> os, re, shutil</span>
<span id="cb2-2">SRC, OUT, AT <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;">"src/manuscript"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"iml-okf"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2026-07-07T00:00:00Z"</span>   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># the book's last commit</span></span>
<span id="cb2-3">PARTS <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb2-4">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Foundations"</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"intro"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"interpretability"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"goals"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"overview"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>],</span>
<span id="cb2-5">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Interpretable Models"</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"limo"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"logistic"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"extend-lm"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tree"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rules"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rulefit"</span>],</span>
<span id="cb2-6">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Local Model-Agnostic Methods"</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ceteris-paribus"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ice"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lime"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"counterfactual"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"anchors"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"shapley"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"shap"</span>],</span>
<span id="cb2-7">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Global Model-Agnostic Methods"</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pdp"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ale"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"interaction"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"decomposition"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"feature-importance"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lofo"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"global"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"proto"</span>],</span>
<span id="cb2-8">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Neural Network Interpretation"</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cnn-features"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pixel-attribution"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"detecting-concepts"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"adversarial"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"influential"</span>],</span>
<span id="cb2-9">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Beyond the Methods"</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"evaluation"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"storytime"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"future"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"translations"</span>],</span>
<span id="cb2-10">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Back Matter"</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cite"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"acknowledgements"</span>],</span>
<span id="cb2-11">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Appendix"</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"what-is-machine-learning"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"math-terms"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r-packages"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"references"</span>],</span>
<span id="cb2-12">}</span>
<span id="cb2-13">shutil.rmtree(OUT, ignore_errors<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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> os.makedirs(OUT)</span>
<span id="cb2-14">pslug <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> p: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"part-"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> re.sub(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">[^a-z0-9]</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span>, p.lower())</span>
<span id="cb2-15">yq <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> s: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"'"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> s.replace(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"'"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"''"</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;">"'"</span>         <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># YAML single-quoted string</span></span>
<span id="cb2-16"></span>
<span id="cb2-17"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> clean(lines):                                       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># keep prose; links come from structure</span></span>
<span id="cb2-18">    out <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb2-19">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> ln <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> lines:</span>
<span id="cb2-20">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> re.match(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">^\s</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">(</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\{\{</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;</span><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">:::</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, ln): <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">continue</span>   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># drop Quarto directives</span></span>
<span id="cb2-21">        ln <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> re.sub(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\s</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\{</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">#</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">[^}]</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\}</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\s</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">$</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, ln)       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># strip {#label} from headings</span></span>
<span id="cb2-22">        ln <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> re.sub(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"!</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\[</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">[^]]</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\]\(</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">[^)]</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\)</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, ln)     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove image embeds</span></span>
<span id="cb2-23">        out.append(re.sub(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\[</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">(</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">[^]]</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\]\(</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">[^)]</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\)</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\1</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, ln))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># de-link to prose</span></span>
<span id="cb2-24">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> out</span>
<span id="cb2-25"></span>
<span id="cb2-26"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> first_sentence(body):                               <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># becomes the description</span></span>
<span id="cb2-27">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> ln <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> body:</span>
<span id="cb2-28">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> ln.strip() <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">and</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> ln.startswith(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#"</span>):</span>
<span id="cb2-29">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> re.sub(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">(</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">[.!?]</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">$</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\1</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, ln.strip())[:<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>]</span>
<span id="cb2-30">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span></span>
<span id="cb2-31"></span>
<span id="cb2-32"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> fm(type_, title, desc):</span>
<span id="cb2-33">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"---"</span>, <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"type: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>type_<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"title: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>yq(title)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"description: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>yq(desc)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>,</span>
<span id="cb2-34">            <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'generated: </span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">{{</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> by: "process:convert", at: "</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>AT<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">" </span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">}}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>,</span>
<span id="cb2-35">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tags: [interpretable-ml]"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"---"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>]</span>
<span id="cb2-36"></span>
<span id="cb2-37"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> write(name, lines):</span>
<span id="cb2-38">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(os.path.join(OUT, name), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"w"</span>, encoding<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;">"utf-8"</span>, newline<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;">"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> fh:</span>
<span id="cb2-39">        fh.write(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>.join(lines) <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;">"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb2-40"></span>
<span id="cb2-41"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> part, stubs <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> PARTS.items():                       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># one concept per chapter</span></span>
<span id="cb2-42">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> stub <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> stubs:</span>
<span id="cb2-43">        raw <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(os.path.join(SRC, stub <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;">".qmd"</span>), encoding<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;">"utf-8"</span>).read().splitlines()</span>
<span id="cb2-44">        h1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">next</span>((l <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> l <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> raw <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> re.match(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">^</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">#</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\s</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, l)), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"# "</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> stub)</span>
<span id="cb2-45">        title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> re.sub(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\{</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">#</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">[^}]</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, re.sub(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">^</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">#</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\s</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, h1)).strip()</span>
<span id="cb2-46">        body <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> clean(raw)</span>
<span id="cb2-47">        write(stub <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;">".md"</span>, fm(part, title, first_sentence(body)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> body)</span>
<span id="cb2-48"></span>
<span id="cb2-49"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> part, stubs <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> PARTS.items():                       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># one concept per Part, linking its chapters</span></span>
<span id="cb2-50">    write(pslug(part) <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;">".md"</span>, fm(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Part"</span>, part, <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Chapters in the </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>part<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> part."</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-51">          [<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"# </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>part<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> [<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"- [</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>s<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">](</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>s<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">.md)"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> s <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> stubs])</span>
<span id="cb2-52"></span>
<span id="cb2-53">write(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"index.md"</span>, [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"---"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'okf_version: "0.3"'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"title: Interpretable Machine Learning"</span>,   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># bundle metadata</span></span>
<span id="cb2-54">                   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"description: Christoph Molnar's book on making black box models explainable, one concept per chapter."</span>,</span>
<span id="cb2-55">                   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"license: CC-BY-NC-SA-4.0"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"---"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>,</span>
<span id="cb2-56">                   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"# Interpretable Machine Learning"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> [<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"- [</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>p<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">](</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>pslug(p)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">.md)"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> p <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> PARTS])</span>
<span id="cb2-57">write(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"log.md"</span>, [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"# Bundle history"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"## 2026-11-30"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>,</span>
<span id="cb2-58">                 <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"- Converted from the Quarto sources of *Interpretable Machine Learning*."</span>])</span></code></pre></div></div>
<p>Two details follow OKF v0.3. The root <code>index.md</code> carries bundle metadata, including the book’s licence. And <code>log.md</code> has no frontmatter and dates its entries with <code>## YYYY-MM-DD</code> headings; a log with frontmatter is a conformance error in v0.3.</p>
</section>
<section id="step-3-validate-and-ingest" class="level2">
<h2 class="anchored" data-anchor-id="step-3-validate-and-ingest">Step 3: validate and ingest</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb3-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">okf</span> validate iml-okf</span>
<span id="cb3-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; bundle: iml-okf</span></span>
<span id="cb3-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; conformant: True  (errors: 0, warnings: 0)</span></span>
<span id="cb3-4"></span>
<span id="cb3-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">okf</span> ingest iml-okf <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--db</span> iml.duckdb</span>
<span id="cb3-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ingested iml-okf -&gt; iml.duckdb</span></span>
<span id="cb3-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   concepts=49 conformant=49 (True) errors=0 warnings=0 links=49 broken=0</span></span></code></pre></div></div>
<p>49 concepts: 41 chapters and 8 Part pages. The same thing from Python returns an open DuckDB connection, and from there it is plain SQL:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> okf</span>
<span id="cb4-2">con, summary <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> okf.ingest(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"iml-okf"</span>, db_path<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;">"iml.duckdb"</span>)</span>
<span id="cb4-3"></span>
<span id="cb4-4">con.execute(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"""SELECT type, count(*) n FROM okf_concept WHERE reserved = FALSE</span></span>
<span id="cb4-5"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">               GROUP BY type ORDER BY n DESC, type"""</span>).fetchall()</span>
<span id="cb4-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [('Global Model-Agnostic Methods', 8), ('Part', 8), ('Local Model-Agnostic Methods', 7), ('Interpretable Models', 6), ('Foundations', 5), ('Neural Network Interpretation', 5), ('Appendix', 4), ('Beyond the Methods', 4), ('Back Matter', 2)]</span></span>
<span id="cb4-7"></span>
<span id="cb4-8">con.execute(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb4-9"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    SELECT c.title, length(c.body) AS chars</span></span>
<span id="cb4-10"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    FROM okf_concept c</span></span>
<span id="cb4-11"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    WHERE c.type = 'Local Model-Agnostic Methods'</span></span>
<span id="cb4-12"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    ORDER BY chars DESC</span></span>
<span id="cb4-13"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"""</span>).df()</span>
<span id="cb4-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;                                    title  chars</span></span>
<span id="cb4-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;                                     LIME  31456</span></span>
<span id="cb4-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;                                     SHAP  31424</span></span>
<span id="cb4-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;              Counterfactual Explanations  28784</span></span>
<span id="cb4-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;                           Shapley Values  25364</span></span>
<span id="cb4-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;                   Scoped Rules (Anchors)  20175</span></span>
<span id="cb4-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;                    Ceteris Paribus Plots  14358</span></span>
<span id="cb4-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Individual Conditional Expectation (ICE)   9330</span></span></code></pre></div></div>
</section>
<section id="step-4-health-check" class="level2">
<h2 class="anchored" data-anchor-id="step-4-health-check">Step 4: health check</h2>
<p><code>okf doctor</code> goes past conformance: orphans, missing recommended fields, stale timestamps, links to deprecated concepts.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb5-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">okf</span> doctor iml-okf</span>
<span id="cb5-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; health: 100/100  (49/49 concepts clean · 0 errors · 0 warnings)</span></span></code></pre></div></div>
<p>That score is earned by the converter. My first version of it wrote no <code>description</code>, and the same command reported 49 warnings and a health of 0/100. The check is cheap enough to run in CI on every change to a bundle.</p>
</section>
<section id="step-5-see-it" class="level2">
<h2 class="anchored" data-anchor-id="step-5-see-it">Step 5: see it</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb6-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">okf</span> graph iml-okf <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--out</span> iml-graph.html</span>
<span id="cb6-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [graph_html] wrote iml-graph.html (51 nodes, 49 edges)</span></span></code></pre></div></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://travisjakel.github.io/blog/posts/iml-book-python/iml-graph.png" class="img-fluid figure-img"></p>
<figcaption>The book as a graph: the index in the centre, eight Part hubs around it, chapters coloured by Part. The stray dot at the lower right is log.md, which nothing links to.</figcaption>
</figure>
</div>
<p>The output is one self-contained HTML page of about 17 KB with no external scripts. You can pan, zoom and search it.</p>
</section>
<section id="step-6-ask-it-a-question" class="level2">
<h2 class="anchored" data-anchor-id="step-6-ask-it-a-question">Step 6: ask it a question</h2>
<p>Embedding is the one place a model is used, and it is optional. <code>okf.embed()</code> chunks the chapter text and embeds it with a local model (Ollama’s <code>nomic-embed-text</code> by default), storing the vectors in the same DuckDB file.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(okf.embed(con), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"chunks embedded"</span>)</span>
<span id="cb7-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> path, title, chunk, score, text <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> okf.rag_search(</span>
<span id="cb7-3">        con, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"How are Shapley values used to explain a prediction?"</span>, k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>):</span>
<span id="cb7-4">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"[</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>score<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">] </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>path<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">#</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>chunk<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>title<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb7-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1369 chunks embedded</span></span>
<span id="cb7-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [0.852] shapley.md#1  Shapley Values</span></span>
<span id="cb7-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [0.848] shapley.md#38  Shapley Values</span></span>
<span id="cb7-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [0.846] shapley.md#21  Shapley Values</span></span>
<span id="cb7-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [0.844] shapley.md#11  Shapley Values</span></span></code></pre></div></div>
<p>All four hits are passages of the Shapley Values chapter, ahead of the SHAP chapter that builds on it.</p>
</section>
<section id="why-not-let-a-model-read-the-book" class="level2">
<h2 class="anchored" data-anchor-id="why-not-let-a-model-read-the-book">Why not let a model read the book?</h2>
<p>Because nothing here asks a model what the structure is. The graph is exactly the structure the author wrote down in the book’s table of contents. The same sources always give the same catalog, the same graph and the same health score, which means you can assert on them in a test. A model enters only at the embedding step, and only if you want it.</p>
<p>The same bundle can be built and read from R with the <code>okf</code> package. I ran the R version of this converter on the same book: it wrote the same 51 files, byte for byte, and gave the same counts, health score and search hits. A <a href="https://travisjakel.github.io/blog/posts/iml-book-r/">companion post</a> covers that side.</p>
<hr>
<p><em>okf-ingest is open source (Apache-2.0): <a href="https://github.com/travisjakel/okf-ingest">GitHub</a>, <a href="https://pypi.org/project/okf-ingest/">PyPI</a>. The book is</em> Interpretable Machine Learning <em>by Christoph Molnar, used here under CC BY-NC-SA for demonstration. The converted bundle is not redistributed; the code above rebuilds it from the author’s repository. Read the real thing at <a href="https://christophm.github.io/interpretable-ml-book/" class="uri">https://christophm.github.io/interpretable-ml-book/</a>.</em></p>
<p><em>Run with okf-ingest 0.14.2 on Python 3.12.10, against the book repository at commit <code>8f470c8</code> (2026-07-07). Embeddings came from <code>nomic-embed-text</code> on a local server. The script that produced every output above is in this blog’s repository under <code>_runs/okf_py_book.py</code>.</em></p>


</section>

 ]]></description>
  <category>Python</category>
  <category>OKF</category>
  <category>DuckDB</category>
  <category>RAG</category>
  <category>knowledge graph</category>
  <guid>https://travisjakel.github.io/blog/posts/iml-book-python/</guid>
  <pubDate>Mon, 30 Nov 2026 00:00:00 GMT</pubDate>
  <media:content url="https://travisjakel.github.io/blog/posts/iml-book-python/iml-graph.png" medium="image" type="image/png" height="135" width="144"/>
</item>
<item>
  <title>Make any OKF knowledge bundle searchable: DuckDB + embeddings in Python</title>
  <dc:creator>Travis Jakel</dc:creator>
  <link>https://travisjakel.github.io/blog/posts/okf-python-rag/</link>
  <description><![CDATA[ 




<p><em>Rewritten on 2026-11-30 for OKF v0.3 and okf-ingest 0.14.2. The June version of this post covered v0.1.</em></p>
<p>An AI agent is only as good as the context it is given. The <a href="https://github.com/open-knowledge-format/open-knowledge-format">Open Knowledge Format (OKF)</a> is a small, vendor-neutral convention for that context: <strong>a directory of markdown files with YAML frontmatter</strong>, one concept per file, with ordinary links between files forming a graph. There is no database and no required tooling. Google Cloud proposed it in June 2026, and the spec, now at v0.3, lives in its own organisation.</p>
<p>That minimalism leaves a practical question. Once you have a bundle, how do you query it, check it, or feed it to a retrieval pipeline? <a href="https://github.com/travisjakel/okf-ingest"><strong>okf-ingest</strong></a> is my answer: it reads a bundle, validates it the permissive way the spec asks for, and loads it into a <strong>DuckDB</strong> catalog you can query from Python or R. Disclosure: I wrote it.</p>
<section id="install-and-get-a-bundle" class="level2">
<h2 class="anchored" data-anchor-id="install-and-get-a-bundle">Install, and get a bundle</h2>
<p>The spec repository ships four reference bundles. This post uses <code>acme_retail</code>, a small finance knowledge base with metrics, policies and attested SQL computations.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">pip</span> install okf-ingest</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> clone <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--depth</span> 1 https://github.com/open-knowledge-format/open-knowledge-format.git</span></code></pre></div></div>
</section>
<section id="validate" class="level2">
<h2 class="anchored" data-anchor-id="validate">Validate</h2>
<p>OKF’s conformance bar is deliberately low. A file with no frontmatter or no <code>type</code> is simply not a concept. Broken links, missing recommended fields and unknown types are findings, never rejections.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb2-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">okf</span> validate open-knowledge-format/bundles/acme_retail</span>
<span id="cb2-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; bundle: open-knowledge-format/bundles/acme_retail</span></span>
<span id="cb2-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; conformant: True  (errors: 0, warnings: 0)</span></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   [info ] non_concept_target     attesters/index.md — reference resolves to a non-concept file: sql_equality.py</span></span>
<span id="cb2-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   [info ] non_concept_target     computations/gross-margin-period.md — reference resolves to a non-concept file: /attesters/sql_equality.py</span></span>
<span id="cb2-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   [info ] non_concept_target     computations/revenue-ytd.md — reference resolves to a non-concept file: /attesters/sql_equality.py</span></span></code></pre></div></div>
<p>The three <code>info</code> lines say that some concepts point at a Python file. That is a real file in the bundle, so the link is fine; it just is not a concept. <code>okf validate</code> exits non-zero on a non-conformant bundle, so it works as a CI check.</p>
</section>
<section id="load-it-into-duckdb" class="level2">
<h2 class="anchored" data-anchor-id="load-it-into-duckdb">Load it into DuckDB</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb3-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">okf</span> ingest open-knowledge-format/bundles/acme_retail <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--db</span> kb.duckdb</span>
<span id="cb3-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ingested open-knowledge-format/bundles/acme_retail -&gt; kb.duckdb</span></span>
<span id="cb3-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   concepts=9 conformant=9 (True) errors=0 warnings=0 links=44 broken=0</span></span></code></pre></div></div>
<p>From here it is plain DuckDB. No okf code is needed to read the catalog:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> duckdb</span>
<span id="cb4-2"></span>
<span id="cb4-3">con <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> duckdb.<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;">"kb.duckdb"</span>)</span>
<span id="cb4-4"></span>
<span id="cb4-5">con.sql(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT okf_version, title, n_concepts, conformant FROM okf_bundle"</span>).fetchall()</span>
<span id="cb4-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [('0.3', 'Acme Retail', 9, True)]</span></span>
<span id="cb4-7"></span>
<span id="cb4-8">con.sql(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb4-9"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    SELECT type, count(*) AS n FROM okf_concept</span></span>
<span id="cb4-10"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    WHERE NOT reserved GROUP BY type ORDER BY n DESC, type</span></span>
<span id="cb4-11"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"""</span>).df()</span>
<span id="cb4-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;                 type  n</span></span>
<span id="cb4-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;               Metric  3</span></span>
<span id="cb4-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; Attested Computation  2</span></span>
<span id="cb4-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;               Policy  2</span></span>
<span id="cb4-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;       BigQuery Table  1</span></span>
<span id="cb4-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;                Skill  1</span></span></code></pre></div></div>
<p><code>type</code> is a free string, not an enum. The bundle’s author chose these five.</p>
</section>
<section id="the-graph-is-more-than-body-links" class="level2">
<h2 class="anchored" data-anchor-id="the-graph-is-more-than-body-links">The graph is more than body links</h2>
<p>A link in the markdown body is one kind of edge. Frontmatter fields that hold a path are edges too: a concept’s <code>sources</code>, a computation’s <code>executor</code> and <code>attester</code>, and <code>superseded_by</code> on a retired concept.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1">con.sql(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb5-2"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    SELECT kind, target, count(*) AS n FROM okf_link</span></span>
<span id="cb5-3"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    GROUP BY ALL ORDER BY n DESC, kind</span></span>
<span id="cb5-4"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"""</span>).df()</span>
<span id="cb5-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;          kind  target  n</span></span>
<span id="cb5-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;          body concept 30</span></span>
<span id="cb5-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;        source concept  8</span></span>
<span id="cb5-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      attester    file  2</span></span>
<span id="cb5-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;      executor concept  2</span></span>
<span id="cb5-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;          body    file  1</span></span>
<span id="cb5-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; superseded_by concept  1</span></span>
<span id="cb5-12"></span>
<span id="cb5-13">con.sql(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT src_path, dst_path FROM okf_link WHERE kind = 'superseded_by'"</span>).fetchall()</span>
<span id="cb5-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [('metrics/gross-margin-legacy.md', 'metrics/gross-margin.md')]</span></span></code></pre></div></div>
<p>Those frontmatter edges matter for impact questions. If the revenue recognition policy changes, what depends on it?</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" 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;">import</span> okf</span>
<span id="cb6-2"></span>
<span id="cb6-3">okf.impact(con, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"policies/revenue-recognition.md"</span>)[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"inbound"</span>]</span>
<span id="cb6-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ['computations/gross-margin-period.md', 'computations/revenue-ytd.md', 'metrics/gross-margin.md', 'metrics/revenue.md', 'policies/index.md', 'tables/orders.md']</span></span></code></pre></div></div>
<p>Five of those six are <code>source</code> edges from frontmatter: both computations, both current metrics and the orders table. With body links alone, only the directory index would point at the policy.</p>
</section>
<section id="trust-and-lifecycle" class="level2">
<h2 class="anchored" data-anchor-id="trust-and-lifecycle">Trust and lifecycle</h2>
<p>A concept can say who verified it, when, and whether it is still current. <code>okf trust</code> reads those fields into one line per file: status, trust tier, and the last verification.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb7-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">okf</span> trust open-knowledge-format/bundles/acme_retail</span>
<span id="cb7-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; attesters/index.md                             -           unverified       -</span></span>
<span id="cb7-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; computations/gross-margin-period.md            stable      human-reviewed   2026-07-01T09:00:00Z</span></span>
<span id="cb7-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; computations/index.md                          -           unverified       -</span></span>
<span id="cb7-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; computations/revenue-ytd.md                    stable      human-reviewed   2026-07-01T09:00:00Z</span></span>
<span id="cb7-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; index.md                                       -           unverified       -</span></span>
<span id="cb7-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; log.md                                         -           unverified       -</span></span>
<span id="cb7-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; metrics/gross-margin-legacy.md                 deprecated  human-reviewed   NOT-CURRENT 2024-01-15T10:00:00Z</span></span>
<span id="cb7-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; metrics/gross-margin.md                        stable      human-reviewed   2026-07-01T09:00:00Z</span></span>
<span id="cb7-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; metrics/index.md                               -           unverified       -</span></span>
<span id="cb7-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; metrics/revenue.md                             stable      human-reviewed   2026-07-01T09:00:00Z</span></span>
<span id="cb7-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; policies/index.md                              -           unverified       -</span></span>
<span id="cb7-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; policies/margin-standard.md                    stable      human-reviewed   2026-06-15T09:00:00Z</span></span>
<span id="cb7-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; policies/revenue-recognition.md                stable      human-reviewed   2026-06-15T09:00:00Z</span></span>
<span id="cb7-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; skills/index.md                                -           unverified       -</span></span>
<span id="cb7-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; skills/run-on-bq.md                            stable      unverified       -</span></span>
<span id="cb7-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; tables/index.md                                -           unverified       -</span></span>
<span id="cb7-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; tables/orders.md                               stable      human-reviewed   2026-07-01T16:00:00Z</span></span></code></pre></div></div>
<p>One metric is deprecated. An agent that retrieves it by keyword will happily use a definition the finance team retired. <code>current_only</code> drops concepts that are not current from a search:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> flag <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> (<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>):</span>
<span id="cb8-2">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"current_only ="</span>, flag)</span>
<span id="cb8-3">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> path, type_, title <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> okf.search(con, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gross margin"</span>, current_only<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>flag):</span>
<span id="cb8-4">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> type_:                       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># skip the directory index pages</span></span>
<span id="cb8-5">            <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"  "</span>, path, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"|"</span>, title)</span>
<span id="cb8-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; current_only = False</span></span>
<span id="cb8-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    metrics/gross-margin-legacy.md | Gross Margin (legacy, pre-FY2026)</span></span>
<span id="cb8-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    metrics/gross-margin.md | Gross Margin</span></span>
<span id="cb8-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    policies/margin-standard.md | Acme Retail — Cost Allocation &amp; Margin Standard (FY2026)</span></span>
<span id="cb8-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    policies/revenue-recognition.md | Acme Retail — Revenue Recognition Policy (FY2026)</span></span>
<span id="cb8-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; current_only = True</span></span>
<span id="cb8-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    metrics/gross-margin.md | Gross Margin</span></span>
<span id="cb8-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    policies/margin-standard.md | Acme Retail — Cost Allocation &amp; Margin Standard (FY2026)</span></span>
<span id="cb8-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    policies/revenue-recognition.md | Acme Retail — Revenue Recognition Policy (FY2026)</span></span></code></pre></div></div>
</section>
<section id="context-for-a-model-without-embeddings" class="level2">
<h2 class="anchored" data-anchor-id="context-for-a-model-without-embeddings">Context for a model, without embeddings</h2>
<p>The format’s own idea of retrieval is not vector search. It is: read the index, then follow links. <code>okf.context()</code> does that. It seeds on the concepts whose text matches the question, follows their links outward, and packs whole concepts into a token budget as one markdown string.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1">ctx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> okf.context(con, 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;">"how is revenue recognized"</span>, max_tokens<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1500</span>)</span>
<span id="cb9-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(ctx[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"est_tokens"</span>], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tokens"</span>)</span>
<span id="cb9-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"included:"</span>, ctx[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"included"</span>])</span>
<span id="cb9-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"omitted: "</span>, ctx[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"omitted"</span>])</span>
<span id="cb9-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1409 tokens</span></span>
<span id="cb9-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; included: ['computations/revenue-ytd.md', 'policies/revenue-recognition.md', 'metrics/revenue.md']</span></span>
<span id="cb9-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; omitted:  ['computations/gross-margin-period.md', 'metrics/gross-margin.md', 'tables/orders.md', 'policies/margin-standard.md', 'skills/run-on-bq.md', 'metrics/gross-margin-legacy.md']</span></span></code></pre></div></div>
<p><code>ctx["text"]</code> is ready to paste into a prompt. It is deterministic: the same bundle and question give the same string, with no model involved. For a small, well-linked bundle this is often all the retrieval you need.</p>
</section>
<section id="semantic-search-no-vector-database" class="level2">
<h2 class="anchored" data-anchor-id="semantic-search-no-vector-database">Semantic search, no vector database</h2>
<p>For a large bundle, or questions that share no words with the answer, embed the concept bodies. <code>okf.embed()</code> chunks and embeds them and stores the vectors in the same catalog. The default embedder is a local <a href="https://ollama.com">Ollama</a> <code>nomic-embed-text</code>, so nothing leaves your machine, and any function from texts to vectors can replace it.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(okf.embed(con), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"chunks embedded"</span>)</span>
<span id="cb10-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> path, title, chunk, score, text <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> okf.rag_search(con, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"how is revenue recognized?"</span>, k<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>):</span>
<span id="cb10-3">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"[</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>score<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">] </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>path<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">#</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>chunk<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>title<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb10-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 32 chunks embedded</span></span>
<span id="cb10-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [0.729] policies/revenue-recognition.md#1  Acme Retail — Revenue Recognition Policy (FY2026)</span></span>
<span id="cb10-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [0.713] policies/revenue-recognition.md#4  Acme Retail — Revenue Recognition Policy (FY2026)</span></span>
<span id="cb10-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [0.712] metrics/revenue.md#1  Revenue</span></span></code></pre></div></div>
<p>The search is a brute-force cosine over a DuckDB list column. No vector extension is required, which is the right trade for bundles of hundreds to tens of thousands of chunks.</p>
</section>
<section id="straight-from-a-url" class="level2">
<h2 class="anchored" data-anchor-id="straight-from-a-url">Straight from a URL</h2>
<p>You do not need a clone. <code>okf ingest</code> takes a git URL or a tar/zip archive, fetches it to a temporary directory, and cleans up:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb11-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">okf</span> ingest https://github.com/open-knowledge-format/open-knowledge-format.git <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb11-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--subdir</span> bundles/crypto_bitcoin <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--db</span> btc.duckdb</span>
<span id="cb11-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; ingested https://github.com/open-knowledge-format/open-knowledge-format.git -&gt; btc.duckdb</span></span>
<span id="cb11-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   concepts=9 conformant=9 (True) errors=0 warnings=0 links=39 broken=0</span></span></code></pre></div></div>
</section>
<section id="scope-honestly" class="level2">
<h2 class="anchored" data-anchor-id="scope-honestly">Scope, honestly</h2>
<ul>
<li><strong>OKF is young.</strong> v0.3 made breaking changes to v0.2, and it may change again.</li>
<li><strong>Validators are plentiful.</strong> If you only need a linter, there are several. okf-ingest is for the step after: a queryable catalog, the graph, lifecycle-aware retrieval, and the same result from Python and R.</li>
<li><strong>Some features are mine, not OKF’s.</strong> <code>[[wikilinks]]</code>, a <code>.okfignore</code> file and a <code>.okfstatus</code> file are okf-ingest extensions. The spec says wikilinks are not links in OKF.</li>
<li><strong>A catalog records where it was built.</strong> <code>okf_bundle.root</code> holds the path the bundle was read from. Check it before you publish a <code>.duckdb</code> file.</li>
</ul>
<p>Links: <a href="https://github.com/travisjakel/okf-ingest">okf-ingest</a> · <a href="https://pypi.org/project/okf-ingest/">PyPI</a> · <a href="https://github.com/open-knowledge-format/open-knowledge-format">OKF spec</a>. Issues and pull requests are welcome.</p>
<p><em>Run with okf-ingest 0.14.2 on Python 3.12.10, against the spec repository at commit <code>527e81a</code> (2026-11-18). Embeddings came from <code>nomic-embed-text</code> on a local server. The script that produced every output above is in this blog’s repository under <code>_runs/okf_py_intro.py</code>.</em></p>


</section>

 ]]></description>
  <category>Python</category>
  <category>OKF</category>
  <category>DuckDB</category>
  <category>RAG</category>
  <category>LLM</category>
  <category>agents</category>
  <guid>https://travisjakel.github.io/blog/posts/okf-python-rag/</guid>
  <pubDate>Mon, 22 Jun 2026 00:00:00 GMT</pubDate>
</item>
</channel>
</rss>
