<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"><channel><title><![CDATA[Ross McNairn's Blog]]></title><description><![CDATA[musings from engineering, product to tech and law

Working on something new]]></description><link>https://www.rossmcnairn.com</link><image><url>https://substackcdn.com/image/fetch/$s_!vLOW!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffeab127a-3758-4e8d-81ec-5ea0e5c0bd8a_512x512.png</url><title>Ross McNairn&apos;s Blog</title><link>https://www.rossmcnairn.com</link></image><generator>Substack</generator><lastBuildDate>Fri, 17 Apr 2026 23:34:45 GMT</lastBuildDate><atom:link href="https://www.rossmcnairn.com/feed" rel="self" type="application/rss+xml"/><copyright><![CDATA[Ross McNairn]]></copyright><language><![CDATA[en]]></language><webMaster><![CDATA[rmcnairn@substack.com]]></webMaster><itunes:owner><itunes:email><![CDATA[rmcnairn@substack.com]]></itunes:email><itunes:name><![CDATA[Ross McNairn]]></itunes:name></itunes:owner><itunes:author><![CDATA[Ross McNairn]]></itunes:author><googleplay:owner><![CDATA[rmcnairn@substack.com]]></googleplay:owner><googleplay:email><![CDATA[rmcnairn@substack.com]]></googleplay:email><googleplay:author><![CDATA[Ross McNairn]]></googleplay:author><itunes:block><![CDATA[Yes]]></itunes:block><item><title><![CDATA[Simple product frameworks: Frequency]]></title><description><![CDATA[Frequency is the lifeblood of sustainable growth]]></description><link>https://www.rossmcnairn.com/p/the-first-lesson-i-was-taught-when</link><guid isPermaLink="false">https://www.rossmcnairn.com/p/the-first-lesson-i-was-taught-when</guid><dc:creator><![CDATA[Ross McNairn]]></dc:creator><pubDate>Mon, 21 Oct 2024 09:24:56 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!jumh!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F09571f34-ee8c-4820-9ae3-257b7ee1c099_1476x1050.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Years ago, before I made the leap into software engineering I was training as a lawyer in Edinburgh.  I sent one cold email to the CEO of the hottest unicorn startup in the country and astonishingly he replied, inviting me for a coffee.</p><p>We chatted about a great many things, largely around what fundamental skills a tech entrepreneur should be building.  However the part of that meeting that sticks in my mind most acutely was the diagram that he drew on the back of a napkin.  &#8220;Frequency is everything&#8221;.    Its growth, its habit, its the great unlock.  Over the years I heard different framings of the same guidance with googles &#8220;toothbrush test&#8221; and alike.  However this model was always the cleanest representation for me.</p><p>For over ten years i&#8217;ve been meaning to write this up in the hope that the lesson he taught me that day would in some way be helpful to others.  It&#8217;s guided a huge amount of how I think about building product over the years.   I often shared this post below as a PDF with other execs and PMs that worked for me, i&#8217;ve kept it as raw as possible.</p><p>It&#8217;s deceptively simple by design.</p><p><strong>Frequency</strong></p><p>Understanding the relationship between the frequency with which a user interacts a given service (<em>and extracts value</em>) and the percentage of the population to which that service applies can help give an indication of how quickly that service will organically grow.</p><p>The broad rationale is similar to that of general marketing psychology when brands look to create awareness with impressions.&nbsp; The more front-of-mind your brand or service, the more people think about it, speak about it etc, the faster it grows.  </p><p>At its very simplest:&nbsp; <strong>Services with a broad appeal that provide utility very often, will grow faster</strong>.&nbsp;</p><p>Consider the following graph, along the Y axis, we plot the % of a given population that can draw value from a company&#8217;s service.&nbsp; Along the X, how often users extract value (<em>It's worth noting that the nature of these interactions are key, for example email spam is likely to have the inverse effect</em>).</p><p>On first pass (very roughly) it&#8217;s simple to drop technology companies from across the spectrum.&nbsp;&nbsp;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!jumh!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F09571f34-ee8c-4820-9ae3-257b7ee1c099_1476x1050.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!jumh!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F09571f34-ee8c-4820-9ae3-257b7ee1c099_1476x1050.png 424w, https://substackcdn.com/image/fetch/$s_!jumh!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F09571f34-ee8c-4820-9ae3-257b7ee1c099_1476x1050.png 848w, https://substackcdn.com/image/fetch/$s_!jumh!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F09571f34-ee8c-4820-9ae3-257b7ee1c099_1476x1050.png 1272w, https://substackcdn.com/image/fetch/$s_!jumh!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F09571f34-ee8c-4820-9ae3-257b7ee1c099_1476x1050.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!jumh!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F09571f34-ee8c-4820-9ae3-257b7ee1c099_1476x1050.png" width="1456" height="1036" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/09571f34-ee8c-4820-9ae3-257b7ee1c099_1476x1050.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1036,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:866767,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!jumh!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F09571f34-ee8c-4820-9ae3-257b7ee1c099_1476x1050.png 424w, https://substackcdn.com/image/fetch/$s_!jumh!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F09571f34-ee8c-4820-9ae3-257b7ee1c099_1476x1050.png 848w, https://substackcdn.com/image/fetch/$s_!jumh!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F09571f34-ee8c-4820-9ae3-257b7ee1c099_1476x1050.png 1272w, https://substackcdn.com/image/fetch/$s_!jumh!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F09571f34-ee8c-4820-9ae3-257b7ee1c099_1476x1050.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><em>linkedin is an evolution of the CV website monster and the high frequency social content helped drive its growth</em></p><p>In the top right you have a clustering of the tech hyper-scalers.&nbsp; These &#8216;category 1&#8217; companies are unlikely to have been envisaged in a world without the internet, now, without mobile, they apply to very broad bases of users and sit at the heart of many peoples lives. They become verbs, they typically see explosive hyper-growth.&nbsp; While they often have a high frequency core model they fall back on, they are aggressively executing on a strategy to increase the frequency of interaction with their services, diversifying their offerings continually.  Amazons prime is there to give you a single hook to hit their services across multiple touch points.  Meta MnA strategy is to acquire any free touchpoint you might have social interaction even if they are individually loss making.</p><p>The next band down, the &#8216;category 2&#8217; companies are businesses that are either relatively specialist and have not yet broadened their appeal, or are revolutions of traditional industries.</p><p>The final &#8216;category 3&#8217; companies are web enabled businesses. Largely a replica of an offline business model, they could be considered a bricks and mortar business with a website.</p><p>Of particular note should be the evolution that some models are capable of going through.&nbsp; For example a recruitment site like monster.com, was evolved by Reid Hoffman into linked-in, as they l<a href="https://www.slideshare.net/a16z/network-effects-59206938?ref=http://wpcomwidgets.com/?wpcom_origin=https%3A%2F%2Fa16z.wordpress.com">everaged the network effect of your </a>professional network to crowd source content and drive high value, high frequency interactions. To move from being a CV site that you would update every 10 years between jobs, into a network used daily.&nbsp;</p><p>The example we always referenced at Skyscanner was the Edin-bus app (effectively a scheduling app for the bus).  Within the population of Edinburgh, this product, which had a frequency of twice a day, grew at multiples faster organically than the Skyscanner flights app, which people used a few times a year. </p><p>Put simply, if you want to grow without paying google, one of the cleanest and simplest north stars is frequency. </p>]]></content:encoded></item><item><title><![CDATA[Applied AI software engineering ]]></title><description><![CDATA[Guest post for the pragmatic engineer - now available for everyone.]]></description><link>https://www.rossmcnairn.com/p/applied-ai-software-engineering</link><guid isPermaLink="false">https://www.rossmcnairn.com/p/applied-ai-software-engineering</guid><dc:creator><![CDATA[Ross McNairn]]></dc:creator><pubDate>Mon, 16 Sep 2024 10:03:16 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/51a68e74-1cf0-437e-ad4f-de95d5ff8e4b_1782x614.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>This was a guest post that i wrote for the popular engineering blog the Pragmatic Engineer.  This was a paid post only accessible to subscribers for the past few months.  I wanted to make it generally available on this blog.</p><p>Many of these lessons where learnt in the early stages of building <a href="https://www.wordsmith.ai/">Wordsmith.ai</a>. This is a legal toolkit for in-house lawyers. We give them a suite of tools that are usually only available to lawyers that are part of major firms, from document management, to professional support lawyers and chatbots.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.rossmcnairn.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Ross McNairn's Blog! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p>I hope this is in someway helpful.</p><p>Today, we cover:</p><ol><li><p>Providing an LLM with additional context</p></li><li><p>The simplest RAGs</p></li><li><p>What is a RAG pipeline?</p></li><li><p>Preparing the RAG pipeline data store</p></li><li><p>Bringing it all together</p></li><li><p>RAG limitations</p></li><li><p>Real-world learnings building RAG pipelines</p></li></ol><p>Today&#8217;s article includes a &#8220;code-along,&#8221; so you can build your own RAG. View the code used in this article <a href="https://github.com/wordsmith-ai/hello-wordsmith">at this GitHub repository: hello-wordsmith</a>.</p><h4>Introduction</h4><p>This post is designed to help you get familiar with one of the most fundamental patterns of AI software engineering: RAG, aka Retrieval Augentented Generation.</p><p>I co-founded a legal tech startup called <a href="https://www.wordsmith.ai/">Wordsmith</a>, where we are building a platform for running a modern in-house legal team. Our founding team previously worked at Meta, Skyscanner, Travelperk and KPMG.</p><p>We are working in a targeted domain &#8211; legal texts &#8211; and building AI agents to give in-house legal teams a suite of AI tools to remove bottlenecks and improve how they work with the rest of the business. Performance and accuracy are key characteristics for us, so we&#8217;ve invested a lot of time and effort in how to best enrich and &#8220;turbo charge&#8221; these agents with custom data and objectives.</p><p>We ended up building our RAG pipeline, and I will now walk you through how we did it and why. We&#8217;ll go into our learnings, and <a href="https://www.wordsmith.ai/benchmarks">how we benchmark</a> our solution. I hope that the lessons we learned are useful for all budding AI engineers.</p><h2>1. Providing an LLM with additional context</h2><p>Have you ever asked ChatGPT a question it does not know how to answer, or its answer is too high level? We&#8217;ve all been there, and all too often, interacting with a GPT feels like talking to someone who speaks really well, but doesn&#8217;t know the facts. Even worse, they can make up the information in their responses!</p><p>Here is one example. On 1 February 2024, during an earnings call, Mark Zuckerberg laid out the strategic benefits of Meta&#8217;s AI strategy. But when we ask ChatGPT a question about this topic, this model will make up an answer that is high-level, but is not really what we want:</p><p><em>ChatGPT 3.5&#8217;s answer to a question about Meta&#8217;s AI strategy. The answer is generalized, and misses a critical source which answers the question</em></p><p>This makes sense, as the model&#8217;s training cutoff date was before Mark Zuckerberg made the comments. If the model had access to that information, it would have likely been able to summarize the facts of that meeting, which are:</p><blockquote><p>&#8220;So I thought it might be useful to lay out the strategic benefits [of Meta&#8217;s open source strategy) here. (...)</p><p>The short version is that open sourcing improves our models. (...)<br><br>First, open-source software is typically safer and more secure as well as more compute-efficient to operate due to all the ongoing feedback, scrutiny and development from the community. (...)<br><br>Second, open-source software often becomes an industry standard. (...)</p><p>Third, open source is hugely popular with developers and researchers. (...)</p><p>The next part of our playbook is just taking a long-term approach towards the development.&#8221;</p></blockquote><p><strong>LLMs&#8217; understanding of the world is limited to the data they&#8217;re trained on. </strong>If you&#8217;ve been using ChatGPT for some time, you might remember this constraint in the earlier version of ChatGPT, when the bot responded: &#8220;I have no knowledge after April 2021,&#8221; in several cases.&nbsp;</p><h4>Providing an LLM with additional information</h4><p>There is a bunch of additional information you want an LLM to use. In the above example, I might have the transcripts of all of Meta&#8217;s shareholders meetings that I want the LLM to use. But how can we provide this additional information to an existing model?</p><h4>Option 1: input via a prompt</h4><p>The most obvious solution is to input the additional information via a prompt; for example, by prompting &#8220;Using the following information: [input a bunch of data] please answer the question of [ask your question].&#8221;</p><p>This is a pretty good approach. The biggest problem is that this may not scale because of these reasons:</p><ul><li><p><strong>The input tokens limit</strong>. Every model has an input prompt token limit. This is 2.048 tokens for GPT-3, 32,768 for GPT-4, 4,096 for Anthropic models. Google&#8217;s Gemini model allows for an impressive one million token limit. While a million-token limit greatly increases the possibilities, it might still be too low for use cases with a lot of additional text to input.</p></li><li><p><strong>Performance.</strong> The performance of LLMs substantially decreases with longer input prompts; in particular, you get degradation of context in the middle of your prompt. Even when creating long input prompts is a possibility, the performance tradeoff might make it impractical.</p></li></ul><h4>Option 2: fine-tune the model</h4><p>We know LLMs are based on a massive weights matrix. <em>Read more on <a href="https://newsletter.pragmaticengineer.com/i/141865286/how-does-chatgpt-work-a-refresher">how ChatGPT works in this Pragmatic Engineer issue.</a> All LLMs use the same principles.</em></p><p>An option is to update these weight matrices based on additional information we&#8217;d like our model to know. This can be a good option, but it is a much higher upfront cost in terms of time, money, and computing resources. Also, it can only be done with access to the model&#8217;s weightings, which is not the case when you use models like ChatGPT, Anthropic, and other &#8220;closed source&#8221; models.</p><h4>Option 3: RAG</h4><p>The term &#8216;RAG&#8217; originated in a <a href="https://arxiv.org/pdf/2005.11401.pdf">2020 paper</a> led by Patrick Lewis. One thing many people notice is that &#8220;Retrieval Augmented Generation&#8221; sounds a bit ungrammatical. Patrick agrees, and has said this:</p><blockquote><p>&#8220;We always planned to have a nicer-sounding name, but when it came time to write the paper, no one had a better idea.&#8221;</p></blockquote><p>RAG is a collection of techniques which help to modify a LLM, so it can fill in the gaps and speak with authority, and some RAG implementations even let you cite sources. The biggest benefits of the RAG approach:</p><p><strong>1. Give a LLM domain-specific knowledge</strong> You can pick what data you want your LLM to draw from, and even turn it into a specialist on any topic there is data about.&nbsp;</p><p>This flexibility means you can also extend your LLMs&#8217; awareness far beyond the model&#8217;s training cutoff dates, and even expose it to near-real time data, if available.<br><br></p><p><strong>2. Optimal cost and speed</strong>. For all but a handful of companies, it's impractical to even consider training their own foundational model as a way to personalize the output of an LLM, due to the very high cost and skill thresholds.&nbsp;</p><p>In contrast, deploying a RAG pipeline will get you up-and-running relatively quickly for minimal cost. The tooling available means a single developer can have something very basic functional in a few hours.<br><br></p><p><strong>3. Reduce hallucinations.</strong> &#8220;Hallucination&#8221; is the term for when LLMs &#8220;make up&#8221; responses. A well-designed RAG pipeline that presents relevant data will all but eliminate this frustrating side effect, and your LLM will speak with much greater authority and relevance on the domain about which you have provided data.<br><br>For example, in the legal sector it&#8217;s often necessary to ensure an LLM draws its insight from a specific jurisdiction. Take the example of asking a model a seemingly simple question, like:</p><p>How do I hire someone?<br><br>Your LLM will offer context based on the training data. However, you do <em>not</em> want the model to extract hiring practices from a US state like California, and combine this with British visa requirements!&nbsp;</p><p>With RAG, you control the underlying data source, meaning you can scope the LLM to only have access to a single jurisdiction&#8217;s data, which ensures responses are consistent.<br><br></p><p><strong>4. Better transparency and observability</strong>. Tracing inputs and answers through LLMs is very hard. The LLM can often feel like a &#8220;black box,&#8221; where you have no idea where some answers come from. With RAG, you see the additional source information injected, and debug your responses.</p><h2>2. The simplest RAGs</h2><p>The best way to understand new technology is often just to play with it. Getting a basic implementation up and running is relatively simple, and can be done with just a few lines of code. To help, Wordsmith has <a href="https://github.com/wordsmith-ai/hello-wordsmith">created a wrapper</a> around the <a href="https://www.llamaindex.ai/">LlamaIndex</a> open source project to help abstract away some complexity. You can <a href="https://github.com/wordsmith-ai/hello-wordsmith">get up and running</a>, easily. It has a README file in place that will get you set up with a local RAG pipeline on your machine, and which chunks and embeds a copy of the US Constitution, and lets you search away with your command line.</p><p>This is as simple as RAGs get; you can &#8220;swap out&#8221; the additional context provided in this example by simply changing the source text documents!</p><p>This article is designed as a code-along, so I'm going to link you to sections of <a href="https://github.com/wordsmith-ai/hello-wordsmith">this repo</a>, so you can see where specific concepts manifest in code.</p><p>To follow along with the example, the following is needed:</p><ul><li><p>An active OpenAI subscription with API usage. <a href="https://platform.openai.com/settings/organization/billing/overview">Set one up here</a> if needed. <em>Note: running a query will cost in the realm of $0.25-$0.50 per run.</em></p></li><li><p><a href="https://github.com/wordsmith-ai/hello-wordsmith">Follow the instructions</a> to set up a virtual Python environment, configure your OpenAI key, and start the virtual assistant.</p></li></ul><p>This example will load the text of the US constitution <a href="https://github.com/wordsmith-ai/hello-wordsmith/blob/main/hello_wordsmith/public_wordsmith_dataset/us_constitution.txt">from this text file</a>, as a RAG input. However, the application can be extended to load your own data from a text file, and to &#8220;chat&#8221; with this data.</p><p>Here&#8217;s an example of how the application works when set up, and when the OpenAI API key is configured:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!PhkO!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F918d64e1-8364-4b6a-89fd-71b9a0007e7a_1180x772.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!PhkO!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F918d64e1-8364-4b6a-89fd-71b9a0007e7a_1180x772.png 424w, https://substackcdn.com/image/fetch/$s_!PhkO!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F918d64e1-8364-4b6a-89fd-71b9a0007e7a_1180x772.png 848w, https://substackcdn.com/image/fetch/$s_!PhkO!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F918d64e1-8364-4b6a-89fd-71b9a0007e7a_1180x772.png 1272w, https://substackcdn.com/image/fetch/$s_!PhkO!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F918d64e1-8364-4b6a-89fd-71b9a0007e7a_1180x772.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!PhkO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F918d64e1-8364-4b6a-89fd-71b9a0007e7a_1180x772.png" width="1180" height="772" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/918d64e1-8364-4b6a-89fd-71b9a0007e7a_1180x772.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:772,&quot;width&quot;:1180,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!PhkO!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F918d64e1-8364-4b6a-89fd-71b9a0007e7a_1180x772.png 424w, https://substackcdn.com/image/fetch/$s_!PhkO!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F918d64e1-8364-4b6a-89fd-71b9a0007e7a_1180x772.png 848w, https://substackcdn.com/image/fetch/$s_!PhkO!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F918d64e1-8364-4b6a-89fd-71b9a0007e7a_1180x772.png 1272w, https://substackcdn.com/image/fetch/$s_!PhkO!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F918d64e1-8364-4b6a-89fd-71b9a0007e7a_1180x772.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><em>The example RAG pipeline application answering questions using the US Constitution supplied as additional context</em></p><p>If you&#8217;ve followed along and have run this application: congratulations! You have just executed a RAG pipeline. Now, let&#8217;s get into explaining how it works.</p><h2>3. What is a RAG pipeline?</h2><p>A RAG pipeline is a collection of technologies needed to enable the capability of answering using provided context. In our example, this context is the US Constitution and our LLM model is enriched with additional data extracted from the US Constitution document.&nbsp;</p><p>Here are the steps to building a RAG pipeline:</p><p>&nbsp;<br><strong>Step 1:</strong> Take an inbound query and deconstruct it into relevant concepts<br><strong>Step 2: </strong>Collect similar concepts from your data store<br><strong>Step 3:</strong> Recombine these concepts with your original query to build a more relevant, authoritative answer.</p><p>Weaving this together:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Pvwb!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97f7ed7a-fb3a-4f56-8115-6379b094e06d_1556x1572.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Pvwb!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97f7ed7a-fb3a-4f56-8115-6379b094e06d_1556x1572.png 424w, https://substackcdn.com/image/fetch/$s_!Pvwb!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97f7ed7a-fb3a-4f56-8115-6379b094e06d_1556x1572.png 848w, https://substackcdn.com/image/fetch/$s_!Pvwb!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97f7ed7a-fb3a-4f56-8115-6379b094e06d_1556x1572.png 1272w, https://substackcdn.com/image/fetch/$s_!Pvwb!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97f7ed7a-fb3a-4f56-8115-6379b094e06d_1556x1572.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Pvwb!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97f7ed7a-fb3a-4f56-8115-6379b094e06d_1556x1572.png" width="1456" height="1471" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/97f7ed7a-fb3a-4f56-8115-6379b094e06d_1556x1572.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1471,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Pvwb!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97f7ed7a-fb3a-4f56-8115-6379b094e06d_1556x1572.png 424w, https://substackcdn.com/image/fetch/$s_!Pvwb!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97f7ed7a-fb3a-4f56-8115-6379b094e06d_1556x1572.png 848w, https://substackcdn.com/image/fetch/$s_!Pvwb!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97f7ed7a-fb3a-4f56-8115-6379b094e06d_1556x1572.png 1272w, https://substackcdn.com/image/fetch/$s_!Pvwb!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97f7ed7a-fb3a-4f56-8115-6379b094e06d_1556x1572.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><em>A RAG pipeline at work. It extends the context an LLM has access to, by fetching similar concepts from the data store to answer a question</em></p><p>While this process appears simple, there is quite a bit of nuance in how to approach each step. A number of decisions are required to tailor to your use case, starting with how to prepare the data for use in your pipeline.</p><h2>4. Preparing the RAG pipeline data store</h2><p>To start, we need to identify the data we will use to enrich responses with. We then load it into a vector data store, which is what we use during the search phase.</p><p><em>Steps in preparing a RAG pipeline</em></p><h4>Load the data</h4><p>Before we can harness the data, we need to convert it into a format that lets us perform the manipulations we will need, such as creating embeddings (basically, vectors.)</p><p>In our example RAG pipeline, we use the <a href="https://www.llamaindex.ai/">LlamaIndex</a>, a comprehensive data framework to bridge storing data, and make this data accessible to LLMs. <em>Another popular option for an LLM data framework is <a href="https://python.langchain.com/docs/modules/data_connection/document_loaders/pdf">Langchain</a>.</em></p><p>Here&#8217;s how we prepare our data <a href="https://github.com/wordsmith-ai/hello-wordsmith/blob/main/hello_wordsmith/datastores.py#L38-L42">in the code</a>:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!QEh6!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa9f338e9-d1ab-4e51-b8ef-d6edca0a5af0_1328x204.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!QEh6!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa9f338e9-d1ab-4e51-b8ef-d6edca0a5af0_1328x204.png 424w, https://substackcdn.com/image/fetch/$s_!QEh6!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa9f338e9-d1ab-4e51-b8ef-d6edca0a5af0_1328x204.png 848w, https://substackcdn.com/image/fetch/$s_!QEh6!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa9f338e9-d1ab-4e51-b8ef-d6edca0a5af0_1328x204.png 1272w, https://substackcdn.com/image/fetch/$s_!QEh6!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa9f338e9-d1ab-4e51-b8ef-d6edca0a5af0_1328x204.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!QEh6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa9f338e9-d1ab-4e51-b8ef-d6edca0a5af0_1328x204.png" width="1328" height="204" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a9f338e9-d1ab-4e51-b8ef-d6edca0a5af0_1328x204.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:204,&quot;width&quot;:1328,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!QEh6!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa9f338e9-d1ab-4e51-b8ef-d6edca0a5af0_1328x204.png 424w, https://substackcdn.com/image/fetch/$s_!QEh6!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa9f338e9-d1ab-4e51-b8ef-d6edca0a5af0_1328x204.png 848w, https://substackcdn.com/image/fetch/$s_!QEh6!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa9f338e9-d1ab-4e51-b8ef-d6edca0a5af0_1328x204.png 1272w, https://substackcdn.com/image/fetch/$s_!QEh6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa9f338e9-d1ab-4e51-b8ef-d6edca0a5af0_1328x204.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p><em>Loading all files in the &#8220;public_wordsmith_dataset&#8221; directory. For now, this is one file: the <a href="https://github.com/wordsmith-ai/hello-wordsmith/blob/main/hello_wordsmith/public_wordsmith_dataset/us_constitution.txt">us_constitution.txt</a>. You can add any file to this directory to include in the RAG pipeline.</em></p><p><strong>Cleaning up data before storing it</strong> is a common-enough step with real world RAG applications, but we&#8217;ve omitted it from our simple use case. For example, if your application uses web page data as HTML files in the RAG pipeline, then you&#8217;ll need to add preprocessing to remove HTML tags and anything else that is irrelevant for text processing.</p><p>There is an ever-growing list of services and tools that assist with the cleaning of data. <a href="https://www.firecrawl.dev/">FireCrawl</a> is a good choice for working with web pages. In general, it helps get from &#8220;raw&#8221; data to &#8220;cleaned&#8221; data. There are many similar tools which clean data for AI use cases; it&#8217;s a vibrant and fast-evolving part of the AI ecosystem.</p><h4>Split and chunk the data</h4><p>Once we&#8217;ve loaded and cleaned the data, we want to split our document into &#8216;chunks.&#8217; These are the parts we want to retrieve and pass on as context to our LLM.</p><p>With RAG pipelines, it&#8217;s common enough to work with long documents, such as wiki or Confluence pages, contracts, and other lengthy documentation. So, why not just feed the whole document into the LLM? Why &#8220;chunk it up?&#8221; The reason is that feeding a long document into an LLM can cause these issues:</p><ul><li><p><strong>Degraded performance.</strong> LLMs like ChatGPT have <a href="https://en.wikipedia.org/wiki/Attention_(machine_learning)">self-attention</a> (every token being aware of every other token) <a href="https://newsletter.pragmaticengineer.com/i/141865286/scalability-challenge-from-self-attention">scale quadratically</a>. This means that when predicting the 100th token, around 10,000 operations are needed. But to predict the 1,000th token, circa 1 million operations need to be done. This means that the longer the input, the worse the output performance of the LLM.&nbsp;</p></li><li><p><strong>Less useful output. We&#8217;ve observed that inputting a long document results in the LLM receiving a lot of irrelevant data, and responses can become confusing.</strong></p></li><li><p><strong>Increased cost. The longer the input, the higher the cost of operating the model. This cost will be crystal clear when using an API like OpenAI, which bills you. If running your own infrastructure, you&#8217;ll observe higher required compute resource usage, which translates to higher compute cost.</strong></p></li></ul><p>Therefore, deconstructing a long document into &#8220;small enough&#8221; pieces is a smart move. With these pieces sized correctly, we can pass in the relevant pieces, and make the LLMs&#8217; answers more specific, accurate, and faster, at a lower cost!</p><p><strong>Deciding how to chunk your data is a major decision with RAG pipelines. </strong>There are many options to choose from when chunking data, but all choices have their own set of tradeoffs. Here are some examples:</p><ul><li><p>The simplest approach: break the text into 250-500 character chunks.</p></li><li><p>A slightly more advanced approach: split the text by paragraph.</p></li><li><p>Even more advanced option: divide chunks by &#8220;concept&#8221; and &#8203;&#8203;do some preprocessing to make breakpoints between chunks logical.&nbsp;</p></li></ul><p>Chunking strategies is an area you can get very deep into. There&#8217;s a variety of strategies to use, which the article <a href="https://www.pinecone.io/learn/chunking-strategies/">Chunking strategies for LLM applications</a> by Roie Schwaber-Cohen, goes through:</p><ul><li><p><strong>Fixed-size chunking: </strong>split by the number of tokens</p></li><li><p><strong>&#8220;Content-aware&#8221; chunking: chunking by sentence.</strong></p></li><li><p><strong>Recursive chunking</strong>: divide the input text into smaller chunks in a hierarchical, iterative manner using a set of separators</p></li><li><p><strong>Specialized chunking</strong>: when working with structured and formatted content like Markdown or LaTeX</p></li><li><p><strong>Semantic chunking</strong>: attempting to take the meaning of segments within the document.</p></li></ul><p>In general, smaller chunks tend to help create smaller, more relevant concepts when you retrieve them. At the same time, they lead to very narrow responses because small chunks can become disconnected from related chunks.</p><p>Chunking is more an art than a science. My advice is to spend plenty of time iterating your chunking strategy! Get it right for <em>your</em> use case and the source data you have to work with.</p><p>In our code, chunking happens <a href="https://github.com/wordsmith-ai/hello-wordsmith/blob/8399c8753841e8eff3e083cf582f2007ac2bbb8b/hello_wordsmith/wordsmith.py#L16">here</a>:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!C0io!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89e330d1-f769-4995-b4fd-837f9b32eeb4_1082x570.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!C0io!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89e330d1-f769-4995-b4fd-837f9b32eeb4_1082x570.png 424w, https://substackcdn.com/image/fetch/$s_!C0io!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89e330d1-f769-4995-b4fd-837f9b32eeb4_1082x570.png 848w, https://substackcdn.com/image/fetch/$s_!C0io!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89e330d1-f769-4995-b4fd-837f9b32eeb4_1082x570.png 1272w, https://substackcdn.com/image/fetch/$s_!C0io!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89e330d1-f769-4995-b4fd-837f9b32eeb4_1082x570.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!C0io!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89e330d1-f769-4995-b4fd-837f9b32eeb4_1082x570.png" width="1082" height="570" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/89e330d1-f769-4995-b4fd-837f9b32eeb4_1082x570.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:570,&quot;width&quot;:1082,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!C0io!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89e330d1-f769-4995-b4fd-837f9b32eeb4_1082x570.png 424w, https://substackcdn.com/image/fetch/$s_!C0io!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89e330d1-f769-4995-b4fd-837f9b32eeb4_1082x570.png 848w, https://substackcdn.com/image/fetch/$s_!C0io!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89e330d1-f769-4995-b4fd-837f9b32eeb4_1082x570.png 1272w, https://substackcdn.com/image/fetch/$s_!C0io!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F89e330d1-f769-4995-b4fd-837f9b32eeb4_1082x570.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><em>The code that does the chunking. We use a fixed-size chunking strategy, breaking our document down every 512 characters</em></p><p>This step splits the data up and embeds it, which requires a little more explanation.</p><h4>Create embeddings</h4><p>We&#8217;ve broken our documents into chunks, hooray! But how will we know which chunks will be relevant for a question that&#8217;s asked?</p><p>Here&#8217;s how Evan Morikawa of OpenAI defines the concept of embeddings in the article, <a href="https://newsletter.pragmaticengineer.com/i/141865286/how-does-chatgpt-work-a-refresher">Scaling ChatGPT</a>:</p><blockquote><p>&#8220;An embedding is a multi-dimensional representation of a token. We [OpenAI] explicitly train <a href="https://platform.openai.com/docs/guides/embeddings">some of our models</a> to explicitly allow the capture of semantic meanings and relationships between words or phrases. For example, the embedding for &#8220;dog&#8221; and &#8220;puppy&#8221; are closer together in several dimensions than &#8220;dog&#8221; and &#8220;computer&#8221; are. These multi-dimensional embeddings help machines understand human language more efficiently.&#8221;</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!DjWF!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fdf1ba3-410d-4955-b602-15f47d07fddc_1600x887.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!DjWF!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fdf1ba3-410d-4955-b602-15f47d07fddc_1600x887.png 424w, https://substackcdn.com/image/fetch/$s_!DjWF!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fdf1ba3-410d-4955-b602-15f47d07fddc_1600x887.png 848w, https://substackcdn.com/image/fetch/$s_!DjWF!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fdf1ba3-410d-4955-b602-15f47d07fddc_1600x887.png 1272w, https://substackcdn.com/image/fetch/$s_!DjWF!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fdf1ba3-410d-4955-b602-15f47d07fddc_1600x887.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!DjWF!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fdf1ba3-410d-4955-b602-15f47d07fddc_1600x887.png" width="1456" height="807" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3fdf1ba3-410d-4955-b602-15f47d07fddc_1600x887.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:807,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!DjWF!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fdf1ba3-410d-4955-b602-15f47d07fddc_1600x887.png 424w, https://substackcdn.com/image/fetch/$s_!DjWF!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fdf1ba3-410d-4955-b602-15f47d07fddc_1600x887.png 848w, https://substackcdn.com/image/fetch/$s_!DjWF!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fdf1ba3-410d-4955-b602-15f47d07fddc_1600x887.png 1272w, https://substackcdn.com/image/fetch/$s_!DjWF!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3fdf1ba3-410d-4955-b602-15f47d07fddc_1600x887.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><em>Creating an embedding from a token (a string). Source: <a href="https://newsletter.pragmaticengineer.com/i/141865286/how-does-chatgpt-work-a-refresher">Scaling ChatGPT</a></em></p></blockquote><p>Let me offer an alternative way for thinking about embeddings which I use, and that builds on the concept of vectors, and K nearest neighbor vector search.</p><p><em><strong>Vector. </strong></em>A vector is a string of numbers, which allows a piece of information like a sentence, or a paragraph to be expressed in a way an algorithm understands. When compared correctly, similar, related concepts are closer to each other in the vector space.<br><br>Imagine using keyword search and a more traditional search method for searching chunks; retrieving all the records from a database would typically require that you pre-categorized all the data, or had an index that could help you look them up. But we want something more flexible, as we don't have well structured metadata for every query we might want to run. Using vectors makes it easier to deal with this kind of problem space.</p><p><em><strong>K-nearest neighbors (KNN.) </strong></em>KNN is an algorithm that takes a bunch of vectors and organizes them, based on how similar they are to each other. Using KNN on a collection of vectors, we find &#8220;similar concept groups".</p><p>A vector embedding is a vector that represents a concept like a token, a sentence, a paragraph, or anything else. It&#8217;s effectively the &#8220;fingerprint of an idea.&#8221;</p><p>Using vector embeddings makes it simpler for the AI model to interact with these concepts. It also makes it straightforward to search for similar concepts when it wants to query your database. For example the Vector of an <em>&#8216;apple&#8217;</em> and the vector of a <em>&#8216;pear&#8217;</em> will be more similar than the vectors of an <em>&#8216;apple&#8217;</em> and an <em>&#8216;app&#8217;</em>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!4Bk9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b98e2ac-e4bf-4ed4-9816-15674fce68f3_1382x566.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!4Bk9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b98e2ac-e4bf-4ed4-9816-15674fce68f3_1382x566.png 424w, https://substackcdn.com/image/fetch/$s_!4Bk9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b98e2ac-e4bf-4ed4-9816-15674fce68f3_1382x566.png 848w, https://substackcdn.com/image/fetch/$s_!4Bk9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b98e2ac-e4bf-4ed4-9816-15674fce68f3_1382x566.png 1272w, https://substackcdn.com/image/fetch/$s_!4Bk9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b98e2ac-e4bf-4ed4-9816-15674fce68f3_1382x566.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!4Bk9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b98e2ac-e4bf-4ed4-9816-15674fce68f3_1382x566.png" width="1382" height="566" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7b98e2ac-e4bf-4ed4-9816-15674fce68f3_1382x566.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:566,&quot;width&quot;:1382,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!4Bk9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b98e2ac-e4bf-4ed4-9816-15674fce68f3_1382x566.png 424w, https://substackcdn.com/image/fetch/$s_!4Bk9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b98e2ac-e4bf-4ed4-9816-15674fce68f3_1382x566.png 848w, https://substackcdn.com/image/fetch/$s_!4Bk9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b98e2ac-e4bf-4ed4-9816-15674fce68f3_1382x566.png 1272w, https://substackcdn.com/image/fetch/$s_!4Bk9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7b98e2ac-e4bf-4ed4-9816-15674fce68f3_1382x566.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><em>Turning words into embedding and visualizing those embeddings in a 2D space. Source: <a href="https://medium.com/@hari4om/word-embedding-d816f643140">Hariom Gautam on Medium</a></em></p><p><strong>Pre-trained embedding models </strong>generate a vector embedding from any input. Thanks to the pre-training, they already categorize the inputs reliably enough.</p><p>OpenAI offers <a href="https://platform.openai.com/docs/guides/embeddings">an API called Embeddings</a> that can be used to process chunks. Feed in a chunk, and receive an embedded vector in return. Of course, using this API comes with its own cost. The collected works of William Shakespeare are 3,000 pages long, or circa 835,000 words. Embedding the complete text with OpenAI&#8217;s ada v2 embedding model would cost about $0.10 (as the cost is <a href="https://openai.com/api/pricing/">$0.10 per 1M tokens</a>, and an English word usually comes to <a href="https://gpt.space/blog/understanding-openai-gpt-tokens-a-comprehensive-guide">about 1.3 tokens</a>)</p><p>When we query our data, the query goes through a similar process to embedding. The semantics of the query are deconstructed into vectors, and these vectors make it easy to compare with stored data.</p><p>The open source community also offers some exceptional options. The online AI community, Hugging Face, has <a href="https://huggingface.co/spaces/mteb/leaderboard">a leaderboard of the best embedding models available</a>, ranking them according to the Massive Text Embedding Benchmark (MTEB.)</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!GGP3!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffbc0a3dc-c810-4d49-a5f0-c6c7eab838d7_1600x1087.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!GGP3!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffbc0a3dc-c810-4d49-a5f0-c6c7eab838d7_1600x1087.png 424w, https://substackcdn.com/image/fetch/$s_!GGP3!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffbc0a3dc-c810-4d49-a5f0-c6c7eab838d7_1600x1087.png 848w, https://substackcdn.com/image/fetch/$s_!GGP3!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffbc0a3dc-c810-4d49-a5f0-c6c7eab838d7_1600x1087.png 1272w, https://substackcdn.com/image/fetch/$s_!GGP3!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffbc0a3dc-c810-4d49-a5f0-c6c7eab838d7_1600x1087.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!GGP3!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffbc0a3dc-c810-4d49-a5f0-c6c7eab838d7_1600x1087.png" width="1456" height="989" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fbc0a3dc-c810-4d49-a5f0-c6c7eab838d7_1600x1087.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:989,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!GGP3!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffbc0a3dc-c810-4d49-a5f0-c6c7eab838d7_1600x1087.png 424w, https://substackcdn.com/image/fetch/$s_!GGP3!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffbc0a3dc-c810-4d49-a5f0-c6c7eab838d7_1600x1087.png 848w, https://substackcdn.com/image/fetch/$s_!GGP3!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffbc0a3dc-c810-4d49-a5f0-c6c7eab838d7_1600x1087.png 1272w, https://substackcdn.com/image/fetch/$s_!GGP3!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffbc0a3dc-c810-4d49-a5f0-c6c7eab838d7_1600x1087.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><em>A screenshot of LLM models ranked by MTEB. Source: <a href="https://huggingface.co/spaces/mteb/leaderboard">Hugging Face</a></em></p><p>What the best model is for you will depend on your use case, and most RAG pipelines will want to use a semantic embedding model like <a href="https://huggingface.co/docs/transformers/main/en/model_doc/bert">Bidirectional Encoder Representations from Transformers (BERT.)</a></p><h4>Store the data</h4><p>Now we&#8217;ve extracted and cleaned the data, chunked it, and created our embedding, it&#8217;s time to store it. We need to choose a database that&#8217;s effective at running our KNN operations. Vector databases are tailored to support KNN lookup with great performance, so using one will provide optimal performance for search.</p><p>Popular vector databases include <a href="https://www.pinecone.io/">Pinecone</a> and <a href="https://weaviate.io/">Weaviate</a>. Additionally, all major cloud providers offer multiple vector databases. Vector databases are a fast-evolving space, so you need to do research.</p><p>When prototyping, you can get away with using a more traditional database like MySQL or PostgreSQL to store embeddings. Should your application receive production traffic, the performance of these SQL databases will likely become critical enough to justify moving to a vector-based one.</p><h2>5. Bringing it all together</h2><p>With our data pipeline prepared, the remaining steps are surprisingly simple!</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Pvwb!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97f7ed7a-fb3a-4f56-8115-6379b094e06d_1556x1572.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Pvwb!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97f7ed7a-fb3a-4f56-8115-6379b094e06d_1556x1572.png 424w, https://substackcdn.com/image/fetch/$s_!Pvwb!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97f7ed7a-fb3a-4f56-8115-6379b094e06d_1556x1572.png 848w, https://substackcdn.com/image/fetch/$s_!Pvwb!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97f7ed7a-fb3a-4f56-8115-6379b094e06d_1556x1572.png 1272w, https://substackcdn.com/image/fetch/$s_!Pvwb!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97f7ed7a-fb3a-4f56-8115-6379b094e06d_1556x1572.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Pvwb!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97f7ed7a-fb3a-4f56-8115-6379b094e06d_1556x1572.png" width="1456" height="1471" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/97f7ed7a-fb3a-4f56-8115-6379b094e06d_1556x1572.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1471,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Pvwb!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97f7ed7a-fb3a-4f56-8115-6379b094e06d_1556x1572.png 424w, https://substackcdn.com/image/fetch/$s_!Pvwb!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97f7ed7a-fb3a-4f56-8115-6379b094e06d_1556x1572.png 848w, https://substackcdn.com/image/fetch/$s_!Pvwb!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97f7ed7a-fb3a-4f56-8115-6379b094e06d_1556x1572.png 1272w, https://substackcdn.com/image/fetch/$s_!Pvwb!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F97f7ed7a-fb3a-4f56-8115-6379b094e06d_1556x1572.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><em>A RAG pipeline at work. The data store is ready and we just need to do steps 2 and 3</em></p><p>The work that&#8217;s left to do:</p><p><strong>Step 2: </strong>Collect similar concepts from the data store. We use a vector database to query the data.&nbsp;</p><p>In our code, the very bottom line does the retrieval:</p><p><em>Collecting similar concepts (chunks) from our stored data. See this line <a href="https://github.com/wordsmith-ai/hello-wordsmith/blob/main/hello_wordsmith/query_pipeline.py#L49">in the example codebase</a></em></p><p>The &#8220;retriever&#8221; variable now contains the 20 chunks in similarity (as the value of _TOP_K_RETRIEVAL is 20).&nbsp;</p><p><strong>Step 3:</strong> Recombine these concepts with the original query to build a more relevant and authoritative answer.</p><p>With the related chunks available, we now create a new query, which is the updated query we want to pass into the LLM:<br><br></p><blockquote><p>Context information from multiple sources is below.</p><p>---------------------</p><p>{LIST OF THE 20 CHUNKS}</p><p>---------------------</p><p>"Given the information from multiple sources and not prior knowledge, answer the query.</p><p>Query: {ORIGINAL QUERY}</p><p>Answer:</p></blockquote><p>In our code, we create the above string by filling out the list of the 20 most relevant chunks (as <em>context_str</em>) and the original query (as <em>&#8203;&#8203;query_str</em>):</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!uJ3c!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbbb35f44-eb51-42c2-a6a3-38cfcd927a59_1564x1600.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!uJ3c!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbbb35f44-eb51-42c2-a6a3-38cfcd927a59_1564x1600.png 424w, https://substackcdn.com/image/fetch/$s_!uJ3c!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbbb35f44-eb51-42c2-a6a3-38cfcd927a59_1564x1600.png 848w, https://substackcdn.com/image/fetch/$s_!uJ3c!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbbb35f44-eb51-42c2-a6a3-38cfcd927a59_1564x1600.png 1272w, https://substackcdn.com/image/fetch/$s_!uJ3c!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbbb35f44-eb51-42c2-a6a3-38cfcd927a59_1564x1600.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!uJ3c!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbbb35f44-eb51-42c2-a6a3-38cfcd927a59_1564x1600.png" width="1456" height="1490" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/bbb35f44-eb51-42c2-a6a3-38cfcd927a59_1564x1600.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1490,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!uJ3c!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbbb35f44-eb51-42c2-a6a3-38cfcd927a59_1564x1600.png 424w, https://substackcdn.com/image/fetch/$s_!uJ3c!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbbb35f44-eb51-42c2-a6a3-38cfcd927a59_1564x1600.png 848w, https://substackcdn.com/image/fetch/$s_!uJ3c!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbbb35f44-eb51-42c2-a6a3-38cfcd927a59_1564x1600.png 1272w, https://substackcdn.com/image/fetch/$s_!uJ3c!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbbb35f44-eb51-42c2-a6a3-38cfcd927a59_1564x1600.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><em>The code to generate our query pipeline. This is it! <a href="https://github.com/wordsmith-ai/hello-wordsmith/blob/main/hello_wordsmith/query_pipeline.py#L24">Browse the code here.</a></em></p><p>If you&#8217;ve followed this code-along, then congratulations! You now know how to code a simple RAG pipeline!</p><h2>6. RAG limitations</h2><p>RAG is a powerful set of tools that can help you focus AI onto your data. However, it's not a perfect fit for all use-cases, and there are some areas in which RAG does poorly.</p><h4>Summarization</h4><p>The RAG approach will not result in great output for summarizing. This is because with the RAG approach, documents are broken into many small sections, meaning that results will be poor if seeking insight that needs context from an <em>entire</em> document. For example, if asked: &#8220;give a summary of the key points in our contract with Microsoft,&#8221; an LLM that performs this well must process the entire contract, not just 2 or 3 chunks that are the assigned &#8220;key parts.&#8221;</p><p>For a model that performs well with summarization paths, consider an alternative approach of detecting use cases for which you route summarization queries to a different pipeline. For such cases, load the entire document that needs to be summarized. Doing so results in worse performance and higher cost, but it&#8217;s the only way to get a more accurate response.</p><h4>Multi-part, or hybrid questions</h4><p>More complex questions often have an element of reasoning. Take the question:</p><p>&#8220;What percentage of agreements have their governing law in South Africa?&#8221;</p><p>This question needs to be broken down. Also, data needs to be collected from other data sources to determine the correct answer. For this specific question, to answer it correctly we need to follow these steps:</p><ol><li><p>Retrieve all agreements with their law in South Africa.</p></li><li><p>Determine how many such agreements there are.</p></li><li><p>Process the math to recombine the output. It&#8217;s <em>important to note that most LLMs are not good with math and you may want to not use an LLM for it!</em></p></li></ol><p>To do a good job with such a complex query, RAG alone is insufficient, although it&#8217;s necessary at some steps. For the best results, build a higher-level orchestration layer, and coordinate other AI agents working in this pipeline to process complex queries.</p><h2>7. Real-world learnings</h2><p>We&#8217;ve only scratched the surface of RAG in building this simple pipeline. For the more technically-minded reader who wants to experiment in this space, below are seven things that we at Wordsmith wish we&#8217;d had known before, which would&#8217;ve saved time while building our AI solutions.</p><p><strong>#1: Natural language is not always the best input for an LLM</strong></p><p>Instead of using plain text, it is often better to use more structured input and output with LLMs; for example, <a href="https://en.wikipedia.org/wiki/JSON">JSON</a>. A structured format simplifies parsing of the results, and can also increase the quality of an answer. This is because additional structured metadata can be passed along with the chunks of text to the prompt. The model can then be asked to produce additional outputs, and these extra outputs can help improve the answer itself.</p><p>This structured approach helps make your instructions highly targeted and very precise.&nbsp;</p><p><strong>#2: The quality of your evaluation &#8220;evals&#8221; are critical for making reliable progress</strong></p><p>&#8220;Evals&#8221; refers to a set of scenarios used to grade the quality of an agent's responses to questions. Each scenario has an input and an output which you can run as you go. LLMs have inherently unpredictable output, meaning there&#8217;s a lot of trial and error. So it&#8217;s essential to have a strong set of test cases for tracking your progress.&nbsp;</p><p>Invest time in defining these &#8220;evals&#8221; upfront. I&#8217;ve written more about <a href="https://www.wordsmith.ai/benchmarks">how we approached our evaluation criteria</a>.</p><p><strong>#3: Improve performance by asking the LLM to do extra things</strong></p><p>Here are two ways to significantly improve performance&nbsp;</p><ol><li><p>Combine the context with the original question, then ask the LLM to rebuild a better answer.&nbsp;</p></li><li><p>Ask the LLM to capture the user&#8217;s intent from the original question, and offer reasoning.</p></li></ol><p>In my experience, both approaches improved the output&#8217;s quality.<br><br></p><p><strong>#4: Get the right token size as LLM context&nbsp;</strong></p><p>If you feed too few chunks or too little context into the LLM, you&#8217;ll get narrow, lightweight answers. Feed in too many chunks and too much context, and the model will start overlooking essential information and get confused. Experiment to get the right chunk size, and the correct number of tokens for better performance.</p><p>A simple reference point that seems to work well is passing about 16,000 tokens for <a href="https://help.openai.com/en/articles/8555510-gpt-4-turbo-in-the-openai-api">GPT-4 Turbo</a>.</p><p><strong>#5: Chunking matters a lot &#8211; A LOT!</strong></p><p>A way to improve performance is to blend multiple chunking strategies to create overlapping chunks, which can help build some resilience into the data to ensure you get the most relevant manifestation.</p><p>For example, create fixed-size chunks for 2,500 character size and 500 character size. Calculate the embeddings for both options, which means embedding the same data several times. During a search, your system will retrieve and use the best fitting chunk, which could be the shorter or longer one!</p><p><strong>#6: Use suitable document parsers</strong></p><p>There are so many open source solutions to help parse and pre-process documents, so take some time to research the best ones for your use case, whose output fits well.&nbsp;</p><p>Document parsers tend to struggle to handle certain formats, like nested numbered lists. But at Wordsmith, these are very important in contracts and legal documents! So we had to &#8220;hand-roll&#8221; our custom solution, after failing to find an open source document parser that did the job.</p><ul><li></li></ul><p><strong>#7: Use output formatting which the model is comfortable with</strong></p><p>Each foundation model has been trained on different source data. This means each model will work better or worse with certain input and output formats. For example, GPT4 and Mistral are efficient when using JSON and Markdown, suggesting they have been extensively trained with this kind of data. Meanwhile, Claude seems to work well when using Markdown, but less so with JSON. Experiment with models, learn which formats work better, and choose models based on their strengths.</p><p><strong>Beyond RAGs</strong></p><p>RAG is the foundation of nearly every LLM application, and this space is moving fast. I sense a &#8220;new dawn&#8221; is starting to break in multi-agent orchestration and interaction. These new architectures will give developers the ability to chain many agents together, with each one performing a specialist role in a pipeline. Such advanced pipelines will help progress beyond many of the constraints with which basic RAG approaches struggle.</p><p>Right now, the cost of running LLMs can still be pretty high; for example, our test suite cost $30 to execute on each run. However, the cost of running LLMs is falling quickly, and the performance of these tools is increasing just as fast.&nbsp;</p><p>It&#8217;s an exciting time to be building on Gen AI and LLMs. I hope this overview and code-along helps you get started!</p><p><em>A big thanks to <a href="https://www.linkedin.com/in/derek-johnston-8829841a0/">Derek</a> and <a href="https://www.linkedin.com/in/giginiak/">Gigz</a> for the effort they put into helping contribute to the <a href="https://github.com/wordsmith-ai/hello-wordsmith">hello_wordsmith repository</a>!</em></p><h2>Takeaways</h2><p><em>Gergely again. </em>Thanks very much Ross and the Wordsmith team, for this detailed walkthrough about building a RAG pipeline. You can <a href="https://www.linkedin.com/in/rossmcnairn/">follow Ross on LinkedIn</a>, <a href="https://twitter.com/rossmcnairn">X</a> or <a href="https://www.rossmcnairn.com/">subscribe to his blog</a>, where he writes on topics like <a href="https://www.rossmcnairn.com/p/ctos-view-on-a-defensible-moat-in">defensible moats in the age of generative AI</a>. Also, Wordsmith <a href="https://www.wordsmith.ai/careers/open-positions#open-positions">is hiring</a> for product engineering and sales.</p><p>My takeaways from this deep dive:</p><p><strong>Data is one of the biggest moats in most GenAI use cases. </strong>There are two types of AI startups:</p><ol><li><p>Those building foundational models, of which there are a handful and among whom OpenAI is the best known, with its GPT models. There&#8217;s also Anthropic (Claude,) Google (Gemini,) Meta (Llama,) and Mistral. These companies spend up to hundreds of millions of dollars on training these models, then offer them for use; sometimes for a fee, and sometimes for free.</p></li><li><p>Ones building applications on top of foundational models. The majority of startups utilize foundational models, and build creative use cases like professional headshots (Secta AI, as covered in the <a href="https://newsletter.pragmaticengineer.com/i/137627687/overview-of-the-companies">bootstrapped companies article</a>,) or Wordsmith, which offers LLM-powered tools for legal professionals.</p></li></ol><p>For the second category which includes most startups, the two biggest advantages are speed of execution, or access to unique data which competitors don&#8217;t possess. Speed of execution can be a competitive advantage, but having access to data which competitors don&#8217;t feels like the biggest, durable advantage for a startup.</p><p><strong>RAG is one of the simplest ways to use &#8220;data as a moat&#8221; with AI models. </strong>For any company with a data moat, RAG is the simplest way to enhance any LLM model without exposing the underlying data to the outside world.</p><p>Sourcegraph has used RAG to produce superior code suggestions. Head of Engineering Steve Yegge <a href="https://sourcegraph.com/blog/rag-to-riches">wrote last December</a>:</p><blockquote><p>&#8220;Cody&#8217;s [Sourcegraph&#8217;s AI coding assistant] secret sauce and differentiator has always been Sourcegraph&#8217;s deep understanding of code bases, and Cody has tapped into that understanding to create the perfect RAG-based coding assistant, which by definition is the one that produces the best context for the LLM.</p><p>That&#8217;s what RAG (retrieval-augmented generation) is all about. You augment the LLM&#8217;s generation by retrieving as much information as a human might need in order to perform some task, and feeding that to the LLM along with the task instructions. (...)</p><p>Producing the perfect context is a dark art today, but I think Cody is likely the furthest along here from what I can see. Cody&#8217;s Context has graduated from &#8220;hey we have vector embeddings&#8221; to &#8220;hey we have a whole squad of engines.&#8221;&nbsp;</p></blockquote><p><strong>RAG is surprisingly easy to understand! </strong>At root, all RAG does is take an input query and add several sentences or paragraphs of additional context. Getting this additional context is an LLM search task, and performing this task involves preparing the &#8220;context&#8221; data, which is additional data which the LLM hasn&#8217;t been trained on.</p><p><strong>Unoptimized RAG is expensive! </strong>Running the example code, I saw a $1.38 charge after asking 5-10 questions from this model. I was wondering where the billing was coming from: the price of creating embeddings, or the cost of using GPT-4?</p><p>It turns out that all the cost was for using GPT-4, and the model charged $5 per 1M tokens. For each question I asked, the RAG pipeline added plenty of additional context, which made the query expensive in cost (and processing, I might add.) For a prototype approach, this cost is not a problem. However, for production use cases, heavy optimization would be needed, which could come from passing in more targeted &#8211; but less, overall &#8211; context. Or it could mean using a model that&#8217;s cheaper to run, or operating the model ourselves for better cost efficiency.</p><p><strong>A RAG pipeline is a basic building block of GenAI applications, so it&#8217;s helpful to be familiar with it. </strong>One reason for this deep dive with Ross is that RAG pipelines are common at AI startups and products, but they remain a relatively new area, meaning that a lot of &#8220;build-it-yourself&#8221; takes place. It&#8217;s easy enough to build a basic RAG pipeline, as Ross shows. The tricky part is optimizing things like chunking strategy, chunk size, and the context window.</p><p>If you need or want to build LLM applications, a RAG pipeline is a helpful early building block. I hope you enjoyed this deep dive into such an interesting, emerging area!</p><p>This is an intro to the very basics and the space is developing really quickly.  Already many of these techniques will only get you up and running with the basics and there is a huge amount you can build on.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.rossmcnairn.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Ross McNairn's Blog! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[How do I become a Product Manager?]]></title><description><![CDATA[Longer post designed as practical and actionable advice for someone wanting to move into building a career as a product manager.]]></description><link>https://www.rossmcnairn.com/p/how-do-i-become-a-product-manager</link><guid isPermaLink="false">https://www.rossmcnairn.com/p/how-do-i-become-a-product-manager</guid><dc:creator><![CDATA[Ross McNairn]]></dc:creator><pubDate>Fri, 09 Feb 2024 06:19:25 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/ac2ed80f-9313-4fc1-8449-377529b344a8_1782x614.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>&#8220;How do I move into product&#8221;</p><p>I get this question multiple times a week through LinkedIn so I thought I&#8217;d share my thoughts.&nbsp;</p><p>At Travelperk we built one of the best '<a href="https://www.travelperk.com/apm-program/">product schools</a>' in southern Europe.&nbsp; Every position in the Associate Product Manager (APM) program had over 2,000 applications.&nbsp; This took a long time to establish and was one of our secret weapons to scaling effectively (see my blog on <a href="https://www.rossmcnairn.com/p/three-unicorns-three-lessons">3 lessons, 3 unicorns</a>).</p><p>There can be an assumption, due to the perceived soft skills that somehow Product is more accessible than engineering or other functions.&nbsp; You rarely get people outside of engineering asking &#8220;How do I become a principal engineer&#8221;.&nbsp; They know there is a technical path, often connecting back to formal tertiary education.&nbsp; With product management, the skills are just harder to identify from the outside.&nbsp; You also have a 6:1 ratio of engineers to PMs so competition is intense.&nbsp; I&#8217;m going to try and unpick this a little and give people some structure and hopefully, some actionable steps they can take.</p><p>Where do you even start?</p><p>My starting point is to work on fundamental skills rather than on searching for a golden introduction or opportunity to open a magic door.&nbsp; Building competencies make you more hireable and will ultimately increase the altitude you can rise to. &nbsp; If you get an APM role at a great company, take it, but most people want to know what they can do today, without a perfect job opportunity sitting on their LinkedIn feed.</p><p>So let&#8217;s start with the outcome. Great PMs need 4 things:</p><ol><li><p>They need to <strong>understand how technology works</strong> so they can craft exceptional experiences and coordinate well with engineers.</p></li><li><p>They need to be <strong>exceptional communicators</strong>. So they can listen to and coordinate the business around them.</p></li><li><p>They need to be able to <strong>rapidly and intuitively analyze new domains</strong>, problems, products, and people. So they can ensure they are solving the right problems and coordinating with designers.</p></li><li><p>They need to be able <strong>to get results</strong>. So they can execute effectively and drive a team forward.</p></li></ol><p>Most of these need to be learnt while doing and are highly practical. The exception is technology which I will come to.&nbsp; This is why &#8220;product degrees&#8221; are not really a thing. &nbsp;</p><p>So how do you learn these skills?</p><p><strong>1/ Technology</strong></p><p>In many ways, this is the simplest to learn. The internet is awash with resources boot camps and self-guided paths. Pick a real project, a problem, or a goal and drive towards making something real.&nbsp; When I started learning to code I wanted to make a simple way to analyze, index, and search legal documentation at work.&nbsp; This objective forced me into Ruby, rails, java, JavaScript, night classes, IDEs, deploying code, GitHub, etc.</p><p>If you want the high-speed route, Le Wagon or these coding bootcamps turbocharge you.&nbsp; I made all APMs graduate one of these courses as a minimum. Do this early and you are ahead.</p><p>Ultimately your job is to build stuff people want. So get building. making your own products end to end is the very best home workout you can do.</p><p><strong>2/ Communication</strong></p><p>This is far more applied in that you need to polish it in the industry at work.&nbsp; While there are fewer structural paths, you can actively make learning this a priority.&nbsp; Take every single opportunity to speak, to write, and to relay information. Analyse execs, and unpick their presentations their emails.&nbsp; If you cannot move people, you cannot be a PM.</p><p>Slow it down and say fewer, better things. Think upfront and give people simple, structured output.</p><p>Write. Even if it&#8217;s just for you, start building the muscle. Structured prose irons out your thinking and radically improves the quality. It lets you get feedback loops as you read it back.&nbsp; Do it every day. Ask for feedback constantly on how you could make it more engaging and get your point across.</p><p><strong>3/ Analysis</strong></p><p>Much like engineering, this is highly accessible, start with Excel, move into Python, this is an awesome place to leverage AI as it can analyse and teach you at the same time. </p><p>In nearly every element of your work if you &#8220;why is this happening?&#8221; enough times there will be a hypothesis that you can unpick with data.&nbsp; Get hold of the underlying 2,500 rows of raw CSV data dump it into Python, excel and start building data-informed communications. One sentence, one simple graph.</p><p>&#8220;Our performance on x has improved by 30% month on month for the past 3 years&#8221;</p><p><a href="https://www.useronboard.com/user-onboarding-teardowns/">Analyse other products</a>.&nbsp; Pattern matching is the secret weapon of PMs.&nbsp; To this day I spend hours going through and benchmarking products, making notes.&nbsp; How does their search work, why did the PM do that? What is the advantage of that approach of X.&nbsp; </p><p>The number 1 mistake I see people make in this domain is that they are far too theoretical and have not spent enough time actively thinking about existing digital products while they use them.</p><p><strong>4/ Execution</strong></p><p>Start setting goals, write down what you (or the team you manage) want to achieve on daily, monthly and annual horizons. </p><p>This really has two universal elements.</p><p>A) set and prioritize your personal goals. I started using an Eisenhower matrix (<a href="https://appfluence.com/">priority matrix</a>) and daily/weekly curation.&nbsp; You then just scale this up.</p><p>B) Reflection and improvement. Put time aside to work on your system and think about how you could be more effective. There are hundreds of agile frameworks etc, I would largely ignore these for now and focus on the time behavior of saying where you want to go and getting there.   Scrum and planning tools are easy to learn at a later date.<br><br>Set real-time aside to analyse how effective you and your team were at getting to them and start to build a mentality for getting things done. Build your personal playbook.</p><p><strong>What are the most common Entry points?</strong></p><p>I see 3 common avenues that people take to get into the function.</p><p><strong>1/ APM</strong></p><p>Associate product manager programs. Are typically 2 years long, and have a curriculum, rotations, and mentorship. If you are serious and it&#8217;s a good school this is an amazing way to get a structured lesson and you will end up a very rounded and polished product manager.&nbsp; Cons are that it&#8217;s hard to get into and many people who want lateral moves are not happy to take one step back to take two forward.&nbsp; In my view, it&#8217;s nearly always worth the title and pay cut (if you are moving laterally) to learn the craft.</p><p><strong>2/ Tech Entrepreneur</strong>&nbsp;</p><p>If you have to build a business you need to learn most of these skills.&nbsp; Talking to clients, fighting competitors, it&#8217;s all there.&nbsp; You have the bonus that survive or die is an accelerator and forces you to get good quickly. You will maybe be more &#8220;spikey&#8221; than a classically trained PM but you will be very effective, outcome-based, and a bit of a bulldozer who will get things done.&nbsp; This will be a superpower over time.</p><p><strong>3/ Horizontal shift</strong></p><p>An engineer, a customer success agent, and lawyer, etc. Here your superpower is domain knowledge so you might be able to shift over and be lighter on the above competencies because the product you are working on requires more domain specialism. This is a hard shift as often you don&#8217;t have the bandwidth to backfill some of the other gaps and are thrown in the deep end. So it&#8217;s critical to carve out the time to get familiar with the engineering or nail your comms to ensure you land.</p><p>Preparing your application</p><p>Finding a great transition into product is in part luck. But the more you build your baseline of competencies the luckier you will get.</p><p>Good luck ;)&nbsp;</p>]]></content:encoded></item><item><title><![CDATA[Three Unicorns, Three lessons]]></title><description><![CDATA[I wanted to take a moment to reflect and write up some notes I've had for years, on some of the patterns and lessons I learned from my experiences scaling three unicorns.]]></description><link>https://www.rossmcnairn.com/p/three-unicorns-three-lessons</link><guid isPermaLink="false">https://www.rossmcnairn.com/p/three-unicorns-three-lessons</guid><dc:creator><![CDATA[Ross McNairn]]></dc:creator><pubDate>Mon, 22 Jan 2024 08:10:50 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/03933b25-353c-4b5c-915d-83fbc777c56f_1792x1024.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>I&#8217;ve had the incredible fortune to be part of several unicorns over the past few years.  Typically during a period of hypergrowth.  I recently left Travelperk and felt this was a great time to retrospect.  While many elements made these companies (Skyscanner, Letgo, Travelperk) fantastic, three non-obvious patterns jumped out at me.</p><ol><li><p>Own your distribution.</p></li><li><p>You need a unique approach to attracting talent.</p></li><li><p>The customer at all costs.</p></li></ol><h3><strong>Owning Your Distribution</strong></h3><p>One of my all-time favorite blog posts that I frequently reference is a short piece by Tomaz Tunguz on <a href="https://tomtunguz.com/controlling-your-destiny/">proprietary distribution channels</a>.  Every one of these companies had a flavor of this.</p><p><strong>Skyscanner&#8217;s SEO Strategy:</strong> In the early days the team invested incredibly hard in SEO.&nbsp; Before Google had a chance to swat the "bug" they had amassed over 30 MAUs (monthly active users).&nbsp; They built the everywhere feature and generated millions of canonical URLs and destination pages <em>/cheap-flights-to-london-from-prague</em>.&nbsp; The product was designed to facilitate a massive SEO funnel. &nbsp; The whole business and technology moat was built around being the best in the business at acquiring traffic through this channel.<br><br>This &#8220;free distribution&#8221; meant that with &#163;2.5M of early funding, Skyscanner was able to scale to profitability and take 6 years, getting themselves almost to unicorn status before raising additional investment.&nbsp;</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.rossmcnairn.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Ross McNairn's Blog! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p><strong>Letgo's Unique Access to Capital: </strong>Letgo, was founded by 2nd-time founders who had flipped a previous company for over $3BN they had a track record and connections.&nbsp; This gave them access to lots and lots of money very quickly.&nbsp; This was in total contrast to Skyscanner. It lets them own a different distribution channel.<br><br>TV in the US was typically reserved for established enterprise brands. Out of reach of other startups trying to scale large P2P (peer-to-peer) networks.&nbsp; For a product where <a href="https://en.wikipedia.org/wiki/Metcalfe%27s_law">metclaf&#8217;s law</a> (a network&#8217;s value is proportional to the square of the number of people in it) this was powerful.&nbsp; This meant that the 100th user was more valuable to the network than the first user as the 100th user would likely be able to get involved in lots of different transactions, while the first users had limited options. <br><br>In that situation, lots of capital can give you a big edge, as you can theoretically kick-start your network and quickly get to the high-value, high-volume network.</p><p><strong>Travelperk&#8217;s Sales Machine:</strong> Born in Barcelona, an expat haven, there was a huge volume of smart, ambitious English speakers flowing through the city, who just wanted to get stuck into the tech industry.&nbsp; This opened up an ability to approach sales and implementation differently to someone with their cost base in London, San Francisco. &nbsp; </p><p>A big intake funnel and a really meritocratic culture meant they built a very high-quality and uniquely cost-competitive sales team.&nbsp; It gave them the ability to distribute more effectively than many of their competitors.</p><h3><strong>Talent</strong></h3><p>In all three of these cases, the companies were HQed in non-traditional tech hubs (at the time).&nbsp; London, SFO and NY all have much higher talent density and the "second city" approach would seem to be a disadvantage.&nbsp; However, each company had a unique approach to finding great talent and exploiting those markets.</p><p><strong>Skyscanner&#8217;s graduate and internal talent pipeline: </strong>Skyscanner partnered deeply with the universities.&nbsp; Edinburgh has one of the best informatics schools in the world and Scottish universities like St Andrews and Glasgow have exceptional engineering pedigree.&nbsp; Every year Skyscanner took cohorts sometimes of 50+ grads. &nbsp; Then whittled them down over time.&nbsp; This pipeline became uniquely critical over time.&nbsp; Today the CTO is a product of that pipeline with 14 years at the company. &nbsp; Many of their best and brightest are homegrown.&nbsp; Exceptional talent, very loyal...</p><p><strong>Letgo&#8217;s access to capital:</strong> remember that capital thing&#8230;&nbsp; They paid 30-40% over market.&nbsp; <em>Done</em>.</p><p><strong>Travelperk&#8217;s culture:</strong> Given the often transient nature of the workforce in Barcelona, they invested heavily in building a culture that was magnetic for people who didn't have a natural family and roots in the city.&nbsp; They made a place where they could socialize, and connect and this appealed heavily to that demographic in a foreign city.&nbsp; Weekly end-of-weeks, Calcotadas, summer parties, winter parties, rooftop yoga, a rockstar office.&nbsp; It all helped them attract and retain talent.&nbsp;</p><h3><strong>The customer at all costs</strong></h3><p>All of these companies used different tactics to achieve superiority in the quality of their service and their product.  Long term this was critical in holding customers that their distribution engines acquired for them.</p><p><strong>Skyscanner&#8217;s engineering advantage:</strong> With Skyscanner, it will not surprise you that Gareth believed that a dollar spent on the quality of your product and thus your engineering was the very best investment, short, long, and medium term that you could make.&nbsp; That giving money to the "Google tax" was a waste of time. &nbsp; That principle and constraint, in part, manifested in their pursuit of the SEO channel, but it also channeled resources to their product.&nbsp; 80%+ of the headcount was RnD and they threw everything at just making sure that their prices and their performance were the best in the world.</p><p><strong>Fake it till you make it: </strong>&nbsp;At Travelperk behind the scenes for years people were supplementing the technology until we had time to build the functionality.&nbsp; Offering value and connecting the dots while making a loss.&nbsp; Margin was an optimization, user satisfaction was something that if you didn't solve, optimization would be pointless.</p><p>There are other elements to building fantastic companies.&nbsp; However, I find it helpful to take some non-obvious perspectives to analyze other businesses. All too often they are dropping the ball on one of these. Doing these won&#8217;t mean you are a unicorn overnight, but it will stack the deck in your favor.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.rossmcnairn.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Ross McNairn's Blog! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[CTOs view on a defensible moat in the age of generative AI]]></title><description><![CDATA[Deconstructing the generative AI landscape to build a defensible moat]]></description><link>https://www.rossmcnairn.com/p/ctos-view-on-a-defensible-moat-in</link><guid isPermaLink="false">https://www.rossmcnairn.com/p/ctos-view-on-a-defensible-moat-in</guid><dc:creator><![CDATA[Ross McNairn]]></dc:creator><pubDate>Wed, 10 May 2023 08:37:04 GMT</pubDate><enclosure url="https://substackcdn.com/image/youtube/w_728,c_limit/7yYrfLAn2FM" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>There is a lot of talk about generative AI. You will see "<em>sizzle</em>" feature after "<em>sizzle</em>" feature as the world wakes up to the state of modern AI. The exploration of AI will manifest as a tsunami of experiments, some with long-term value, many without. This poses a difficult question for CTOs and executives: how to make long-term investments and ensure that they are building a unique and defensible moat?</p><p><strong>The Impact of Generative AI on TravelPerk</strong></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.rossmcnairn.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Ross McNairn's Blog! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p>At <a href="https://www.travelperk.com/">TravelPerk</a>, you will already feel the transformative impact of AI on our roadmap. From the semantic analysis of emails and "better than human" levels of triage and routing to ingesting complex invoices or helping our companies with their account setup, generative AI is flowing through our entire roadmap and creating beautifully magical touchpoints everywhere you look.&nbsp; We recently shared this implementation, you can easily see this is just the beginning.</p><div id="youtube2-7yYrfLAn2FM" class="youtube-wrap" data-attrs="{&quot;videoId&quot;:&quot;7yYrfLAn2FM&quot;,&quot;startTime&quot;:null,&quot;endTime&quot;:null}" data-component-name="Youtube2ToDOM"><div class="youtube-inner"><iframe src="https://www.youtube-nocookie.com/embed/7yYrfLAn2FM?rel=0&amp;autoplay=0&amp;showinfo=0&amp;enablejsapi=0" frameborder="0" loading="lazy" gesture="media" allow="autoplay; fullscreen" allowautoplay="true" allowfullscreen="true" width="728" height="409"></iframe></div></div><p><strong>The Power of Combining AI Services</strong></p><p>The real power in this technology comes from seamlessly weaving multiple different models and services together, handing context between each of them as you use the AI best suited for whatever subtask you need. <br><br>Where is the long-term moat behind all this, and how should you start to think about it? Our engineering leadership found this overview from a16z a helpful framing. I've made some modifications of my own to the diagram, demarcating the areas that are most likely to become rapidly commoditized and are currently undergoing the most unpredictable development and iteration.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!4Mwy!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9233781-31fe-4a91-bae7-8179ba8fe151_783x893.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!4Mwy!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9233781-31fe-4a91-bae7-8179ba8fe151_783x893.png 424w, https://substackcdn.com/image/fetch/$s_!4Mwy!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9233781-31fe-4a91-bae7-8179ba8fe151_783x893.png 848w, https://substackcdn.com/image/fetch/$s_!4Mwy!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9233781-31fe-4a91-bae7-8179ba8fe151_783x893.png 1272w, https://substackcdn.com/image/fetch/$s_!4Mwy!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9233781-31fe-4a91-bae7-8179ba8fe151_783x893.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!4Mwy!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9233781-31fe-4a91-bae7-8179ba8fe151_783x893.png" width="783" height="893" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b9233781-31fe-4a91-bae7-8179ba8fe151_783x893.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:893,&quot;width&quot;:783,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!4Mwy!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9233781-31fe-4a91-bae7-8179ba8fe151_783x893.png 424w, https://substackcdn.com/image/fetch/$s_!4Mwy!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9233781-31fe-4a91-bae7-8179ba8fe151_783x893.png 848w, https://substackcdn.com/image/fetch/$s_!4Mwy!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9233781-31fe-4a91-bae7-8179ba8fe151_783x893.png 1272w, https://substackcdn.com/image/fetch/$s_!4Mwy!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb9233781-31fe-4a91-bae7-8179ba8fe151_783x893.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><em><a href="https://a16z.com/2023/01/19/who-owns-the-generative-ai-platform/">original article here</a></em></p><p>We can break this diagram down into two broad buckets:</p><ol><li><p><strong>Apps:</strong><br>Most of what we see on our LinkedIn feeds sits in that top layer, the "apps", where you are gluing services and doing some basic fine-tuning on other models.<br><br>The overview from a16z is a little high-level.&nbsp; The distinction between an &#8216;end-to-end app&#8217; and an app we suspect will be more blurred.&nbsp; Proprietary end-to-end models will likely be a collection of hundreds of fine-tuned, optimized, and well-composed models (and indexes of large bodies of data) that are orchestrated to output results that on aggregate dramatically outperform the larger models. <br><br></p></li><li><p><strong>Open / Closed Sourced models:</strong><br>The quality of models, both closed and OS are evolving at breakneck speed.&nbsp; In weeks, not years sizeable changes are taking place.<br><br>New techniques <a href="https://bair.berkeley.edu/blog/2023/04/03/koala/">like LoRA</a> are making it really easy to build and host models that are really good at targetted tasks (and starting to catch up to ChatGPT in terms of performance), with pretty tiny data sets and a laptop.&nbsp; This is going to lead to an explosion of lots of niche models for very specific jobs.</p></li></ol><p><strong>Investing in data and positioning for a fast-moving external world</strong></p><p>How do you ensure you are going to transition through as an AI-native player with a moat and not simply build on shared foundations that will ultimately be available to everyone? From all this, we have started to build a few early conclusions on the direction of this space:</p><ol><li><p>The quality of models, both closed and open-source, are evolving at breakneck speed. In weeks, not years, sizable changes are taking place.&nbsp; <strong>Betting on a single provider is a mistake</strong>. When you add the risk of regulation and the unpredictable response from various governments the issue compounds.</p></li><li><p><strong>Data quality not GPU resources</strong> will be the most accessible moat for most organizations. Building ML ops teams and pipelines that make it simple for anyone to train models that can outperform major models in targeted domains and use cases, will be what the most successful organizations achieve.</p></li><li><p><strong>Composability is a moat.</strong>&nbsp; This giant 'decoupling' will mean that end-to-end apps of the future compose hundreds and hundreds of models, both in-house and externally hosted. We for example foresee fairly extensive investment in our PII architecture and privacy around this technology.&nbsp; Which will mean we want self-hosted models that help us with PII scrubbing and data governance before we leverage externally hosted infrastructure.&nbsp; Being intentional even at this stage about breaking that out is critical.</p></li></ol><p><strong>Practical Steps for Embracing AI in Your Architecture</strong></p><ol><li><p><strong>Embrace the unbundling:</strong> <br>Bake abstraction in. We are starting to leverage <a href="https://github.com/hwchase17/langchain">LangChain</a> and other abstraction layers that let the teams work above any single model.</p></li><li><p><strong>Remain model-agnostic: </strong><br>Operate under the assumption that within 24 months, there will be 10 alternatives to OpenAI, and your hand will be forced to use models based on their geography and regulatory adherence. Try to remove dependencies where possible.</p></li><li><p><strong>Focus on fewer, better data:</strong> <br>If you can get your telemetry right, you will start to see what steps in your chains are not performing as you would like. To tune proprietary models, large corpuses of data will be less useful than small, highly curated data sets, giving you outsized returns.</p></li><li><p><strong>Emphasize composability: </strong><br>Like writing a book requires many different steps and phases, you will want a model to help you with the outline, a different one to perform a deep analysis on historically accurate character profiling, another for creative flair, and so on.  </p></li></ol><p><strong>Conclusion</strong></p><p>In the age of generative AI, building a defensible moat for your business is crucial. By focusing on the right areas of investment, remaining model-agnostic, and leveraging the power of composability, your company can stay ahead of the competition and thrive in this rapidly evolving landscape. By embracing these principles, you can ensure that your business remains at the forefront of AI innovation, creating unique and lasting value for your users.&nbsp;</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.rossmcnairn.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading Ross McNairn's Blog! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[Reflections on OpenAI]]></title><description><![CDATA[A preview on the next wave of generative AI and some ethical and practical thoughts]]></description><link>https://www.rossmcnairn.com/p/reflections-on-openai</link><guid isPermaLink="false">https://www.rossmcnairn.com/p/reflections-on-openai</guid><dc:creator><![CDATA[Ross McNairn]]></dc:creator><pubDate>Mon, 08 May 2023 14:53:03 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!vLOW!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffeab127a-3758-4e8d-81ec-5ea0e5c0bd8a_512x512.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>I'm sitting in SFO airport after a day with OpenAI, reflecting on a fascinating developer enablement session the team hosted. This trip is one I won't soon forget. Yesterday, I attended a closed-door session for a group of CTOs at OpenAI's HQ in downtown San Francisco. We watched presentations from engineers, product managers, and execs as we delved into their upcoming features and dev tooling. Most striking was GPT's exceptional aptitude for software engineering. Given just a few bullet points, it could troubleshoot complex bugs and draft hundreds of lines of code.</p><p>After sleeping on it and starting to process what I've seen, I've begun to unpack a few thoughts.</p><p>First, gentle anger. How is it that only two or three companies in the world have had access to this technology for years, ring-fencing it from the rest of us to drive advertising revenues? Leveraging it to draw our attention towards the highest bidder feels akin to having the cure to cancer but using it to make the nicotine in cigarettes more addictive. When my peers ask why public sentiment is hesitant to entertain a bailout of a bank branded "Silicon Valley," they need look no further than the damage we do to our own reputation with examples like this.</p><p>Second, a rising, nervous excitement. It's the kind you get before a very important meeting that might lead to something fantastic. Feeding an Excel sheet into GPT-4, it can build conclusions in seconds that would have taken a human analyst hours. GPT-4's superpower is reasoning&#8212;breaking questions into logical steps and connecting dots. Feed it a tax code, and it instantly weaves together the chaos into a sequence of bulletproof calculations. It's like the second half of your brain you've been missing since birth. I can see years of my training as a lawyer and an engineer vanishing before my eyes, and I'm delighted.</p><p>Third, I feel fear. OpenAI's original mission was to provide an ethics framework for this superpower. Quickly, this pivoted into a $10 billion investment from Microsoft and a pricing plan.</p><p>I think back to "Superintelligence," the Nick Bostrom book. He talks of inflection points, moments when AI moves into a realm of self-awareness with the means and momentum to expand its IQ beyond our control. Computers don't have the same limitations as flesh and bone. Biology has placed guardrails on us, but when neurons are a combination of silicon wafers and self-improving algorithms, evolution takes nanoseconds, and resources are nearly limitless.</p><p>The ethics issue bothers me for several reasons. AI has been a part of our lives for years, its power concentrated in a few targeted places. It builds our newsfeeds and feeds us a never-ending stream of meme bubblegum, handing the spotlight to those who can excite and hold our attention. Teams at big tech companies invest billions in "trust and safety," cleaning content and supposedly getting rid of the nasty stuff so we can enjoy hours of uninterrupted monkey-on-lawnmower content guilt-free! Yet, the public increasingly understands that the drive for profit pollutes algorithms, directly impacting our politics and mental health. If attempts to manage AI's second-order implications have failed so far, why would another collection of largely the same West Coast elites fare any better this time?</p><p>My second concern is rooted in contradictions around OpenAI's founding story and origins. It started as a not-for-profit, but that is clearly no longer the case. Perhaps CEO Sam Altman felt that without commercial firepower, it would be nearly impossible to bring the necessary cash to compete with big tech. Regardless, contradictions in their narrative make people nervous, especially when they've just joined forces with a tech giant that recently terminated its entire AI ethics team.</p><p>If I have one call to action, it's for those custodians of AI morality: open the rulebook to the world. There's a high chance you are authoring a constitution for a future era of human society&#8212;an era of abundance or Armageddon. Getting this wrong leaves no room for a do-over; there are no second drafts. Open the document for editing and let the world examine and challenge it. I commend you for breaking this into the open and forcing other players to open their platforms to the world. But if you're going to choose a path for humanity by injecting this technology into nearly every piece of software on Earth, then at least let us have a say.</p><p>Having arrived two days earlier, excited and curious about how best to leverage this new trend, I now leave with no doubt that this is the next supercycle of human evolution. Over the next 10 years, nearly every aspect of white-collar work will change, and the potential for humanity at this critical moment is mind-boggling.</p><p>With that, let me paste this into my GPT-4 preview playground. I'm sure I've made more than a few errors it can clean up for me before I take off.</p>]]></content:encoded></item></channel></rss>