<?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</title>
<link>https://travisjakel.github.io/blog/</link>
<atom:link href="https://travisjakel.github.io/blog/index.xml" rel="self" type="application/rss+xml"/>
<description>Notes on R, Python and open tools for quantitative research: DuckDB, retrieval over documents, and knowledge formats.</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>Turn a whole book into a queryable knowledge graph in R</title>
  <dc:creator>Travis Jakel</dc:creator>
  <link>https://travisjakel.github.io/blog/posts/iml-book-r/</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 query with <code>data.table</code> and search by meaning, 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 the R package <code>okf</code> from <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 r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"okf"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">repos =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb1-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">travisjakel =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://travisjakel.r-universe.dev"</span>,</span>
<span id="cb1-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CRAN        =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://cloud.r-project.org"</span></span>
<span id="cb1-4">))</span></code></pre></div></div>
<p>CRAN carries an older release of <code>okf</code>. This post needs the r-universe build.</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="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="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">src <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"src/manuscript"</span>; out <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"iml-okf"</span>; at <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</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="cb3-2">parts <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb3-3">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Foundations"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</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="cb3-4">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Interpretable Models"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</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="cb3-5">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Local Model-Agnostic Methods"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</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="cb3-6">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Global Model-Agnostic Methods"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</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="cb3-7">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Neural Network Interpretation"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</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="cb3-8">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Beyond the Methods"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</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="cb3-9">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Back Matter"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</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="cb3-10">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Appendix"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</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="cb3-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unlink</span>(out, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">recursive =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>); <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dir.create</span>(out)</span>
<span id="cb3-12">pslug <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(p) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"part-"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gsub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[^a-z0-9]+"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tolower</span>(p)))</span>
<span id="cb3-13">yq    <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(s) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"'"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gsub</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>, s, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fixed =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</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="cb3-14"></span>
<span id="cb3-15">clean <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) {                                <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># keep prose; links come from structure</span></span>
<span id="cb3-16">  x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> x[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grepl</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">s*(</span><span class="sc" 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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">{&lt;|:::)"</span>, x)]             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># drop Quarto directives</span></span>
<span id="cb3-17">  x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">s*</span><span class="sc" 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="sc" 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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">s*$"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, x)            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># strip {#label} from headings</span></span>
<span id="cb3-18">  x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gsub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"!</span><span class="sc" 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="sc" 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="sc" 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="sc" 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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, x)         <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove image embeds</span></span>
<span id="cb3-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gsub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" 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="sc" 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="sc" 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="sc" 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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">1"</span>, x)          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># de-link to prose</span></span>
<span id="cb3-20">}</span>
<span id="cb3-21">first_sentence <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(body) {                    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># becomes the description</span></span>
<span id="cb3-22">  ln <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> body[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nzchar</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">trimws</span>(body)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">startsWith</span>(body, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#"</span>)][<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb3-23">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(ln)) <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">substr</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sub</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 class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">1"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">trimws</span>(ln)), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>)</span>
<span id="cb3-24">}</span>
<span id="cb3-25">fm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(type, title, desc) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb3-26">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"---"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type: "</span>, type), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"title: "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">yq</span>(title)), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"description: "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">yq</span>(desc)),</span>
<span id="cb3-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'generated: { by: "process:convert", at: "%s" }'</span>, at),</span>
<span id="cb3-28">  <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="cb3-29">put <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(name, lines) {                        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># UTF-8, LF line endings</span></span>
<span id="cb3-30">  con <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">file</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">file.path</span>(out, name), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"wb"</span>); <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">on.exit</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">close</span>(con))</span>
<span id="cb3-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">writeLines</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">enc2utf8</span>(lines), con, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">useBytes =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb3-32">}</span>
<span id="cb3-33"></span>
<span id="cb3-34"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (part <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(parts)) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (stub <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> parts[[part]]) {          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># one concept per chapter</span></span>
<span id="cb3-35">  raw   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readLines</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">file.path</span>(src, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(stub, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".qmd"</span>)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">warn =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">encoding =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"UTF-8"</span>)</span>
<span id="cb3-36">  h1    <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^#</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">s"</span>, raw, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]                    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># three chapters have no H1</span></span>
<span id="cb3-37">  title <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(h1)) stub <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">trimws</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gsub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" 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="sc" 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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^#</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">s+"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, h1)))</span>
<span id="cb3-38">  body  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">clean</span>(raw)</span>
<span id="cb3-39">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">put</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(stub, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".md"</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fm</span>(part, title, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">first_sentence</span>(body)), body))</span>
<span id="cb3-40">}</span>
<span id="cb3-41"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (part <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(parts))                                         <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># one concept per Part</span></span>
<span id="cb3-42">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">put</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pslug</span>(part), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".md"</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fm</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Part"</span>, part, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Chapters in the %s part."</span>, part)),</span>
<span id="cb3-43">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"# "</span>, part), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"- [%s](%s.md)"</span>, parts[[part]], parts[[part]])))</span>
<span id="cb3-44"></span>
<span id="cb3-45"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">put</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"index.md"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</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="cb3-46">  <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="cb3-47">  <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="cb3-48">  <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"- [%s](%s.md)"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(parts), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pslug</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(parts)))))</span>
<span id="cb3-49"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">put</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"log.md"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</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="cb3-50">  <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-ingest-and-query-it-with-data.table" class="level2">
<h2 class="anchored" data-anchor-id="step-3-ingest-and-query-it-with-data.table">Step 3: ingest, and query it with data.table</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(okf)</span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(DBI)</span>
<span id="cb4-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(data.table)</span>
<span id="cb4-4"></span>
<span id="cb4-5">res <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">okf_ingest</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"iml-okf"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">db_path =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"iml.duckdb"</span>)</span>
<span id="cb4-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.table</span>(res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>summary[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n_concepts"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"conformant"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"errors"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"warnings"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"links_total"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"links_broken"</span>)])</span>
<span id="cb4-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    n_concepts conformant errors warnings links_total links_broken</span></span>
<span id="cb4-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1:         49       TRUE      0        0          49            0</span></span>
<span id="cb4-9">con <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>con</span></code></pre></div></div>
<p>49 concepts: 41 chapters and 8 Part pages, conformant, with no broken links. <code>res$con</code> is an ordinary DuckDB connection, so the rest is <code>DBI</code> and <code>data.table</code>. Where does the book spend its words?</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">concepts <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.table</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbGetQuery</span>(con, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT path, type, title, length(body) AS chars</span></span>
<span id="cb5-2"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">                                           FROM okf_concept WHERE NOT reserved"</span>))</span>
<span id="cb5-3">concepts[, .(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">chapters =</span> .N, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">chars =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(chars)), by <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> type][<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">order</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>chars)]</span>
<span id="cb5-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;                             type chapters  chars</span></span>
<span id="cb5-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1:          Interpretable Models        6 171156</span></span>
<span id="cb5-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 2: Global Model-Agnostic Methods        8 170853</span></span>
<span id="cb5-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 3:  Local Model-Agnostic Methods        7 160891</span></span>
<span id="cb5-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 4: Neural Network Interpretation        5 115830</span></span>
<span id="cb5-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 5:                   Foundations        5  78995</span></span>
<span id="cb5-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 6:            Beyond the Methods        4  39440</span></span>
<span id="cb5-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 7:                      Appendix        4   5472</span></span>
<span id="cb5-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 8:                   Back Matter        2   3527</span></span>
<span id="cb5-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 9:                          Part        8   1340</span></span>
<span id="cb5-14"></span>
<span id="cb5-15">concepts[type <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Local Model-Agnostic Methods"</span>, .(title, chars)][<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">order</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>chars)]</span>
<span id="cb5-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;                                       title chars</span></span>
<span id="cb5-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1:                                     LIME 31456</span></span>
<span id="cb5-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 2:                                     SHAP 31424</span></span>
<span id="cb5-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 3:              Counterfactual Explanations 28784</span></span>
<span id="cb5-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 4:                           Shapley Values 25364</span></span>
<span id="cb5-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 5:                   Scoped Rules (Anchors) 20175</span></span>
<span id="cb5-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 6:                    Ceteris Paribus Plots 14358</span></span>
<span id="cb5-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 7: 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: duplicate titles, orphans, missing recommended fields, stale timestamps, links to deprecated concepts.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">d <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">okf_doctor</span>(con)</span>
<span id="cb6-2">d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>score</span>
<span id="cb6-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] 100</span></span>
<span id="cb6-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>by_rule)</span>
<span id="cb6-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] 0</span></span></code></pre></div></div>
<p>A clean score, and it was not clean on my first attempt. Three chapters of the book start at a level-two heading, so my first converter gave all three the title <code>NA</code>. The bundle was still conformant. <code>okf_doctor()</code> scored it 94 and named the rule, <code>duplicate_title</code>, which is how I found the bug. The fallback to the file name in the loop above is the fix.</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="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">okf_graph_html</span>(con, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"iml-graph.html"</span>)</span>
<span id="cb7-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [okf_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-r/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 is log.md, which nothing links to.</figcaption>
</figure>
</div>
<p>The output is one self-contained HTML page 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="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">okf_embed</span>(con)</span>
<span id="cb8-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] 1369</span></span>
<span id="cb8-3"></span>
<span id="cb8-4">hits <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.table</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">okf_rag</span>(con, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"How are Shapley values used to explain a prediction?"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>))</span>
<span id="cb8-5">hits[, .(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">score =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(score, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>), path, title)]</span>
<span id="cb8-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    score       path          title</span></span>
<span id="cb8-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1: 0.852 shapley.md Shapley Values</span></span>
<span id="cb8-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 2: 0.848 shapley.md Shapley Values</span></span>
<span id="cb8-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 3: 0.846 shapley.md Shapley Values</span></span>
<span id="cb8-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 4: 0.844 shapley.md Shapley Values</span></span></code></pre></div></div>
<p>1,369 chunks, and all four hits are passages of the Shapley Values chapter.</p>
</section>
<section id="the-same-bundle-from-python" class="level2">
<h2 class="anchored" data-anchor-id="the-same-bundle-from-python">The same bundle from Python</h2>
<p>okf-ingest has a Python package with the same functions and the same catalog tables. I ran the Python version of this converter on the same book: it wrote the same 51 files, byte for byte, and its catalog gave the same counts, the same health score and the same four search hits with the same scores. A <a href="https://travisjakel.github.io/blog/posts/iml-book-python/">companion post</a> walks through that side.</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>
<hr>
<p><em>okf-ingest is open source (Apache-2.0): <a href="https://github.com/travisjakel/okf-ingest">GitHub</a>, <a href="https://travisjakel.r-universe.dev/okf">r-universe</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 0.14.2 installed from r-universe, R 4.5.0, duckdb 1.3.2 and data.table 1.17.6, against the book repository at commit <code>8f470c8</code>. 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_r_book.R</code>.</em></p>


</section>

 ]]></description>
  <category>R</category>
  <category>OKF</category>
  <category>DuckDB</category>
  <category>RAG</category>
  <category>knowledge graph</category>
  <category>data.table</category>
  <guid>https://travisjakel.github.io/blog/posts/iml-book-r/</guid>
  <pubDate>Mon, 30 Nov 2026 00:00:00 GMT</pubDate>
  <media:content url="https://travisjakel.github.io/blog/posts/iml-book-r/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>
<item>
  <title>Querying Google’s Open Knowledge Format from R</title>
  <dc:creator>Travis Jakel</dc:creator>
  <link>https://travisjakel.github.io/blog/posts/okf-r-duckdb/</link>
  <description><![CDATA[ 




<p><em>Rewritten on 2026-11-30 for OKF v0.3 and okf 0.14.2. The June version of this post covered v0.1.</em></p>
<p>The <a href="https://github.com/open-knowledge-format/open-knowledge-format">Open Knowledge Format (OKF)</a> is a vendor-neutral way to write down the curated knowledge an AI agent needs as context. Google Cloud proposed it in June 2026 and the spec, now at v0.3, lives in its own organisation. The format is plain: <strong>a directory of markdown files with YAML frontmatter</strong>, one concept per file, with markdown links between files forming a graph. If you can read a text file, you can read OKF.</p>
<p>What the format does not give you is a way to query a bundle. I wrote <a href="https://github.com/travisjakel/okf-ingest"><strong>okf-ingest</strong></a> for that. Its R package, <code>okf</code>, reads a bundle, validates it, and loads it into a <strong>DuckDB</strong> catalog that you work with through <code>DBI</code> and, here, <code>data.table</code>. Disclosure: it is my package.</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>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"okf"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">repos =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb1-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">travisjakel =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://travisjakel.r-universe.dev"</span>,</span>
<span id="cb1-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">CRAN        =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://cloud.r-project.org"</span></span>
<span id="cb1-4">))</span></code></pre></div></div>
<p>CRAN carries an older release of <code>okf</code>. Everything below needs the r-universe build.</p>
<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="cb2" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb2-1"><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-permissively" class="level2">
<h2 class="anchored" data-anchor-id="validate-permissively">Validate, permissively</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="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(okf)</span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(DBI)</span>
<span id="cb3-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(data.table)</span>
<span id="cb3-4"></span>
<span id="cb3-5">B   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"open-knowledge-format/bundles/acme_retail"</span></span>
<span id="cb3-6">rd  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">okf_read</span>(B)</span>
<span id="cb3-7">val <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.table</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">okf_validate</span>(rd))</span>
<span id="cb3-8">val[, .N, by <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> .(severity, rule)]</span>
<span id="cb3-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    severity               rule N</span></span>
<span id="cb3-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1:     info non_concept_target 3</span></span></code></pre></div></div>
<p>Three informational findings and nothing else. They say that some concepts point at a Python file in the bundle, which is a real file but not a concept.</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="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">res <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">okf_ingest</span>(B, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">db_path =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"kb.duckdb"</span>)</span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str</span>(res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>summary)</span>
<span id="cb4-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; List of 9</span></span>
<span id="cb4-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  $ n_files     : int 17</span></span>
<span id="cb4-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  $ n_concepts  : int 9</span></span>
<span id="cb4-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  $ n_conformant: int 9</span></span>
<span id="cb4-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  $ conformant  : logi TRUE</span></span>
<span id="cb4-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  $ errors      : int 0</span></span>
<span id="cb4-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  $ warnings    : int 0</span></span>
<span id="cb4-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  $ links_total : int 44</span></span>
<span id="cb4-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  $ links_broken: int 0</span></span>
<span id="cb4-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;  $ not_concepts: int 0</span></span>
<span id="cb4-13">con <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>con</span></code></pre></div></div>
<p><code>res$con</code> is an ordinary DuckDB connection. From here it is <code>DBI</code> and <code>data.table</code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbGetQuery</span>(con, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT okf_version, title, n_concepts, conformant FROM okf_bundle"</span>)</span>
<span id="cb5-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;   okf_version       title n_concepts conformant</span></span>
<span id="cb5-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1         0.3 Acme Retail          9       TRUE</span></span>
<span id="cb5-4"></span>
<span id="cb5-5">concepts <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.table</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbGetQuery</span>(con, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT path, type, title FROM okf_concept WHERE NOT reserved"</span>))</span>
<span id="cb5-6">concepts[, .N, by <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> type][<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">order</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>N, type)]</span>
<span id="cb5-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;                    type N</span></span>
<span id="cb5-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1:               Metric 3</span></span>
<span id="cb5-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 2: Attested Computation 2</span></span>
<span id="cb5-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 3:               Policy 2</span></span>
<span id="cb5-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 4:       BigQuery Table 1</span></span>
<span id="cb5-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 5:                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. The catalog is a single file, and the Python package writes the same tables, so a bundle ingested in one language can be queried from the other.</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="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">links <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.table</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dbGetQuery</span>(con, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT src_path, dst_path, kind, target FROM okf_link"</span>))</span>
<span id="cb6-2">links[, .N, by <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> .(kind, target)][<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">order</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>N, kind)]</span>
<span id="cb6-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;             kind  target  N</span></span>
<span id="cb6-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1:          body concept 30</span></span>
<span id="cb6-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 2:        source concept  8</span></span>
<span id="cb6-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 3:      attester    file  2</span></span>
<span id="cb6-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 4:      executor concept  2</span></span>
<span id="cb6-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 5:          body    file  1</span></span>
<span id="cb6-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 6: superseded_by concept  1</span></span>
<span id="cb6-10"></span>
<span id="cb6-11">links[kind <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"superseded_by"</span>, .(src_path, dst_path)]</span>
<span id="cb6-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;                          src_path                dst_path</span></span>
<span id="cb6-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1: 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="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">okf_impact</span>(con, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"policies/revenue-recognition.md"</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>inbound</span>
<span id="cb7-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] "computations/gross-margin-period.md" "computations/revenue-ytd.md"</span></span>
<span id="cb7-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [3] "metrics/gross-margin.md"             "metrics/revenue.md"</span></span>
<span id="cb7-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [5] "policies/index.md"                   "tables/orders.md"</span></span>
<span id="cb7-5"></span>
<span id="cb7-6">links[dst_path <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"policies/revenue-recognition.md"</span>, .(src_path, kind)][<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">order</span>(src_path)]</span>
<span id="cb7-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;                               src_path   kind</span></span>
<span id="cb7-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1: computations/gross-margin-period.md source</span></span>
<span id="cb7-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 2:         computations/revenue-ytd.md source</span></span>
<span id="cb7-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 3:             metrics/gross-margin.md source</span></span>
<span id="cb7-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 4:                  metrics/revenue.md source</span></span>
<span id="cb7-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 5:                   policies/index.md   body</span></span>
<span id="cb7-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 6:                    tables/orders.md source</span></span></code></pre></div></div>
<p>Five of the six are <code>source</code> edges. 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 row per file.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">tr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.table</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">okf_trust</span>(rd))</span>
<span id="cb8-2">tr[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(status), .(path, status, trust_tier, verified_at, is_current)]</span>
<span id="cb8-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;                                   path     status     trust_tier          verified_at is_current</span></span>
<span id="cb8-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1: computations/gross-margin-period.md     stable human-reviewed 2026-07-01T09:00:00Z       TRUE</span></span>
<span id="cb8-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 2:         computations/revenue-ytd.md     stable human-reviewed 2026-07-01T09:00:00Z       TRUE</span></span>
<span id="cb8-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 3:      metrics/gross-margin-legacy.md deprecated human-reviewed 2024-01-15T10:00:00Z      FALSE</span></span>
<span id="cb8-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 4:             metrics/gross-margin.md     stable human-reviewed 2026-07-01T09:00:00Z       TRUE</span></span>
<span id="cb8-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 5:                  metrics/revenue.md     stable human-reviewed 2026-07-01T09:00:00Z       TRUE</span></span>
<span id="cb8-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 6:         policies/margin-standard.md     stable human-reviewed 2026-06-15T09:00:00Z       TRUE</span></span>
<span id="cb8-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 7:     policies/revenue-recognition.md     stable human-reviewed 2026-06-15T09:00:00Z       TRUE</span></span>
<span id="cb8-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 8:                 skills/run-on-bq.md     stable     unverified                 &lt;NA&gt;       TRUE</span></span>
<span id="cb8-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 9:                    tables/orders.md     stable human-reviewed 2026-07-01T16:00:00Z       TRUE</span></span></code></pre></div></div>
<p>One metric is deprecated. An agent that retrieves it by keyword would use a definition the finance team retired. <code>current_only</code> drops concepts that are not current:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">hits <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.table</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">okf_search</span>(con, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gross margin"</span>))</span>
<span id="cb9-2">hits[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(type), .(path, title)]</span>
<span id="cb9-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;                               path                                                    title</span></span>
<span id="cb9-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1:  metrics/gross-margin-legacy.md                        Gross Margin (legacy, pre-FY2026)</span></span>
<span id="cb9-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 2:         metrics/gross-margin.md                                             Gross Margin</span></span>
<span id="cb9-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 3:     policies/margin-standard.md Acme Retail — Cost Allocation &amp; Margin Standard (FY2026)</span></span>
<span id="cb9-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 4: policies/revenue-recognition.md        Acme Retail — Revenue Recognition Policy (FY2026)</span></span>
<span id="cb9-8"></span>
<span id="cb9-9">hits <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.table</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">okf_search</span>(con, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gross margin"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">current_only =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))</span>
<span id="cb9-10">hits[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(type), .(path, title)]</span>
<span id="cb9-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;                               path                                                    title</span></span>
<span id="cb9-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1:         metrics/gross-margin.md                                             Gross Margin</span></span>
<span id="cb9-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 2:     policies/margin-standard.md Acme Retail — Cost Allocation &amp; Margin Standard (FY2026)</span></span>
<span id="cb9-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 3: 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="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">ctx <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">okf_context</span>(con, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">query =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"how is revenue recognized"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max_tokens =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1500</span>)</span>
<span id="cb10-2">ctx<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>est_tokens</span>
<span id="cb10-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] 1409</span></span>
<span id="cb10-4">ctx<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>included</span>
<span id="cb10-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] "computations/revenue-ytd.md"     "policies/revenue-recognition.md" "metrics/revenue.md"</span></span>
<span id="cb10-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(ctx<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>omitted)</span>
<span id="cb10-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] 6</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, and no model is involved.</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="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">okf_embed</span>(con)</span>
<span id="cb11-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; [1] 32</span></span>
<span id="cb11-3"></span>
<span id="cb11-4">hits <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.table</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">okf_rag</span>(con, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"how is revenue recognized?"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>))</span>
<span id="cb11-5">hits[, .(path, title, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">score =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(score, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>))]</span>
<span id="cb11-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;                               path                                             title score</span></span>
<span id="cb11-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1: policies/revenue-recognition.md Acme Retail — Revenue Recognition Policy (FY2026) 0.729</span></span>
<span id="cb11-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 2: policies/revenue-recognition.md Acme Retail — Revenue Recognition Policy (FY2026) 0.713</span></span>
<span id="cb11-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 3:              metrics/revenue.md                                           Revenue 0.712</span></span></code></pre></div></div>
<p>The search is a brute-force cosine in DuckDB over the stored vectors. No vector extension is required.</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><code>okf_ingest()</code> also takes a git URL or a tar/zip archive. It fetches to a temporary directory, finds the bundle, and cleans up:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">btc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">okf_ingest</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://github.com/open-knowledge-format/open-knowledge-format.git"</span>,</span>
<span id="cb12-2">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subdir =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bundles/crypto_bitcoin"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">db_path =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"btc.duckdb"</span>)</span>
<span id="cb12-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.table</span>(btc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>summary[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n_concepts"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"conformant"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"errors"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"warnings"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"links_total"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"links_broken"</span>)])</span>
<span id="cb12-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt;    n_concepts conformant errors warnings links_total links_broken</span></span>
<span id="cb12-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#&gt; 1:          9       TRUE      0        0          39            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. <code>okf</code> is for the step after: a queryable catalog, the graph, lifecycle-aware retrieval, and the same result from R and Python.</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://travisjakel.r-universe.dev/okf">r-universe</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 0.14.2 installed from r-universe, R 4.5.0, duckdb 1.3.2 and data.table 1.17.6, against the spec repository at commit <code>527e81a</code>. 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_r_intro.R</code>.</em></p>


</section>

 ]]></description>
  <category>R</category>
  <category>OKF</category>
  <category>DuckDB</category>
  <category>RAG</category>
  <category>knowledge</category>
  <category>data.table</category>
  <guid>https://travisjakel.github.io/blog/posts/okf-r-duckdb/</guid>
  <pubDate>Mon, 22 Jun 2026 00:00:00 GMT</pubDate>
</item>
</channel>
</rss>
