<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>RAG on Alex Jacobs</title>
    <link>https://alex-jacobs.com/tags/rag/</link>
    <description>Recent content in RAG on Alex Jacobs</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Mon, 17 Feb 2025 00:00:00 +0000</lastBuildDate><atom:link href="https://alex-jacobs.com/tags/rag/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>RAG: From Context Injection to Knowledge Integration</title>
      <link>https://alex-jacobs.com/posts/rag/</link>
      <pubDate>Mon, 17 Feb 2025 00:00:00 +0000</pubDate>
      
      <guid>https://alex-jacobs.com/posts/rag/</guid>
      <description>A technical dive into the limitations of current RAG approaches, examining architectural challenges and exploring pathways to more integrated knowledge-aware LLM architectures.</description>
      <content:encoded><![CDATA[<h1 id="retrieval-augmented-generation-architectural-limitations-and-future-directions">Retrieval-Augmented Generation: Architectural Limitations and Future Directions</h1>
<p>Retrieval-Augmented Generation (RAG) has rapidly become a cornerstone in the practical application of Large Language Models (LLMs). Its promise is compelling: to expand LLMs beyond their training data by connecting them to external knowledge sources – from enterprise databases and real-time data streams to proprietary knowledge bases. The allure of RAG lies in its apparent simplicity – augment the LLM&rsquo;s input context with retrieved information, and witness enhanced output quality. However, beneath this layer of simplicity lies a more complex reality&ndash;its a bit of a hack. RAG only works because LLMs are generally robust. The more you think on it, the more it becomes clear it <em>shouldn&rsquo;t</em> really work, and should serve only as a stepping stone to a new paradigm.</p>
<h2 id="generation-vs-retrieval">Generation vs. Retrieval</h2>
<p>At their core, LLMs are generative models that produce text by navigating through a high-dimensional latent space. During pre-training on large datasets, these models learn to map language into this space, capturing relationships between words, phrases, and concepts. Text generation isn&rsquo;t a simple lookup process - it&rsquo;s a sequential operation where the model predicts each token based on both the previous context and its learned representations.</p>
<p>RAG changes this core process significantly. Rather than relying only on the model&rsquo;s learned representations, RAG injects external information directly into the context window alongside the user&rsquo;s query. While this works well in practice, it raises important questions about the theoretical and architectural implications:</p>
<ol>
<li>
<p><strong>Impact on Generation Quality:</strong> How does inserting external information affect the model&rsquo;s learned generation process? Does mixing training-derived and retrieved information create inconsistencies in the model&rsquo;s outputs?</p>
</li>
<li>
<p><strong>Information Integration:</strong> Can the model effectively combine information from different sources during generation? Or is it simply stitching together pieces without truly understanding how they relate?</p>
</li>
<li>
<p><strong>Architectural Fitness:</strong> Are transformer architectures and their training objectives actually suited for combining retrieved information with generation? Or are we forcing an approach that doesn&rsquo;t align with how these models were designed to work?</p>
</li>
</ol>
<h2 id="real-world-limitations">Real-World Limitations</h2>
<p>These theoretical concerns manifest in several practical ways:</p>
<h3 id="1-context-integration-problems">1. Context Integration Problems</h3>
<p>Current RAG implementations often struggle with:</p>
<ul>
<li>Abrupt transitions between retrieved content and generated text</li>
<li>Inconsistent voice and style when mixing sources</li>
<li>Difficulty maintaining coherent reasoning across retrieved facts</li>
<li>Limited ability to synthesize information from multiple sources</li>
</ul>
<h3 id="2-attention-mechanism-overload">2. Attention Mechanism Overload</h3>
<p>The transformer&rsquo;s attention mechanism faces significant challenges:</p>
<ul>
<li>Managing attention across disconnected chunks of information</li>
<li>Balancing focus between query, retrieved content, and generated text</li>
<li>Handling potentially contradictory information from different sources</li>
<li>Maintaining coherence when dealing with multiple retrieved documents</li>
</ul>
<h3 id="3-knowledge-conflicts">3. Knowledge Conflicts</h3>
<p>RAG systems often struggle to resolve conflicts between:</p>
<ul>
<li>The model&rsquo;s pretrained knowledge</li>
<li>Retrieved information</li>
<li>Different retrieved sources</li>
<li>User queries and retrieved content</li>
</ul>
<h2 id="the-path-forward-beyond-basic-rag">The Path Forward: Beyond Basic RAG</h2>
<p>Recent research and development suggest several promising directions for addressing these limitations:</p>
<h3 id="1-improved-knowledge-integration">1. Improved Knowledge Integration</h3>
<p>Future systems might:</p>
<ul>
<li>Process retrieved information before injection</li>
<li>Maintain explicit source tracking throughout generation</li>
<li>Use structured knowledge representations</li>
<li>Implement hierarchical attention mechanisms</li>
</ul>
<h3 id="2-enhanced-source-handling">2. Enhanced Source Handling</h3>
<p>Advanced approaches could:</p>
<ul>
<li>Evaluate source reliability and relevance</li>
<li>Resolve conflicts between sources</li>
<li>Maintain provenance information</li>
<li>Generate explicit citations and references</li>
</ul>
<h3 id="3-architectural-innovations">3. Architectural Innovations</h3>
<p>New architectures might include:</p>
<ul>
<li>Dedicated pathways for retrieved information</li>
<li>Specialized attention mechanisms for source integration</li>
<li>Dynamic context window management</li>
<li>Explicit fact-checking mechanisms</li>
</ul>
<h2 id="the-next-evolution-anthropics-citations-api">The Next Evolution: Anthropic&rsquo;s Citations API</h2>
<p>Anthropic&rsquo;s Citations API represents a significant step beyond traditional RAG implementations. While the exact implementation details aren&rsquo;t public, we can make informed speculations about its architectural innovations based on the capabilities it demonstrates.</p>
<h3 id="architectural-innovations">Architectural Innovations</h3>
<p>The Citations API likely goes beyond simple prompt engineering to include fundamental architectural changes:</p>
<ol>
<li>
<p><strong>Enhanced Context Processing</strong></p>
<ul>
<li>Specialized attention mechanisms for source document processing</li>
<li>Dedicated layers for maintaining source awareness throughout generation</li>
<li>Architectural separation between query processing and source document handling</li>
<li>Advanced chunking and document representation strategies</li>
</ul>
</li>
<li>
<p><strong>Citation-Aware Generation</strong></p>
<ul>
<li>Built-in tracking of source-claim relationships</li>
<li>Automatic detection of when citations are needed</li>
<li>Dynamic weighting of source relevance</li>
<li>Real-time fact verification against sources</li>
</ul>
</li>
<li>
<p><strong>Training Innovations</strong></p>
<ul>
<li>Custom loss functions for citation accuracy</li>
<li>Source fidelity metrics during training</li>
<li>Explicit training for source grounding</li>
<li>Specialized datasets for citation learning</li>
</ul>
</li>
</ol>
<h3 id="speculation-on-implementation">Speculation on Implementation</h3>
<p>The system likely employs several key mechanisms:</p>
<ol>
<li>
<p><strong>Dual-Stream Processing</strong></p>
<ul>
<li>Separate processing paths for user queries and source documents</li>
<li>Specialized attention heads for citation tracking</li>
<li>Fusion layers for combining information streams</li>
<li>Dynamic context management</li>
</ul>
</li>
<li>
<p><strong>Source Integration</strong></p>
<ul>
<li>Fine-grained document chunking</li>
<li>Semantic similarity tracking</li>
<li>Citation boundary detection</li>
<li>Provenance preservation</li>
</ul>
</li>
<li>
<p><strong>Training Approach</strong></p>
<ul>
<li>Multi-task training combining generation and citation</li>
<li>Custom datasets focused on source grounding</li>
<li>Citation-specific loss functions</li>
<li>Source fidelity metrics</li>
</ul>
</li>
</ol>
<h2 id="beyond-traditional-rag">Beyond Traditional RAG</h2>
<p>The Citations API and similar emerging technologies point to a future where knowledge integration isn&rsquo;t just an add-on but a core capability of language models. This evolution requires moving beyond simply stuffing context windows with retrieved documents toward architectures specifically designed for knowledge-aware generation.</p>
<p>The next generation of these systems will likely feature:</p>
<ul>
<li>Native citation capabilities</li>
<li>Real-time fact verification</li>
<li>Seamless source integration</li>
<li>Dynamic knowledge updates</li>
<li>Explicit handling of source conflicts</li>
</ul>
<p>As we move forward, the goal isn&rsquo;t to patch the limitations of current RAG systems but to fundamentally rethink how we combine language models with external knowledge. This might lead to entirely new architectures specifically designed for knowledge-enhanced generation, moving us beyond the current paradigm of context window injection toward truly integrated knowledge-aware AI systems.</p>
]]></content:encoded>
    </item>
    
    <item>
      <title>CheeseGPT</title>
      <link>https://alex-jacobs.com/posts/cheesegpt/</link>
      <pubDate>Tue, 14 Mar 2023 00:00:00 +0000</pubDate>
      
      <guid>https://alex-jacobs.com/posts/cheesegpt/</guid>
      <description>A (Very) Simple RAG Tutorial</description>
      <content:encoded><![CDATA[<h2 id="introduction">Introduction</h2>
<p>This toy project was originally created for a guest lecture to a Data Science 101 course (and its quality may reflect that :)
This post extends that lecture, designed to provide a high-level understanding and example of <em>Retrieval Augmented Generation</em> (RAG).</p>
<p>We&rsquo;ll go through the steps of creating a RAG based LLM system, explaining what we&rsquo;re doing along the way, and why.</p>
<p>You can follow along with the slides and code <a href="https://github.com/alexjacobs08/cheeseGPT">here</a></p>
<p><img loading="lazy" src="/posts/cheesegpt/img1.png" type="" alt="cheeseGPT"  /></p>
<h3 id="the-cheesegpt-system">The CheeseGPT System</h3>
<p>CheeseGPT combines Large Language Models (LLMs) with the advanced capabilities
of Retrieval-Augmented Generation (RAG). At its core, CheeseGPT uses OpenAI&rsquo;s GPT-4 model for natural language processing.
This model serves as the backbone for generating human-like text responses. However, what sets CheeseGPT apart is its
integration with Langchain and a Redis database containing all of the information on Wikipedia relating to cheese.</p>
<p>When a user asks a question, the system utilizes RAG to retrieve the most relevant information/documents from its vector database, and then includes those in its message to the LLM.  This
allows the LLM to have specific and up-to-date information to use, extending from the data that it was trained on.</p>
<p>The image below, flow from right to left (steps 1-5) shows the high level design of this.  The user&rsquo;s query is passed into our embedding model.  We do a similarity search against our
database to retrieve the most relevant documents to our users question. And then these are included in context passed to our LLM.</p>
<p><img loading="lazy" src="/posts/cheesegpt/img2.png" type="" alt="rag_based_llm_design"  /></p>
<h6 id="httpswwwanyscalecombloga-comprehensive-guide-for-building-rag-based-llm-applications-part-1"><em><a href="https://www.anyscale.com/blog/a-comprehensive-guide-for-building-rag-based-llm-applications-part-1">https://www.anyscale.com/blog/a-comprehensive-guide-for-building-rag-based-llm-applications-part-1</a></em></h6>
<p>Below, we&rsquo;ll outline the steps to building this system.</p>
<p><strong>NOTE</strong>: this is an example, and probably doesn&rsquo;t make a ton of sense as a useful system.  (For one, we&rsquo;re getting our
data from wikipedia, which is already contained within the training data of GPT-4)  This is meant to be a high level
example that can show how a RAG based system can work, and to show what the possibilities are when integrating external
data with LLMs  (proprietary data, industry specific technical docs, etc.)</p>
<h2 id="data-collection-and-processing">Data Collection and Processing</h2>
<p>As with most projects, getting and munging your data is one of the most time consuming yet crucial elements.
For our CheeseGPT example, this involved scraping Wikipedia for
cheese-related articles, generating embeddings, and storing them in a Redis database. Below, I&rsquo;ll outline these steps
with code snippets for clarity.</p>
<h3 id="scraping-wikipedia">Scraping Wikipedia</h3>
<p>We start by extracting content from Wikipedia. We made a recursive function <code>get_page_content</code> to fetch pages related
to cheese, including summaries and sections.  (Note: This function could definitely be improved.)</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="ln"> 1</span><span class="cl">
</span></span><span class="line"><span class="ln"> 2</span><span class="cl"><span class="k">def</span> <span class="nf">get_page_content</span><span class="p">(</span><span class="n">page_title</span><span class="p">,</span> <span class="n">depth</span><span class="p">,</span> <span class="n">max_depth</span><span class="p">):</span>
</span></span><span class="line"><span class="ln"> 3</span><span class="cl">    <span class="n">wiki_wiki</span> <span class="o">=</span> <span class="n">wikipediaapi</span><span class="o">.</span><span class="n">Wikipedia</span><span class="p">(</span><span class="s1">&#39;MyCheeseRAGApp/1.0 (myemail@example.com)&#39;</span><span class="p">)</span>
</span></span><span class="line"><span class="ln"> 4</span><span class="cl">    <span class="k">if</span> <span class="n">depth</span> <span class="o">&gt;</span> <span class="n">max_depth</span> <span class="ow">or</span> <span class="n">page_title</span> <span class="ow">in</span> <span class="n">visited_pages</span><span class="p">:</span>
</span></span><span class="line"><span class="ln"> 5</span><span class="cl">        <span class="k">return</span> <span class="p">[],</span> <span class="p">[]</span>
</span></span><span class="line"><span class="ln"> 6</span><span class="cl">    <span class="n">visited_pages</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">page_title</span><span class="p">)</span>
</span></span><span class="line"><span class="ln"> 7</span><span class="cl">
</span></span><span class="line"><span class="ln"> 8</span><span class="cl">    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">visited_pages</span><span class="p">)</span> <span class="o">%</span> <span class="mi">10</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
</span></span><span class="line"><span class="ln"> 9</span><span class="cl">        <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="sa">f</span><span class="s2">&#34;Visited pages: </span><span class="si">{</span><span class="nb">len</span><span class="p">(</span><span class="n">visited_pages</span><span class="p">)</span><span class="si">}</span><span class="s2">&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="ln">10</span><span class="cl">        <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="sa">f</span><span class="s2">&#34;Fetching page &#39;</span><span class="si">{</span><span class="n">page_title</span><span class="si">}</span><span class="s2">&#39; (depth=</span><span class="si">{</span><span class="n">depth</span><span class="si">}</span><span class="s2">)&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="ln">11</span><span class="cl">
</span></span><span class="line"><span class="ln">12</span><span class="cl">    <span class="k">try</span><span class="p">:</span>
</span></span><span class="line"><span class="ln">13</span><span class="cl">        <span class="n">page</span> <span class="o">=</span> <span class="n">wiki_wiki</span><span class="o">.</span><span class="n">page</span><span class="p">(</span><span class="n">page_title</span><span class="p">)</span>
</span></span><span class="line"><span class="ln">14</span><span class="cl">        <span class="k">if</span> <span class="ow">not</span> <span class="n">page</span><span class="o">.</span><span class="n">exists</span><span class="p">():</span>
</span></span><span class="line"><span class="ln">15</span><span class="cl">            <span class="k">return</span> <span class="p">[],</span> <span class="p">[]</span>
</span></span><span class="line"><span class="ln">16</span><span class="cl">
</span></span><span class="line"><span class="ln">17</span><span class="cl">        <span class="n">texts</span><span class="p">,</span> <span class="n">metadata</span> <span class="o">=</span> <span class="p">[],</span> <span class="p">[]</span>
</span></span><span class="line"><span class="ln">18</span><span class="cl">        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">texts</span><span class="p">)</span> <span class="o">%</span> <span class="mi">100</span><span class="p">:</span>
</span></span><span class="line"><span class="ln">19</span><span class="cl">            <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="sa">f</span><span class="s2">&#34;Texts length </span><span class="si">{</span><span class="nb">len</span><span class="p">(</span><span class="n">texts</span><span class="p">)</span><span class="si">}</span><span class="s2">&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="ln">20</span><span class="cl">
</span></span><span class="line"><span class="ln">21</span><span class="cl">        <span class="c1"># Add page summary</span>
</span></span><span class="line"><span class="ln">22</span><span class="cl">        <span class="n">texts</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">page</span><span class="o">.</span><span class="n">summary</span><span class="p">)</span>
</span></span><span class="line"><span class="ln">23</span><span class="cl">        <span class="n">metadata</span><span class="o">.</span><span class="n">append</span><span class="p">({</span><span class="s1">&#39;title&#39;</span><span class="p">:</span> <span class="n">page_title</span><span class="p">,</span> <span class="s1">&#39;section&#39;</span><span class="p">:</span> <span class="s1">&#39;Summary&#39;</span><span class="p">,</span> <span class="s1">&#39;url&#39;</span><span class="p">:</span> <span class="n">page</span><span class="o">.</span><span class="n">fullurl</span><span class="p">})</span>
</span></span><span class="line"><span class="ln">24</span><span class="cl">
</span></span><span class="line"><span class="ln">25</span><span class="cl">        <span class="c1"># Add sections</span>
</span></span><span class="line"><span class="ln">26</span><span class="cl">        <span class="k">for</span> <span class="n">section</span> <span class="ow">in</span> <span class="n">page</span><span class="o">.</span><span class="n">sections</span><span class="p">:</span>
</span></span><span class="line"><span class="ln">27</span><span class="cl">            <span class="n">texts</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">section</span><span class="o">.</span><span class="n">text</span><span class="p">)</span>
</span></span><span class="line"><span class="ln">28</span><span class="cl">            <span class="n">metadata</span><span class="o">.</span><span class="n">append</span><span class="p">({</span><span class="s1">&#39;title&#39;</span><span class="p">:</span> <span class="n">page_title</span><span class="p">,</span> <span class="s1">&#39;section&#39;</span><span class="p">:</span> <span class="n">section</span><span class="o">.</span><span class="n">title</span><span class="p">,</span> <span class="s1">&#39;url&#39;</span><span class="p">:</span> <span class="n">page</span><span class="o">.</span><span class="n">fullurl</span><span class="p">})</span>
</span></span><span class="line"><span class="ln">29</span><span class="cl">
</span></span><span class="line"><span class="ln">30</span><span class="cl">        <span class="c1"># Recursive fetching for links</span>
</span></span><span class="line"><span class="ln">31</span><span class="cl">        <span class="k">if</span> <span class="n">depth</span> <span class="o">&lt;</span> <span class="n">max_depth</span><span class="p">:</span>
</span></span><span class="line"><span class="ln">32</span><span class="cl">            <span class="k">for</span> <span class="n">link_title</span> <span class="ow">in</span> <span class="n">page</span><span class="o">.</span><span class="n">links</span><span class="p">:</span>
</span></span><span class="line"><span class="ln">33</span><span class="cl">                <span class="n">link_texts</span><span class="p">,</span> <span class="n">link_metadata</span> <span class="o">=</span> <span class="n">get_page_content</span><span class="p">(</span><span class="n">link_title</span><span class="p">,</span> <span class="n">depth</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">max_depth</span><span class="p">)</span>
</span></span><span class="line"><span class="ln">34</span><span class="cl">                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">link_texts</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
</span></span><span class="line"><span class="ln">35</span><span class="cl">                    <span class="n">texts</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">link_texts</span><span class="p">)</span>
</span></span><span class="line"><span class="ln">36</span><span class="cl">                    <span class="n">metadata</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">link_metadata</span><span class="p">)</span>
</span></span><span class="line"><span class="ln">37</span><span class="cl">
</span></span><span class="line"><span class="ln">38</span><span class="cl">        <span class="k">return</span> <span class="n">texts</span><span class="p">,</span> <span class="n">metadata</span>
</span></span><span class="line"><span class="ln">39</span><span class="cl">    <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
</span></span><span class="line"><span class="ln">40</span><span class="cl">        <span class="n">logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="sa">f</span><span class="s2">&#34;Error fetching page &#39;</span><span class="si">{</span><span class="n">page_title</span><span class="si">}</span><span class="s2">&#39;: </span><span class="si">{</span><span class="n">e</span><span class="si">}</span><span class="s2">&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="ln">41</span><span class="cl">        <span class="k">return</span> <span class="p">[],</span> <span class="p">[]</span>
</span></span></code></pre></div><p>This is a very greedy (and lazy) approach.  We don&rsquo;t discriminate at all, and we end up
with a ton of noise (things not related to cheese at all), but for our purposes of example, it works.</p>
<h3 id="generating-embeddings">Generating Embeddings</h3>
<p>Next, we need to generate our embeddings from our collected documents.</p>
<h4 id="what-are-embeddings">What are embeddings?</h4>
<p>Embeddings are high-dimensional, continuous vector representations of text, words, or other types of data,
where similar items have similar representations. They capture semantic relationships and features in a space where
operations like distance or angle measurement can indicate similarity or dissimilarity.</p>
<p>In machine learning, embeddings are used to convert categorical, symbolic, or textual data into a form that
algorithms can process more effectively, enabling tasks like natural language processing, recommendation systems,
and more sophisticated pattern recognition.</p>
<p>With our textual data collected, we&rsquo;ll be using OpenAI and Langchain to generate our embeddings. There are
lots of different ways to generate embeddings (plenty packages that run locally, too), but using OpenAI API to get them
is fast and easy for us. (and also dirt cheap)</p>
<p><strong>NOTE:</strong>
In a true production system, there would be <em>much</em> more consideration taken around generating embeddings.  This
is arguably the most important step in a RAG based system.  We&rsquo;d need to do experimentation with chunk size to see what
gives us the best results.  We&rsquo;d need to explore our vectors to make sure their working as expected, remove noise, etc.</p>
<h4 id="generating-embeddings-using-langchain-and-openai">Generating embeddings using LangChain and OpenAI</h4>
<p>Langchain makes it very easy to do create embeddings and store them in Redis without much thought,
but this step requires extreme care to generate good results in a production system</p>
<p>The snippet below takes our scraped wikipedia sections, generates embeddings for them using OpenAI&rsquo;s embeddings API,
and stores them in Redis.  Again, LangChain abstracts away a ton of complexity and makes this <em>really</em> easy for us.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="ln"> 1</span><span class="cl"><span class="kn">from</span> <span class="nn">langchain.embeddings</span> <span class="kn">import</span> <span class="n">OpenAIEmbeddings</span>
</span></span><span class="line"><span class="ln"> 2</span><span class="cl">
</span></span><span class="line"><span class="ln"> 3</span><span class="cl"><span class="n">embeddings</span> <span class="o">=</span> <span class="n">OpenAIEmbeddings</span><span class="p">()</span>
</span></span><span class="line"><span class="ln"> 4</span><span class="cl"><span class="n">rds</span> <span class="o">=</span> <span class="n">Redis</span><span class="o">.</span><span class="n">from_texts</span><span class="p">(</span>
</span></span><span class="line"><span class="ln"> 5</span><span class="cl">    <span class="n">texts</span><span class="p">,</span>
</span></span><span class="line"><span class="ln"> 6</span><span class="cl">    <span class="n">embeddings</span><span class="p">,</span>
</span></span><span class="line"><span class="ln"> 7</span><span class="cl">    <span class="n">metadatas</span><span class="o">=</span><span class="n">metadata</span><span class="p">,</span>
</span></span><span class="line"><span class="ln"> 8</span><span class="cl">    <span class="n">redis_url</span><span class="o">=</span><span class="s2">&#34;redis://localhost:6379&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="ln"> 9</span><span class="cl">    <span class="n">index_name</span><span class="o">=</span><span class="s2">&#34;cheese&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="ln">10</span><span class="cl"><span class="p">)</span>
</span></span></code></pre></div><h2 id="implementation-of-rag">Implementation of RAG</h2>
<p>Our RAG operates by creating an embedding of the user&rsquo;s question and then finding the most semantically similar documents
in our database (via cosine similarity between the embedding of our user&rsquo;s query and the N closest documents in our database).</p>
<p>We then include these documents / snippets in our request to the LLM, telling it that they are the most relevant documents based
on a similarity search.  The LLM can then use these documents as reference when generating its response.</p>
<p>Here&rsquo;s a simplified overview of the process with code snippets:</p>
<h3 id="embedding-user-queries">Embedding User Queries</h3>
<p>The user&rsquo;s query is converted into an embedding using the OpenAI API. This embedding represents the semantic content of
the query in a format that can be compared against the pre-computed embeddings of the database articles.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="ln">1</span><span class="cl"><span class="kn">from</span> <span class="nn">langchain.embeddings</span> <span class="kn">import</span> <span class="n">OpenAIEmbeddings</span>
</span></span><span class="line"><span class="ln">2</span><span class="cl">
</span></span><span class="line"><span class="ln">3</span><span class="cl"><span class="n">embeddings</span> <span class="o">=</span> <span class="n">OpenAIEmbeddings</span><span class="p">()</span>
</span></span><span class="line"><span class="ln">4</span><span class="cl"><span class="n">query_embedding</span> <span class="o">=</span> <span class="n">embeddings</span><span class="o">.</span><span class="n">embed_text</span><span class="p">(</span><span class="n">user_query</span><span class="p">)</span>
</span></span></code></pre></div><h3 id="retrieving-related-articles">Retrieving Related Articles</h3>
<p>We then use the query embedding to perform a similarity search in the Redis database. It retrieves a set number
of articles that are most semantically similar to the query.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="ln">1</span><span class="cl"><span class="k">def</span> <span class="nf">get_related_articles</span><span class="p">(</span><span class="n">query_embedding</span><span class="p">,</span> <span class="n">k</span><span class="o">=</span><span class="mi">3</span><span class="p">):</span>
</span></span><span class="line"><span class="ln">2</span><span class="cl">    <span class="k">return</span> <span class="n">rds</span><span class="o">.</span><span class="n">similarity_search</span><span class="p">(</span><span class="n">query_embedding</span><span class="p">,</span> <span class="n">k</span><span class="o">=</span><span class="n">k</span><span class="p">)</span>
</span></span></code></pre></div><h3 id="integrating-retrieved-data-into-gpt-4-prompts">Integrating Retrieved Data into GPT-4 Prompts</h3>
<p>The retrieved articles are formatted and integrated into the prompt for GPT-4. This allows GPT-4 to use the information
from these articles to generate a response that is not only contextually relevant but also rich in content.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="ln">1</span><span class="cl"><span class="k">def</span> <span class="nf">create_prompt_with_articles</span><span class="p">(</span><span class="n">query</span><span class="p">,</span> <span class="n">articles</span><span class="p">):</span>
</span></span><span class="line"><span class="ln">2</span><span class="cl">    <span class="n">article_summaries</span> <span class="o">=</span> <span class="p">[</span><span class="sa">f</span><span class="s2">&#34;</span><span class="si">{</span><span class="n">article</span><span class="p">[</span><span class="s1">&#39;title&#39;</span><span class="p">]</span><span class="si">}</span><span class="s2">: </span><span class="si">{</span><span class="n">article</span><span class="p">[</span><span class="s1">&#39;summary&#39;</span><span class="p">]</span><span class="si">}</span><span class="s2">&#34;</span> <span class="k">for</span> <span class="n">article</span> <span class="ow">in</span> <span class="n">articles</span><span class="p">]</span>
</span></span><span class="line"><span class="ln">3</span><span class="cl">    <span class="k">return</span> <span class="sa">f</span><span class="s2">&#34;Question: </span><span class="si">{</span><span class="n">query</span><span class="si">}</span><span class="se">\n\n</span><span class="s2">Related Information:</span><span class="se">\n</span><span class="s2">&#34;</span> <span class="o">+</span> <span class="s2">&#34;</span><span class="se">\n</span><span class="s2">&#34;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">article_summaries</span><span class="p">)</span>
</span></span></code></pre></div><h3 id="generating-the-response">Generating the Response</h3>
<p>Finally, the enriched prompt is fed to GPT-4, which generates a response based on both the user&rsquo;s query and the
additional context provided by the retrieved articles.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="ln">1</span><span class="cl"><span class="n">response</span> <span class="o">=</span> <span class="n">openai</span><span class="o">.</span><span class="n">ChatCompletion</span><span class="o">.</span><span class="n">create</span><span class="p">(</span>
</span></span><span class="line"><span class="ln">2</span><span class="cl"><span class="n">model</span><span class="o">=</span><span class="s2">&#34;gpt-4&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="ln">3</span><span class="cl"><span class="n">messages</span><span class="o">=</span><span class="p">[{</span><span class="s2">&#34;role&#34;</span><span class="p">:</span> <span class="s2">&#34;system&#34;</span><span class="p">,</span> <span class="s2">&#34;content&#34;</span><span class="p">:</span> <span class="n">create_prompt_with_articles</span><span class="p">(</span><span class="n">user_query</span><span class="p">,</span> <span class="n">related_articles</span><span class="p">)}]</span>
</span></span><span class="line"><span class="ln">4</span><span class="cl"><span class="p">)</span>
</span></span></code></pre></div><p>Through this process, CheeseGPT effectively combines the generative power of GPT-4 with the information retrieval
capabilities of RAG, resulting in responses that are informative, accurate, and contextually rich.</p>
<h2 id="the-chat-interface">The Chat Interface</h2>
<p>CheeseGPT&rsquo;s chat interface is an important component, orchestrating the interaction between the user, the
retrieval-augmented generation system, and the underlying Large Language Model (LLM).</p>
<p>For the purposes of our example, we have built the bindings for the interface, but did not create a fully interactive interface.</p>
<p>Let&rsquo;s dive into the key functions that make this interaction possible.</p>
<h3 id="connecting-to-redis">Connecting to Redis</h3>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="ln">1</span><span class="cl"><span class="k">def</span> <span class="nf">rds_connect</span><span class="p">():</span>
</span></span><span class="line"><span class="ln">2</span><span class="cl">    <span class="n">rds</span> <span class="o">=</span> <span class="n">Redis</span><span class="o">.</span><span class="n">from_existing_index</span><span class="p">(</span>
</span></span><span class="line"><span class="ln">3</span><span class="cl">        <span class="n">embeddings</span><span class="p">,</span>
</span></span><span class="line"><span class="ln">4</span><span class="cl">        <span class="n">redis_url</span><span class="o">=</span><span class="s2">&#34;redis://localhost:6379&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="ln">5</span><span class="cl">        <span class="n">index_name</span><span class="o">=</span><span class="s2">&#34;cheese&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="ln">6</span><span class="cl">        <span class="n">schema</span><span class="o">=</span><span class="s2">&#34;redis_schema.yaml&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="ln">7</span><span class="cl">    <span class="p">)</span>
</span></span><span class="line"><span class="ln">8</span><span class="cl">    <span class="k">return</span> <span class="n">rds</span>
</span></span></code></pre></div><p>This function establishes a connection to the Redis database, where the precomputed embeddings of cheese-related
Wikipedia pages are stored.</p>
<h3 id="applying-filters">Applying Filters</h3>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="ln">1</span><span class="cl"><span class="k">def</span> <span class="nf">get_filters</span><span class="p">():</span>
</span></span><span class="line"><span class="ln">2</span><span class="cl">    <span class="n">is_not_external_link</span> <span class="o">=</span> <span class="n">RedisFilter</span><span class="o">.</span><span class="n">text</span><span class="p">(</span><span class="s2">&#34;section&#34;</span><span class="p">)</span> <span class="o">!=</span> <span class="s1">&#39;External Links&#39;</span>
</span></span><span class="line"><span class="ln">3</span><span class="cl">    <span class="n">is_not_see_also</span> <span class="o">=</span> <span class="n">RedisFilter</span><span class="o">.</span><span class="n">text</span><span class="p">(</span><span class="s2">&#34;section&#34;</span><span class="p">)</span> <span class="o">!=</span> <span class="s1">&#39;See Also&#39;</span>
</span></span><span class="line"><span class="ln">4</span><span class="cl">    <span class="n">_filter</span> <span class="o">=</span> <span class="n">is_not_external_link</span> <span class="o">&amp;</span> <span class="n">is_not_see_also</span>
</span></span><span class="line"><span class="ln">5</span><span class="cl">    <span class="k">return</span> <span class="n">_filter</span>
</span></span></code></pre></div><p>Filters are applied to ensure that irrelevant sections like &lsquo;External Links&rsquo; and &lsquo;See Also&rsquo; are excluded from the search
results.</p>
<h3 id="deduplicating-results">Deduplicating Results</h3>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="ln">1</span><span class="cl"><span class="k">def</span> <span class="nf">dedupe_results</span><span class="p">(</span><span class="n">results</span><span class="p">):</span>
</span></span><span class="line"><span class="ln">2</span><span class="cl">    <span class="n">seen</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
</span></span><span class="line"><span class="ln">3</span><span class="cl">    <span class="n">deduped_results</span> <span class="o">=</span> <span class="p">[]</span>
</span></span><span class="line"><span class="ln">4</span><span class="cl">    <span class="k">for</span> <span class="n">result</span> <span class="ow">in</span> <span class="n">results</span><span class="p">:</span>
</span></span><span class="line"><span class="ln">5</span><span class="cl">        <span class="k">if</span> <span class="n">result</span><span class="o">.</span><span class="n">page_content</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">seen</span><span class="p">:</span>
</span></span><span class="line"><span class="ln">6</span><span class="cl">            <span class="n">deduped_results</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">result</span><span class="p">)</span>
</span></span><span class="line"><span class="ln">7</span><span class="cl">            <span class="n">seen</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">result</span><span class="o">.</span><span class="n">page_content</span><span class="p">)</span>
</span></span><span class="line"><span class="ln">8</span><span class="cl">    <span class="k">return</span> <span class="n">deduped_results</span>
</span></span></code></pre></div><p>This function ensures that duplicate content from the search results is removed, enhancing the quality of the final
output.  (This is necessary in our case because we were greedy / lazy when pulling our data / generating our vectors)</p>
<h3 id="retrieving-document-results">Retrieving Document Results</h3>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="ln">1</span><span class="cl"><span class="k">def</span> <span class="nf">get_results</span><span class="p">(</span><span class="n">rds</span><span class="p">,</span> <span class="n">question</span><span class="p">,</span> <span class="n">k</span><span class="o">=</span><span class="mi">3</span><span class="p">):</span>
</span></span><span class="line"><span class="ln">2</span><span class="cl">    <span class="n">_filters</span> <span class="o">=</span> <span class="n">get_filters</span><span class="p">()</span>
</span></span><span class="line"><span class="ln">3</span><span class="cl">    <span class="n">results</span> <span class="o">=</span> <span class="n">dedupe_results</span><span class="p">(</span><span class="n">rds</span><span class="o">.</span><span class="n">similarity_search</span><span class="p">(</span><span class="n">question</span><span class="p">,</span> <span class="n">k</span><span class="o">=</span><span class="n">k</span><span class="p">,</span> <span class="nb">filter</span><span class="o">=</span><span class="n">_filters</span><span class="p">))</span>
</span></span><span class="line"><span class="ln">4</span><span class="cl">    <span class="k">return</span> <span class="n">results</span>
</span></span></code></pre></div><p>This key function performs a similarity search in the Redis database using the user&rsquo;s query, filtered and deduplicated.</p>
<h3 id="formatting-rag-results">Formatting RAG Results</h3>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="ln">1</span><span class="cl"><span class="k">def</span> <span class="nf">format_rag_results</span><span class="p">(</span><span class="n">rag_results</span><span class="p">):</span>
</span></span><span class="line"><span class="ln">2</span><span class="cl">    <span class="n">divider</span> <span class="o">=</span> <span class="s2">&#34;*********************RESULT*********************</span><span class="se">\n</span><span class="s2">&#34;</span>
</span></span><span class="line"><span class="ln">3</span><span class="cl">    <span class="k">return</span> <span class="p">[</span><span class="sa">f</span><span class="s2">&#34;</span><span class="si">{</span><span class="n">divider</span><span class="si">}</span><span class="s2"> </span><span class="si">{</span><span class="n">result</span><span class="o">.</span><span class="n">page_content</span><span class="si">}</span><span class="s2"> (</span><span class="si">{</span><span class="n">result</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s1">&#39;url&#39;</span><span class="p">]</span><span class="si">}</span><span class="s2">#</span><span class="si">{</span><span class="n">result</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s1">&#39;section&#39;</span><span class="p">]</span><span class="si">}</span><span class="s2">)&#34;</span> <span class="k">for</span> <span class="n">result</span> <span class="ow">in</span>
</span></span><span class="line"><span class="ln">4</span><span class="cl">            <span class="n">rag_results</span><span class="p">]</span>
</span></span></code></pre></div><p>The function formats the search results, making them readable and including the source information for transparency.</p>
<p>This is what our message looks like when we send it to GPT-4.  Our system prompt is first and includes instructions for the
model to use the retrieved documents when answering the question.</p>
<p>In our user message, you can see the user&rsquo;s question, and then the documents we retrieved, presented as a list with some formatting.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-json" data-lang="json"><span class="line"><span class="cl"><span class="p">[</span>
</span></span><span class="line"><span class="cl">   <span class="p">{</span>
</span></span><span class="line"><span class="cl">      <span class="nt">&#34;role&#34;</span><span class="p">:</span> <span class="s2">&#34;system&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">      <span class="nt">&#34;content&#34;</span><span class="p">:</span> <span class="s2">&#34;You are cheeseGPT, a retrieval augmented chatbot with expert knowledge of cheese. You are here to answer questions about cheese, and you should, when possible, cite your sources with the documents provided to you.&#34;</span>
</span></span><span class="line"><span class="cl">   <span class="p">},</span>
</span></span><span class="line"><span class="cl">   <span class="p">{</span>
</span></span><span class="line"><span class="cl">      <span class="nt">&#34;role&#34;</span><span class="p">:</span> <span class="s2">&#34;user&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">      <span class="nt">&#34;content&#34;</span><span class="p">:</span> <span class="s2">&#34;User question: what is the biggest cheese sporting event.  Retrieved documents: [&#39;*********************RESULT*********************\\n The Lucerne Cheese Festival (German: Käsefest Luzern) is a cheese festival held annually in Lucerne, Switzerland. It was established in 2001 and is normally run on a weekend in the middle of October at the Kapellplatz (Chapel Square) in the city centre. The next festival is planned to take place on 14 October 2023.The event features the biggest cheese market in central Switzerland, and offers the greatest selection of cheeses. As well as the cheese market and live demonstrations of cheesemaking, typical events during the festival include a milking competition and music such as the Swiss alphorn.The 2012 event featured over 200 varieties of cheese over 23 market stalls, including goat and sheep cheese. The 2020 event was almost cancelled because of social distancing restrictions during the COVID-19 pandemic, but was approved a few days before with a strict requirement to wear masks. Instead of the Kapellplatz, the festival was run from the nearby Kurplatz (Spa Square). 288 variety of cheeses were available at the festival, including cheesemakers from outside the local region such as the Bernese Jura and Ticino, who had their own festivals cancelled. Around 5,800 people attended the festival, lower than the previous year, with around two-thirds fewer sales. The following year\\&#39;s event continued restrictions, where customers had to taste and buy cheese at a distance, though masks were no longer mandatory. The 2022 event featured demonstrations of the cheese making process, a chalet built of Swiss cheese, and a \&#34;cheese chalet\&#34; hosting cheese fondue and raclette.India Times in 2014 called it out as one of 10 world food festivals for foodies. (https://en.wikipedia.org/wiki/Lucerne_Cheese_Festival#Summary)&#39;, &#39;*********************RESULT*********************\\n In addition to sampling and purchasing more than 4,600 cheeses in the Cheese Pavilion, visitors to the show are treated to various attractions throughout the day including cheese making demonstrations, trophy presentations and live cookery demonstrations. (https://en.wikipedia.org/wiki/International_Cheese_Awards#Show features)&#39;, &#39;*********************RESULT*********************\\n The first annual event was held in 2000 in Oxfordshire, and was founded by Juliet Harbutt. Each year it is preceded by the British Cheese Awards, a ceremony which Harbutt created in 1994, judged by food experts and farmers, in which the best cheeses are awarded bronze, silver and gold medals.\\nAll cheeses are tasted blind, and the winners can then display their awards during the public-attended festival.  There are usually a variety of events at the festival such as seminars, masterclasses, and cheesemaking demonstrations.The event moved to Cheltenham in Gloucestershire in 2005. In 2006 the Sunday of the weekend was cancelled at great cost after the venue experienced flooding, and the decision was made to return to Oxfordshire in 2007.Early in 2008 the festival was sold to Cardiff Council; subsequently the event has been held in the grounds of Cardiff Castle in 2008, 2009, 2010, and 2011.The 2012 Great British Cheese Festival was held at Cardiff Castle on Saturday, September 22, and Sunday, September 23.\\nIn 2015, Harbutt returned to her native New Zealand. (https://en.wikipedia.org/wiki/The_Great_British_Cheese_Festival#History)&#39;]&#34;</span>
</span></span><span class="line"><span class="cl">   <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">]</span>
</span></span></code></pre></div><h3 id="generating-messages-for-the-llm">Generating Messages for the LLM</h3>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="ln">1</span><span class="cl"><span class="k">def</span> <span class="nf">get_messages</span><span class="p">(</span><span class="n">question</span><span class="p">,</span> <span class="n">rag_results</span><span class="p">):</span>
</span></span><span class="line"><span class="ln">2</span><span class="cl">    <span class="n">messages</span> <span class="o">=</span> <span class="p">[</span>
</span></span><span class="line"><span class="ln">3</span><span class="cl">        <span class="p">{</span><span class="s2">&#34;role&#34;</span><span class="p">:</span> <span class="s2">&#34;system&#34;</span><span class="p">,</span> <span class="s2">&#34;content&#34;</span><span class="p">:</span> <span class="n">system_prompt</span><span class="p">},</span>
</span></span><span class="line"><span class="ln">4</span><span class="cl">        <span class="p">{</span><span class="s2">&#34;role&#34;</span><span class="p">:</span> <span class="s2">&#34;user&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="ln">5</span><span class="cl">         <span class="s2">&#34;content&#34;</span><span class="p">:</span> <span class="sa">f</span><span class="s2">&#34;User question: </span><span class="si">{</span><span class="n">question</span><span class="si">}</span><span class="s2">.  Retrieved documents: </span><span class="si">{</span><span class="n">format_rag_results</span><span class="p">(</span><span class="n">rag_results</span><span class="p">)</span><span class="si">}</span><span class="s2">&#34;</span><span class="p">}</span>
</span></span><span class="line"><span class="ln">6</span><span class="cl">    <span class="p">]</span>
</span></span><span class="line"><span class="ln">7</span><span class="cl">    <span class="k">return</span> <span class="n">messages</span>
</span></span></code></pre></div><p>This function prepares the input for the LLM, combining the system prompt, user question, and the retrieved documents.</p>
<p>The integration of these functions creates a seamless flow from the user&rsquo;s question to the LLM&rsquo;s informed response,
enabling CheeseGPT to provide expert-level insights into the world of cheese.</p>
<p>Putting it all together might look something like&hellip;</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="ln">1</span><span class="cl"><span class="n">question</span> <span class="o">=</span> <span class="s2">&#34;what is the biggest cheese sporting event&#34;</span>
</span></span><span class="line"><span class="ln">2</span><span class="cl"><span class="n">results</span> <span class="o">=</span> <span class="n">get_results</span><span class="p">(</span><span class="n">rds</span><span class="p">,</span> <span class="n">question</span><span class="p">,</span> <span class="n">k</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
</span></span><span class="line"><span class="ln">3</span><span class="cl"><span class="n">messages</span> <span class="o">=</span> <span class="n">get_messages</span><span class="p">(</span><span class="n">question</span><span class="p">,</span> <span class="n">results</span><span class="p">)</span>
</span></span><span class="line"><span class="ln">4</span><span class="cl"><span class="nb">print</span><span class="p">(</span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">messages</span><span class="p">,</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
</span></span><span class="line"><span class="ln">5</span><span class="cl"><span class="n">response</span> <span class="o">=</span> <span class="n">openai</span><span class="o">.</span><span class="n">ChatCompletion</span><span class="o">.</span><span class="n">create</span><span class="p">(</span>
</span></span><span class="line"><span class="ln">6</span><span class="cl">    <span class="n">model</span><span class="o">=</span><span class="s2">&#34;gpt-4&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="ln">7</span><span class="cl">    <span class="n">messages</span><span class="o">=</span><span class="n">messages</span><span class="p">,</span>
</span></span><span class="line"><span class="ln">8</span><span class="cl"><span class="p">)</span>
</span></span><span class="line"><span class="ln">9</span><span class="cl"><span class="nb">print</span><span class="p">(</span><span class="n">response</span><span class="p">)</span>
</span></span></code></pre></div><h2 id="outcomes">Outcomes</h2>
<p>So, let&rsquo;s compare a question using our system vs. asking ChatGPT.  We&rsquo;ll use the same question above.</p>
<p>Using our system, we get this response:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="ln"> 1</span><span class="cl">The Lucerne Cheese Festival in Switzerland and the International Cheese Awards are two of the biggest cheese sporting events. 
</span></span><span class="line"><span class="ln"> 2</span><span class="cl">
</span></span><span class="line"><span class="ln"> 3</span><span class="cl">The Lucerne Cheese Festival, a yearly event in Lucerne, Switzerland, features the largest cheese market in central Switzerland 
</span></span><span class="line"><span class="ln"> 4</span><span class="cl">and provides a vast selection of cheeses. The festival also includes live cheesemaking demonstrations, a milking competition, 
</span></span><span class="line"><span class="ln"> 5</span><span class="cl">and music. In past years, it has presented over 200 varieties of cheese over 23 market stalls, including goat and sheep cheese. 
</span></span><span class="line"><span class="ln"> 6</span><span class="cl">The festival attracted around 5,800 attendees in 2020, despite social distancing restrictions. The India Times recognized it 
</span></span><span class="line"><span class="ln"> 7</span><span class="cl">as one of 10 world food festivals for foodies in 2014 [^1^].
</span></span><span class="line"><span class="ln"> 8</span><span class="cl">
</span></span><span class="line"><span class="ln"> 9</span><span class="cl">On the other hand, the International Cheese Awards allow visitors 
</span></span><span class="line"><span class="ln">10</span><span class="cl">to sample and purchase over 4,600 different cheeses in the Cheese Pavilion. The show features various attractions such as
</span></span><span class="line"><span class="ln">11</span><span class="cl">cheese making demonstrations, trophy presentations, and live cookery demonstrations [^2^].
</span></span><span class="line"><span class="ln">12</span><span class="cl">
</span></span><span class="line"><span class="ln">13</span><span class="cl">The Great British Cheese Festival, although not as large as the other two events, is also a notable cheese event. It 
</span></span><span class="line"><span class="ln">14</span><span class="cl">hosts the British Cheese Awards, where the best cheeses are awarded bronze, silver, and gold medals, judged by food 
</span></span><span class="line"><span class="ln">15</span><span class="cl">experts and farmers. All cheeses are tasted blindly, and the winners can display their awards during the festival. 
</span></span><span class="line"><span class="ln">16</span><span class="cl">The festival typically includes seminars, masterclasses, and cheesemaking demonstrations [^3^].
</span></span><span class="line"><span class="ln">17</span><span class="cl">
</span></span><span class="line"><span class="ln">18</span><span class="cl">In conclusion, both the Lucerne Cheese Festival and the International Cheese Awards are considered some of the biggest 
</span></span><span class="line"><span class="ln">19</span><span class="cl">cheese sporting events worldwide.
</span></span><span class="line"><span class="ln">20</span><span class="cl">
</span></span><span class="line"><span class="ln">21</span><span class="cl">[^1^]: (https://en.wikipedia.org/wiki/Lucerne_Cheese_Festival#Summary)
</span></span><span class="line"><span class="ln">22</span><span class="cl">[^2^]: (https://en.wikipedia.org/wiki/International_Cheese_Awards#Show_features)
</span></span><span class="line"><span class="ln">23</span><span class="cl">[^3^]: (https://en.wikipedia.org/wiki/The_Great_British_Cheese_Festival#History)&#34;
</span></span></code></pre></div><p>And if we ask ChatGPT the same question&hellip;</p>
<p><img loading="lazy" src="/posts/cheesegpt/img3.jpg" type="" alt="img3.jpg"  /></p>
<p>I&rsquo;m not sure which of these answer is <em>more</em> correct, and it doesn&rsquo;t matter for the purposes of this example.  The point is that we were
able to retrieve and include our own information, external to the model, and make it use that information in it&rsquo;s response.<br>
It&rsquo;s clear how many amazing use cases there are for something like this!</p>
<p>Hopefully this high level toy example was able to shed some light on what a RAG based system may look like.  Checkout the
additional resources linked before for more in-depth information. Thanks for reading!</p>
<h3 id="additional-resources">Additional resources</h3>
<p><a href="https://github.com/ray-project/llm-applications/blob/main/notebooks/rag.ipynb">https://github.com/ray-project/llm-applications/blob/main/notebooks/rag.ipynb</a>
<a href="https://github.com/pchunduri6/rag-demystified">https://github.com/pchunduri6/rag-demystified</a>
<a href="https://www.anyscale.com/blog/a-comprehensive-guide-for-building-rag-based-llm-applications-part-1">https://www.anyscale.com/blog/a-comprehensive-guide-for-building-rag-based-llm-applications-part-1</a></p>
]]></content:encoded>
    </item>
    
  </channel>
</rss>
