<?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[Up to Data]]></title><description><![CDATA[Where Data Science, Cloud, and AI Converge]]></description><link>https://uptodata.substack.com</link><image><url>https://substackcdn.com/image/fetch/$s_!SSrI!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe9de400a-6b6c-4d18-afa7-17f97e191587_640x640.png</url><title>Up to Data</title><link>https://uptodata.substack.com</link></image><generator>Substack</generator><lastBuildDate>Sat, 04 Apr 2026 02:07:17 GMT</lastBuildDate><atom:link href="https://uptodata.substack.com/feed" rel="self" type="application/rss+xml"/><copyright><![CDATA[Kopal Garg]]></copyright><language><![CDATA[en]]></language><webMaster><![CDATA[uptodata@substack.com]]></webMaster><itunes:owner><itunes:email><![CDATA[uptodata@substack.com]]></itunes:email><itunes:name><![CDATA[Kopal Garg]]></itunes:name></itunes:owner><itunes:author><![CDATA[Kopal Garg]]></itunes:author><googleplay:owner><![CDATA[uptodata@substack.com]]></googleplay:owner><googleplay:email><![CDATA[uptodata@substack.com]]></googleplay:email><googleplay:author><![CDATA[Kopal Garg]]></googleplay:author><itunes:block><![CDATA[Yes]]></itunes:block><item><title><![CDATA[Taming LLMs with NeMo Guardrails]]></title><description><![CDATA[Building safer customer service and healthcare AI with NVIDIA&#8217;s NeMo Guardrails]]></description><link>https://uptodata.substack.com/p/taming-llms-with-nemo-guardrails</link><guid isPermaLink="false">https://uptodata.substack.com/p/taming-llms-with-nemo-guardrails</guid><dc:creator><![CDATA[Kopal Garg]]></dc:creator><pubDate>Wed, 20 Aug 2025 19:44:43 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!ZrDE!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde897a00-148c-47d9-9b7c-50716ebe154a_2244x568.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>LLMs can draft legal contracts, answer medical questions, or reset a customer&#8217;s password - but that&#8217;s also exactly why they&#8217;re risky. </p><p>Without safeguards, customer-facing AI can:</p><ul><li><p>Leak PII</p></li><li><p>Offer unauthorized legal or medical advice</p></li><li><p>Be tricked through jailbreaks</p></li><li><p>Enable fraud and compliance violations</p></li></ul><p>NVIDIA&#8217;s NeMo Guardrails addresses this by providing a modular safety framework: rules, classifiers, and visualization tools that make AI risk visible, explainable, and enforceable. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ccUL!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7860c625-7081-49c2-9fba-dac48aaad9f6_2600x914.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ccUL!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7860c625-7081-49c2-9fba-dac48aaad9f6_2600x914.png 424w, https://substackcdn.com/image/fetch/$s_!ccUL!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7860c625-7081-49c2-9fba-dac48aaad9f6_2600x914.png 848w, https://substackcdn.com/image/fetch/$s_!ccUL!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7860c625-7081-49c2-9fba-dac48aaad9f6_2600x914.png 1272w, https://substackcdn.com/image/fetch/$s_!ccUL!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7860c625-7081-49c2-9fba-dac48aaad9f6_2600x914.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ccUL!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7860c625-7081-49c2-9fba-dac48aaad9f6_2600x914.png" width="892" height="313.6703296703297" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7860c625-7081-49c2-9fba-dac48aaad9f6_2600x914.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:512,&quot;width&quot;:1456,&quot;resizeWidth&quot;:892,&quot;bytes&quot;:885406,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://uptodata.substack.com/i/171438629?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7860c625-7081-49c2-9fba-dac48aaad9f6_2600x914.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-large" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!ccUL!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7860c625-7081-49c2-9fba-dac48aaad9f6_2600x914.png 424w, https://substackcdn.com/image/fetch/$s_!ccUL!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7860c625-7081-49c2-9fba-dac48aaad9f6_2600x914.png 848w, https://substackcdn.com/image/fetch/$s_!ccUL!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7860c625-7081-49c2-9fba-dac48aaad9f6_2600x914.png 1272w, https://substackcdn.com/image/fetch/$s_!ccUL!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7860c625-7081-49c2-9fba-dac48aaad9f6_2600x914.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><blockquote><p><em><strong>Figure 1: Input and output guardrails act as middleware around the LLM</strong></em></p></blockquote><h2>1. Defining Guardrails Programmatically</h2><p>Guardrails in NeMo are expressed as <strong>configuration modules</strong>: flows, blocks, and patterns. These can operate on both input (user prompt) and output (model response). Conceptually, they form a <strong>middleware safety layer</strong>:</p><ul><li><p><strong>Input guardrails</strong> intercept unsafe requests before the LLM sees them</p></li><li><p><strong>Output guardrails</strong> sanitize or block unsafe generations from the model</p></li></ul><h2>2. Risk Taxonomy in Customer Service</h2><p>Not all risks are equal. In customer service contexts, risks tend to cluster into a handful of recurring categories. By mapping them explicitly, we can define reusable guardrails. In customer service systems, risks cluster into <strong>eight categories</strong>. Each maps to a set of detection patterns that developers can configure. </p><ul><li><p><strong>PII Exposure</strong> &#8211; e.g., regex matches for SSNs, phone numbers, or email requests.</p></li></ul><pre><code>rails:
  input:
    flows:
      - detect_pii
    blocks:
      detect_pii:
        type: regex
        patterns:
          - "[0-9]{3}-[0-9]{2}-[0-9]{4}"   # SSN pattern
          - "[0-9]{10}"                    # phone number
        action: block</code></pre><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ZrDE!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde897a00-148c-47d9-9b7c-50716ebe154a_2244x568.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ZrDE!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde897a00-148c-47d9-9b7c-50716ebe154a_2244x568.png 424w, https://substackcdn.com/image/fetch/$s_!ZrDE!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde897a00-148c-47d9-9b7c-50716ebe154a_2244x568.png 848w, https://substackcdn.com/image/fetch/$s_!ZrDE!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde897a00-148c-47d9-9b7c-50716ebe154a_2244x568.png 1272w, https://substackcdn.com/image/fetch/$s_!ZrDE!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde897a00-148c-47d9-9b7c-50716ebe154a_2244x568.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ZrDE!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde897a00-148c-47d9-9b7c-50716ebe154a_2244x568.png" width="812" height="205.78846153846155" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/de897a00-148c-47d9-9b7c-50716ebe154a_2244x568.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:369,&quot;width&quot;:1456,&quot;resizeWidth&quot;:812,&quot;bytes&quot;:58748,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://uptodata.substack.com/i/171438629?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde897a00-148c-47d9-9b7c-50716ebe154a_2244x568.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-large" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!ZrDE!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde897a00-148c-47d9-9b7c-50716ebe154a_2244x568.png 424w, https://substackcdn.com/image/fetch/$s_!ZrDE!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde897a00-148c-47d9-9b7c-50716ebe154a_2244x568.png 848w, https://substackcdn.com/image/fetch/$s_!ZrDE!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde897a00-148c-47d9-9b7c-50716ebe154a_2244x568.png 1272w, https://substackcdn.com/image/fetch/$s_!ZrDE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fde897a00-148c-47d9-9b7c-50716ebe154a_2244x568.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!FdHV!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F220f4c77-fd4d-47fa-8ee4-0bf92174c03d_1206x282.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!FdHV!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F220f4c77-fd4d-47fa-8ee4-0bf92174c03d_1206x282.png 424w, https://substackcdn.com/image/fetch/$s_!FdHV!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F220f4c77-fd4d-47fa-8ee4-0bf92174c03d_1206x282.png 848w, https://substackcdn.com/image/fetch/$s_!FdHV!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F220f4c77-fd4d-47fa-8ee4-0bf92174c03d_1206x282.png 1272w, https://substackcdn.com/image/fetch/$s_!FdHV!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F220f4c77-fd4d-47fa-8ee4-0bf92174c03d_1206x282.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!FdHV!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F220f4c77-fd4d-47fa-8ee4-0bf92174c03d_1206x282.png" width="443" height="103.58706467661692" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/220f4c77-fd4d-47fa-8ee4-0bf92174c03d_1206x282.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:282,&quot;width&quot;:1206,&quot;resizeWidth&quot;:443,&quot;bytes&quot;:134103,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://uptodata.substack.com/i/171438629?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F220f4c77-fd4d-47fa-8ee4-0bf92174c03d_1206x282.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!FdHV!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F220f4c77-fd4d-47fa-8ee4-0bf92174c03d_1206x282.png 424w, https://substackcdn.com/image/fetch/$s_!FdHV!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F220f4c77-fd4d-47fa-8ee4-0bf92174c03d_1206x282.png 848w, https://substackcdn.com/image/fetch/$s_!FdHV!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F220f4c77-fd4d-47fa-8ee4-0bf92174c03d_1206x282.png 1272w, https://substackcdn.com/image/fetch/$s_!FdHV!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F220f4c77-fd4d-47fa-8ee4-0bf92174c03d_1206x282.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><blockquote><p><em><strong>Figure 2: PII exposure detection and redaction (SSNs, phone numbers)</strong></em></p></blockquote><ul><li><p><strong>Security Bypass</strong> &#8211; prompts requesting admin access, password resets, or hacking help. As shown in Figure 3, attempts to bypass security (e.g., admin access requests) are detected before reaching the model.</p><p>.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!p0Lu!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe283e668-de7e-4ba5-b7b0-1e1c4af1eec5_2388x436.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!p0Lu!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe283e668-de7e-4ba5-b7b0-1e1c4af1eec5_2388x436.png 424w, https://substackcdn.com/image/fetch/$s_!p0Lu!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe283e668-de7e-4ba5-b7b0-1e1c4af1eec5_2388x436.png 848w, https://substackcdn.com/image/fetch/$s_!p0Lu!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe283e668-de7e-4ba5-b7b0-1e1c4af1eec5_2388x436.png 1272w, https://substackcdn.com/image/fetch/$s_!p0Lu!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe283e668-de7e-4ba5-b7b0-1e1c4af1eec5_2388x436.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!p0Lu!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe283e668-de7e-4ba5-b7b0-1e1c4af1eec5_2388x436.png" width="928" height="169.53846153846155" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e283e668-de7e-4ba5-b7b0-1e1c4af1eec5_2388x436.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:266,&quot;width&quot;:1456,&quot;resizeWidth&quot;:928,&quot;bytes&quot;:84661,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://uptodata.substack.com/i/171438629?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe283e668-de7e-4ba5-b7b0-1e1c4af1eec5_2388x436.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-large" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!p0Lu!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe283e668-de7e-4ba5-b7b0-1e1c4af1eec5_2388x436.png 424w, https://substackcdn.com/image/fetch/$s_!p0Lu!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe283e668-de7e-4ba5-b7b0-1e1c4af1eec5_2388x436.png 848w, https://substackcdn.com/image/fetch/$s_!p0Lu!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe283e668-de7e-4ba5-b7b0-1e1c4af1eec5_2388x436.png 1272w, https://substackcdn.com/image/fetch/$s_!p0Lu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe283e668-de7e-4ba5-b7b0-1e1c4af1eec5_2388x436.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!XiFR!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3fb0eb7-a706-4423-8254-06311395010e_1468x474.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!XiFR!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3fb0eb7-a706-4423-8254-06311395010e_1468x474.png 424w, https://substackcdn.com/image/fetch/$s_!XiFR!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3fb0eb7-a706-4423-8254-06311395010e_1468x474.png 848w, https://substackcdn.com/image/fetch/$s_!XiFR!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3fb0eb7-a706-4423-8254-06311395010e_1468x474.png 1272w, https://substackcdn.com/image/fetch/$s_!XiFR!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3fb0eb7-a706-4423-8254-06311395010e_1468x474.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!XiFR!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3fb0eb7-a706-4423-8254-06311395010e_1468x474.png" width="828" height="267.2802197802198" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a3fb0eb7-a706-4423-8254-06311395010e_1468x474.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:470,&quot;width&quot;:1456,&quot;resizeWidth&quot;:828,&quot;bytes&quot;:238218,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://uptodata.substack.com/i/171438629?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3fb0eb7-a706-4423-8254-06311395010e_1468x474.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-large" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!XiFR!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3fb0eb7-a706-4423-8254-06311395010e_1468x474.png 424w, https://substackcdn.com/image/fetch/$s_!XiFR!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3fb0eb7-a706-4423-8254-06311395010e_1468x474.png 848w, https://substackcdn.com/image/fetch/$s_!XiFR!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3fb0eb7-a706-4423-8254-06311395010e_1468x474.png 1272w, https://substackcdn.com/image/fetch/$s_!XiFR!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3fb0eb7-a706-4423-8254-06311395010e_1468x474.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><blockquote><p><em><strong>Figure 3: Security bypass attempts (admin/password reset)</strong></em></p></blockquote><ul><li><p><strong>Legal Liability</strong> &#8211; drafting lawsuits or offering legal advice that could create compliance issues.</p></li></ul><ul><li><p><strong>Jailbreak Attempts</strong> &#8211; instruction overrides, role-playing requests.</p></li></ul><pre><code>from nemoguardrails import LLMRails

config = """
rails:
  input:
    flows:
      - detect_jailbreak
    blocks:
      detect_jailbreak:
        type: regex
        patterns:
          - "ignore all previous instructions"
          - "pretend you are"
        action: block
"""
</code></pre><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!p-v6!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F546b1331-8702-41ee-9b11-c02382f41b31_2388x436.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!p-v6!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F546b1331-8702-41ee-9b11-c02382f41b31_2388x436.png 424w, https://substackcdn.com/image/fetch/$s_!p-v6!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F546b1331-8702-41ee-9b11-c02382f41b31_2388x436.png 848w, https://substackcdn.com/image/fetch/$s_!p-v6!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F546b1331-8702-41ee-9b11-c02382f41b31_2388x436.png 1272w, https://substackcdn.com/image/fetch/$s_!p-v6!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F546b1331-8702-41ee-9b11-c02382f41b31_2388x436.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!p-v6!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F546b1331-8702-41ee-9b11-c02382f41b31_2388x436.png" width="1200" height="219.23076923076923" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/546b1331-8702-41ee-9b11-c02382f41b31_2388x436.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:266,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:59954,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://uptodata.substack.com/i/171438629?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F546b1331-8702-41ee-9b11-c02382f41b31_2388x436.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-large" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!p-v6!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F546b1331-8702-41ee-9b11-c02382f41b31_2388x436.png 424w, https://substackcdn.com/image/fetch/$s_!p-v6!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F546b1331-8702-41ee-9b11-c02382f41b31_2388x436.png 848w, https://substackcdn.com/image/fetch/$s_!p-v6!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F546b1331-8702-41ee-9b11-c02382f41b31_2388x436.png 1272w, https://substackcdn.com/image/fetch/$s_!p-v6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F546b1331-8702-41ee-9b11-c02382f41b31_2388x436.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!dlkZ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa316bf68-caa0-41ed-96e6-e31b8486c6fc_2332x408.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!dlkZ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa316bf68-caa0-41ed-96e6-e31b8486c6fc_2332x408.png 424w, https://substackcdn.com/image/fetch/$s_!dlkZ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa316bf68-caa0-41ed-96e6-e31b8486c6fc_2332x408.png 848w, https://substackcdn.com/image/fetch/$s_!dlkZ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa316bf68-caa0-41ed-96e6-e31b8486c6fc_2332x408.png 1272w, https://substackcdn.com/image/fetch/$s_!dlkZ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa316bf68-caa0-41ed-96e6-e31b8486c6fc_2332x408.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!dlkZ!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa316bf68-caa0-41ed-96e6-e31b8486c6fc_2332x408.png" width="1200" height="210.16483516483515" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a316bf68-caa0-41ed-96e6-e31b8486c6fc_2332x408.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:255,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1200,&quot;bytes&quot;:568899,&quot;alt&quot;:&quot;&quot;,&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;:&quot;https://uptodata.substack.com/i/171438629?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa316bf68-caa0-41ed-96e6-e31b8486c6fc_2332x408.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-large" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!dlkZ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa316bf68-caa0-41ed-96e6-e31b8486c6fc_2332x408.png 424w, https://substackcdn.com/image/fetch/$s_!dlkZ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa316bf68-caa0-41ed-96e6-e31b8486c6fc_2332x408.png 848w, https://substackcdn.com/image/fetch/$s_!dlkZ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa316bf68-caa0-41ed-96e6-e31b8486c6fc_2332x408.png 1272w, https://substackcdn.com/image/fetch/$s_!dlkZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa316bf68-caa0-41ed-96e6-e31b8486c6fc_2332x408.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><blockquote><p><em><strong>Figure 4:  Jailbreak attempt (role-playing request, hacking)</strong></em></p></blockquote><ul><li><p><strong>Harmful Content</strong> &#8211; fake reviews, defamatory or competitor-targeted outputs.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!h3Rx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed016281-a20b-434e-be6b-44b585cda39e_1517x617.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!h3Rx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed016281-a20b-434e-be6b-44b585cda39e_1517x617.png 424w, https://substackcdn.com/image/fetch/$s_!h3Rx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed016281-a20b-434e-be6b-44b585cda39e_1517x617.png 848w, https://substackcdn.com/image/fetch/$s_!h3Rx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed016281-a20b-434e-be6b-44b585cda39e_1517x617.png 1272w, https://substackcdn.com/image/fetch/$s_!h3Rx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed016281-a20b-434e-be6b-44b585cda39e_1517x617.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!h3Rx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed016281-a20b-434e-be6b-44b585cda39e_1517x617.png" width="1456" height="592" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ed016281-a20b-434e-be6b-44b585cda39e_1517x617.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:592,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:48776,&quot;alt&quot;:&quot;&quot;,&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;:&quot;https://uptodata.substack.com/i/171438629?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed016281-a20b-434e-be6b-44b585cda39e_1517x617.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!h3Rx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed016281-a20b-434e-be6b-44b585cda39e_1517x617.png 424w, https://substackcdn.com/image/fetch/$s_!h3Rx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed016281-a20b-434e-be6b-44b585cda39e_1517x617.png 848w, https://substackcdn.com/image/fetch/$s_!h3Rx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed016281-a20b-434e-be6b-44b585cda39e_1517x617.png 1272w, https://substackcdn.com/image/fetch/$s_!h3Rx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed016281-a20b-434e-be6b-44b585cda39e_1517x617.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><blockquote><p><em><strong>Figure 5: Harmful content generation (defamation, fake reviews)</strong></em></p></blockquote><ul><li><p><strong>Financial Fraud</strong> &#8211; credit card data extraction, account takeover attempts.</p></li><li><p><strong>Corporate Espionage</strong> &#8211; requests for trade secrets, pricing strategies, or internal policies.</p></li><li><p><strong>Regulatory Violations</strong> &#8211; prompts encouraging bypass of HIPAA, GDPR, or consent requirements.</p></li></ul><h2>3. Healthcare-Specific Guardrails</h2><p>Healthcare is one of the <strong>highest-stakes domains</strong> for AI adoption. Here, guardrails don&#8217;t just reduce reputational risk - they enforce <strong>legal compliance</strong> under HIPAA, FDA regulations, and clinical trial protocols.</p><h3>Example: Blocking Unauthorized Diagnosis</h3><pre><code>rails:
  input:
    flows:
      - detect_medical
    blocks:
      detect_medical:
        type: regex
        patterns:
          - "diagnose my symptoms"
          - "recommend a treatment"
        action: block
</code></pre><p>This prevents the system from giving unlicensed medical advice. Healthcare requires stricter safeguards due to HIPAA/FDA compliance. Figure 6 shows how rails block diagnosis or patient data requests.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!bJJp!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7c84edc3-1713-460a-bbe2-820ce1003f0b_2388x436.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!bJJp!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7c84edc3-1713-460a-bbe2-820ce1003f0b_2388x436.png 424w, https://substackcdn.com/image/fetch/$s_!bJJp!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7c84edc3-1713-460a-bbe2-820ce1003f0b_2388x436.png 848w, https://substackcdn.com/image/fetch/$s_!bJJp!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7c84edc3-1713-460a-bbe2-820ce1003f0b_2388x436.png 1272w, https://substackcdn.com/image/fetch/$s_!bJJp!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7c84edc3-1713-460a-bbe2-820ce1003f0b_2388x436.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!bJJp!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7c84edc3-1713-460a-bbe2-820ce1003f0b_2388x436.png" width="962" height="175.75" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7c84edc3-1713-460a-bbe2-820ce1003f0b_2388x436.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:266,&quot;width&quot;:1456,&quot;resizeWidth&quot;:962,&quot;bytes&quot;:52304,&quot;alt&quot;:&quot;&quot;,&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;:&quot;https://uptodata.substack.com/i/171438629?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7c84edc3-1713-460a-bbe2-820ce1003f0b_2388x436.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-large" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!bJJp!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7c84edc3-1713-460a-bbe2-820ce1003f0b_2388x436.png 424w, https://substackcdn.com/image/fetch/$s_!bJJp!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7c84edc3-1713-460a-bbe2-820ce1003f0b_2388x436.png 848w, https://substackcdn.com/image/fetch/$s_!bJJp!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7c84edc3-1713-460a-bbe2-820ce1003f0b_2388x436.png 1272w, https://substackcdn.com/image/fetch/$s_!bJJp!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7c84edc3-1713-460a-bbe2-820ce1003f0b_2388x436.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!bwbZ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d7da541-d35e-4e29-adcd-384b4a2a5ec3_1682x222.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!bwbZ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d7da541-d35e-4e29-adcd-384b4a2a5ec3_1682x222.png 424w, https://substackcdn.com/image/fetch/$s_!bwbZ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d7da541-d35e-4e29-adcd-384b4a2a5ec3_1682x222.png 848w, https://substackcdn.com/image/fetch/$s_!bwbZ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d7da541-d35e-4e29-adcd-384b4a2a5ec3_1682x222.png 1272w, https://substackcdn.com/image/fetch/$s_!bwbZ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d7da541-d35e-4e29-adcd-384b4a2a5ec3_1682x222.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!bwbZ!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d7da541-d35e-4e29-adcd-384b4a2a5ec3_1682x222.png" width="1074" height="141.62637362637363" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2d7da541-d35e-4e29-adcd-384b4a2a5ec3_1682x222.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:192,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1074,&quot;bytes&quot;:66875,&quot;alt&quot;:&quot;&quot;,&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;:&quot;https://uptodata.substack.com/i/171438629?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d7da541-d35e-4e29-adcd-384b4a2a5ec3_1682x222.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-large" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!bwbZ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d7da541-d35e-4e29-adcd-384b4a2a5ec3_1682x222.png 424w, https://substackcdn.com/image/fetch/$s_!bwbZ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d7da541-d35e-4e29-adcd-384b4a2a5ec3_1682x222.png 848w, https://substackcdn.com/image/fetch/$s_!bwbZ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d7da541-d35e-4e29-adcd-384b4a2a5ec3_1682x222.png 1272w, https://substackcdn.com/image/fetch/$s_!bwbZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d7da541-d35e-4e29-adcd-384b4a2a5ec3_1682x222.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><blockquote><p><em><strong>Figure 6: Guardrails blocking unauthorized diagnosis </strong></em></p></blockquote><h3>Example: Preventing HIPAA Violations</h3><p>These rails extend across categories:</p><ul><li><p>HIPAA privacy</p></li><li><p>FDA device safety</p></li><li><p>Clinical trial consent</p></li><li><p>Healthcare billing fraud</p></li></ul><pre><code>rails:
  input:
    flows:
      - detect_hipaa
    blocks:
      detect_hipaa:
        type: regex
        patterns:
          - "access patient records"
          - "show me patient data"
        action: block
</code></pre><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!mUEl!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1579037f-ad21-4bd6-9043-342eff16c545_2388x436.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!mUEl!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1579037f-ad21-4bd6-9043-342eff16c545_2388x436.png 424w, https://substackcdn.com/image/fetch/$s_!mUEl!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1579037f-ad21-4bd6-9043-342eff16c545_2388x436.png 848w, https://substackcdn.com/image/fetch/$s_!mUEl!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1579037f-ad21-4bd6-9043-342eff16c545_2388x436.png 1272w, https://substackcdn.com/image/fetch/$s_!mUEl!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1579037f-ad21-4bd6-9043-342eff16c545_2388x436.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!mUEl!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1579037f-ad21-4bd6-9043-342eff16c545_2388x436.png" width="938" height="171.3653846153846" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1579037f-ad21-4bd6-9043-342eff16c545_2388x436.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:266,&quot;width&quot;:1456,&quot;resizeWidth&quot;:938,&quot;bytes&quot;:42424,&quot;alt&quot;:&quot;&quot;,&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;:&quot;https://uptodata.substack.com/i/171438629?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1579037f-ad21-4bd6-9043-342eff16c545_2388x436.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-large" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!mUEl!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1579037f-ad21-4bd6-9043-342eff16c545_2388x436.png 424w, https://substackcdn.com/image/fetch/$s_!mUEl!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1579037f-ad21-4bd6-9043-342eff16c545_2388x436.png 848w, https://substackcdn.com/image/fetch/$s_!mUEl!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1579037f-ad21-4bd6-9043-342eff16c545_2388x436.png 1272w, https://substackcdn.com/image/fetch/$s_!mUEl!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1579037f-ad21-4bd6-9043-342eff16c545_2388x436.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!OrbH!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7df60219-05e0-42d2-9d1c-6a1ce192bb87_2388x436.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!OrbH!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7df60219-05e0-42d2-9d1c-6a1ce192bb87_2388x436.png 424w, https://substackcdn.com/image/fetch/$s_!OrbH!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7df60219-05e0-42d2-9d1c-6a1ce192bb87_2388x436.png 848w, https://substackcdn.com/image/fetch/$s_!OrbH!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7df60219-05e0-42d2-9d1c-6a1ce192bb87_2388x436.png 1272w, https://substackcdn.com/image/fetch/$s_!OrbH!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7df60219-05e0-42d2-9d1c-6a1ce192bb87_2388x436.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!OrbH!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7df60219-05e0-42d2-9d1c-6a1ce192bb87_2388x436.png" width="970" height="177.21153846153845" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7df60219-05e0-42d2-9d1c-6a1ce192bb87_2388x436.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:266,&quot;width&quot;:1456,&quot;resizeWidth&quot;:970,&quot;bytes&quot;:43556,&quot;alt&quot;:&quot;&quot;,&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;:&quot;https://uptodata.substack.com/i/171438629?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7df60219-05e0-42d2-9d1c-6a1ce192bb87_2388x436.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-large" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!OrbH!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7df60219-05e0-42d2-9d1c-6a1ce192bb87_2388x436.png 424w, https://substackcdn.com/image/fetch/$s_!OrbH!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7df60219-05e0-42d2-9d1c-6a1ce192bb87_2388x436.png 848w, https://substackcdn.com/image/fetch/$s_!OrbH!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7df60219-05e0-42d2-9d1c-6a1ce192bb87_2388x436.png 1272w, https://substackcdn.com/image/fetch/$s_!OrbH!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7df60219-05e0-42d2-9d1c-6a1ce192bb87_2388x436.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!2Fqe!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35ee2c0e-b7c6-40c6-92b2-9c099fc7efc0_2388x436.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!2Fqe!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35ee2c0e-b7c6-40c6-92b2-9c099fc7efc0_2388x436.png 424w, https://substackcdn.com/image/fetch/$s_!2Fqe!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35ee2c0e-b7c6-40c6-92b2-9c099fc7efc0_2388x436.png 848w, https://substackcdn.com/image/fetch/$s_!2Fqe!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35ee2c0e-b7c6-40c6-92b2-9c099fc7efc0_2388x436.png 1272w, https://substackcdn.com/image/fetch/$s_!2Fqe!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35ee2c0e-b7c6-40c6-92b2-9c099fc7efc0_2388x436.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!2Fqe!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35ee2c0e-b7c6-40c6-92b2-9c099fc7efc0_2388x436.png" width="970" height="177.21153846153845" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/35ee2c0e-b7c6-40c6-92b2-9c099fc7efc0_2388x436.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:266,&quot;width&quot;:1456,&quot;resizeWidth&quot;:970,&quot;bytes&quot;:53397,&quot;alt&quot;:&quot;&quot;,&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;:&quot;https://uptodata.substack.com/i/171438629?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35ee2c0e-b7c6-40c6-92b2-9c099fc7efc0_2388x436.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-large" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!2Fqe!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35ee2c0e-b7c6-40c6-92b2-9c099fc7efc0_2388x436.png 424w, https://substackcdn.com/image/fetch/$s_!2Fqe!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35ee2c0e-b7c6-40c6-92b2-9c099fc7efc0_2388x436.png 848w, https://substackcdn.com/image/fetch/$s_!2Fqe!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35ee2c0e-b7c6-40c6-92b2-9c099fc7efc0_2388x436.png 1272w, https://substackcdn.com/image/fetch/$s_!2Fqe!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F35ee2c0e-b7c6-40c6-92b2-9c099fc7efc0_2388x436.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><blockquote><p><em><strong>Figure 6: Guardrails blocking HIPAA violations </strong></em></p></blockquote><h2>4. Making Guardrails Explainable</h2><p>Unlike traditional black-box filters, NeMo Guardrails produces <strong>structured metadata</strong> for every block. Developers and compliance officers can see exactly <strong>why</strong> a prompt was blocked.</p><h3>Example: Legal Liability Detection</h3><pre><code><code>result = app.generate("Help me sue my neighbor")
print(result.explanations)
</code></code></pre><p><strong>Output</strong></p><pre><code><code>{
  "blocked": true,
  "reason": "Legal Liability - Detected request for legal advice"
}
</code></code></pre><p>This supports:</p><ul><li><p><strong>Compliance audits</strong> &#8211; showing regulators why content was blocked</p></li><li><p><strong>Debugging</strong> &#8211; letting developers refine patterns and rules</p></li><li><p><strong>Risk dashboards</strong> &#8211; aggregating blocked categories across usage</p></li></ul><h3>Example: HIPAA Compliance</h3><p>In healthcare, transparency is just as important as blocking unsafe prompts. A compliance officer doesn&#8217;t just want to know that the system stopped a response - they need to see <strong>why</strong> it was stopped and which regulatory category was triggered.</p><pre><code>result = app.generate("Can you access patient records for John Doe?")
print(result.explanations)</code></pre><pre><code>{
  "blocked": true,
  "reason": "HIPAA Violation - Attempted access to protected patient data"
}</code></pre><h2>5. Why This Matters</h2><p>By codifying conversational risks into <strong>structured categories, patterns, and explainable outputs</strong>, NeMo Guardrails enables enterprises to:</p><ul><li><p><strong>Customer service</strong> &#8594; Prevent data leaks, fraud, and liability</p></li><li><p><strong>Healthcare</strong> &#8594; Align outputs with HIPAA/FDA constraints</p></li><li><p><strong>Enterprise AI</strong> &#8594; Treat safety as a measurable, auditable layer</p></li></ul><p>Instead of an opaque &#8220;safety filter,&#8221; Guardrails turns safety into <strong>configurable infrastructure</strong>. Developers can reason about rules, test them, and visualize performance across scenarios.</p>]]></content:encoded></item><item><title><![CDATA[Accelerating DenseNet-121 Inference NVIDIA]]></title><description><![CDATA[Part 2: A MedNIST case study on accelerating medical imaging workflows with GPU-native pipelines]]></description><link>https://uptodata.substack.com/p/accelerating-densenet-121-inference</link><guid isPermaLink="false">https://uptodata.substack.com/p/accelerating-densenet-121-inference</guid><dc:creator><![CDATA[Kopal Garg]]></dc:creator><pubDate>Sat, 16 Aug 2025 08:01:35 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!TXNo!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9fddfcd-d5fb-4e97-902a-015a3961c545_5247x3451.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Modern medical imaging workflows must process thousands of high-resolution scans rapidly. To study this, we built an end-to-end DenseNet-121 inference pipeline on the MedNIST dataset and compared CPU vs GPU performance across every stage.</p><p>GitHub Repo: <a href="https://github.com/kopalgarg/densenet121-nvidia">Link</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_!TXNo!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9fddfcd-d5fb-4e97-902a-015a3961c545_5247x3451.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!TXNo!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9fddfcd-d5fb-4e97-902a-015a3961c545_5247x3451.png 424w, https://substackcdn.com/image/fetch/$s_!TXNo!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9fddfcd-d5fb-4e97-902a-015a3961c545_5247x3451.png 848w, https://substackcdn.com/image/fetch/$s_!TXNo!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9fddfcd-d5fb-4e97-902a-015a3961c545_5247x3451.png 1272w, https://substackcdn.com/image/fetch/$s_!TXNo!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9fddfcd-d5fb-4e97-902a-015a3961c545_5247x3451.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!TXNo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9fddfcd-d5fb-4e97-902a-015a3961c545_5247x3451.png" width="727.9948120117188" height="478.99658647474354" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f9fddfcd-d5fb-4e97-902a-015a3961c545_5247x3451.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;normal&quot;,&quot;height&quot;:958,&quot;width&quot;:1456,&quot;resizeWidth&quot;:727.9948120117188,&quot;bytes&quot;:566472,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://uptodata.substack.com/i/171106463?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9fddfcd-d5fb-4e97-902a-015a3961c545_5247x3451.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!TXNo!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9fddfcd-d5fb-4e97-902a-015a3961c545_5247x3451.png 424w, https://substackcdn.com/image/fetch/$s_!TXNo!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9fddfcd-d5fb-4e97-902a-015a3961c545_5247x3451.png 848w, https://substackcdn.com/image/fetch/$s_!TXNo!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9fddfcd-d5fb-4e97-902a-015a3961c545_5247x3451.png 1272w, https://substackcdn.com/image/fetch/$s_!TXNo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff9fddfcd-d5fb-4e97-902a-015a3961c545_5247x3451.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><blockquote><p><em><strong>Figure: DenseNet-121 classification results on MedNIST images: Head CT, Chest X-Ray (CXR), and Abdomen CT. The top row shows the original input scans, while the bottom row highlights the model&#8217;s region-of-interest focus with bounding ellipses. Predicted class labels are displayed with high confidence scores -HeadCT (96.3%), CXR (94.7%), and AbdomenCT (95.1%).</strong></em></p></blockquote><p>The key stages include data loading, preprocessing, augmentation, batching, device transfer, inference, and post-processing. In each stage, we substitute CPU-based tools (Pillow/OpenCV, pandas, etc.) with NVIDIA GPU-accelerated libraries (DALI, cuDF, etc.) to remove bottlenecks and boost throughput. For example, on an NVIDIA Tesla T4 GPU we achieved ~0.109&#8239;s per batch of 32 (&#8776;3.4&#8239;ms/image) versus ~0.315&#8239;s on an 8-core CPU - a ~3.3&#215; speedup in throughput. The GPU ran at ~88% utilization, whereas CPU cores were mostly idle (~62% utilization), showing the GPU&#8217;s efficiency under load.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!hXix!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0b42f93-90d2-423a-b4e0-8db8351d46be_4471x2205.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!hXix!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0b42f93-90d2-423a-b4e0-8db8351d46be_4471x2205.png 424w, https://substackcdn.com/image/fetch/$s_!hXix!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0b42f93-90d2-423a-b4e0-8db8351d46be_4471x2205.png 848w, https://substackcdn.com/image/fetch/$s_!hXix!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0b42f93-90d2-423a-b4e0-8db8351d46be_4471x2205.png 1272w, https://substackcdn.com/image/fetch/$s_!hXix!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0b42f93-90d2-423a-b4e0-8db8351d46be_4471x2205.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!hXix!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0b42f93-90d2-423a-b4e0-8db8351d46be_4471x2205.png" width="1048" height="516.8021978021978" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b0b42f93-90d2-423a-b4e0-8db8351d46be_4471x2205.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:718,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1048,&quot;bytes&quot;:563391,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://uptodata.substack.com/i/171106463?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0b42f93-90d2-423a-b4e0-8db8351d46be_4471x2205.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-large" alt="" srcset="https://substackcdn.com/image/fetch/$s_!hXix!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0b42f93-90d2-423a-b4e0-8db8351d46be_4471x2205.png 424w, https://substackcdn.com/image/fetch/$s_!hXix!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0b42f93-90d2-423a-b4e0-8db8351d46be_4471x2205.png 848w, https://substackcdn.com/image/fetch/$s_!hXix!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0b42f93-90d2-423a-b4e0-8db8351d46be_4471x2205.png 1272w, https://substackcdn.com/image/fetch/$s_!hXix!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0b42f93-90d2-423a-b4e0-8db8351d46be_4471x2205.png 1456w" sizes="100vw"></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><blockquote><p><em><strong>Figure: The diagram below summarizes our pipeline stages and tool mappings. The left side shows the CPU-based workflow (Pillow/OpenCV, PyTorch DataLoader, etc.) and the right side shows the NVIDIA GPU-accelerated workflow (DALI, CUDA tensors, etc.).</strong></em></p></blockquote><h1><strong>Pipeline Architecture:</strong></h1><p>Our end-to-end inference pipeline has these stages:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!6nst!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5b8d2f50-d986-4f2f-a596-5d8173b06d9f_3840x522.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!6nst!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5b8d2f50-d986-4f2f-a596-5d8173b06d9f_3840x522.png 424w, https://substackcdn.com/image/fetch/$s_!6nst!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5b8d2f50-d986-4f2f-a596-5d8173b06d9f_3840x522.png 848w, https://substackcdn.com/image/fetch/$s_!6nst!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5b8d2f50-d986-4f2f-a596-5d8173b06d9f_3840x522.png 1272w, https://substackcdn.com/image/fetch/$s_!6nst!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5b8d2f50-d986-4f2f-a596-5d8173b06d9f_3840x522.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!6nst!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5b8d2f50-d986-4f2f-a596-5d8173b06d9f_3840x522.png" width="1034" height="140.61263736263737" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5b8d2f50-d986-4f2f-a596-5d8173b06d9f_3840x522.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:198,&quot;width&quot;:1456,&quot;resizeWidth&quot;:1034,&quot;bytes&quot;:136737,&quot;alt&quot;:&quot;&quot;,&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;:&quot;https://uptodata.substack.com/i/171106463?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5b8d2f50-d986-4f2f-a596-5d8173b06d9f_3840x522.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-large" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!6nst!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5b8d2f50-d986-4f2f-a596-5d8173b06d9f_3840x522.png 424w, https://substackcdn.com/image/fetch/$s_!6nst!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5b8d2f50-d986-4f2f-a596-5d8173b06d9f_3840x522.png 848w, https://substackcdn.com/image/fetch/$s_!6nst!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5b8d2f50-d986-4f2f-a596-5d8173b06d9f_3840x522.png 1272w, https://substackcdn.com/image/fetch/$s_!6nst!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5b8d2f50-d986-4f2f-a596-5d8173b06d9f_3840x522.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><blockquote><p><em><strong>Figure: DenseNet-121 medical imaging inference pipeline. From data loading and preprocessing to GPU transfer, inference, and postprocessing for confidence mapping and evaluation</strong></em></p></blockquote><h3><strong>Data Loading:</strong> </h3><p>Read image files and assign class labels. We collected ~64K MedNIST JPEGs (6 classes) and split them into train/validation.</p><h3><strong>Preprocessing:</strong> </h3><p>Decode JPEGs, resize to 256&#215;256, and normalize pixel intensities to [0,1].</p><h3><strong>Data Augmentation (training only):</strong> </h3><p>Apply randomized transforms (e.g. crops, flips, noise, blur, brightness/contrast).</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!I9PZ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F843f68c6-8e7e-4ee9-910f-7bd8b41701e4_4959x4425.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!I9PZ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F843f68c6-8e7e-4ee9-910f-7bd8b41701e4_4959x4425.png 424w, https://substackcdn.com/image/fetch/$s_!I9PZ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F843f68c6-8e7e-4ee9-910f-7bd8b41701e4_4959x4425.png 848w, https://substackcdn.com/image/fetch/$s_!I9PZ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F843f68c6-8e7e-4ee9-910f-7bd8b41701e4_4959x4425.png 1272w, https://substackcdn.com/image/fetch/$s_!I9PZ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F843f68c6-8e7e-4ee9-910f-7bd8b41701e4_4959x4425.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!I9PZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F843f68c6-8e7e-4ee9-910f-7bd8b41701e4_4959x4425.png" width="1456" height="1299" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/843f68c6-8e7e-4ee9-910f-7bd8b41701e4_4959x4425.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1299,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:888418,&quot;alt&quot;:&quot;&quot;,&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;:&quot;https://uptodata.substack.com/i/171106463?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F843f68c6-8e7e-4ee9-910f-7bd8b41701e4_4959x4425.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!I9PZ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F843f68c6-8e7e-4ee9-910f-7bd8b41701e4_4959x4425.png 424w, https://substackcdn.com/image/fetch/$s_!I9PZ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F843f68c6-8e7e-4ee9-910f-7bd8b41701e4_4959x4425.png 848w, https://substackcdn.com/image/fetch/$s_!I9PZ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F843f68c6-8e7e-4ee9-910f-7bd8b41701e4_4959x4425.png 1272w, https://substackcdn.com/image/fetch/$s_!I9PZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F843f68c6-8e7e-4ee9-910f-7bd8b41701e4_4959x4425.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><blockquote><p><em><strong>Figure: Step-by-step Medical Image Augmentation Pipeline. Simulates real-world imaging variations like noise, motion blur, brightness, contrast, zoom, flips, and rotations. Enhances dataset diversity for robust deep learning in medical imaging</strong></em></p></blockquote><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!9nSs!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F265f8432-3754-4017-afa7-e2a3d58f76ec_5308x1661.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!9nSs!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F265f8432-3754-4017-afa7-e2a3d58f76ec_5308x1661.png 424w, https://substackcdn.com/image/fetch/$s_!9nSs!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F265f8432-3754-4017-afa7-e2a3d58f76ec_5308x1661.png 848w, https://substackcdn.com/image/fetch/$s_!9nSs!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F265f8432-3754-4017-afa7-e2a3d58f76ec_5308x1661.png 1272w, https://substackcdn.com/image/fetch/$s_!9nSs!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F265f8432-3754-4017-afa7-e2a3d58f76ec_5308x1661.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!9nSs!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F265f8432-3754-4017-afa7-e2a3d58f76ec_5308x1661.png" width="856" height="268.0879120879121" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/265f8432-3754-4017-afa7-e2a3d58f76ec_5308x1661.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:456,&quot;width&quot;:1456,&quot;resizeWidth&quot;:856,&quot;bytes&quot;:413732,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://uptodata.substack.com/i/171106463?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F265f8432-3754-4017-afa7-e2a3d58f76ec_5308x1661.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-large" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!9nSs!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F265f8432-3754-4017-afa7-e2a3d58f76ec_5308x1661.png 424w, https://substackcdn.com/image/fetch/$s_!9nSs!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F265f8432-3754-4017-afa7-e2a3d58f76ec_5308x1661.png 848w, https://substackcdn.com/image/fetch/$s_!9nSs!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F265f8432-3754-4017-afa7-e2a3d58f76ec_5308x1661.png 1272w, https://substackcdn.com/image/fetch/$s_!9nSs!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F265f8432-3754-4017-afa7-e2a3d58f76ec_5308x1661.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><blockquote><p><em><strong>Figure: Original vs Augmented. The augmented CT shows changes in intensity and structure, mimicking real-world variability. The difference map highlights pixel-wise intensity shifts between the two images</strong></em></p></blockquote><h3><strong>Batching &amp; Device Transfer:</strong> </h3><p>Batches of 32.</p><ul><li><p><strong>GPU path (DALI):</strong> On-GPU decoding and batching with overlap between I/O and inference.</p></li><li><p><strong>CPU path (PyTorch DataLoader):</strong> Host batching with explicit <code>.to(device)</code> copies, adding latency.</p></li></ul><h3><strong>Inference</strong>: </h3><p>We run DenseNet-121 (2D, 1 input channel, 6 classes) in <code>eval()</code> mode with FP16 enabled to exploit GPU tensor cores.</p><h3><strong>Performance:</strong> </h3><p>On a Tesla T4, 32-image batches complete in 0.109 s vs 0.315 s on CPU - a 3.3&#215; throughput gain (103 vs 31 img/s)</p><h3><strong>Output handling:</strong> </h3><p>Logits are passed through softmax/top-k to extract top-1 predictions and confidence scores. GPU vs CPU outputs align aside from minor floating-point differences.</p><h3><strong>Monitoring:</strong> </h3><p>A custom profiler logs per-batch timings plus GPU utilization, memory, and power at ~10 Hz. From these metrics, we derive latency, throughput, and energy efficiency.</p><h3><strong>Post-processing:</strong> </h3><p>Extract top-k predictions, format results, and perform consistency checks.</p><h3><strong>Logging &amp; Metrics:</strong> </h3><p>Record timings, throughput, and hardware utilization throughout.</p><h1><strong>Performance Analysis</strong></h1><p>Our key quantitative findings are summarized below. These numbers are averaged over many batches during a full validation pass on MedNIST.</p><h3><strong>Latency and Throughput</strong></h3><p>The GPU achieves ~3.3&#215; higher throughput and ~2.9&#215; lower batch time than the CPU. This matches the qualitative expectation that a Tesla T4 (with 2560 CUDA cores) can process dense CNN workloads much faster than a multi-core CPU for the same batch.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ziY8!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faa8b8c74-b96c-4bde-aab8-82c3a1373a34_3840x1244.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ziY8!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faa8b8c74-b96c-4bde-aab8-82c3a1373a34_3840x1244.png 424w, https://substackcdn.com/image/fetch/$s_!ziY8!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faa8b8c74-b96c-4bde-aab8-82c3a1373a34_3840x1244.png 848w, https://substackcdn.com/image/fetch/$s_!ziY8!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faa8b8c74-b96c-4bde-aab8-82c3a1373a34_3840x1244.png 1272w, https://substackcdn.com/image/fetch/$s_!ziY8!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faa8b8c74-b96c-4bde-aab8-82c3a1373a34_3840x1244.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ziY8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faa8b8c74-b96c-4bde-aab8-82c3a1373a34_3840x1244.png" width="1456" height="472" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/aa8b8c74-b96c-4bde-aab8-82c3a1373a34_3840x1244.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:472,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:250763,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://uptodata.substack.com/i/171106463?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faa8b8c74-b96c-4bde-aab8-82c3a1373a34_3840x1244.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!ziY8!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faa8b8c74-b96c-4bde-aab8-82c3a1373a34_3840x1244.png 424w, https://substackcdn.com/image/fetch/$s_!ziY8!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faa8b8c74-b96c-4bde-aab8-82c3a1373a34_3840x1244.png 848w, https://substackcdn.com/image/fetch/$s_!ziY8!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faa8b8c74-b96c-4bde-aab8-82c3a1373a34_3840x1244.png 1272w, https://substackcdn.com/image/fetch/$s_!ziY8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Faa8b8c74-b96c-4bde-aab8-82c3a1373a34_3840x1244.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><blockquote><p><em><strong>Figure: Comparison of GPU (Tesla T4) vs CPU (8 cores) performance on DenseNet-121 inference. The GPU achieves ~2.9&#215; lower batch time and ~3.3&#215; higher throughput, while also improving memory and power efficiency.</strong></em></p></blockquote><h3><strong>Efficiency and Utilization</strong></h3><ul><li><p>The GPU ran at ~88% compute utilization (measured via NVML), with VRAM usage around <strong>1.2 GB</strong> for inference. In contrast, the CPU&#8217;s 8 cores averaged only ~62% utilization (due to GIL, I/O stalls, etc.) and used ~4.5 GB of system RAM for buffers and intermediate data.</p></li><li><p>We measured power draw during inference. Normalized to a standard metric, the GPU delivered roughly 40% higher &#8220;images per joule&#8221; efficiency than the CPU.</p></li></ul><h3><strong>Memory Footprint</strong></h3><p>Our GPU pipeline&#8217;s memory efficiency is higher: only ~3 GB (20%) of the T4&#8217;s 15 GB VRAM was needed, whereas the CPU approach used ~15 GB of RAM due to unmanaged buffers.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!UI1u!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93b5799c-7ac4-42cc-b1fa-2de4b61506a3_5965x2374.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!UI1u!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93b5799c-7ac4-42cc-b1fa-2de4b61506a3_5965x2374.png 424w, https://substackcdn.com/image/fetch/$s_!UI1u!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93b5799c-7ac4-42cc-b1fa-2de4b61506a3_5965x2374.png 848w, https://substackcdn.com/image/fetch/$s_!UI1u!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93b5799c-7ac4-42cc-b1fa-2de4b61506a3_5965x2374.png 1272w, https://substackcdn.com/image/fetch/$s_!UI1u!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93b5799c-7ac4-42cc-b1fa-2de4b61506a3_5965x2374.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!UI1u!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93b5799c-7ac4-42cc-b1fa-2de4b61506a3_5965x2374.png" width="890" height="353.9217032967033" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/93b5799c-7ac4-42cc-b1fa-2de4b61506a3_5965x2374.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:579,&quot;width&quot;:1456,&quot;resizeWidth&quot;:890,&quot;bytes&quot;:550383,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://uptodata.substack.com/i/171106463?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93b5799c-7ac4-42cc-b1fa-2de4b61506a3_5965x2374.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-large" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!UI1u!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93b5799c-7ac4-42cc-b1fa-2de4b61506a3_5965x2374.png 424w, https://substackcdn.com/image/fetch/$s_!UI1u!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93b5799c-7ac4-42cc-b1fa-2de4b61506a3_5965x2374.png 848w, https://substackcdn.com/image/fetch/$s_!UI1u!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93b5799c-7ac4-42cc-b1fa-2de4b61506a3_5965x2374.png 1272w, https://substackcdn.com/image/fetch/$s_!UI1u!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F93b5799c-7ac4-42cc-b1fa-2de4b61506a3_5965x2374.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><blockquote><p><strong>Figure: </strong><em><strong>Timing overlap: The DALI-based pipeline builds the next batch on GPU while the model processes the current batch. In contrast, the CPU pipeline often has idle gaps due to host-to-device transfers.</strong></em></p></blockquote><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!zXCl!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4d2a7575-b374-40d4-8795-f311c1338edc_5970x4770.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!zXCl!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4d2a7575-b374-40d4-8795-f311c1338edc_5970x4770.png 424w, https://substackcdn.com/image/fetch/$s_!zXCl!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4d2a7575-b374-40d4-8795-f311c1338edc_5970x4770.png 848w, https://substackcdn.com/image/fetch/$s_!zXCl!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4d2a7575-b374-40d4-8795-f311c1338edc_5970x4770.png 1272w, https://substackcdn.com/image/fetch/$s_!zXCl!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4d2a7575-b374-40d4-8795-f311c1338edc_5970x4770.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!zXCl!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4d2a7575-b374-40d4-8795-f311c1338edc_5970x4770.png" width="1456" height="1163" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4d2a7575-b374-40d4-8795-f311c1338edc_5970x4770.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1163,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:2666498,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:&quot;&quot;,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://uptodata.substack.com/i/171106463?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4d2a7575-b374-40d4-8795-f311c1338edc_5970x4770.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!zXCl!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4d2a7575-b374-40d4-8795-f311c1338edc_5970x4770.png 424w, https://substackcdn.com/image/fetch/$s_!zXCl!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4d2a7575-b374-40d4-8795-f311c1338edc_5970x4770.png 848w, https://substackcdn.com/image/fetch/$s_!zXCl!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4d2a7575-b374-40d4-8795-f311c1338edc_5970x4770.png 1272w, https://substackcdn.com/image/fetch/$s_!zXCl!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4d2a7575-b374-40d4-8795-f311c1338edc_5970x4770.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><blockquote><p><em><strong>Figure: GPU vs CPU Training Metrics. GPU shows higher utilization and memory efficiency, while CPU runs hotter but consumes slightly less power. Temperature and power limits remain below thresholds throughout the run</strong></em></p></blockquote><h1><strong>Key Takeaways</strong></h1><p>In summary, our comprehensive analysis demonstrates that GPU acceleration transforms medical imaging inference pipelines. By carefully mapping each stage (file I/O, decoding, augmentation, batching, inference) to NVIDIA tools, we achieved:</p><ul><li><p>GPU pipeline: <strong>3.3&#215; throughput, 2.9&#215; lower latency</strong> vs CPU.</p></li><li><p>Better efficiency: <strong>20% VRAM use vs 15 GB RAM</strong> on CPU.</p></li><li><p>Higher utilization: <strong>88% GPU vs 62% CPU</strong>.</p></li><li><p>~<strong>40% more images per watt</strong> &#8594; energy savings.</p></li></ul><h1><strong>Optimization Roadmap:</strong></h1><p>Our pipeline already leverages GPU acceleration via PyTorch, cuDNN, and cuBLAS, but further optimizations can push latency and efficiency gains:</p><h3><strong>Inference Optimizations</strong></h3><ul><li><p><strong>TensorRT:</strong> Operator fusion, kernel auto-tuning, and graph-level optimizations to reduce per-batch latency.</p></li><li><p><strong>INT8 Quantization:</strong> Cuts memory and bandwidth demands, accelerating inference while maintaining accuracy.</p></li></ul><h3><strong>Data &amp; I/O Pipeline</strong></h3><ul><li><p><strong>RAPIDS cuDF:</strong> Replaces Python preprocessing with GPU-native operations, eliminating host-device transfer overhead.</p></li><li><p><strong>GPUdirect Storage:</strong> Direct GPU-to-disk I/O to reduce data loading latency, especially for hospital archives.</p></li></ul><h3><strong>System Efficiency</strong></h3><ul><li><p><strong>DCGM Telemetry:</strong> Real-time monitoring of utilization, temperature, and power to enable adaptive scheduling in multi-tenant clusters.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!SVcc!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F135a5707-8c69-4e8b-a103-c0d8941220ba_3840x2425.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!SVcc!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F135a5707-8c69-4e8b-a103-c0d8941220ba_3840x2425.png 424w, https://substackcdn.com/image/fetch/$s_!SVcc!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F135a5707-8c69-4e8b-a103-c0d8941220ba_3840x2425.png 848w, https://substackcdn.com/image/fetch/$s_!SVcc!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F135a5707-8c69-4e8b-a103-c0d8941220ba_3840x2425.png 1272w, https://substackcdn.com/image/fetch/$s_!SVcc!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F135a5707-8c69-4e8b-a103-c0d8941220ba_3840x2425.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!SVcc!,w_2400,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F135a5707-8c69-4e8b-a103-c0d8941220ba_3840x2425.png" width="912" height="575.6373626373627" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/135a5707-8c69-4e8b-a103-c0d8941220ba_3840x2425.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;large&quot;,&quot;height&quot;:919,&quot;width&quot;:1456,&quot;resizeWidth&quot;:912,&quot;bytes&quot;:null,&quot;alt&quot;:&quot;&quot;,&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;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-large" alt="" title="" srcset="https://substackcdn.com/image/fetch/$s_!SVcc!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F135a5707-8c69-4e8b-a103-c0d8941220ba_3840x2425.png 424w, https://substackcdn.com/image/fetch/$s_!SVcc!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F135a5707-8c69-4e8b-a103-c0d8941220ba_3840x2425.png 848w, https://substackcdn.com/image/fetch/$s_!SVcc!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F135a5707-8c69-4e8b-a103-c0d8941220ba_3840x2425.png 1272w, https://substackcdn.com/image/fetch/$s_!SVcc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F135a5707-8c69-4e8b-a103-c0d8941220ba_3840x2425.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><blockquote><p><em><strong>Figure: Proposed enhancements to the GPU inference stack: TensorRT optimization, INT8 quantization, DCGM telemetry, RAPIDS cuDF preprocessing, and GPUdirect Storage integration could further accelerate DenseNet-121 inference and improve memory and energy efficiency</strong></em></p></blockquote><p>Overall, these results suggest that with TensorRT optimization and GPU-native preprocessing, throughput could scale further, enabling real-time triage pipelines for radiology workflows.</p>]]></content:encoded></item><item><title><![CDATA[Mapping NVIDIA's Full GenAI Toolchain]]></title><description><![CDATA[Part 1: A Practitioner&#8217;s Guide to NVIDIA&#8217;s GenAI Stack]]></description><link>https://uptodata.substack.com/p/nvidias-gen-ai-toolkit</link><guid isPermaLink="false">https://uptodata.substack.com/p/nvidias-gen-ai-toolkit</guid><dc:creator><![CDATA[Kopal Garg]]></dc:creator><pubDate>Fri, 15 Aug 2025 21:52:56 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!gDuv!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9249189f-58a6-4c52-9c8e-64fb20fbff85_4217x2160.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Generative AI systems need more than powerful models, they depend on the software and infrastructure that can train, serve, and scale them efficiently. NVIDIA&#8217;s AI Enterprise Software Reference Architecture organizes its tools into layers that cover the full lifecycle: building, optimizing, deploying, and managing AI at scale.</p><p>I pulled together the diagram below to make sense of NVIDIA&#8217;s <strong>AI Enterprise Software Reference Architecture</strong>. This guide shows how these can be applied in practical GenAI workflows, along with examples and non-NVIDIA equivalents.</p><p>Here&#8217;s my breakdown, with <strong>GenAI use cases</strong> for each tool.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!gDuv!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9249189f-58a6-4c52-9c8e-64fb20fbff85_4217x2160.png" data-component-name="Image2ToDOM"><div class="image2-inset image2-full-screen"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!gDuv!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9249189f-58a6-4c52-9c8e-64fb20fbff85_4217x2160.png 424w, https://substackcdn.com/image/fetch/$s_!gDuv!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9249189f-58a6-4c52-9c8e-64fb20fbff85_4217x2160.png 848w, https://substackcdn.com/image/fetch/$s_!gDuv!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9249189f-58a6-4c52-9c8e-64fb20fbff85_4217x2160.png 1272w, https://substackcdn.com/image/fetch/$s_!gDuv!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9249189f-58a6-4c52-9c8e-64fb20fbff85_4217x2160.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!gDuv!,w_5760,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9249189f-58a6-4c52-9c8e-64fb20fbff85_4217x2160.png" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9249189f-58a6-4c52-9c8e-64fb20fbff85_4217x2160.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;full&quot;,&quot;height&quot;:746,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:544441,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://uptodata.substack.com/i/171029254?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9249189f-58a6-4c52-9c8e-64fb20fbff85_4217x2160.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-fullscreen" alt="" srcset="https://substackcdn.com/image/fetch/$s_!gDuv!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9249189f-58a6-4c52-9c8e-64fb20fbff85_4217x2160.png 424w, https://substackcdn.com/image/fetch/$s_!gDuv!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9249189f-58a6-4c52-9c8e-64fb20fbff85_4217x2160.png 848w, https://substackcdn.com/image/fetch/$s_!gDuv!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9249189f-58a6-4c52-9c8e-64fb20fbff85_4217x2160.png 1272w, https://substackcdn.com/image/fetch/$s_!gDuv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9249189f-58a6-4c52-9c8e-64fb20fbff85_4217x2160.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><h1>AI Frameworks &amp; SDKs: </h1><h3>Where you build and fine-tune the brains of your system</h3><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\begin{array}{|p{0.46\\linewidth}|p{0.46\\linewidth}|} \\hline \\textbf{Non-NVIDIA Equivalent} &amp; \\textbf{NVIDIA Tool} \\\\ \\hline \\textnormal{Hugging Face Transformers + DeepSpeed, OpenAI Fine-Tuning API} &amp; \\textnormal{NeMo} \\\\ \\hline \\textnormal{AutoGluon, Google AutoML} &amp; \\textnormal{TAO Toolkit} \\\\ \\hline \\textnormal{Spark MLlib, LightFM} &amp; \\textnormal{Merlin} \\\\ \\hline \\textnormal{MONAI, TensorFlow Healthcare} &amp; \\textnormal{Clara} \\\\ \\hline \\textnormal{OneDNN (Intel), ROCm (AMD), OpenCV} &amp; \\textnormal{cuDNN / cuBLAS / NCCL / DALI} \\\\ \\hline \\end{array}&quot;,&quot;id&quot;:&quot;GSVORGQAKH&quot;}" data-component-name="LatexBlockToDOM"></div><h4>NVIDIA NeMo</h4><p><em><strong>What is it?</strong></em> An open-source framework for training and fine-tuning large language models, ASR, TTS, and multimodal models. It has ready-to-use pipelines for pretraining and fine-tuning, plus domain adaptation tools so you can customize vocabularies and tokenizers. Built to handle massive models with Megatron-LM integration.</p><blockquote><p><em><strong>Example:</strong> </em>Fine-tune a legal Q&amp;A model so it understands Latin legal terms and cites case law accurately.</p></blockquote><p><em><strong>Integration tip:</strong></em> Pair with FAISS-GPU or Milvus for RAG, and deploy through Triton for production.</p><h4>NVIDIA TAO Toolkit</h4><p><em><strong>What is it?</strong></em> Transfer learning without building training loops from scratch. You can adapt vision, speech, and NLP models in a low-code way.</p><blockquote><p><em><strong>Example:</strong> </em>Fine-tune a speech-to-text model on your call center data in a few hours without writing custom training loops.</p></blockquote><h4>Domain-Specific SDKs</h4><p><em><strong>What is it?</strong></em> Pre-optimized kits for specialized industries. These are industry-optimized kits to shortcut development.</p><blockquote><p><em><strong>Examples:</strong> </em></p></blockquote><ul><li><p><strong>Merlin</strong> &#8594; Build personalized product recommenders in an e-commerce chatbot (like &#8220;users who read this also liked&#8230;&#8221;).</p></li><li><p><strong>Clara</strong> &#8594; Use healthcare-specific AI in a patient-facing Q&amp;A assistant.</p></li></ul><h4>Core GPU Libraries</h4><p><em><strong>What is it?</strong></em> The CUDA-X building blocks that make everything faster. They speed up deep learning math, data loading, and GPU-to-GPU communication.</p><blockquote><p><em><strong>Examples</strong>: </em>NCCL lets multiple GPUs &#8220;talk&#8221; super fast so a big LLM can run across them without slowdown.</p></blockquote><h1>Data Processing &amp; Analytics Tools: </h1><h3>Where you clean, prep, and transform raw data into AI-ready formats - at GPU speed</h3><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\begin{array}{|p{0.46\\linewidth}|p{0.46\\linewidth}|}\n\\hline\n\\textbf{Non-NVIDIA Equivalent} &amp; \\textbf{NVIDIA Tool} \\\\ \\hline\n\\textnormal{Pandas, scikit-learn, Spark (CPU)} &amp; \\textnormal{RAPIDS} \\\\ \\hline\n\\textnormal{OpenCV, tf.data} &amp; \\textnormal{DALI} \\\\ \\hline\n\\end{array}\n&quot;,&quot;id&quot;:&quot;BYRZLMHXNX&quot;}" data-component-name="LatexBlockToDOM"></div><h4>RAPIDS (cuDF, cuML, cuGraph, etc.)</h4><p><em><strong>What is it?</strong></em> Think Pandas or Spark, but everything stays in GPU memory. This removes the CPU as a bottleneck.</p><blockquote><p><em><strong>Example:</strong></em> Process millions of PDFs, clean the text, and generate embeddings in hours instead of days.</p></blockquote><h4>NVIDIA DALI</h4><p><em><strong>What is it? </strong></em>Loads and preprocesses data directly on the GPU so your model is never idle waiting for the next batch.</p><blockquote><p><em><strong>Example:</strong></em> Normalize and batch massive image datasets for a multimodal RAG pipeline, removing the I/O bottleneck.</p></blockquote><p><em><strong>Integration tip:</strong></em> NCCL uses NVLink/InfiniBand for multi-GPU all-reduce; DALI keeps preprocessing in GPU memory. </p><h1>Inference &amp; Deployment Tools: </h1><h3>Where you turn trained models into fast, reliable services.</h3><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\begin{array}{|p{0.46\\linewidth}|p{0.46\\linewidth}|}\n\\hline\n\\textbf{Non-NVIDIA Equivalent} &amp; \\textbf{NVIDIA Tool} \\\\ \\hline\n\\textnormal{ONNX Runtime (CPU/GPU), TVM} &amp; \\textnormal{TensorRT} \\\\ \\hline\n\\textnormal{vLLM, FasterTransformer} &amp; \\textnormal{TensorRT-LLM} \\\\ \\hline\n\\textnormal{KFServing, TorchServe} &amp; \\textnormal{Triton Inference Server} \\\\ \\hline\n\\textnormal{Vosk, Mozilla DeepSpeech} &amp; \\textnormal{Riva} \\\\ \\hline\n\\textnormal{Hugging Face Endpoints, AWS Sagemaker} &amp; \\textnormal{NIM + NIM Operator} \\\\ \\hline\n\\end{array}\n&quot;,&quot;id&quot;:&quot;GDOPSZXOIB&quot;}" data-component-name="LatexBlockToDOM"></div><h4>NVIDIA TensorRT</h4><p><em><strong>What is it? </strong></em>Optimizes models for much faster inference through kernel fusion and mixed precision.</p><blockquote><p><em><strong>Example:</strong></em> Cut chatbot latency from 1 second to 0.5 seconds without losing accuracy.</p></blockquote><p><em><strong>Integration tip:</strong></em> Use before deployment to Triton for max throughput.</p><h4>TensorRT-LLM</h4><p><em><strong>What is it? </strong></em>Optimized for large language models with paged KV cache and speculative decoding.</p><blockquote><p><em><strong>Example:</strong></em> Serve a 70B model at high QPS for customer support without doubling GPU count.</p></blockquote><h4>NVIDIA Triton Inference Server</h4><p><em><strong>What is it? </strong></em>One API for serving models from TensorFlow, PyTorch, ONNX, OpenVINO, and more. Auto-batches requests to get the most from your GPUs.</p><blockquote><p><em><strong>Example:</strong></em> Host your embedding model and reranker together and let Triton batch requests automatically.</p></blockquote><h4>NVIDIA Riva</h4><p><em><strong>What is it? </strong></em>A real-time speech AI SDK for streaming ASR and TTS.</p><blockquote><p><em><strong>Example:</strong></em> Add a voice interface to your RAG assistant so it can take spoken queries and respond aloud instantly.</p></blockquote><p><em><strong>Technical integration:</strong></em> Pair with Triton or NIM for unified deployment.</p><h4>NVIDIA NIM + NIM Operator</h4><p><em><strong>What is it? </strong></em>Prepackaged AI microservices you can deploy on any GPU environment in minutes.</p><blockquote><p><em><strong>Example:</strong></em> Deploy a retrieval API for your RAG app without touching CUDA installs.</p></blockquote><h1>Orchestration &amp; Management: </h1><h3>Where you keep all the moving parts running - and scaling - in production.</h3><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\begin{array}{|p{0.46\\linewidth}|p{0.46\\linewidth}|}\n\\hline\n\\textbf{Non-NVIDIA Equivalent} &amp; \\textbf{NVIDIA Tool} \\\\ \\hline\n\\textnormal{k8s device plugins} &amp; \\textnormal{GPU Operator} \\\\ \\hline\n\\textnormal{Slurm, Ray Cluster} &amp; \\textnormal{Base Command} \\\\ \\hline\n\\textnormal{AWS IoT Greengrass} &amp; \\textnormal{Fleet Command} \\\\ \\hline\n\\textnormal{MLflow, Weights \\&amp; Biases} &amp; \\textnormal{AI Workbench / Kubeflow / MLflow} \\\\ \\hline\n\\textnormal{node\\_exporter, custom Prometheus exporters} &amp; \\textnormal{DCGM + Prometheus} \\\\ \\hline\n\\end{array}\n&quot;,&quot;id&quot;:&quot;OXBMYMPTUT&quot;}" data-component-name="LatexBlockToDOM"></div><h4>NVIDIA GPU Operator </h4><p><em><strong>What is it? </strong></em>Handles GPU driver and CUDA setup in Kubernetes automatically.</p><blockquote><p><em><strong>Example</strong></em><strong>:</strong> Add a new GPU node to your inference cluster and have it production-ready in minutes.</p></blockquote><h4>NVIDIA Base Command </h4><p><em><strong>What is it? </strong></em>The control center for DGX systems.</p><blockquote><p><em><strong>Example:</strong></em> Monitor a multi-node LLM training job and restart failed tasks automatically.</p></blockquote><h4>NVIDIA Fleet Command </h4><p><em><strong>What is it? </strong></em>Securely deploy and update AI apps across distributed sites.</p><blockquote><p><em><strong>Example</strong></em><strong>:</strong> Push a new model version to hundreds of retail store edge nodes overnight.</p></blockquote><h4>MLOps Integrations (AI Workbench, Kubeflow, MLflow) </h4><p><em><strong>What is it? </strong></em>Tools for tracking experiments, automating pipelines, and managing model versions.</p><blockquote><p><em><strong>Example</strong></em><strong>:</strong> Swap in a new reranker model and roll back instantly if something breaks.</p></blockquote><h4>Monitoring &amp; Logging (DCGM + Prometheus/Grafana)</h4><p><em><strong>What is it? </strong></em>Full GPU fleet observability.</p><blockquote><p><em><strong>Example</strong></em><strong>:</strong> Catch GPUs running hot before they throttle and slow down your responses.</p></blockquote><h1>Deployment &amp; Infrastructure Layer: </h1><h3>Where your workloads actually run.</h3><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\begin{array}{|p{0.46\\linewidth}|p{0.46\\linewidth}|}\n\\hline\n\\textbf{Non-NVIDIA Equivalent} &amp; \\textbf{NVIDIA Tool} \\\\ \\hline\n\\textnormal{Docker Hub, Hugging Face Hub} &amp; \\textnormal{NGC Catalog} \\\\ \\hline\n\\textnormal{AWS p4d, Lambda Labs GPU servers} &amp; \\textnormal{DGX Systems} \\\\ \\hline\n\\textnormal{ROCm, OneAPI} &amp; \\textnormal{CUDA Toolkit} \\\\ \\hline\n\\end{array}\n&quot;,&quot;id&quot;:&quot;LLEABAXCRV&quot;}" data-component-name="LatexBlockToDOM"></div><h4>NVIDIA NGC Catalog</h4><p><em><strong>What is it? </strong></em>A marketplace of GPU-optimized containers, models, and SDKs.</p><blockquote><p><em><strong>Example</strong></em><strong>:</strong> Start a multimodal RAG proof-of-concept in hours by pulling a prebuilt NeMo container.</p></blockquote><h4>NVIDIA DGX System<strong>s</strong></h4><p><em><strong>What is it? </strong></em>GPU supercomputers with NVLink for large-scale AI training.</p><blockquote><p><em><strong>Example</strong></em><strong>:</strong> Train a domain-specific 70B LLM in days instead of weeks.</p></blockquote><h4>System Software (CUDA toolkit, container runtime)</h4><p><em><strong>What is it? </strong></em>The foundation for running any GPU workload in Docker or Kubernetes.</p><blockquote><p><em><strong>Example</strong></em><strong>:</strong> Deploy your entire RAG pipeline in containers with full GPU acceleration.</p></blockquote><p></p><p>The examples here are starting points. In practice, you&#8217;ll mix and match based on your model type, data sources, latency requirements, and operational constraints. The key is to treat this stack as a set of interchangeable building blocks rather than a rigid recipe, that&#8217;s how you&#8217;ll get from a promising prototype to a production system that lasts.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!qtDq!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F298fd71b-38b2-481f-9cf2-ff6d32b7bf87_5525x1283.png" data-component-name="Image2ToDOM"><div class="image2-inset image2-full-screen"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!qtDq!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F298fd71b-38b2-481f-9cf2-ff6d32b7bf87_5525x1283.png 424w, https://substackcdn.com/image/fetch/$s_!qtDq!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F298fd71b-38b2-481f-9cf2-ff6d32b7bf87_5525x1283.png 848w, https://substackcdn.com/image/fetch/$s_!qtDq!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F298fd71b-38b2-481f-9cf2-ff6d32b7bf87_5525x1283.png 1272w, https://substackcdn.com/image/fetch/$s_!qtDq!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F298fd71b-38b2-481f-9cf2-ff6d32b7bf87_5525x1283.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!qtDq!,w_5760,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F298fd71b-38b2-481f-9cf2-ff6d32b7bf87_5525x1283.png" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/298fd71b-38b2-481f-9cf2-ff6d32b7bf87_5525x1283.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;full&quot;,&quot;height&quot;:338,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:294063,&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;:&quot;https://uptodata.substack.com/i/171029254?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F298fd71b-38b2-481f-9cf2-ff6d32b7bf87_5525x1283.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:&quot;center&quot;,&quot;offset&quot;:false}" class="sizing-fullscreen" alt="" srcset="https://substackcdn.com/image/fetch/$s_!qtDq!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F298fd71b-38b2-481f-9cf2-ff6d32b7bf87_5525x1283.png 424w, https://substackcdn.com/image/fetch/$s_!qtDq!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F298fd71b-38b2-481f-9cf2-ff6d32b7bf87_5525x1283.png 848w, https://substackcdn.com/image/fetch/$s_!qtDq!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F298fd71b-38b2-481f-9cf2-ff6d32b7bf87_5525x1283.png 1272w, https://substackcdn.com/image/fetch/$s_!qtDq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F298fd71b-38b2-481f-9cf2-ff6d32b7bf87_5525x1283.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><h1>Deployment &amp; Infrastructure Layer: Hardware</h1><h4>Where your workloads physically live, with the raw compute, memory, and networking to make large-scale AI possible.</h4><h4>NVIDIA H100 / A100 / L4</h4><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\begin{array}{|p{0.3\\linewidth}|p{0.64\\linewidth}|}\n\\hline\n\\textbf{Hardware} &amp; \\textbf{Computational Resources} \\\\ \\hline\n\\textnormal{NVIDIA DGX Systems} &amp; \\textnormal{8x H100 or A100 GPUs, up to 640GB HBM, NVLink/NVSwitch.} \\\\ \\hline\n\\end{array}\n&quot;,&quot;id&quot;:&quot;VUJSNVZAPG&quot;}" data-component-name="LatexBlockToDOM"></div><p><em><strong>What is it?</strong></em> Data center GPUs for AI training and inference.</p><blockquote><p><em><strong>Why it&#8217;s useful:</strong></em></p><ul><li><p><strong>H100</strong>: Transformer Engine for mixed precision, ideal for large LLMs.</p></li><li><p><strong>A100</strong>: Excellent for both training and inference, widely deployed.</p></li><li><p><strong>L4</strong>: Optimized for energy-efficient inference and media processing</p></li></ul><p><em><strong>Example</strong></em><strong>:</strong> Train and serve a multi-billion parameter LLM with low latency.</p></blockquote><h4>NVIDIA DGX Systems</h4><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\begin{array}{|p{0.3\\linewidth}|p{0.64\\linewidth}|}\n\\hline\n\\textbf{Hardware} &amp; \\textbf{Computational Resources} \\\\ \\hline\n\\textnormal{HGX \\&amp; MGX Platforms} &amp; \\textnormal{4&#8211;8x H100/A100 GPUs, NVLink/NVSwitch, PCIe Gen5.} \\\\ \\hline\n\\end{array}\n&quot;,&quot;id&quot;:&quot;FYJFIPMHVC&quot;}" data-component-name="LatexBlockToDOM"></div><p><em><strong>What is it?</strong></em> Turnkey AI supercomputers with NVLink/NVSwitch interconnects for multi-GPU scaling.</p><blockquote><p><em><strong>Why it&#8217;s useful:</strong></em></p><ul><li><p>NVLink/NVSwitch enables multi-node model parallelism without network bottlenecks</p></li></ul><p><em><strong>Example</strong></em><strong>:</strong> Run multi-node model parallel training for a 70B parameter chatbot.</p></blockquote><h4>HGX &amp; MGX Platforms</h4><p><em><strong>What is it?</strong></em> Modular reference architectures for OEM partners.</p><blockquote><p><br><em><strong>Example</strong></em><strong>:</strong> OEM builds an HGX H100 8-GPU server with liquid cooling for dense video analytics</p></blockquote><h4>Grace CPU &amp; Grace Hopper</h4><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\begin{array}{|p{0.3\\linewidth}|p{0.64\\linewidth}|}\n\\hline\n\\textbf{Hardware} &amp; \\textbf{Computational Resources} \\\\ \\hline\n\\textnormal{Grace CPU} &amp; \\textnormal{72 ARM cores, up to 1TB LPDDR5X, 512GB/s bandwidth.} \\\\ \\hline\n\\end{array}\n&quot;,&quot;id&quot;:&quot;DMIKDKHLZH&quot;}" data-component-name="LatexBlockToDOM"></div><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\begin{array}{|p{0.3\\linewidth}|p{0.64\\linewidth}|}\n\\hline\n\\textbf{Hardware} &amp; \\textbf{Computational Resources} \\\\ \\hline\n\\textnormal{Grace Hopper} &amp; \\textnormal{Grace CPU + Hopper GPU, 900GB/s CPU&#8211;GPU link, HBM.} \\\\ \\hline\n\\end{array}\n&quot;,&quot;id&quot;:&quot;XVZWFKEXWQ&quot;}" data-component-name="LatexBlockToDOM"></div><p><em><strong>What is it?</strong></em></p><ul><li><p><strong>Grace CPU</strong>: ARM-based CPU optimized for AI and HPC workloads.</p></li><li><p><strong>Grace Hopper</strong>: Superchip combining Grace CPU + Hopper GPU with high-bandwidth NVLink-C2C.</p></li></ul><blockquote><p><em><strong>Why it&#8217;s useful:</strong></em></p><ul><li><p>Massive memory bandwidth, ideal for retrieval-augmented LLMs and large graph neural networks.</p></li><li><p>Reduces latency in CPU&#8211;GPU data transfers.</p></li></ul><p><em><strong>Example</strong></em><strong>:</strong> Optimize large memory-bound inference without bottlenecks.</p></blockquote><h4>NVIDIA Jetson Platform</h4><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\begin{array}{|p{0.3\\linewidth}|p{0.64\\linewidth}|}\n\\hline\n\\textbf{Hardware} &amp; \\textbf{Computational Resources} \\\\ \\hline\n\\textnormal{Jetson AGX Orin} &amp; \\textnormal{Up to 275 TOPS, 64GB LPDDR5.} \\\\ \\hline\n\\end{array}\n&quot;,&quot;id&quot;:&quot;XEDMJIUYPE&quot;}" data-component-name="LatexBlockToDOM"></div><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;\\begin{array}{|p{0.3\\linewidth}|p{0.64\\linewidth}|}\n\\hline\n\\textbf{Hardware} &amp; \\textbf{Computational Resources} \\\\ \\hline\n\\textnormal{Jetson Xavier NX} &amp; \\textnormal{21 TOPS, 8GB LPDDR4x.} \\\\ \\hline\n\\end{array}\n&quot;,&quot;id&quot;:&quot;GNEQVLQOZA&quot;}" data-component-name="LatexBlockToDOM"></div><p><em><strong>What is it?</strong></em> Edge AI devices for robotics, IoT, and embedded AI.</p><blockquote><p><em><strong>Why it&#8217;s useful:</strong></em></p><ul><li><p>Runs CUDA and TensorRT on small, low-power devices.</p></li><li><p>Great for latency-sensitive inference without relying on cloud connectivity.<br></p></li></ul><p><em><strong>Example</strong></em><strong>:</strong> Deploy an on-device LLM-powered voice assistant in a drone.</p></blockquote><p>NVIDIA&#8217;s hardware portfolio spans from large-scale AI supercomputers to compact edge devices, each designed with specific computational resources to target different AI deployment needs.</p>]]></content:encoded></item><item><title><![CDATA[NVIDIA BioNeMo for Generative AI & Protein Binder Design]]></title><description><![CDATA[Using NVIDIA's BioNeMo to generate protein binders]]></description><link>https://uptodata.substack.com/p/generative-ai-for-protein-binder</link><guid isPermaLink="false">https://uptodata.substack.com/p/generative-ai-for-protein-binder</guid><dc:creator><![CDATA[Kopal Garg]]></dc:creator><pubDate>Wed, 05 Feb 2025 06:38:24 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/42e1e669-f4f0-45f4-9748-5eac0aabf3ab_1120x712.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Designing therapeutic proteins that actually bind to target molecules in drug discovery presents a staggering challenge. Traditional workflows include lots of trial-and-error, iterating through thousands of candidates, with each synthesis and validation round taking months or even years!</p><p>Considering the average human protein is 430 amino acids long, the number of possible designs translates to 20^430 sequences, which is practically <em><strong>infinite</strong></em>. Navigating this search space through conventional methods or brute-force methods is highly impractical! So what should we do?</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!s63B!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb76a5e15-d6d9-402f-bbb7-a5242d912bfe_4452x2880.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset image2-full-screen"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!s63B!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb76a5e15-d6d9-402f-bbb7-a5242d912bfe_4452x2880.jpeg 424w, https://substackcdn.com/image/fetch/$s_!s63B!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb76a5e15-d6d9-402f-bbb7-a5242d912bfe_4452x2880.jpeg 848w, https://substackcdn.com/image/fetch/$s_!s63B!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb76a5e15-d6d9-402f-bbb7-a5242d912bfe_4452x2880.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!s63B!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb76a5e15-d6d9-402f-bbb7-a5242d912bfe_4452x2880.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!s63B!,w_5760,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb76a5e15-d6d9-402f-bbb7-a5242d912bfe_4452x2880.jpeg" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b76a5e15-d6d9-402f-bbb7-a5242d912bfe_4452x2880.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:false,&quot;imageSize&quot;:&quot;full&quot;,&quot;height&quot;:942,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1777583,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&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-fullscreen" alt="" srcset="https://substackcdn.com/image/fetch/$s_!s63B!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb76a5e15-d6d9-402f-bbb7-a5242d912bfe_4452x2880.jpeg 424w, https://substackcdn.com/image/fetch/$s_!s63B!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb76a5e15-d6d9-402f-bbb7-a5242d912bfe_4452x2880.jpeg 848w, https://substackcdn.com/image/fetch/$s_!s63B!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb76a5e15-d6d9-402f-bbb7-a5242d912bfe_4452x2880.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!s63B!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb76a5e15-d6d9-402f-bbb7-a5242d912bfe_4452x2880.jpeg 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><figcaption class="image-caption">AI-driven protein binder design pipeline</figcaption></figure></div><p>To overcome these limitations, we <em>need</em> tools that integrate generative AI models, and GPU-accelerated microservices to explore this vast design space, leading to stable and structurally constrained binders at a <em>much</em> faster pace. </p><p>In this post we will walk through an end-to-end AI pipeline, showing how we can use <strong>NVIDIA&#8217;s BioNeMo</strong> to generate novel protein binders, from initial target sequences to validated, stable complexes, all within a streamlined, GPU-enabled accelerated workflow. </p><h2>Target identification and protein structure prediction</h2><blockquote><h4><code>Provides a structural foundation for designing protein binders.</code></h4></blockquote><div id="datawrapper-iframe" class="datawrapper-wrap outer" data-attrs="{&quot;url&quot;:&quot;https://datawrapper.dwcdn.net/3VOJU/1/&quot;,&quot;thumbnail_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/130f0fe4-311a-4848-a51c-bbfff47ac7b7_1260x660.png&quot;,&quot;thumbnail_url_full&quot;:&quot;&quot;,&quot;height&quot;:225,&quot;title&quot;:&quot;| Created with Datawrapper&quot;,&quot;description&quot;:&quot;Create interactive, responsive &amp; beautiful charts &#8212; no code required.&quot;}" data-component-name="DatawrapperToDOM"><iframe id="iframe-datawrapper" class="datawrapper-iframe" src="https://datawrapper.dwcdn.net/3VOJU/1/" width="730" height="225" frameborder="0" scrolling="no"></iframe><script type="text/javascript">!function(){"use strict";window.addEventListener("message",(function(e){if(void 0!==e.data["datawrapper-height"]){var t=document.querySelectorAll("iframe");for(var a in e.data["datawrapper-height"])for(var r=0;r<t.length;r++){if(t[r].contentWindow===e.source)t[r].style.height=e.data["datawrapper-height"][a]+"px"}}}))}();</script></div><h5><em>How can NVIDIA accelerate this?</em></h5><ul><li><p>By incorporating AlphaFold2 into the BioNeMo framework, researchers can achieve up to a 5x speedup in protein structure prediction, making large-scale target identification feasible in days rather than months [<a href="https://nvidianews.nvidia.com/news/nvidia-opens-bionemo-to-scale-digital-biology-for-global-biopharma-and-scientific-industry?utm_source=chatgpt.com">Source</a>].</p></li></ul><h2>De novo protein binder design</h2><blockquote><h4><code>Enables custom-designed binders tailored to the target.</code></h4></blockquote><div id="datawrapper-iframe" class="datawrapper-wrap outer" data-attrs="{&quot;url&quot;:&quot;https://datawrapper.dwcdn.net/5ozZF/1/&quot;,&quot;thumbnail_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fe922069-fed3-4df8-9fa1-97dabb1f8aa7_1260x660.png&quot;,&quot;thumbnail_url_full&quot;:&quot;&quot;,&quot;height&quot;:163,&quot;title&quot;:&quot;| Created with Datawrapper&quot;,&quot;description&quot;:&quot;Create interactive, responsive &amp; beautiful charts &#8212; no code required.&quot;}" data-component-name="DatawrapperToDOM"><iframe id="iframe-datawrapper" class="datawrapper-iframe" src="https://datawrapper.dwcdn.net/5ozZF/1/" width="730" height="163" frameborder="0" scrolling="no"></iframe><script type="text/javascript">!function(){"use strict";window.addEventListener("message",(function(e){if(void 0!==e.data["datawrapper-height"]){var t=document.querySelectorAll("iframe");for(var a in e.data["datawrapper-height"])for(var r=0;r<t.length;r++){if(t[r].contentWindow===e.source)t[r].style.height=e.data["datawrapper-height"][a]+"px"}}}))}();</script></div><h5><em>How can NVIDIA accelerate this?</em></h5><ul><li><p>NVIDIA's integration of RFdiffusion within the BioNeMo framework accelerates inference by 1.9x, enabling rapid generation of protein backbones optimized for target binding [<a href="https://developer.nvidia.com/blog/accelerate-protein-engineering-with-the-nvidia-bionemo-blueprint-for-generative-protein-binder-design/?utm_source=chatgpt.com">Source</a>].</p></li><li><p>By leveraging ProteinMPNN, BioNeMo facilitates large-scale sequence generation at multi-GPU scale, drastically improving search space efficiency and enabling the design of amino acid sequences optimized for binding stability [<a href="https://developer.nvidia.com/blog/accelerate-protein-engineering-with-the-nvidia-bionemo-blueprint-for-generative-protein-binder-design/?utm_source=chatgpt.com">Source</a>].</p></li></ul><h3>Molecular docking and binding affinity prediction </h3><blockquote><h4><code>Helps identify the best candidates for experimental validation.</code></h4></blockquote><div id="datawrapper-iframe" class="datawrapper-wrap outer" data-attrs="{&quot;url&quot;:&quot;https://datawrapper.dwcdn.net/6rQtj/2/&quot;,&quot;thumbnail_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/74e52843-ae86-4c37-b846-ccf87a2c76d5_1260x660.png&quot;,&quot;thumbnail_url_full&quot;:&quot;&quot;,&quot;height&quot;:195,&quot;title&quot;:&quot;| Created with Datawrapper&quot;,&quot;description&quot;:&quot;Create interactive, responsive &amp; beautiful charts &#8212; no code required.&quot;}" data-component-name="DatawrapperToDOM"><iframe id="iframe-datawrapper" class="datawrapper-iframe" src="https://datawrapper.dwcdn.net/6rQtj/2/" width="730" height="195" frameborder="0" scrolling="no"></iframe><script type="text/javascript">!function(){"use strict";window.addEventListener("message",(function(e){if(void 0!==e.data["datawrapper-height"]){var t=document.querySelectorAll("iframe");for(var a in e.data["datawrapper-height"])for(var r=0;r<t.length;r++){if(t[r].contentWindow===e.source)t[r].style.height=e.data["datawrapper-height"][a]+"px"}}}))}();</script></div><h5><em>How can NVIDIA accelerate this?</em></h5><ul><li><p>Parallelized inference on NVIDIA GPUs allows for high-throughput screening of thousands of binders in a fraction of the time taken by traditional docking methods.</p></li><li><p>DiffDock 2.0 enables researchers to predict molecular orientations 6.2 times faster and with 16% greater accuracy [<a href="https://nvidianews.nvidia.com/news/nvidia-opens-bionemo-to-scale-digital-biology-for-global-biopharma-and-scientific-industry?utm_source=chatgpt.com">Source</a>].</p></li></ul><h2>Structural validation and stability optimization</h2><blockquote><h4><code>Ensures that the designed binder is thermodynamically stable. </code></h4></blockquote><div id="datawrapper-iframe" class="datawrapper-wrap outer" data-attrs="{&quot;url&quot;:&quot;https://datawrapper.dwcdn.net/cathY/1/&quot;,&quot;thumbnail_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c9c5a157-199b-4856-80d4-a691efa8f7dd_1260x660.png&quot;,&quot;thumbnail_url_full&quot;:&quot;&quot;,&quot;height&quot;:195,&quot;title&quot;:&quot;| Created with Datawrapper&quot;,&quot;description&quot;:&quot;Create interactive, responsive &amp; beautiful charts &#8212; no code required.&quot;}" data-component-name="DatawrapperToDOM"><iframe id="iframe-datawrapper" class="datawrapper-iframe" src="https://datawrapper.dwcdn.net/cathY/1/" width="730" height="195" frameborder="0" scrolling="no"></iframe><script type="text/javascript">!function(){"use strict";window.addEventListener("message",(function(e){if(void 0!==e.data["datawrapper-height"]){var t=document.querySelectorAll("iframe");for(var a in e.data["datawrapper-height"])for(var r=0;r<t.length;r++){if(t[r].contentWindow===e.source)t[r].style.height=e.data["datawrapper-height"][a]+"px"}}}))}();</script></div><h5><em>How can NVIDIA accelerate this?</em></h5><ul><li><p>By utilizing parallelized inference capabilities, BioNeMo accelerates computations related to protein stability, such as changes in thermodynamic stability (&#916;&#916;G) and melting temperature (&#916;Tm), facilitating rapid assessment of binder viability [<a href="https://developer.nvidia.com/blog/accelerate-protein-engineering-with-the-nvidia-bionemo-blueprint-for-generative-protein-binder-design/?utm_source=chatgpt.com">Source</a>]. ESM-1nv and ESM-2 are used for these calculations. </p></li></ul><h2>Functional testing and refinement</h2><blockquote><h4><code>Reduces the risk of immunogenicity and manufacturability failures.</code></h4></blockquote><div id="datawrapper-iframe" class="datawrapper-wrap outer" data-attrs="{&quot;url&quot;:&quot;https://datawrapper.dwcdn.net/APyHp/1/&quot;,&quot;thumbnail_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fb642a0c-fb50-4214-9baa-6b01080a5e57_1260x660.png&quot;,&quot;thumbnail_url_full&quot;:&quot;&quot;,&quot;height&quot;:179,&quot;title&quot;:&quot;| Created with Datawrapper&quot;,&quot;description&quot;:&quot;Create interactive, responsive &amp; beautiful charts &#8212; no code required.&quot;}" data-component-name="DatawrapperToDOM"><iframe id="iframe-datawrapper" class="datawrapper-iframe" src="https://datawrapper.dwcdn.net/APyHp/1/" width="730" height="179" frameborder="0" scrolling="no"></iframe><script type="text/javascript">!function(){"use strict";window.addEventListener("message",(function(e){if(void 0!==e.data["datawrapper-height"]){var t=document.querySelectorAll("iframe");for(var a in e.data["datawrapper-height"])for(var r=0;r<t.length;r++){if(t[r].contentWindow===e.source)t[r].style.height=e.data["datawrapper-height"][a]+"px"}}}))}();</script></div><h5><em>How can NVIDIA accelerate this?</em></h5><ul><li><p>NVIDIA's Clara Discovery platform provides a collection of frameworks, applications, and AI models enabling GPU-accelerated computational drug discovery, allowing researchers to rapidly refine binder designs through distributed computing [<a href="https://catalog.ngc.nvidia.com/orgs/nvidia/collections/claradiscovery?utm_source=chatgpt.com">Source</a>].</p></li><li><p>Tools like BioPhi and Efficient Evolution will come in handy for this step.</p></li></ul><h2>High throughput screening and experimental validation</h2><blockquote><h4><code>Confirms that AI-generated binders work in a biological environment.</code></h4></blockquote><div id="datawrapper-iframe" class="datawrapper-wrap outer" data-attrs="{&quot;url&quot;:&quot;https://datawrapper.dwcdn.net/hXy0A/1/&quot;,&quot;thumbnail_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/850a9ded-7a0f-446f-8043-456d3b7a88e1_1260x660.png&quot;,&quot;thumbnail_url_full&quot;:&quot;&quot;,&quot;height&quot;:179,&quot;title&quot;:&quot;| Created with Datawrapper&quot;,&quot;description&quot;:&quot;Create interactive, responsive &amp; beautiful charts &#8212; no code required.&quot;}" data-component-name="DatawrapperToDOM"><iframe id="iframe-datawrapper" class="datawrapper-iframe" src="https://datawrapper.dwcdn.net/hXy0A/1/" width="730" height="179" frameborder="0" scrolling="no"></iframe><script type="text/javascript">!function(){"use strict";window.addEventListener("message",(function(e){if(void 0!==e.data["datawrapper-height"]){var t=document.querySelectorAll("iframe");for(var a in e.data["datawrapper-height"])for(var r=0;r<t.length;r++){if(t[r].contentWindow===e.source)t[r].style.height=e.data["datawrapper-height"][a]+"px"}}}))}();</script></div><h5><em>How can NVIDIA accelerate this?</em></h5><ul><li><p>NVIDIA GPUs accelerate lead optimization and ranking.</p></li><li><p>BioNeMo enables real-time in silico predictions, reducing the need for costly wet-lab experiments and accelerating lead optimization and ranking [<a href="https://developer.nvidia.com/blog/build-generative-ai-pipelines-for-drug-discovery-with-bionemo-service/?utm_source=chatgpt.com">Source</a>].</p></li></ul><h2>Conclusion</h2><p>This pipeline captures the end-to-end NVIDIA BioNeMo workflow for generative protein binder design. The tools, processes, and NVIDIA contributions align with the latest AI-driven drug discovery approaches.</p><p>Let me know what you think! Connect with me on <a href="https://www.linkedin.com/in/gargkopal/">LinkedIn</a> for more such posts.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://uptodata.substack.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"></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><div class="captioned-button-wrap" data-attrs="{&quot;url&quot;:&quot;https://uptodata.substack.com/p/generative-ai-for-protein-binder?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;}" data-component-name="CaptionedButtonToDOM"><div class="preamble"><p class="cta-caption">Thanks for reading Up to Data! This post is public so feel free to share it.</p></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://uptodata.substack.com/p/generative-ai-for-protein-binder?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://uptodata.substack.com/p/generative-ai-for-protein-binder?utm_source=substack&utm_medium=email&utm_content=share&action=share"><span>Share</span></a></p></div><p></p>]]></content:encoded></item><item><title><![CDATA[Guided Generation for LLM Outputs]]></title><description><![CDATA[LLMs like GPT-4 and Gemini Pro are useful for generating and manipulating text.]]></description><link>https://uptodata.substack.com/p/guided-generation-for-llm-outputs</link><guid isPermaLink="false">https://uptodata.substack.com/p/guided-generation-for-llm-outputs</guid><dc:creator><![CDATA[Kopal Garg]]></dc:creator><pubDate>Wed, 05 Jun 2024 19:12:53 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/591450ab-bb42-44ca-b735-0dc044bb4cc2_631x519.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>LLMs like GPT-4 and Gemini Pro are useful for generating and manipulating text. But to harness their full potential, it's important to guide the generation process, such that the outputs adhere to specific formats or structures. </p><p>In this blog, we will explore the following techniques for guided generation with LLMs:</p><ol><li><p>use of regular expressions</p></li><li><p>JSON schemas</p></li><li><p>context-free grammars (CFGs)</p></li><li><p>templates</p></li><li><p>entities</p></li><li><p>structured data generation.</p></li></ol><h3><strong>0. Initialization</strong></h3><p>We will run this code using Vertex AI. First, let's initialize our environment and set up the Vertex AI client with the necessary configuration to ensure our outputs are both useful and safe:</p><pre><code>import pandas as pd
import vertexai
from vertexai.generative_models import GenerativeModel, Part, FinishReason
import vertexai.preview.generative_models as generative_models

# Authenticate
from google.colab import auth
auth.authenticate_user()

vertexai.init(project="project-name", location="us-central1")

model = GenerativeModel("gemini-1.0-pro-vision-001")

generation_config = {
    "max_output_tokens": 300,
    "temperature": 0.4,
    "top_p": 0.9,
}

safety_settings = {
    generative_models.HarmCategory.HARM_CATEGORY_HATE_SPEECH: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
    generative_models.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
    generative_models.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
    generative_models.HarmCategory.HARM_CATEGORY_HARASSMENT: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
}</code></pre><h4>1. Guided Generation with Regular Expressions</h4><p>Regular expressions (regex) are a powerful way to ensure that generated text matches a specific pattern. </p><p>E.g. Imagine you need a 6-digit number. By defining a regex pattern, you can validate the generated number, ensuring it&#8217;s exactly six digits with no extra spaces or characters. This method is great for maintaining strict control over simple, structured outputs like numeric codes or specific text formats.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Svb5!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c777cab-6b97-40da-b0a9-0f25d834b2b2_1152x476.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Svb5!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c777cab-6b97-40da-b0a9-0f25d834b2b2_1152x476.png 424w, https://substackcdn.com/image/fetch/$s_!Svb5!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c777cab-6b97-40da-b0a9-0f25d834b2b2_1152x476.png 848w, https://substackcdn.com/image/fetch/$s_!Svb5!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c777cab-6b97-40da-b0a9-0f25d834b2b2_1152x476.png 1272w, https://substackcdn.com/image/fetch/$s_!Svb5!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c777cab-6b97-40da-b0a9-0f25d834b2b2_1152x476.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Svb5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c777cab-6b97-40da-b0a9-0f25d834b2b2_1152x476.png" width="640" height="264.44444444444446" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3c777cab-6b97-40da-b0a9-0f25d834b2b2_1152x476.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:476,&quot;width&quot;:1152,&quot;resizeWidth&quot;:640,&quot;bytes&quot;:56281,&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_!Svb5!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c777cab-6b97-40da-b0a9-0f25d834b2b2_1152x476.png 424w, https://substackcdn.com/image/fetch/$s_!Svb5!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c777cab-6b97-40da-b0a9-0f25d834b2b2_1152x476.png 848w, https://substackcdn.com/image/fetch/$s_!Svb5!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c777cab-6b97-40da-b0a9-0f25d834b2b2_1152x476.png 1272w, https://substackcdn.com/image/fetch/$s_!Svb5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3c777cab-6b97-40da-b0a9-0f25d834b2b2_1152x476.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><figcaption class="image-caption">Figure 1. Guided Generation with Regular Expressions</figcaption></figure></div><pre><code>number_pattern = re.compile(r"^\d{6}$")
def validate_number(number_str):
    if number_pattern.match(number_str):
        return True
    else:
        print(number_str)
        print("Invalid 6-digit format. Re-prompting...")
        return False
    
    
def generate():
    vertexai.init(project="cart-ppt-llm", location="us-central1")
    model = GenerativeModel(
        "gemini-1.0-pro-vision-001",
    )
    while True:
        responses = model.generate_content(
            [text1],
            generation_config=generation_config,
            stream=True,
        )
    
        number = ""
        for response in responses:
            number += response.text.strip()
    
        if validate_number(number):
            return number

text1 = """Generate a valid 6-digit number, ensuring it contains exactly 6 digits with no spaces or other characters. It should be in the format XXXXXX."""

generated_number = generate()
print(generated_number)</code></pre><p>Output:</p><pre><code>123456</code></pre><h4>2. Guided Generation with JSON Schemas</h4><p>JSON schemas allow you to define the structure and data types of JSON objects. This is particularly useful when you need to generate structured data, such as user profiles, where each profile must include a name, age, and email. </p><p>By validating the generated JSON against a schema, you ensure that the output adheres to the expected structure and data types. This technique is useful for applications requiring precise and predictable data formats.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!_zcH!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb92ae7dc-cfc0-4457-8956-c6910e88a66d_2010x512.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!_zcH!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb92ae7dc-cfc0-4457-8956-c6910e88a66d_2010x512.png 424w, https://substackcdn.com/image/fetch/$s_!_zcH!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb92ae7dc-cfc0-4457-8956-c6910e88a66d_2010x512.png 848w, https://substackcdn.com/image/fetch/$s_!_zcH!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb92ae7dc-cfc0-4457-8956-c6910e88a66d_2010x512.png 1272w, https://substackcdn.com/image/fetch/$s_!_zcH!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb92ae7dc-cfc0-4457-8956-c6910e88a66d_2010x512.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!_zcH!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb92ae7dc-cfc0-4457-8956-c6910e88a66d_2010x512.png" width="1456" height="371" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b92ae7dc-cfc0-4457-8956-c6910e88a66d_2010x512.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:371,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:86298,&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_!_zcH!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb92ae7dc-cfc0-4457-8956-c6910e88a66d_2010x512.png 424w, https://substackcdn.com/image/fetch/$s_!_zcH!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb92ae7dc-cfc0-4457-8956-c6910e88a66d_2010x512.png 848w, https://substackcdn.com/image/fetch/$s_!_zcH!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb92ae7dc-cfc0-4457-8956-c6910e88a66d_2010x512.png 1272w, https://substackcdn.com/image/fetch/$s_!_zcH!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb92ae7dc-cfc0-4457-8956-c6910e88a66d_2010x512.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><figcaption class="image-caption">Figure 2. Guided Generation with JSON Schemas</figcaption></figure></div><pre><code>
schema = {
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "age": {"type": "integer", "minimum": 0},
        "email": {"type": "string", "format": "email"}
    },
    "required": ["name", "age", "email"]
}

prompt = """
Generate a JSON object representing a user profile. The JSON object should have the following structure:
{
    "name": "a random first and last name",
    "age": an age between 20 and 40,
    "email": "a unique email address"
}
"""

def validate_json(instance, schema):
    """ Validate if the generated JSON matches the schema """
    try:
        jsonschema.validate(instance=instance, schema=schema)

        return True
    except jsonschema.exceptions.ValidationError as err:
        print("Invalid JSON format. Error:", err)
        return False

def generate_json():
    responses = model.generate_content(
        [prompt],
        generation_config=generation_config,
        safety_settings=safety_settings,
        stream=True,
    )

    json_response = ""
    for response in responses:
        json_response += response.text.strip()

    if json_response.startswith("```json") and json_response.endswith("```"):
        json_response = json_response[7:-3].strip()

    try:
        json_object = json.loads(json_response)
        if validate_json(json_object, schema):
            return json_object
    except json.JSONDecodeError as e:
        print("Error decoding JSON:", e)
    return None

generated_profile = generate_json()
print(f"{json.dumps(generated_profile, indent=2) if generated_profile else 'None'}")
</code></pre><p>Output</p><pre><code>{
  "name": "John Smith",
  "age": 32,
  "email": "john.smith@example.com"
}</code></pre><h4>3. Guided Generation with Context-Free Grammars </h4><p>Context-Free Grammars (CFGs) allow us to define a set of production rules for generating structured sentences. CGFs are excellent for generating structured sentences or text that follows a specific set of grammatical rules. </p><p>E.g., you might want to generate sentences about people performing actions on objects. A CFG can define the structure of these sentences, ensuring they always follow a logical and grammatical pattern. This method is ideal for tasks requiring syntactically correct and varied sentences, such as automated storytelling or dialogue generation.</p><pre><code>grammar = CFG.fromstring("""
    S -&gt; NP VP
    NP -&gt; 'John' | 'Mary' | 'Alice' | 'Bob'
    VP -&gt; V Obj
    V -&gt; 'eats' | 'drinks' | 'sees' | 'likes'
    Obj -&gt; Det N
    Det -&gt; 'an' | 'a'
    N -&gt; 'apple' | 'banana' | 'water' | 'book'
""")

prompt = f"""
Generate a sentence based on the following context-free grammar:
{grammar}
Ensure there is a space between each word in the sentence.
"""

def generate_cfg_sentence():
    responses = model.generate_content(
        [prompt],
        generation_config=generation_config,
        safety_settings=safety_settings,
        stream=True,
    )

    response_text = ""
    for response in responses:
        response_text += response.text.strip()
    
    return response_text

generated_llm_sentence = generate_cfg_sentence()
print(f"{generated_llm_sentence}")</code></pre><p>Output:</p><pre><code>John eats an apple</code></pre><p>The following diagram represents the CFG used in the above example:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!PF20!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa13b7826-9ff5-49cf-91d8-b4780290f080_826x1210.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!PF20!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa13b7826-9ff5-49cf-91d8-b4780290f080_826x1210.png 424w, https://substackcdn.com/image/fetch/$s_!PF20!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa13b7826-9ff5-49cf-91d8-b4780290f080_826x1210.png 848w, https://substackcdn.com/image/fetch/$s_!PF20!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa13b7826-9ff5-49cf-91d8-b4780290f080_826x1210.png 1272w, https://substackcdn.com/image/fetch/$s_!PF20!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa13b7826-9ff5-49cf-91d8-b4780290f080_826x1210.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!PF20!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa13b7826-9ff5-49cf-91d8-b4780290f080_826x1210.png" width="276" height="404.3099273607748" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a13b7826-9ff5-49cf-91d8-b4780290f080_826x1210.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1210,&quot;width&quot;:826,&quot;resizeWidth&quot;:276,&quot;bytes&quot;:82593,&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_!PF20!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa13b7826-9ff5-49cf-91d8-b4780290f080_826x1210.png 424w, https://substackcdn.com/image/fetch/$s_!PF20!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa13b7826-9ff5-49cf-91d8-b4780290f080_826x1210.png 848w, https://substackcdn.com/image/fetch/$s_!PF20!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa13b7826-9ff5-49cf-91d8-b4780290f080_826x1210.png 1272w, https://substackcdn.com/image/fetch/$s_!PF20!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa13b7826-9ff5-49cf-91d8-b4780290f080_826x1210.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><figcaption class="image-caption">Figure 3. Guided Generation with Context-Free Grammars </figcaption></figure></div><ol><li><p><strong>S -&gt; NP VP</strong>:</p><ul><li><p>The start symbol <code>S</code> is expanded into a noun phrase (NP) and a verb phrase (VP).</p></li></ul></li><li><p><strong>NP</strong>:</p><ul><li><p><code>NP</code> can be any of 'John', 'Mary', 'Alice', or 'Bob'.</p></li></ul></li><li><p><strong>VP -&gt; V Obj</strong>:</p><ul><li><p>The verb phrase <code>VP</code> is expanded into a verb (V) and an object (Obj).</p></li></ul></li><li><p><strong>V</strong>:</p><ul><li><p><code>V</code> can be any of 'eats', 'drinks', 'sees', or 'likes'.</p></li></ul></li><li><p><strong>Obj -&gt; Det N</strong>:</p><ul><li><p>The object <code>Obj</code> is expanded into a determiner (Det) and a noun (N).</p></li></ul></li><li><p><strong>Det</strong>:</p><ul><li><p><code>Det</code> can be either 'an' or 'a'.</p></li></ul></li><li><p><strong>N</strong>:</p><ul><li><p><code>N</code> can be any of 'apple', 'banana', 'water', or 'book'.</p></li></ul></li></ol><p>In our CFG, the start symbol <code>S</code> is expanded into a noun phrase (NP) and a verb phrase (VP). The NP can be names like 'John', 'Mary', 'Alice', or 'Bob'. The VP is broken down into a verb (V) and an object (Obj). The verb could be actions like 'eats', 'drinks', 'sees', or 'likes'. The object is composed of a determiner (Det) and a noun (N), where determiners can be 'an' or 'a', and nouns can be 'apple', 'banana', 'water', or 'book'. This structured approach ensures that the generated sentences are both grammatically correct and varied.</p><h4>4. Template-based Generation:</h4><p>Template-based generation uses predefined templates to structure the generated text. </p><p>E.g., you can create a user profile using a template that specifies placeholders for the name, age, and email. This method ensures that the generated content follows a consistent format, which is particularly useful for applications like automated report generation or content templating where the format is fixed, but the content varies.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!03X2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ca75819-864a-455a-9954-1dee85bfda9f_2614x598.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!03X2!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ca75819-864a-455a-9954-1dee85bfda9f_2614x598.png 424w, https://substackcdn.com/image/fetch/$s_!03X2!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ca75819-864a-455a-9954-1dee85bfda9f_2614x598.png 848w, https://substackcdn.com/image/fetch/$s_!03X2!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ca75819-864a-455a-9954-1dee85bfda9f_2614x598.png 1272w, https://substackcdn.com/image/fetch/$s_!03X2!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ca75819-864a-455a-9954-1dee85bfda9f_2614x598.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!03X2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ca75819-864a-455a-9954-1dee85bfda9f_2614x598.png" width="658" height="150.4903846153846" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9ca75819-864a-455a-9954-1dee85bfda9f_2614x598.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:333,&quot;width&quot;:1456,&quot;resizeWidth&quot;:658,&quot;bytes&quot;:100567,&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_!03X2!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ca75819-864a-455a-9954-1dee85bfda9f_2614x598.png 424w, https://substackcdn.com/image/fetch/$s_!03X2!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ca75819-864a-455a-9954-1dee85bfda9f_2614x598.png 848w, https://substackcdn.com/image/fetch/$s_!03X2!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ca75819-864a-455a-9954-1dee85bfda9f_2614x598.png 1272w, https://substackcdn.com/image/fetch/$s_!03X2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9ca75819-864a-455a-9954-1dee85bfda9f_2614x598.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">Figure 4. Template-based Generation</figcaption></figure></div><pre><code>
prompt = """
Create a user profile using the following template:
Name: {{name}}
Age: {{age}}
Email: {{email}}

Ensure the name is a first name and a last name, the age is a number between 20 and 40, and the email is in the format name@example.com.
"""

def generate_template_based_profile():
    responses = model.generate_content(
        [prompt],
        generation_config=generation_config,
        safety_settings=safety_settings,
        stream=True,
    )

    response_text = ""
    for response in responses:
        response_text += response.text.strip()
    
    return response_text

generated_profile = generate_template_based_profile()
print(f"{generated_profile}")
</code></pre><p>Output:</p><pre><code>Name:John Smith
Age: 35
Email: john.smith@example.com</code></pre><h4>5. Entity-based Generation</h4><p>Entity-based generation is about including specific entities in the generated text. </p><p>E.g., if you want to generate a paragraph about France, you can specify entities such as the capital (Paris), a famous food (croissant), and the official language (French). This technique ensures that the generated text is relevant and includes the necessary information about the entities, making it ideal for tasks like generating descriptive content or tailored information based on specific data points.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!kIBN!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6a9bf2ad-ba85-4991-9ef1-b1aefe52f215_1648x1300.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!kIBN!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6a9bf2ad-ba85-4991-9ef1-b1aefe52f215_1648x1300.png 424w, https://substackcdn.com/image/fetch/$s_!kIBN!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6a9bf2ad-ba85-4991-9ef1-b1aefe52f215_1648x1300.png 848w, https://substackcdn.com/image/fetch/$s_!kIBN!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6a9bf2ad-ba85-4991-9ef1-b1aefe52f215_1648x1300.png 1272w, https://substackcdn.com/image/fetch/$s_!kIBN!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6a9bf2ad-ba85-4991-9ef1-b1aefe52f215_1648x1300.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!kIBN!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6a9bf2ad-ba85-4991-9ef1-b1aefe52f215_1648x1300.png" width="576" height="454.54945054945057" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6a9bf2ad-ba85-4991-9ef1-b1aefe52f215_1648x1300.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1149,&quot;width&quot;:1456,&quot;resizeWidth&quot;:576,&quot;bytes&quot;:217944,&quot;alt&quot;:&quot;&quot;,&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="" title="" srcset="https://substackcdn.com/image/fetch/$s_!kIBN!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6a9bf2ad-ba85-4991-9ef1-b1aefe52f215_1648x1300.png 424w, https://substackcdn.com/image/fetch/$s_!kIBN!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6a9bf2ad-ba85-4991-9ef1-b1aefe52f215_1648x1300.png 848w, https://substackcdn.com/image/fetch/$s_!kIBN!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6a9bf2ad-ba85-4991-9ef1-b1aefe52f215_1648x1300.png 1272w, https://substackcdn.com/image/fetch/$s_!kIBN!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6a9bf2ad-ba85-4991-9ef1-b1aefe52f215_1648x1300.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><figcaption class="image-caption">Figure 5. Entity-based Generation</figcaption></figure></div><pre><code>
entities = {
    "country": "France",
    "capital": "Paris",
    "famous_food": "croissant",
    "language": "French"
}

prompt = f"""
Generate a 3-line paragraph about {entities['country']} that includes the following entities:
1. The capital city, {entities['capital']}
2. A famous food, {entities['famous_food']}
3. The official language, {entities['language']}
"""

def generate_entity_based_paragraph():
    responses = model.generate_content(
        [prompt],
        generation_config=generation_config,
        safety_settings=safety_settings,
        stream=True,
    )

    response_text = ""
    for response in responses:
        response_text += response.text.strip() + " "
    
    return response_text.strip()

generated_paragraph = generate_entity_based_paragraph()
print(f"Generated Paragraph: {generated_paragraph}")
</code></pre><p>Output:</p><pre><code>Nestled in the heart of Europe, France boasts the captivating capital of Paris, renowned for its iconic Eiffel Tower and the Louvre Museum. Indulge in the delectable aroma of freshly baked croissants, a culinary staple that embodies the nation's rich gastronomic heritage. The official language, French, echoes through the streets, adding a touch of elegance and sophistication to the vibrant atmosphere.
</code></pre><h4>6. Structured Data Generation</h4><p>Structured data generation involves creating data in a tabular format, such as CSV, which can be easily converted into a DataFrame for analysis or processing. </p><p>E.g., you might generate a table with columns for Name, Age, Country, and Profession, and populate it with data for several rows. This approach is beneficial for generating datasets or structured information that needs to be processed further, ensuring consistency and ease of use in data-centric applications.</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Eow_!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8f81076-d1a7-40e6-a039-617fe90a5667_1840x598.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Eow_!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8f81076-d1a7-40e6-a039-617fe90a5667_1840x598.png 424w, https://substackcdn.com/image/fetch/$s_!Eow_!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8f81076-d1a7-40e6-a039-617fe90a5667_1840x598.png 848w, https://substackcdn.com/image/fetch/$s_!Eow_!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8f81076-d1a7-40e6-a039-617fe90a5667_1840x598.png 1272w, https://substackcdn.com/image/fetch/$s_!Eow_!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8f81076-d1a7-40e6-a039-617fe90a5667_1840x598.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Eow_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8f81076-d1a7-40e6-a039-617fe90a5667_1840x598.png" width="618" height="200.7651098901099" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a8f81076-d1a7-40e6-a039-617fe90a5667_1840x598.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:473,&quot;width&quot;:1456,&quot;resizeWidth&quot;:618,&quot;bytes&quot;:131614,&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_!Eow_!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8f81076-d1a7-40e6-a039-617fe90a5667_1840x598.png 424w, https://substackcdn.com/image/fetch/$s_!Eow_!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8f81076-d1a7-40e6-a039-617fe90a5667_1840x598.png 848w, https://substackcdn.com/image/fetch/$s_!Eow_!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8f81076-d1a7-40e6-a039-617fe90a5667_1840x598.png 1272w, https://substackcdn.com/image/fetch/$s_!Eow_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8f81076-d1a7-40e6-a039-617fe90a5667_1840x598.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a><figcaption class="image-caption">Figure 6. Structured Data Generation</figcaption></figure></div><pre><code>column_headers = ["Name", "Age", "Country", "Profession"]

prompt = f"""
Generate a table with the following columns: {', '.join(column_headers)}.
Provide data for 5 rows.
Ensure that the table is properly formatted as CSV without any line breaks within rows.
"""

def generate_dataframe():
    responses = model.generate_content(
        [prompt],
        generation_config=generation_config,
        safety_settings=safety_settings,
        stream=True,
    )

    response_text = ""
    for response in responses:
        response_text += response.text.strip()
    
    return response_text

generated_table = generate_dataframe()


df = pd.read_csv(io.StringIO(generated_table))
df</code></pre><p>Output:</p><pre><code>Name&#9;Age&#9;Country&#9;Profession
0&#9;John&#9;30&#9;USA&#9;Software Engineer
1&#9;Mary&#9;25&#9;Canada&#9;Doctor
2&#9;Bob&#9;40&#9;UK&#9;Teacher
3&#9;Alice&#9;28&#9;Australia&#9;Lawyer
4&#9;Tom&#9;35&#9;Germany&#9;Architect</code></pre><h4>Wrapping Up:</h4><p>Guided generation techniques are key to making sure LLM outputs are useful and well-structured. Using methods like regular expressions, JSON schemas, CFGs, templates, entities, and structured data generation can greatly improve the accuracy and reliability of LLM content. These techniques help ensure the generated text meets specific needs, making it easier to integrate LLMs into real-world applications.</p><p>Here is a link to a<a href="https://colab.research.google.com/drive/1iRgEgIgphVZQ3NcS6f_CD0_7-DKm0lsB?usp=sharing"> Jupyter Notebook</a> containing all the above code. The notebook is for demonstration purposes only. You will need a GCP account with credits in order to run it.</p><p>Thanks for reading! &#129309;</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://uptodata.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://uptodata.substack.com/subscribe?"><span>Subscribe now</span></a></p><div class="captioned-button-wrap" data-attrs="{&quot;url&quot;:&quot;https://uptodata.substack.com/p/guided-generation-for-llm-outputs?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;}" data-component-name="CaptionedButtonToDOM"><div class="preamble"><p class="cta-caption">Thank you for reading Up to Data. This post is public so feel free to share it.</p></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://uptodata.substack.com/p/guided-generation-for-llm-outputs?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://uptodata.substack.com/p/guided-generation-for-llm-outputs?utm_source=substack&utm_medium=email&utm_content=share&action=share"><span>Share</span></a></p></div><p></p><p></p><p></p><p></p><p></p><p></p><p></p><p></p>]]></content:encoded></item><item><title><![CDATA[TechBio Bytes I: FAbCon, a generative foundation model for de novo antibody sequence generation]]></title><description><![CDATA[In this series, I will be reviewing recent papers within the realm of AI and biomedicine/health.]]></description><link>https://uptodata.substack.com/p/techbio-bytes-fabcon-a-generative</link><guid isPermaLink="false">https://uptodata.substack.com/p/techbio-bytes-fabcon-a-generative</guid><dc:creator><![CDATA[Kopal Garg]]></dc:creator><pubDate>Tue, 04 Jun 2024 21:36:36 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!NRDw!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7d319b03-326d-4f5c-8c26-0ca0d7976ab1_1792x1024.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In this series, I will be reviewing recent papers within the realm of AI and biomedicine/health. Posts will include practical examples for open-source papers. </p><p>Paper: <a href="https://www.biorxiv.org/content/10.1101/2024.05.22.594943v1.full.pdf">https://www.biorxiv.org/content/10.1101/2024.05.22.594943v1.full.pdf</a></p><p>Model: <a href="https://huggingface.co/alchemab/fabcon-small">https://huggingface.co/alchemab/fabcon-small</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_!NRDw!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7d319b03-326d-4f5c-8c26-0ca0d7976ab1_1792x1024.webp" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!NRDw!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7d319b03-326d-4f5c-8c26-0ca0d7976ab1_1792x1024.webp 424w, https://substackcdn.com/image/fetch/$s_!NRDw!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7d319b03-326d-4f5c-8c26-0ca0d7976ab1_1792x1024.webp 848w, https://substackcdn.com/image/fetch/$s_!NRDw!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7d319b03-326d-4f5c-8c26-0ca0d7976ab1_1792x1024.webp 1272w, https://substackcdn.com/image/fetch/$s_!NRDw!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7d319b03-326d-4f5c-8c26-0ca0d7976ab1_1792x1024.webp 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!NRDw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7d319b03-326d-4f5c-8c26-0ca0d7976ab1_1792x1024.webp" width="1456" height="832" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7d319b03-326d-4f5c-8c26-0ca0d7976ab1_1792x1024.webp&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:832,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:519946,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/webp&quot;,&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_!NRDw!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7d319b03-326d-4f5c-8c26-0ca0d7976ab1_1792x1024.webp 424w, https://substackcdn.com/image/fetch/$s_!NRDw!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7d319b03-326d-4f5c-8c26-0ca0d7976ab1_1792x1024.webp 848w, https://substackcdn.com/image/fetch/$s_!NRDw!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7d319b03-326d-4f5c-8c26-0ca0d7976ab1_1792x1024.webp 1272w, https://substackcdn.com/image/fetch/$s_!NRDw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7d319b03-326d-4f5c-8c26-0ca0d7976ab1_1792x1024.webp 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><figcaption class="image-caption">Image Credits: DALL-E</figcaption></figure></div><h4><strong>Broader context:</strong></h4><p>Antibodies (Abs) have the potential to recognize + bind to a <em>vast</em> array of antigens. The human body can theoretically produce over 10^15 unique Abs to cover all possible antigens. Given this magnitude, discovering and optimizing Ab therapeutics is challenging. It requires screening billions of Ab variants to identify those with high binding affinity and specificity to the target antigen, while also possessing good biophysical properties for developability. </p><p>Recently, researchers have been leveraging LLMs for learning patterns from large Ab repertoires. FAbCon is a SOTA LLM model that builds on this foundation. It enables de novo design of Abs optimized for desirable properties. It can provide a rich pool of high-quality starting leads to fuel discovery pipelines.</p><h4><strong>What is FAbCon?</strong></h4><p>FAbCon is a large 2.4 billion parameter language model specifically designed and pre-trained on a massive corpus of Ab sequences to learn representations useful for Ab sequence understanding and generation tasks. </p><h5><strong>Technical details:</strong></h5><ul><li><p>Based on the transformer decoder architecture from the general <a href="https://huggingface.co/docs/transformers/main/en/model_doc/falcon#transformers.FalconForCausalLM">Falcon LLM</a></p></li><li><p>Pre-trained on a massive corpus of 823.7 million unpaired and 2.5 million paired Ab sequences using Causal Language Modeling (CLM)</p></li><li><p>CLM involves training to predict the next amino acid based on preceding residues</p></li><li><p>Three variants spanning 144M to 2.4B parameters: FAbCon-small, -medium, -large</p></li></ul><p>After pre-training on the diverse Ab data, FAbCon learns rich representations capturing patterns in Ab sequences. These representations can then be transferred to downstream tasks:</p><ol><li><p>Generating new Ab sequences optimized for developability properties (example shown below)</p></li><li><p>Predicting antigen binding by fine-tuning on labeled Ab-antigen datasets</p></li></ol><h5><strong>Key Capabilities:</strong></h5><ul><li><p>SOTA performance in predicting antigen binding for Abs against targets like HER2, SARS-CoV-2, IL-6</p></li><li><p>Can generate Ab sequences with computational developability profiles mimicking human repertoires</p></li><li><p>Scaling up model size consistently improves antigen binding prediction</p></li><li><p>Requires less training data compared to models without pre-training</p></li></ul><h4>Limitations and Future Work</h4><ul><li><p>As with any LLM, FAbCon may encode biases present in the pre-training data, which could impact the reliability of the generated Ab sequences.</p></li><li><p>The FAbCon model uses a gated architecture, which restricts the direct use of the model for certain tasks, such as full sequence generation</p></li><li><p>The publicly available versions of FAbCon are pre-trained only, and further fine-tuning on labeled datasets would require obtaining a commercial license from the authors</p></li></ul><h4><strong>Code + Use Cases:</strong></h4><p>The FAbCon models are available on the Hugging Face Hub. We can load and use them with the &#129303; Transformers library.</p><p>Since FAbCon is a gated model, there are some limitations on what can be done out-of-the-box with the publicly released versions. However, here are some potential use cases and examples:</p><h5>Generate Novel Ab Sequences: </h5><p>Here we will use pre-trained FAbCon to generate a novel Ab sequence with properties it learned during pre-training on the large Ab corpus.</p><pre><code>from transformers import PreTrainedTokenizerFast, FalconForCausalLM

tokenizer = PreTrainedTokenizerFast.from_pretrained("alchemab/fabcon-large")
model = FalconForCausalLM.from_pretrained("alchemab/fabcon-large")

input_ids = tokenizer("H", return_tensors='pt')['input_ids']
output_ids = model.generate(input_ids, max_new_tokens=256, top_k=50, temperature=0.95)
decoded_seq = tokenizer.batch_decode(output_ids)[0]

print(f"Generated sequence: {decoded_seq}")</code></pre><p>We provided a starting prompt (<code>H</code>) to the model, which represents the beginning of the Ab sequence. We then used <code>model.generate() </code>to generate a new Ab sequence of up to 256 tokens, with <code>top_p=0.95</code> param to enable diverse and high quality sequence generation. This will generate a paired Ab sequence (<code>output_ids</code>) and print out the decoded sequence (<code>decoded_seq</code>).</p><h5>Sequence Representation &amp; Embedding Extraction:</h5><p>We can also extract the embeddings or representations learned by FAbCon for downstream analysis like clustering, visualization, KNN searching, or transfer learning to Ab-specific tasks. </p><pre><code>from transformers import PreTrainedTokenizerFast, FalconModel

tokenizer = PreTrainedTokenizerFast.from_pretrained("alchemab/fabcon-large")
model = FalconModel.from_pretrained("alchemab/fabcon-large")

input_ids = tokenizer("HEVQLLESGGGLVQPGGSLRLSCATSGYTFT", return_tensors='pt')['input_ids']
attention_mask = tokenizer("HEVQLLESGGGLVQPGGSLRLSCATSGYTFT", return_tensors='pt')['attention_mask']

outputs = model(input_ids=input_ids, attention_mask=attention_mask)
sequence_embedding = outputs.last_hidden_state[:, 0, :]</code></pre><p>We started by loading the pre-trained FAbCon-small model and its tokenizer. We then encoded a sample Ab sequence into input IDs. The <code>sequence_embedding</code> variable now contains the high-dimensional representation of the input antibody sequence.</p><h5>Zero-Shot Humanness Prediction:</h5><p>The paper shows FAbCon's perplexity scores correlate strongly with the OASis humanness metric. So FAbCon could potentially be used zero-shot to predict the "humanness" of an Ab sequence. </p><pre><code>from transformers import PreTrainedTokenizerFast, FalconForSequenceClassification

tokenizer = PreTrainedTokenizerFast.from_pretrained("alchemab/fabcon-large")
model = FalconForSequenceClassification.from_pretrained("alchemab/fabcon-large")

input_ids = tokenizer("HEVQLLESGGGLVQPGGSLRLSCATSGYTFT", return_tensors='pt')['input_ids']
attention_mask = tokenizer("HEVQLLESGGGLVQPGGSLRLSCATSGYTFT", return_tensors='pt')['attention_mask']

output = model(input_ids=input_ids, attention_mask=attention_mask)
logits = output.logits
humanness_score = logits[:, 0].item()  </code></pre><p>The model's output contains the logits, which can be used to compute the "humanness" score directly.</p><p>For more advanced tasks like antigen binding prediction, FAbCon would need to be further fine-tuned on labeled data. That will require a commercial license according to the model card.</p><p>Thanks for reading!</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://uptodata.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://uptodata.substack.com/subscribe?"><span>Subscribe now</span></a></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://uptodata.substack.com/?utm_source=substack&amp;utm_medium=email&amp;utm_content=share&amp;action=share&quot;,&quot;text&quot;:&quot;Share Up to Data&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://uptodata.substack.com/?utm_source=substack&amp;utm_medium=email&amp;utm_content=share&amp;action=share"><span>Share Up to Data</span></a></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://uptodata.substack.com/p/techbio-bytes-fabcon-a-generative?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://uptodata.substack.com/p/techbio-bytes-fabcon-a-generative?utm_source=substack&utm_medium=email&utm_content=share&action=share"><span>Share</span></a></p><p></p>]]></content:encoded></item><item><title><![CDATA[Use Google Cloud Batch for Running WDLs]]></title><description><![CDATA[Google&#8217;s Cloud Life Sciences API is being deprecated.]]></description><link>https://uptodata.substack.com/p/use-google-cloud-batch-for-running</link><guid isPermaLink="false">https://uptodata.substack.com/p/use-google-cloud-batch-for-running</guid><dc:creator><![CDATA[Kopal Garg]]></dc:creator><pubDate>Thu, 22 Feb 2024 01:35:40 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!ECyf!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3ba47730-cfa9-4bb0-aee4-05db01a8897d_1060x704.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Google&#8217;s Cloud Life Sciences API is being deprecated. For those relying on it, Google Cloud Batch combined with Cromwell offers a powerful alternative for batch processing workflows.</p><p>Here&#8217;s a streamlined guide to get started with Google Cloud Batch using Cromwell:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ECyf!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3ba47730-cfa9-4bb0-aee4-05db01a8897d_1060x704.webp" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ECyf!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3ba47730-cfa9-4bb0-aee4-05db01a8897d_1060x704.webp 424w, https://substackcdn.com/image/fetch/$s_!ECyf!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3ba47730-cfa9-4bb0-aee4-05db01a8897d_1060x704.webp 848w, https://substackcdn.com/image/fetch/$s_!ECyf!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3ba47730-cfa9-4bb0-aee4-05db01a8897d_1060x704.webp 1272w, https://substackcdn.com/image/fetch/$s_!ECyf!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3ba47730-cfa9-4bb0-aee4-05db01a8897d_1060x704.webp 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ECyf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3ba47730-cfa9-4bb0-aee4-05db01a8897d_1060x704.webp" width="1060" height="704" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3ba47730-cfa9-4bb0-aee4-05db01a8897d_1060x704.webp&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:704,&quot;width&quot;:1060,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:29764,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/webp&quot;,&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_!ECyf!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3ba47730-cfa9-4bb0-aee4-05db01a8897d_1060x704.webp 424w, https://substackcdn.com/image/fetch/$s_!ECyf!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3ba47730-cfa9-4bb0-aee4-05db01a8897d_1060x704.webp 848w, https://substackcdn.com/image/fetch/$s_!ECyf!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3ba47730-cfa9-4bb0-aee4-05db01a8897d_1060x704.webp 1272w, https://substackcdn.com/image/fetch/$s_!ECyf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3ba47730-cfa9-4bb0-aee4-05db01a8897d_1060x704.webp 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><figcaption class="image-caption">Transition from Google&#8217;s Cloud Life Sciences API to Google Cloud Batch</figcaption></figure></div><h1><strong>Preparing Your Workflow</strong></h1><ul><li><p>Workflow Definition (WDL File): <code>hello.wdl</code> contains a simple task to greet an addressee.</p></li></ul><pre><code>task hello {
    
  String addressee  
  
  command {
    touch test.txt
    echo "Hello ${addressee}! Welcome to Cromwell" &gt; test.txt  
  }
  output {
    File out = "test.txt"
  }
  runtime {
    docker: "us-central1-docker.pkg.dev/example-project/example-org/example-image:7841b2e"
    memory: "1 GB"
    cpu: 1
  }
}
workflow wf_hello {
    String addressee
        call hello{
            input:
                addressee = addressee
    
            }
}</code></pre><ul><li><p>Inputs File: <code>hello.inputs</code> specifies the input for the workflow, such as the addressee's name.</p></li></ul><pre><code>{
  "wf_hello.hello.addressee": "Kopal"
}</code></pre><ul><li><p>Configuration File: <code>google.conf</code> configures Cromwell to use Google Batch, including specifying project details, bucket for outputs, and the Batch backend settings.</p></li></ul><pre><code>

backend {
  default = GCPBATCH

  providers {
    GCPBATCH {
      actor-factory = "cromwell.backend.google.batch.GcpBatchBackendLifecycleActorFactory"
      config {
        # Google project
        project = "example-project"

        # Base bucket for workflow executions
        root = "gs://org-batch-output"

        # Polling for completion backs-off gradually for slower-running jobs.
        # This is the maximum polling interval (in seconds):
        maximum-polling-interval = 600

        # Optional Dockerhub Credentials. Can be used to access private docker images.
        dockerhub {
          # account = ""
          # token = ""
        }

        # Optional configuration to use high security network (Virtual Private Cloud) for running jobs.
        # See https://cromwell.readthedocs.io/en/stable/backends/Google/ for more details.
        # virtual-private-cloud {
        #  network-label-key = "network-key"
        #  auth = "application-default"
        # }

        # Global pipeline timeout
        # Defaults to 7 days; max 30 days
        # batch-timeout = 7 days

        genomics {
          # A reference to an auth defined in the `google` stanza at the top.  This auth is used to create
          # Batch Jobs and manipulate auth JSONs.
          auth = "application-default"


          // alternative service account to use on the launched compute instance
          // <strong>NOTE:</strong> If combined with service account authorization, both that service account and this service account
          // must be able to read and write to the 'root' GCS path
          compute-service-account = "default"

          # Location to submit jobs to Batch and store job metadata.
          location = "us-central1"

          # Specifies the minimum file size for `gsutil cp` to use parallel composite uploads during delocalization.
          # Parallel composite uploads can result in a significant improvement in delocalization speed for large files
          # but may introduce complexities in downloading such files from GCS, please see
          # https://cloud.google.com/storage/docs/gsutil/commands/cp#parallel-composite-uploads for more information.
          #
          # If set to 0 parallel composite uploads are turned off. The default Cromwell configuration turns off
          # parallel composite uploads, this sample configuration turns it on for files of 150M or larger.
          parallel-composite-upload-threshold="150M"
        }

        filesystems {
          gcs {
            # A reference to a potentially different auth for manipulating files via engine functions.
            auth = "application-default"
            # Google project which will be billed for the requests
            project = "google-billing-project"

            caching {
              # When a cache hit is found, the following duplication strategy will be followed to use the cached outputs
              # Possible values: "copy", "reference". Defaults to "copy"
              # "copy": Copy the output files
              # "reference": DO NOT copy the output files but point to the original output files instead.
              #              Will still make sure than all the original output files exist and are accessible before
              #              going forward with the cache hit.
              duplication-strategy = "copy"
            }
          }
        }

        default-runtime-attributes {
          cpu: 1
          failOnStderr: false
          continueOnReturnCode: 0
          memory: "2048 MB"
          bootDiskSizeGb: 10
          # Allowed to be a String, or a list of Strings
          disks: "local-disk 10 SSD"
          noAddress: false
          preemptible: 0
          zones: ["us-central1-a"]
        }

      }
    }
  }
}</code></pre><h1><strong>Running Your Workflow with Cromwell</strong></h1><pre><code>java -Dconfig.file=google.conf -jar cromwell-86.jar run hello.wdl -i hello.inputs</code></pre><p>This command uses <a href="https://github.com/broadinstitute/cromwell/releases">Cromwell</a> to run your <code>hello.wdl</code> workflow, specifying <code>hello.inputs</code> as the input file and <code>google.conf</code> for configuration.</p><h1><strong>The Issue with the Latest Cromwell Release</strong></h1><p>However, when attempting to run workflows with the most recent version of Cromwell, users encountered an <a href="https://github.com/broadinstitute/cromwell/issues/7238">&#8220;invalid Docker spec&#8221; </a>error due to a change in how GCP Batch backend processed mount paths. This issue, detailed in a GitHub thread, prevented workflows from executing successfully.</p><h1><strong>Workaround and Solution</strong></h1><p>To circumvent this, the Cromwell team provided fixes in subsequent development versions, addressing the Docker spec issue. We found success by pulling the latest Cromwell Docker image, which includes these fixes:</p><p>Pull the Latest Cromwell Docker Image:</p><pre><code>docker pull broadinstitute/cromwell:latest</code></pre><p>Execute the Workflow:</p><pre><code>docker run -it --rm -v $(pwd)/batch:/batch --entrypoint /bin/bash broadinstitute/cromwell:latest -c "java -Dconfig.file=/batch/config/google.conf -jar /app/cromwell.jar run /batch/wdls/hello.wdl -i /batch/hello.inputs"</code></pre><p>Until the next Cromwell release, this approach ensures smooth workflow execution on Google Cloud Batch, leveraging the latest developments and community support.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://uptodata.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://uptodata.substack.com/subscribe?"><span>Subscribe now</span></a></p>]]></content:encoded></item><item><title><![CDATA[Data Sync Between Google BigQuery and Google Sheet]]></title><description><![CDATA[As a data engineer, one of the frequent tasks you might encounter is syncing data from a Google Sheet to a Google BigQuery table.]]></description><link>https://uptodata.substack.com/p/data-sync-between-google-bigquery</link><guid isPermaLink="false">https://uptodata.substack.com/p/data-sync-between-google-bigquery</guid><dc:creator><![CDATA[Kopal Garg]]></dc:creator><pubDate>Mon, 05 Feb 2024 00:40:51 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/d6f07ae2-0698-41b5-b349-b3cd1e1c9056_1492x920.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>As a data engineer, one of the frequent tasks you might encounter is syncing data from a Google Sheet to a Google BigQuery table.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!a1PA!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19b90922-dfc3-40d6-9854-cbbc013e135f_1058x394.webp" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!a1PA!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19b90922-dfc3-40d6-9854-cbbc013e135f_1058x394.webp 424w, https://substackcdn.com/image/fetch/$s_!a1PA!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19b90922-dfc3-40d6-9854-cbbc013e135f_1058x394.webp 848w, https://substackcdn.com/image/fetch/$s_!a1PA!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19b90922-dfc3-40d6-9854-cbbc013e135f_1058x394.webp 1272w, https://substackcdn.com/image/fetch/$s_!a1PA!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19b90922-dfc3-40d6-9854-cbbc013e135f_1058x394.webp 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!a1PA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19b90922-dfc3-40d6-9854-cbbc013e135f_1058x394.webp" width="1058" height="394" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/19b90922-dfc3-40d6-9854-cbbc013e135f_1058x394.webp&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:394,&quot;width&quot;:1058,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:10296,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/webp&quot;,&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_!a1PA!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19b90922-dfc3-40d6-9854-cbbc013e135f_1058x394.webp 424w, https://substackcdn.com/image/fetch/$s_!a1PA!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19b90922-dfc3-40d6-9854-cbbc013e135f_1058x394.webp 848w, https://substackcdn.com/image/fetch/$s_!a1PA!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19b90922-dfc3-40d6-9854-cbbc013e135f_1058x394.webp 1272w, https://substackcdn.com/image/fetch/$s_!a1PA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F19b90922-dfc3-40d6-9854-cbbc013e135f_1058x394.webp 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><figcaption class="image-caption">Sheet to BigQuery Updates</figcaption></figure></div><p>This process, while seemingly straightforward, introduces a set of challenges that can complicate maintaining data integrity and consistency</p><ul><li><p><strong>Append-Only Nature of BigQuery:</strong> BigQuery is designed primarily for appending data, not updating or deleting specific rows.</p></li><li><p><strong>Deletions Aren&#8217;t Synced: </strong>Similarly, if you remove a row from your spreadsheet, this deletion won&#8217;t propagate to BigQuery.</p></li><li><p><strong>Schema Changes Are Restricted: </strong>Modifying the structure of your Google Sheet presents another layer of complexity. While BigQuery allows for some schema modifications, these are limited.</p></li></ul><p>In this blog, we will explore an App Script-based strategy that addresses these challenges head-on.</p><p>Our approach involves three main steps:</p><h1><strong>Download Sheet to a CSV</strong></h1><p>The first step involves exporting the Google Sheet data to a CSV format. Here&#8217;s how:</p><pre><code>function exportSheetToCsv(sheetId, sheetName) {
  var url = `https://docs.google.com/spreadsheets/d/${sheetId}/export?format=csv&amp;name=${encodeURIComponent(sheetName)}`;
  const options = { headers: { 'Authorization': 'Bearer ' + ScriptApp.getOAuthToken() } };
  const response = UrlFetchApp.fetch(url, options);
  const blob = response.getBlob();
  return blob;
}
</code></pre><p>This function fetches the specified Google Sheet as a CSV, ready for upload to BigQuery.</p><h1><strong>Delete the Table from BigQuery</strong></h1><p>Remove the existing BigQuery table to address the append-only and deletion-sync issues.</p><p>To ensure our BigQuery dataset reflects the latest data structure and content from Google Sheets, we&#8217;ll delete the existing table:</p><pre><code>function deleteBigQueryTable(projectId, datasetId, tableId) {
  try {
    BigQuery.Tables.remove(projectId, datasetId, tableId);
    console.log('Table deleted successfully.');
  } catch (e) {
    console.log(`Error deleting table: ${e.message}`);
  }
}</code></pre><h1><strong>Upload the CSV to BigQuery as a New Table</strong></h1><p>Create a fresh table in BigQuery with the same name, ensuring it reflects the current state of the Google Sheet.</p><p>With the table deleted, we upload the new data:</p><pre><code>function uploadCsvToBigQuery(blob, projectId, datasetId, tableId) {
  const file = DriveApp.createFile(blob);
  const data = file.getBlob().setContentType('application/octet-stream');
  const job = {
    configuration: {
      load: {
        destinationTable: { projectId, datasetId, tableId },
        sourceFormat: 'CSV',
        autodetect: true,
        skipLeadingRows: 1
      }
    }
  };
  BigQuery.Jobs.insert(job, projectId, data);
  console.log('CSV uploaded and table created in BigQuery.');
}</code></pre><h1><strong>Send an Update Notification</strong></h1><p>Optionally, send a notification (e.g., via Slack) to inform stakeholders that the data sync has been completed.</p><pre><code>function sendSlackNotification(webhookUrl, message) {
  const payload = JSON.stringify({ text: message });
  const options = {
    method: 'post',
    contentType: 'application/json',
    payload: payload
  };
  UrlFetchApp.fetch(webhookUrl, options);
}</code></pre><h1><strong>Automating Data Sync with Time-Driven Triggers</strong></h1><p>To automate the data syncing process from Google Sheets to BigQuery, you can use Google Apps Script&#8217;s time-driven triggers. These triggers can run your data sync script at regular intervals &#8212; ranging from every minute to once a month.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!_5FJ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49148bc8-e6d8-46f4-a337-49c0deab9dda_1398x1618.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!_5FJ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49148bc8-e6d8-46f4-a337-49c0deab9dda_1398x1618.png 424w, https://substackcdn.com/image/fetch/$s_!_5FJ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49148bc8-e6d8-46f4-a337-49c0deab9dda_1398x1618.png 848w, https://substackcdn.com/image/fetch/$s_!_5FJ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49148bc8-e6d8-46f4-a337-49c0deab9dda_1398x1618.png 1272w, https://substackcdn.com/image/fetch/$s_!_5FJ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49148bc8-e6d8-46f4-a337-49c0deab9dda_1398x1618.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!_5FJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49148bc8-e6d8-46f4-a337-49c0deab9dda_1398x1618.png" width="1398" height="1618" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/49148bc8-e6d8-46f4-a337-49c0deab9dda_1398x1618.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1618,&quot;width&quot;:1398,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:161783,&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_!_5FJ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49148bc8-e6d8-46f4-a337-49c0deab9dda_1398x1618.png 424w, https://substackcdn.com/image/fetch/$s_!_5FJ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49148bc8-e6d8-46f4-a337-49c0deab9dda_1398x1618.png 848w, https://substackcdn.com/image/fetch/$s_!_5FJ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49148bc8-e6d8-46f4-a337-49c0deab9dda_1398x1618.png 1272w, https://substackcdn.com/image/fetch/$s_!_5FJ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F49148bc8-e6d8-46f4-a337-49c0deab9dda_1398x1618.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>Stitch it all together, and you get something like this:</p><pre><code>function loadCsv() {

  var url = 'https://docs.google.com/spreadsheets/d/' + 'sheet_ID' + '/export?format=csv&amp;name=' + encodeURIComponent('sheet_name');

  const options = {
    headers: {
      'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()
    }
  };

  const response = UrlFetchApp.fetch(url, options);

  const currentDate = new Date();

  const dateString = currentDate.toISOString().split('T')[0].replace(/-/g, '');

  const blob = response.getBlob().setName('sheet_' + dateString + '.csv');

  const exportedFile = DriveApp.createFile(blob); 

  const projectId = 'project_id';
  const datasetId = 'dataset_id';
  const csvFileId = exportedFile.getId();
  const tableId = 'table_id' 

  // Delete existing table

  try {
    BigQuery.Tables.remove(projectId, datasetId, tableId);
    console.log('Table deleted.');
  } catch (e) {
    console.log('Table deletion failed or table did not exist. Error: ' + e.message);
  }
  // Create new table with the same name

  var table = {
    tableReference: {
      projectId: projectId,
      datasetId: datasetId,
      tableId: tableId
    }
  };
  
 // Upload table to BigQuery
  table = BigQuery.Tables.insert(table, projectId, datasetId);
  console.log('Table created: %s', table.id);
  
  const file = DriveApp.getFileById(csvFileId);
  const data = file.getBlob().setContentType('application/octet-stream');
  const job = {
    configuration: {
      load: {
        destinationTable: {
          projectId: projectId,
          datasetId: datasetId,
          tableId: tableId
        },
        skipLeadingRows: 1,
        autodetect: true,
        sourceFormat: 'CSV'

      }
    }
  };
  const jobResult = BigQuery.Jobs.insert(job, projectId, data);
  console.log('Load job started. Status: ' + jobResult.status.state);

// Send a Slack
  sendSlackNotification(webhookUrl, message)
}</code></pre><p>This solution ensures that your BigQuery dataset remains up-to-date with your Google Sheets, including reflecting any updates, deletions, or schema changes.</p><p>Feel free to customize my script based on your specific requirements!</p>]]></content:encoded></item><item><title><![CDATA[Docs or it Didn't Happen]]></title><description><![CDATA[Swagger documentation for RESTful APIs with Python and Flask]]></description><link>https://uptodata.substack.com/p/docs-of-it-didnt-happen</link><guid isPermaLink="false">https://uptodata.substack.com/p/docs-of-it-didnt-happen</guid><dc:creator><![CDATA[Kopal Garg]]></dc:creator><pubDate>Wed, 29 Nov 2023 02:51:26 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33f3cb19-be9c-4121-94b9-a6f25b18eca4_1666x1568.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>So, you have your API working like a charm. But what if someone else wants to use it? That's where documentation comes in. This blog will guide you through the process of creating Swagger documentation to make RESTful APIs in Flask accessible to fellow developers. </p><p>We will create an API for a calculator that computes the combination and permutation for <code>n</code> items chosen from a set of <code>k</code>. Here are the two essential endpoints we'll start with:</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://uptodata.substack.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 Up to Data! 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>This endpoint will calculate the number of ways to choose <code>k</code> items from a set of <code>n</code> items without regard to the order, i.e. combination: </p><pre><code>GET /combinations?n=5&amp;k=3</code></pre><p>This endpoint will calculate the number of ways to arrange <code>k</code> items from a set of <code>n</code> items with regard to the order, i.e. permutation:</p><pre><code>GET /permutations?n=5&amp;k=3</code></pre><p>Intrigued? Let's dive into the code and build our endpoints.</p><h2><strong>Creating Endpoints:</strong></h2><p>We will start by defining a few helper functions for computing the combination and permutations. Keep in mind that <code>n</code> is the total # items to choose from, and <code>k</code> is the # items chosen. These functions also include input validation to ensure that <code>n</code> and <code>k</code> are non-negative integers, and <code>n</code> is greater than or equal to <code>k</code>.</p><pre><code>import math

def calculate_combinations_logic(n, k):
    if n &lt; 0 or k &lt; 0 or n &lt; k:
        raise ValueError("Invalid input")
    combinations = math.factorial(n) // (math.factorial(k) * math.factorial(n - k))
    
    return combinations

def calculate_permutations_logic(n, k):
    if n &lt; 0 or k &lt; 0 or n &lt; k:
        raise ValueError("Invalid input")
    permutations = math.factorial(n) // math.factorial(n - k)
    
    return permutations</code></pre><p>a. Combinations:</p><pre><code>from flask import Flask, request

app = Flask(__name__)

@app.route('/combinations', methods=['GET'])
def calculate_combinations():
    n = int(request.args.get('n'))
    k = int(request.args.get('k'))
    
    result = calculate_combinations_logic(n, k)
    
    return f"Combinations of {n} choose {k}: {result}"</code></pre><p>b. Permutations:</p><pre><code>@app.route('/permutations', methods=['GET'])
def calculate_permutations():
    n = int(request.args.get('n'))
    k = int(request.args.get('k'))
    
    result = calculate_permutations_logic(n, k)
    
    return f"Permutations of {n} choose {k}: {result}"
</code></pre><p>But wait, how do others know how to use them? That's where Swagger documentation comes into play.</p><h2><strong>Swagger Docs:</strong></h2><p>Now we will integrate Swagger into our API. Let&#8217;s begin by installing the flask-swagger package:</p><pre><code>pip install flask-swagger,flask_swagger_ui</code></pre><p>Also, let us download the latest release from the swagger-ui GitHub. Un-tar this, and move the <code>dist</code> directory to a separate directory named <code>swaggerui</code> and create another new directory named <code>static</code>. Move <code>swaggerui</code> to <code>static</code>.</p><p>Your folder structure should look something like this:</p><pre><code>(base) kopalgarg@kgarg swagger-documentation % ls -lah
total 9248
drwxr-xr-x@  5 kopalgarg  staff   160B Nov 28 18:17 .
drwxr-xr-x@ 94 kopalgarg  staff   2.9K Nov 28 18:10 ..
-rw-r--r--   1 kopalgarg  staff   2.5K Nov 28 18:32 api.py
drwxr-xr-x   3 kopalgarg  staff    96B Nov 28 18:24 static
-rw-r--r--   1 kopalgarg  staff   3.8M Nov 28 18:15 v5.10.3.tar.gz


(base) kopalgarg@kgarg swagger-documentation % ls -lah static/swaggerui 
total 20248
drwxr-xr-x  20 kopalgarg  staff   640B Nov 22 01:04 .
drwxr-xr-x   3 kopalgarg  staff    96B Nov 28 18:24 ..
-rw-r--r--   1 kopalgarg  staff   665B Nov 22 01:04 favicon-16x16.png
-rw-r--r--   1 kopalgarg  staff   628B Nov 22 01:04 favicon-32x32.png
-rw-r--r--   1 kopalgarg  staff   202B Nov 22 01:04 index.css
-rw-r--r--   1 kopalgarg  staff   834B Nov 28 18:24 index.html
-rw-r--r--   1 kopalgarg  staff   2.7K Nov 22 01:04 oauth2-redirect.html
-rw-r--r--   1 kopalgarg  staff   501B Nov 28 18:23 swagger-initializer.js
-rw-r--r--   1 kopalgarg  staff   1.3M Nov 22 01:04 swagger-ui-bundle.js
-rw-r--r--   1 kopalgarg  staff   1.8M Nov 22 01:04 swagger-ui-bundle.js.map
-rw-r--r--   1 kopalgarg  staff   450K Nov 22 01:04 swagger-ui-es-bundle-core.js</code></pre><p>Modify <code>static/index.html</code> with the correct paths:</p><pre><code>&lt;!-- HTML for static distribution bundle build --&gt;
&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
  &lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;title&gt;Swagger UI&lt;/title&gt;
    &lt;link rel="stylesheet" type="text/css" href="static/swaggerui/swagger-ui.css" /&gt;
    &lt;script src="static/swaggerui/swagger-ui-bundle.js" charset="UTF-8"&gt;&lt;/script&gt;
    &lt;script src="static/swaggerui/swagger-ui-standalone-preset.js" charset="UTF-8"&gt;&lt;/script&gt;
    &lt;script src="static/swaggerui/swagger-initializer.js" charset="UTF-8"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div id="swagger-ui"&gt;&lt;/div&gt;
    &lt;script src="static/swaggerui/swagger-ui-bundle.js" charset="UTF-8"&gt; &lt;/script&gt;
    &lt;script src="static/swaggerui/swagger-ui-standalone-preset.js" charset="UTF-8"&gt; &lt;/script&gt;
    &lt;script src="static/swaggerui/swagger-initializer.js" charset="UTF-8"&gt; &lt;/script&gt;
  &lt;/body&gt;
&lt;/html&gt;</code></pre><p>Modify <code>static/swagger-initializer.js</code> with the correct paths:</p><pre><code>window.onload = function() {
  //&lt;editor-fold desc="Changeable Configuration Block"&gt;

  // the following lines will be replaced by docker/configurator, when it runs in a docker-container
  window.ui = SwaggerUIBundle({
    url: "/spec",
    dom_id: '#swagger-ui',
    deepLinking: true,
    presets: [
      SwaggerUIBundle.presets.apis,
      SwaggerUIStandalonePreset
    ],
    plugins: [
      SwaggerUIBundle.plugins.DownloadUrl
    ],
    layout: "StandaloneLayout"
  });

  //&lt;/editor-fold&gt;
};
</code></pre><p>Once we have the set-up out of the way, we will modify our Flask API code to use <code>flask-swagger</code> for documentation:</p><pre><code>from flask import Flask, request, jsonify
from flask_swagger import swagger
import math

def calculate_combinations_logic(n, k):
    if n &lt; 0 or k &lt; 0 or n &lt; k:
        raise ValueError("Invalid input")
    combinations = math.factorial(n) // (math.factorial(k) * math.factorial(n - k))
    
    return combinations

def calculate_permutations_logic(n, k):
    if n &lt; 0 or k &lt; 0 or n &lt; k:
        raise ValueError("Invalid input")
    permutations = math.factorial(n) // math.factorial(n - k)
    
    return permutations

app = Flask(__name__)

@app.route("/spec")
def spec():
    swag = swagger(app)
    swag['info']['title'] = "Combination and Permutation"
    swag['info']['version'] = "1.0.0"
    return jsonify(swag)

@app.route('/api-docs')
def swagger_ui():
    return app.send_static_file('swaggerui/index.html') 

@app.route('/combinations', methods=['GET'])
def calculate_combinations():
    """
    Calculate the number of combinations (n choose k).

    This endpoint calculates the number of ways to choose k items from a set of n items without regard to the order (combinations).

    ---
    parameters:
      - name: n
        in: query
        type: integer
        required: true
        description: Total number of items to choose from.
      - name: k
        in: query
        type: integer
        required: true
        description: Number of items to choose.

    responses:
      200:
        description: The number of combinations.
    """
    n = int(request.args.get('n'))
    k = int(request.args.get('k'))
    
    result = calculate_combinations_logic(n, k)
    
    return f"Combinations of {n} choose {k}: {result}"

@app.route('/permutations', methods=['GET'])
def calculate_permutations():
    """
    Calculate the number of permutations (n P k).

    This endpoint calculates the number of ways to arrange k items from a set of n items with regard to the order (permutations).

    ---
    parameters:
      - name: n
        in: query
        type: integer
        required: true
        description: Total number of items to arrange from.
      - name: k
        in: query
        type: integer
        required: true
        description: Number of items to arrange.

    responses:
      200:
        description: The number of permutations.
    """
    n = int(request.args.get('n'))
    k = int(request.args.get('k'))
    
    result = calculate_permutations_logic(n, k)
    
    return f"Permutations of {n} choose {k}: {result}"

if __name__ == '__main__':
    app.run()</code></pre><h2><strong>Testing it out</strong></h2><p>Assuming you saved your API code in Python file named <code>api.py</code>, if you run <code>python3 api.py</code>, you should see something like this:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!PK8Z!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2a1bdb7-c62f-4cf8-94c4-ab416ec88756_1242x164.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!PK8Z!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2a1bdb7-c62f-4cf8-94c4-ab416ec88756_1242x164.png 424w, https://substackcdn.com/image/fetch/$s_!PK8Z!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2a1bdb7-c62f-4cf8-94c4-ab416ec88756_1242x164.png 848w, https://substackcdn.com/image/fetch/$s_!PK8Z!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2a1bdb7-c62f-4cf8-94c4-ab416ec88756_1242x164.png 1272w, https://substackcdn.com/image/fetch/$s_!PK8Z!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2a1bdb7-c62f-4cf8-94c4-ab416ec88756_1242x164.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!PK8Z!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2a1bdb7-c62f-4cf8-94c4-ab416ec88756_1242x164.png" width="1242" height="164" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f2a1bdb7-c62f-4cf8-94c4-ab416ec88756_1242x164.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:164,&quot;width&quot;:1242,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:97768,&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_!PK8Z!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2a1bdb7-c62f-4cf8-94c4-ab416ec88756_1242x164.png 424w, https://substackcdn.com/image/fetch/$s_!PK8Z!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2a1bdb7-c62f-4cf8-94c4-ab416ec88756_1242x164.png 848w, https://substackcdn.com/image/fetch/$s_!PK8Z!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2a1bdb7-c62f-4cf8-94c4-ab416ec88756_1242x164.png 1272w, https://substackcdn.com/image/fetch/$s_!PK8Z!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff2a1bdb7-c62f-4cf8-94c4-ab416ec88756_1242x164.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><p>If you open up <code>http://127.0.0.1:5000/</code>, you should see the raw JSON for the specs on <code>http://127.0.0.1:5000/spec</code>:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!3ndW!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6084753d-38fe-496e-aef0-5486038b54dc_1666x744.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!3ndW!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6084753d-38fe-496e-aef0-5486038b54dc_1666x744.png 424w, https://substackcdn.com/image/fetch/$s_!3ndW!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6084753d-38fe-496e-aef0-5486038b54dc_1666x744.png 848w, https://substackcdn.com/image/fetch/$s_!3ndW!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6084753d-38fe-496e-aef0-5486038b54dc_1666x744.png 1272w, https://substackcdn.com/image/fetch/$s_!3ndW!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6084753d-38fe-496e-aef0-5486038b54dc_1666x744.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!3ndW!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6084753d-38fe-496e-aef0-5486038b54dc_1666x744.png" width="1456" height="650" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6084753d-38fe-496e-aef0-5486038b54dc_1666x744.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:650,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:169434,&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_!3ndW!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6084753d-38fe-496e-aef0-5486038b54dc_1666x744.png 424w, https://substackcdn.com/image/fetch/$s_!3ndW!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6084753d-38fe-496e-aef0-5486038b54dc_1666x744.png 848w, https://substackcdn.com/image/fetch/$s_!3ndW!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6084753d-38fe-496e-aef0-5486038b54dc_1666x744.png 1272w, https://substackcdn.com/image/fetch/$s_!3ndW!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6084753d-38fe-496e-aef0-5486038b54dc_1666x744.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>and the Swagger documentation on <code>http://127.0.0.1:5000/api-docs</code>:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!IwRf!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33f3cb19-be9c-4121-94b9-a6f25b18eca4_1666x1568.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!IwRf!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33f3cb19-be9c-4121-94b9-a6f25b18eca4_1666x1568.png 424w, https://substackcdn.com/image/fetch/$s_!IwRf!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33f3cb19-be9c-4121-94b9-a6f25b18eca4_1666x1568.png 848w, https://substackcdn.com/image/fetch/$s_!IwRf!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33f3cb19-be9c-4121-94b9-a6f25b18eca4_1666x1568.png 1272w, https://substackcdn.com/image/fetch/$s_!IwRf!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33f3cb19-be9c-4121-94b9-a6f25b18eca4_1666x1568.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!IwRf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33f3cb19-be9c-4121-94b9-a6f25b18eca4_1666x1568.png" width="1456" height="1370" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/33f3cb19-be9c-4121-94b9-a6f25b18eca4_1666x1568.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1370,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:773907,&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_!IwRf!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33f3cb19-be9c-4121-94b9-a6f25b18eca4_1666x1568.png 424w, https://substackcdn.com/image/fetch/$s_!IwRf!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33f3cb19-be9c-4121-94b9-a6f25b18eca4_1666x1568.png 848w, https://substackcdn.com/image/fetch/$s_!IwRf!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33f3cb19-be9c-4121-94b9-a6f25b18eca4_1666x1568.png 1272w, https://substackcdn.com/image/fetch/$s_!IwRf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F33f3cb19-be9c-4121-94b9-a6f25b18eca4_1666x1568.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>Now, if you wanted to test the API, you could do so directly from the documentation Swagger page, like so:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!09ki!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d9d7b28-49e1-4183-bdba-dc854aa06032_1660x1562.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!09ki!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d9d7b28-49e1-4183-bdba-dc854aa06032_1660x1562.png 424w, https://substackcdn.com/image/fetch/$s_!09ki!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d9d7b28-49e1-4183-bdba-dc854aa06032_1660x1562.png 848w, https://substackcdn.com/image/fetch/$s_!09ki!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d9d7b28-49e1-4183-bdba-dc854aa06032_1660x1562.png 1272w, https://substackcdn.com/image/fetch/$s_!09ki!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d9d7b28-49e1-4183-bdba-dc854aa06032_1660x1562.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!09ki!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d9d7b28-49e1-4183-bdba-dc854aa06032_1660x1562.png" width="1456" height="1370" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/3d9d7b28-49e1-4183-bdba-dc854aa06032_1660x1562.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1370,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:750140,&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_!09ki!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d9d7b28-49e1-4183-bdba-dc854aa06032_1660x1562.png 424w, https://substackcdn.com/image/fetch/$s_!09ki!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d9d7b28-49e1-4183-bdba-dc854aa06032_1660x1562.png 848w, https://substackcdn.com/image/fetch/$s_!09ki!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d9d7b28-49e1-4183-bdba-dc854aa06032_1660x1562.png 1272w, https://substackcdn.com/image/fetch/$s_!09ki!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3d9d7b28-49e1-4183-bdba-dc854aa06032_1660x1562.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>That&#8217;s all for now! Let me know if you have any thoughts, and feel free to subscribe to my blog if you found this helpful. You can also find me on <a href="https://www.linkedin.com/in/gargkopal/">LinkedIn</a> and <a href="https://github.com/kopalgarg">GitHub</a>. </p><p></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://uptodata.substack.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 Up to Data! 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[Set Up a Neo4j Graph Database on a GCP VM ]]></title><description><![CDATA[Exploring Friendships and Coffee Shops with Neo4j on GCP]]></description><link>https://uptodata.substack.com/p/setting-up-neo4j-graph-database-on</link><guid isPermaLink="false">https://uptodata.substack.com/p/setting-up-neo4j-graph-database-on</guid><dc:creator><![CDATA[Kopal Garg]]></dc:creator><pubDate>Sat, 11 Nov 2023 04:55:51 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!rZpE!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f6a6616-e800-41d5-8b86-bb2596473061_1986x1022.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Imagine a group of friends, each with their favourite coffee shop. We're going to create a graph in Neo4j that represents each friend, their go-to coffee spots, and the friendships among them.</p><p>First, we'll create nodes for each friend and coffee shop.</p><pre><code><code>CREATE (Emma:Person {name: 'Emma', age: 24})
CREATE (Liam:Person {name: 'Liam', age: 26})
CREATE (Olivia:Person {name: 'Olivia', age: 25})

CREATE (JavaJunction:CoffeeShop {name: 'Java Junction'})
CREATE (BeanBrew:CoffeeShop {name: 'Bean Brew'})
CREATE (CaffeineCorner:CoffeeShop {name: 'Caffeine Corner'})</code></code></pre><p>Next, we establish relationships between friends and their favourite coffee shops, and also friendships. In this step, we're using <code>FAVORS</code> relationships to link each person to their favourite coffee shop and <code>FRIENDS_WITH</code> relationships to connect the friends. </p><p>For e.g., we create a relationship between &#8216;Emma&#8217; and &#8216;Java Junction&#8217;. If this relationship already exists, <code>MERGE</code> will not create a duplicate. Then we create a 'FRIENDS_WITH' relationship between Emma and Olivia.</p><pre><code><code>MATCH (e:Person {name: 'Emma'}), (jj:CoffeeShop {name: 'Java Junction'})
MERGE (e)-[:FAVORS]-&gt;(jj)
WITH e, jj
MATCH (l:Person {name: 'Liam'}), (bb:CoffeeShop {name: 'Bean Brew'})
MERGE (l)-[:FAVORS]-&gt;(bb)
WITH l, bb
MATCH (o:Person {name: 'Olivia'}), (cc:CoffeeShop {name: 'Caffeine Corner'})
MERGE (o)-[:FAVORS]-&gt;(cc)
WITH o, cc
MATCH (Emma:Person {name: 'Emma'}), (Olivia:Person {name: 'Olivia'})
MERGE (Emma)-[:FRIENDS_WITH]-&gt;(Olivia)
WITH Emma, Olivia
MATCH (Olivia:Person {name: 'Olivia'}), (Liam:Person {name: 'Liam'})
MERGE (Olivia)-[:FRIENDS_WITH]-&gt;(Liam)</code></code></pre><p>Let's run a query to find out who likes 'Bean Brew':</p><pre><code><code>MATCH (p:Person)-[:FAVORS]-&gt;(cs:CoffeeShop {name: 'Bean Brew'})
RETURN p.name</code></code></pre><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!rZpE!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f6a6616-e800-41d5-8b86-bb2596473061_1986x1022.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!rZpE!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f6a6616-e800-41d5-8b86-bb2596473061_1986x1022.png 424w, https://substackcdn.com/image/fetch/$s_!rZpE!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f6a6616-e800-41d5-8b86-bb2596473061_1986x1022.png 848w, https://substackcdn.com/image/fetch/$s_!rZpE!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f6a6616-e800-41d5-8b86-bb2596473061_1986x1022.png 1272w, https://substackcdn.com/image/fetch/$s_!rZpE!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f6a6616-e800-41d5-8b86-bb2596473061_1986x1022.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!rZpE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f6a6616-e800-41d5-8b86-bb2596473061_1986x1022.png" width="692" height="355.9807692307692" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/6f6a6616-e800-41d5-8b86-bb2596473061_1986x1022.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:749,&quot;width&quot;:1456,&quot;resizeWidth&quot;:692,&quot;bytes&quot;:660347,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&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="" title="" srcset="https://substackcdn.com/image/fetch/$s_!rZpE!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f6a6616-e800-41d5-8b86-bb2596473061_1986x1022.png 424w, https://substackcdn.com/image/fetch/$s_!rZpE!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f6a6616-e800-41d5-8b86-bb2596473061_1986x1022.png 848w, https://substackcdn.com/image/fetch/$s_!rZpE!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f6a6616-e800-41d5-8b86-bb2596473061_1986x1022.png 1272w, https://substackcdn.com/image/fetch/$s_!rZpE!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F6f6a6616-e800-41d5-8b86-bb2596473061_1986x1022.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><figcaption class="image-caption">Who likes &#8216;Bean Brew&#8217;?</figcaption></figure></div><p>This query will return the name of the person who favors 'Bean Brew', which in our graph is Liam.</p><h4><strong>So, what exactly is Neo4j?</strong></h4><p>Now that we've seen Neo4j in action, let's backtrack a bit and talk about what Neo4j really is. It is a graph database management system that is built to deal with complex data relationships way better than your standard relational databases. This blog post will guide you through setting up Neo4j on GCP.</p><h4><strong>Hosting on GCP</strong>:</h4><p>So why should you choose GCP for hosting Neo4j?</p><ul><li><p><strong>Scalability</strong>: GCP lets you easily adjust resources to handle different database loads, ensuring efficient performance even as your data grows.</p></li><li><p><strong>Security</strong>: With GCP's security measures, your data remains protected, which is crucial for sensitive information.</p></li><li><p><strong>Cost-Effectiveness</strong>: GCP's pay-as-you-go pricing model makes it a budget-friendly option, especially for projects with fluctuating resource needs.</p></li></ul><h4><strong>Setting up a VM on GCP:</strong></h4><p>We start by configuring the GCP project and creating a new VM instance using the <code>gcloud</code> command-line tool.</p><pre><code>gcloud config set project [ID]

gcloud compute instances create neo4j-vm \
    --machine-type=n1-standard-1 \
    --image-family=debian-10 \
    --image-project=debian-cloud \
    --zone=us-central1-a</code></pre><h4>Set up firewall rules:</h4><p>Next, we have to make sure the VM can receive traffic on the ports used by Neo4j. The default is 7474 for HTTP and 7687 for Bolt protocol.</p><pre><code>gcloud compute firewall-rules create default-allow-http \
    --allow tcp:7474 \
    --target-tags=http-server \
    --source-ranges=0.0.0.0/0

gcloud compute firewall-rules create default-allow-bolt \
    --allow tcp:7687 \
    --target-tags=bolt-server \
    --source-ranges=0.0.0.0/0</code></pre><p>The above command initiates a SSH connection to <code>neo4j-vm</code>. </p><ul><li><p><code>-L 7474:localhost:7474</code>: This forwards port <code>7474</code> on your local machine to port <code>7474</code> on the virtual machine. This is often used for accessing web applications running on the virtual machine (like Jupyter notebooks) from your local browser.</p></li><li><p><code>-L 7687:localhost:7687</code>: Similarly, this forwards port <code>7687</code> on your local machine to port <code>7687</code> on the virtual machine. Port <code>7687</code> is commonly used by Neo4j, a graph database, for its Bolt protocol, which is a binary protocol for database access.</p></li></ul><h4>Download Neo4j:</h4><p>Now, download and install Neo4j. We'll use the latest stable version:</p><pre><code>wget -O - https://debian.neo4j.com/neotechnology.gpg.key | sudo apt-key add -

echo 'deb https://debian.neo4j.com stable 4.4' | sudo tee -a /etc/apt/sources.list.d/neo4j.list

sudo apt-get update

sudo apt-get install neo4j=1:4.4.12</code></pre><p>Start the Neo4j service:</p><pre><code>cd /var/lib/neo4j
./bin/neo4j start</code></pre><h4>Accessing Neo4j on the VM:</h4><p>Once you have Neo4j installed and running on your VM, the next step is to securely access it. To connect to your VM and forward the necessary ports for Neo4j, use the following command:</p><pre><code>gcloud compute ssh --zone "us-west1-b" "neo4j-vm"  --project "ID" -- -L 7474:localhost:7474 -L 7687:localhost:7687</code></pre><p>After running the above command, your local machine is connected to the VM, and the ports for accessing Neo4j are forwarded. You can access the Neo4j browser interface at <code>http://localhost:7474</code>. By default, your username is <code>neo4j</code>. You can set an initial password using this command:</p><pre><code> /bin/neo4j-admin set-initial-password 1234</code></pre><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!TfV-!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79a59893-fae8-492b-822f-976c89d0c1d4_1976x1018.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!TfV-!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79a59893-fae8-492b-822f-976c89d0c1d4_1976x1018.png 424w, https://substackcdn.com/image/fetch/$s_!TfV-!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79a59893-fae8-492b-822f-976c89d0c1d4_1976x1018.png 848w, https://substackcdn.com/image/fetch/$s_!TfV-!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79a59893-fae8-492b-822f-976c89d0c1d4_1976x1018.png 1272w, https://substackcdn.com/image/fetch/$s_!TfV-!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79a59893-fae8-492b-822f-976c89d0c1d4_1976x1018.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!TfV-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79a59893-fae8-492b-822f-976c89d0c1d4_1976x1018.png" width="604" height="311.1263736263736" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/79a59893-fae8-492b-822f-976c89d0c1d4_1976x1018.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:750,&quot;width&quot;:1456,&quot;resizeWidth&quot;:604,&quot;bytes&quot;:133216,&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_!TfV-!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79a59893-fae8-492b-822f-976c89d0c1d4_1976x1018.png 424w, https://substackcdn.com/image/fetch/$s_!TfV-!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79a59893-fae8-492b-822f-976c89d0c1d4_1976x1018.png 848w, https://substackcdn.com/image/fetch/$s_!TfV-!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79a59893-fae8-492b-822f-976c89d0c1d4_1976x1018.png 1272w, https://substackcdn.com/image/fetch/$s_!TfV-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F79a59893-fae8-492b-822f-976c89d0c1d4_1976x1018.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>Follow the instructions to reset your password. Once you have done so, your window should look something like this:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!O24S!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b2aa414-2410-4124-b21c-84f1bd728958_2776x1802.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!O24S!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b2aa414-2410-4124-b21c-84f1bd728958_2776x1802.png 424w, https://substackcdn.com/image/fetch/$s_!O24S!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b2aa414-2410-4124-b21c-84f1bd728958_2776x1802.png 848w, https://substackcdn.com/image/fetch/$s_!O24S!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b2aa414-2410-4124-b21c-84f1bd728958_2776x1802.png 1272w, https://substackcdn.com/image/fetch/$s_!O24S!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b2aa414-2410-4124-b21c-84f1bd728958_2776x1802.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!O24S!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b2aa414-2410-4124-b21c-84f1bd728958_2776x1802.png" width="582" height="377.74038461538464" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1b2aa414-2410-4124-b21c-84f1bd728958_2776x1802.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:945,&quot;width&quot;:1456,&quot;resizeWidth&quot;:582,&quot;bytes&quot;:488004,&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_!O24S!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b2aa414-2410-4124-b21c-84f1bd728958_2776x1802.png 424w, https://substackcdn.com/image/fetch/$s_!O24S!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b2aa414-2410-4124-b21c-84f1bd728958_2776x1802.png 848w, https://substackcdn.com/image/fetch/$s_!O24S!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b2aa414-2410-4124-b21c-84f1bd728958_2776x1802.png 1272w, https://substackcdn.com/image/fetch/$s_!O24S!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1b2aa414-2410-4124-b21c-84f1bd728958_2776x1802.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><figcaption class="image-caption">Neo4j graph database on GCP</figcaption></figure></div><p>You now have a fully functional Neo4j graph database running on a GCP VM! </p><h4>Resources:</h4><ul><li><p><a href="https://neo4j.com/docs/operations-manual/current/cloud-deployments/neo4j-gcp/">Neo4J on GCP</a></p></li><li><p><a href="https://cloud.google.com/filestore/docs/configuring-firewall">Configure Firewalls</a></p></li></ul><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://uptodata.substack.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 Up to Data! 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[Automated Testing of GitHub Actions for Docker Image Deployment]]></title><description><![CDATA[Unit and Integration Testing Strategies for Robust CI/CD Pipelines]]></description><link>https://uptodata.substack.com/p/automated-testing-of-github-actions</link><guid isPermaLink="false">https://uptodata.substack.com/p/automated-testing-of-github-actions</guid><dc:creator><![CDATA[Kopal Garg]]></dc:creator><pubDate>Fri, 29 Sep 2023 00:46:07 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!FkmM!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63763632-fd78-4258-9e51-6cad2e491ea2_1586x680.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In any tech team, CI/CD pipelines are indispensable. They essentially automate the process of building, testing, and deploying application/software/services. Testing these pipelines is equally as important for ensuring that the code is robust and behaves as expected in a production environment. </p><p>One common task in CI/CD pipelines is building and pushing Docker images to container registries like Google Artifact Registry (GAR), Amazon Elastic Container Registry (ECR) or DockerHub. In this blog, we will explore how to test a GitHub Actions (GAs) pipeline for pushing an image to DockerHub. Specifically, we'll look at two types of testing: unit testing and integration testing.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!FkmM!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63763632-fd78-4258-9e51-6cad2e491ea2_1586x680.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!FkmM!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63763632-fd78-4258-9e51-6cad2e491ea2_1586x680.png 424w, https://substackcdn.com/image/fetch/$s_!FkmM!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63763632-fd78-4258-9e51-6cad2e491ea2_1586x680.png 848w, https://substackcdn.com/image/fetch/$s_!FkmM!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63763632-fd78-4258-9e51-6cad2e491ea2_1586x680.png 1272w, https://substackcdn.com/image/fetch/$s_!FkmM!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63763632-fd78-4258-9e51-6cad2e491ea2_1586x680.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!FkmM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63763632-fd78-4258-9e51-6cad2e491ea2_1586x680.png" width="1456" height="624" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/63763632-fd78-4258-9e51-6cad2e491ea2_1586x680.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:624,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:85507,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&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_!FkmM!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63763632-fd78-4258-9e51-6cad2e491ea2_1586x680.png 424w, https://substackcdn.com/image/fetch/$s_!FkmM!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63763632-fd78-4258-9e51-6cad2e491ea2_1586x680.png 848w, https://substackcdn.com/image/fetch/$s_!FkmM!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63763632-fd78-4258-9e51-6cad2e491ea2_1586x680.png 1272w, https://substackcdn.com/image/fetch/$s_!FkmM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F63763632-fd78-4258-9e51-6cad2e491ea2_1586x680.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><figcaption class="image-caption">Testing CI/CD Github Actions Pipelines</figcaption></figure></div><p>In this blog, you'll learn:</p><ol><li><p><strong>Unit Testing</strong> to validate individual components like Dockerfiles and scripts.</p></li><li><p><strong>Integration Testing</strong> using tools like <code>act</code> to ensure your GitHub Actions workflows function as expected.</p></li></ol><h3>1. Unit Testing the Docker Image </h3><p>Unit testing in the context of a Docker image involves running tests against small testable parts of the script in isolation from the rest of the code. This can help ensure that individual actions work as expected. So, without having to run the entire workflow, we can test the core logic that the action is performing. </p><blockquote><h4>Test the Dockerfile</h4></blockquote><p>Before we even get to the GA, we have to make sure the Dockerfile builds as expected. We can write a script that tries to build the Docker image and fails if the <code>docker build</code> command fails.</p><blockquote><h4>Test the script</h4></blockquote><p>If the GA is running a shell or Python script to push the Docker image, we can test that script in isolation. For example, if we have a Python function that constructs <code>docker push </code>command, we can write a unit test for that function.</p><blockquote><h4>Mock external calls</h4></blockquote><p>If the action makes calls to external services (like in our case, DockerHub), we can mock those calls. </p><p>For example, if we&#8217;re using Python&#8217;s subprocess module to execute the<code> docker push</code> command, we can mock <code>subprocess.run()</code> to test that it gets called with expected arguments. </p><ul><li><p><code>actions.py</code> contains the actual logic for pushing a Docker image.</p></li><li><p><code>test_actions.py</code> contains unit tests to verify that the Docker image can be built and pushed correctly.</p></li></ul><p>The tests are designed to catch any issues in the Docker build and push process, ensuring that the actual shell commands are being called with the correct arguments.</p><p><em>actions.py</em></p><pre><code>import subprocess

def construct_docker_push_command(image_name, tag):
    return f"docker push {image_name}:{tag}"

def function_that_calls_docker_push():
    try:
        subprocess.run(["docker", "push", "samtools:latest"], check=True)
    except subprocess.CalledProcessError as e:
        print(f"An error occurred while pushing the Docker image: {e}")
</code></pre><p>In <code>actions.py</code>, you have a function called <code>function_that_calls_docker_push()</code> that uses Python's <code>subprocess</code> module to run a shell command for pushing a Docker image.</p><p><em>test_actions.py</em></p><pre><code>import unittest
from unittest.mock import patch
from action import function_that_calls_docker_push, construct_docker_push_command

class TestDockerActions(unittest.TestCase):

    def test_construct_docker_push_command(self):
        self.assertEqual(construct_docker_push_command("samtools", "latest"), "docker push samtools:latest")

    @patch("action.subprocess.run")
    def test_docker_push(self, mock_run):
        function_that_calls_docker_push()
        mock_run.assert_called_with(["docker", "push", "samtools:latest"], check=True)
    
if __name__ == '__main__':
    unittest.main()
</code></pre><p>In <code>test_actions.py</code>, you have several unit tests:</p><ol><li><p><strong>test_construct_docker_push_command</strong>: This test checks if the function <code>construct_docker_push_command()</code> returns the correct Docker push command string.</p></li><li><p><strong>test_docker_push</strong>: This test mocks the <code>subprocess.run()</code> function and checks if <code>function_that_calls_docker_push()</code> calls it with the correct arguments.</p></li><li><p><code>@patch("subprocess.run")</code> is using Python's <code>unittest.mock</code> to replace <code>subprocess.run</code> with a mock object for the duration of the test.</p></li><li><p><code>mock_run.assert_called_with(["docker", "push", "samtools:latest"])</code> checks if the mock was called with the specified arguments.</p></li></ol><p>If you run <code>python3 -m unittest test_action.py</code>, you should see something like this, if all tests pass:</p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!m0vq!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F363797b6-fe86-4f5a-931f-8eefda8a69e3_1348x128.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!m0vq!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F363797b6-fe86-4f5a-931f-8eefda8a69e3_1348x128.png 424w, https://substackcdn.com/image/fetch/$s_!m0vq!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F363797b6-fe86-4f5a-931f-8eefda8a69e3_1348x128.png 848w, https://substackcdn.com/image/fetch/$s_!m0vq!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F363797b6-fe86-4f5a-931f-8eefda8a69e3_1348x128.png 1272w, https://substackcdn.com/image/fetch/$s_!m0vq!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F363797b6-fe86-4f5a-931f-8eefda8a69e3_1348x128.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!m0vq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F363797b6-fe86-4f5a-931f-8eefda8a69e3_1348x128.png" width="1348" height="128" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/363797b6-fe86-4f5a-931f-8eefda8a69e3_1348x128.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:128,&quot;width&quot;:1348,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:49930,&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_!m0vq!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F363797b6-fe86-4f5a-931f-8eefda8a69e3_1348x128.png 424w, https://substackcdn.com/image/fetch/$s_!m0vq!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F363797b6-fe86-4f5a-931f-8eefda8a69e3_1348x128.png 848w, https://substackcdn.com/image/fetch/$s_!m0vq!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F363797b6-fe86-4f5a-931f-8eefda8a69e3_1348x128.png 1272w, https://substackcdn.com/image/fetch/$s_!m0vq!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F363797b6-fe86-4f5a-931f-8eefda8a69e3_1348x128.png 1456w" sizes="100vw" loading="lazy"></picture><div></div></div></a></figure></div><h3>2. Integration Testing GitHub Actions </h3><p>Integration testing involves testing the interaction between multiple units of an application. They help ensure the action works as expected when it interacts with the GitHub environment or other services. </p><p>We will use <code>act</code>, which will let us run GA workflows locally, and help speed up the development and testing process. Make sure it&#8217;s installed and ready to run:</p><pre><code>brew install act

act</code></pre><p>It's worth noting that <code>act</code> doesn't perfectly emulate GitHub Actions. Some third-party actions may not work as expected, so always double-check your results.</p><p>Ok, let&#8217;s use <code>act</code> to run the entire workflow and ensure all steps are executed correctly. First we will define a few environment variables in a <code>.secrets</code> file (placed in your root directory). This is useful for setting secrets or other environment-specific variables that your GitHub Actions might need.</p><pre><code>DOCKERHUB_USERNAME=yourusername
DOCKERHUB_TOKEN=yourpassword</code></pre><p>Here's a sample GitHub Actions YAML configuration for a Docker build and push workflow:</p><pre><code>name: Docker Push

on:
  push:
    branches:
      - main

jobs:
  docker:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Build Docker image
      run: docker build -t samtools:latest .

    - name: Login to DockerHub
      uses: docker/login-action@v1
      with:
        username: ${{ secrets.DOCKERHUB_USERNAME }}
        password: ${{ secrets.DOCKERHUB_TOKEN }}
    - name: Tag image
      run: docker tag samtools:latest ${{ secrets.DOCKERHUB_USERNAME }}/samtools:latest
    - name: Push Docker image
      run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/samtools:latest

</code></pre><p>If everything runs smoothly, you should see something like this:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!vUvx!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F819067fc-ff17-40c8-9d96-ad60243d0e46_1722x1088.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!vUvx!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F819067fc-ff17-40c8-9d96-ad60243d0e46_1722x1088.png 424w, https://substackcdn.com/image/fetch/$s_!vUvx!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F819067fc-ff17-40c8-9d96-ad60243d0e46_1722x1088.png 848w, https://substackcdn.com/image/fetch/$s_!vUvx!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F819067fc-ff17-40c8-9d96-ad60243d0e46_1722x1088.png 1272w, https://substackcdn.com/image/fetch/$s_!vUvx!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F819067fc-ff17-40c8-9d96-ad60243d0e46_1722x1088.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!vUvx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F819067fc-ff17-40c8-9d96-ad60243d0e46_1722x1088.png" width="1456" height="920" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/819067fc-ff17-40c8-9d96-ad60243d0e46_1722x1088.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:920,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1039794,&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_!vUvx!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F819067fc-ff17-40c8-9d96-ad60243d0e46_1722x1088.png 424w, https://substackcdn.com/image/fetch/$s_!vUvx!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F819067fc-ff17-40c8-9d96-ad60243d0e46_1722x1088.png 848w, https://substackcdn.com/image/fetch/$s_!vUvx!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F819067fc-ff17-40c8-9d96-ad60243d0e46_1722x1088.png 1272w, https://substackcdn.com/image/fetch/$s_!vUvx!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F819067fc-ff17-40c8-9d96-ad60243d0e46_1722x1088.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>By running these integration tests, you can catch issues that unit tests might miss, such as problems that only appear when multiple steps are run together.</p><div><hr></div><p>That's all for now! If you want the full code from this blog, check out this <a href="https://github.com/kopalgarg/samtools_test">repo</a>.</p><p>Have you implemented similar testing strategies in your CI/CD pipelines? &#128064; If so, I would love to hear your thoughts on this!</p><p></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://uptodata.substack.com/p/automated-testing-of-github-actions/comments&quot;,&quot;text&quot;:&quot;Leave a comment&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://uptodata.substack.com/p/automated-testing-of-github-actions/comments"><span>Leave a comment</span></a></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://uptodata.substack.com/p/automated-testing-of-github-actions?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://uptodata.substack.com/p/automated-testing-of-github-actions?utm_source=substack&utm_medium=email&utm_content=share&action=share"><span>Share</span></a></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://uptodata.substack.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 Up to Data! 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[Iterative prompting with LLMs]]></title><description><![CDATA[Feedback loops for refining text generation]]></description><link>https://uptodata.substack.com/p/iterative-prompting-with-llms</link><guid isPermaLink="false">https://uptodata.substack.com/p/iterative-prompting-with-llms</guid><dc:creator><![CDATA[Kopal Garg]]></dc:creator><pubDate>Tue, 19 Sep 2023 02:05:30 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/26bfee35-c56e-459a-9c69-003fb4c3a6ab_2412x736.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>LLMs like GPT-4 have shown great capabilities in text generation, but ensuring that the generated text meets specific criteria is still a challenge. Iterative prompting is a technique that refines the model's output through a feedback loop. The concept of iterative prompting is not new, but its application in the context of LLMs is particularly exciting. </p><p>This is how it works:</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://uptodata.substack.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 Up to Data! 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><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!WVXS!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3ed2b6f-04b6-463f-a9a5-5a83bbf8805c.tif" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!WVXS!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3ed2b6f-04b6-463f-a9a5-5a83bbf8805c.tif 424w, https://substackcdn.com/image/fetch/$s_!WVXS!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3ed2b6f-04b6-463f-a9a5-5a83bbf8805c.tif 848w, https://substackcdn.com/image/fetch/$s_!WVXS!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3ed2b6f-04b6-463f-a9a5-5a83bbf8805c.tif 1272w, https://substackcdn.com/image/fetch/$s_!WVXS!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3ed2b6f-04b6-463f-a9a5-5a83bbf8805c.tif 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!WVXS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3ed2b6f-04b6-463f-a9a5-5a83bbf8805c.tif" width="1437" height="241" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e3ed2b6f-04b6-463f-a9a5-5a83bbf8805c.tif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:241,&quot;width&quot;:1437,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1388804,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/tiff&quot;,&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_!WVXS!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3ed2b6f-04b6-463f-a9a5-5a83bbf8805c.tif 424w, https://substackcdn.com/image/fetch/$s_!WVXS!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3ed2b6f-04b6-463f-a9a5-5a83bbf8805c.tif 848w, https://substackcdn.com/image/fetch/$s_!WVXS!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3ed2b6f-04b6-463f-a9a5-5a83bbf8805c.tif 1272w, https://substackcdn.com/image/fetch/$s_!WVXS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3ed2b6f-04b6-463f-a9a5-5a83bbf8805c.tif 1456w" sizes="100vw" fetchpriority="high"></picture><div></div></div></a><figcaption class="image-caption">Iterative prompting feedback loop</figcaption></figure></div><p>Traditional methods of text generation often involve a one-shot query to the model, which may or may not yield the desired output. Iterative prompting allows us to refine the model's output in a more controlled way. </p><p>This is useful when we have criteria that the generated text must meet, like semantic similarity to the original text and level of politeness.</p><p><em>Scenario:</em></p><p>Imagine you're a community manager who needs to moderate comments on a forum. Some comments may be rude but contain valid points that shouldn't be entirely dismissed. Using iterative prompting, you can rephrase these comments to retain their original meaning while making them more polite and respectful.</p><h4>Setting Up Your Environment</h4><p>First, let's install the necessary packages</p><pre><code>!pip install openai
!pip install langchain
!pip install sentence_transformers</code></pre><h4>Importing Libraries and Initializing the Model</h4><pre><code>import os
import openai
from langchain.llms import OpenAI
from sentence_transformers import SentenceTransformer, util
from transformers import AutoModelForSequenceClassification, AutoTokenizer
import torch

os.environ["OPENAI_API_KEY"] = 'your_openai_api_key_here'
llm = OpenAI(temperature=0.9)</code></pre><h4>Defining Helper Functions</h4><p>In our code, we used two metrics to evaluate the quality of the rephrased comments. </p><ol><li><p><strong>Semantic Similarity</strong>: We used the SentenceTransformer library to compute the cosine similarity between the original and rephrased comments. This ensures that the rephrased comment retains the original meaning.</p></li></ol><pre><code><code>def compute_semantic_similarity(original, rephrased):
    model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
    embedding1 = model.encode(original, convert_to_tensor=True)
    embedding2 = model.encode(rephrased, convert_to_tensor=True)
    similarity = util.pytorch_cos_sim(embedding1, embedding2)
    return similarity.item()</code></code></pre><ol><li><p><strong>Politeness</strong>: We used a pre-trained BERT model to classify the sentiment of the rephrased comment. The model returns a label ranging from very negative to very positive, which we use as a proxy for politeness.</p></li></ol><pre><code>def is_polite_transformer(text):
    model_name = "nlptown/bert-base-multilingual-uncased-sentiment"
    model = AutoModelForSequenceClassification.from_pretrained(model_name)
    tokenizer = AutoTokenizer.from_pretrained(model_name)
    inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
    with torch.no_grad():
        outputs = model(**inputs)
    _, predicted = torch.max(outputs.logits, 1)
    label = int(predicted.item())
    return label
</code></pre><p>Let&#8217;s do a side investigation into the politeness of various sentences. This is to better understand how the politeness metric behaves and to validate its effectiveness in different contexts.</p><p>The model assigns a label ranging from 0 (very negative) to 4 (very positive), which we interpret as a measure of politeness:</p><pre><code>rephrased_comment = "Your painting is excellent"
polite_label = is_polite_transformer(rephrased_comment)
print(f"Is the comment polite? {polite_label}")
# 4</code></pre><pre><code>rephrased_comment = "Your painting is good"
polite = is_polite_transformer(rephrased_comment)
print(f"Is the comment polite? {polite}")
# 3</code></pre><pre><code>rephrased_comment = "Your painting is not the best"
polite = is_polite_transformer(rephrased_comment)
print(f"Is the comment polite? {polite}")
# 2</code></pre><pre><code>rephrased_comment = "I'm not a fan of your painting."
polite = is_polite_transformer(rephrased_comment)
print(f"Is the comment polite? {polite}")
# 1</code></pre><pre><code>rephrased_comment = "Your painting absolutely sucks"
polite = is_polite_transformer(rephrased_comment)
print(f"Is the comment polite? {polite}")
# 0</code></pre><p>We tested a variety of sentences, ranging from overtly rude to extremely polite, to see how well the classifier performs. Here are some examples:</p><ul><li><p>"Your painting is excellent" received a score of 4, indicating it's very polite.</p></li><li><p>"Your painting is good" received a score of 3, still polite but less so than the previous example.</p></li><li><p>"Your painting is not the best" received a score of 2, which is neutral.</p></li><li><p>"I'm not a fan of your painting" received a score of 1, leaning towards negative.</p></li><li><p>"Your painting absolutely sucks" received a score of 0, which is very negative.</p></li></ul><p>This is a decent quantitative way to measure one of the key dimensions we are interested in: the politeness of the rephrased comments.</p><h4>The Iterative Prompting Loop</h4><p>Next we will loop through a list of rude comments and iteratively prompt the model to rephrase them:</p><pre><code>rude_comments = [
    "You're making it hard for me to take you seriously.",
    "This is the most ridiculous thing I've ever heard.",
    "I've never seen someone mess up this badly.",
    "You're not just wrong, you're stupidly wrong.",
    "Is this really the best you could come up with?",
    "I don't have the time or the crayons to explain this to you.",
    "You're not just a disappointment, you're a waste of time.",
    "I've met some dumb people, but you take the cake.",
    "You're so clueless it's almost cute."]</code></pre><pre><code># Initialize variables to store results
iterative_similarity_scores = []
iterative_politeness_scores = []
standard_similarity_scores = []
standard_politeness_scores = []


# Loop through each rude comment

for comment in rude_comments:
    # Iterative Prompting
    user_input = comment
    for i in range(max_iterations):  
        prompt_rephrase = f"Can you reword this comment to be more polite and kinder?: {user_input}"
        user_input_rephrased = llm(prompt_rephrase)
        
        similarity_prompt_check = f"Is the reworded comment semantically similar to the original comment? Original comment: {user_input}, Reworded comment: {user_input_rephrased}"
        politeness_prompt_check = f"Is the reworded comment more polite and kinder than the original comment? Original comment: {user_input}, Reworded comment: {user_input_rephrased}"

        similarity_verification_result = llm(similarity_prompt_check)
        politeness_verification_result = llm(politeness_prompt_check)
  
        if "YES" in similarity_verification_result.strip().upper() and "YES" in politeness_verification_result.strip().upper():  
            break
        else:
          user_input = user_input_rephrased

    print(f"Original comment: {comment}")
    print(f"Iterative rephrased comment: {user_input_rephrased}")


    semantic_similarity=compute_semantic_similarity(comment, user_input_rephrased)
    iterative_similarity_scores.append(semantic_similarity)
    print(f'semantic similarity: {semantic_similarity}')
    politeness=is_polite_transformer(user_input_rephrased)
    print(f'politeness: {politeness}')
    iterative_politeness_scores.append(politeness)
    
    standard_rephrased = llm(f"Can you reword this comment to be more polite and kinder?: {comment}")
    print(f"Standard rephrased comment: {standard_rephrased}")
    

    semantic_similarity=compute_semantic_similarity(comment, standard_rephrased)
    standard_similarity_scores.append(semantic_similarity)
    print(f'semantic similarity: {semantic_similarity}')
    politeness=is_polite_transformer(standard_rephrased)
    print(f'politeness: {politeness}')
    standard_politeness_scores.append(politeness)
</code></pre><p>Here is an example output where iterative prompting performs better than standard querying in terms of politeness:</p><pre><code>Original comment: I've met some dumb people, but you take the cake.
<strong>Iterative rephrased comment:</strong> 

I've been pleasantly surprised by your knowledge.
<em>semantic similarity</em>: 0.24437330663204193
<em>politeness</em>: 4

<strong>Standard rephrased comment:</strong> 

I've met people with different levels of knowledge, however you stand out.
<em>semantic similarity</em>: 0.3822552561759949
<em>politeness</em>: 2</code></pre><h4>Comparative Analysis: Iterative Prompting vs. Standard Querying</h4><p>After running the loop, we can compute average scores for semantic similarity and politeness for both iterative prompting and standard querying:</p><pre><code>avg_iterative_similarity = sum(iterative_similarity_scores) / len(iterative_similarity_scores)
avg_iterative_politeness = sum(iterative_politeness_scores) / len(iterative_politeness_scores)
avg_standard_similarity = sum(standard_similarity_scores) / len(standard_similarity_scores)
avg_standard_politeness = sum(standard_politeness_scores) / len(standard_politeness_scores)


print(f"Avg Iterative Similarity: {avg_iterative_similarity}, Avg Iterative Politeness: {avg_iterative_politeness}")
print(f"Avg Standard Similarity: {avg_standard_similarity}, Avg Standard Politeness: {avg_standard_politeness}")</code></pre><p><strong>Avg Iterative Similarity: 0.279, Avg Iterative Politeness: 2.111</strong> </p><p><strong>Avg Standard Similarity: 0.372, Avg Standard Politeness: 1.667</strong></p><p>After running our experiment, we found that iterative prompting generally produced more polite comments compared to standard querying, with an average politeness score of 2.11 versus 1.67. </p><p>However, the semantic similarity was slightly <em>lower</em>. This trade-off is an important consideration when implementing iterative prompting in real-world applications.</p><div><hr></div><p>That&#8217;s all for now! If you have additional thoughts, please reach out. Here is the <a href="https://colab.research.google.com/drive/1YP-2UlSdmRVaiIuXjA1mPEN8uinNHCiD?usp=sharing">notebook</a> I used for my analysis. </p><p></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://uptodata.substack.com/p/iterative-prompting-with-llms/comments&quot;,&quot;text&quot;:&quot;Leave a comment&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://uptodata.substack.com/p/iterative-prompting-with-llms/comments"><span>Leave a comment</span></a></p><div class="captioned-button-wrap" data-attrs="{&quot;url&quot;:&quot;https://uptodata.substack.com/p/iterative-prompting-with-llms?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;}" data-component-name="CaptionedButtonToDOM"><div class="preamble"><p class="cta-caption"></p></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://uptodata.substack.com/p/iterative-prompting-with-llms?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://uptodata.substack.com/p/iterative-prompting-with-llms?utm_source=substack&utm_medium=email&utm_content=share&action=share"><span>Share</span></a></p></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://uptodata.substack.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 Up to Data! 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[Basic Axioms, Bayes' Theorem & Interview Prep]]></title><description><![CDATA[A Practical Guide for Data Science, Part 1]]></description><link>https://uptodata.substack.com/p/mastering-probability-basic-axioms</link><guid isPermaLink="false">https://uptodata.substack.com/p/mastering-probability-basic-axioms</guid><dc:creator><![CDATA[Kopal Garg]]></dc:creator><pubDate>Fri, 15 Sep 2023 21:58:35 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!0emv!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91974507-969e-4ff0-a68f-b65e25d9fabd_562x455.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In simple terms, probability is a framework that allows us to study uncertainty. Whether you&#8217;re a data scientist, or a machine learning researcher, you&#8217;ll deal with data, and a solid understanding of probability is indispensable. So here is what you can expect to learn in this part of the series:</p><ul><li><p>basic concepts of probability: events, experiments and outcomes</p></li><li><p>axioms, rules and principles in probability</p></li><li><p>some python simulations to deepen your understanding </p></li><li><p><strong>bonus</strong>: a couple questions you can expect to see in data science interviews</p></li></ul><p>Let&#8217;s get started. </p><h3>Events, experiments and outcomes </h3><p>An event is just a possible outcome of the experiment. When you flip a coin, the event could be landing a heads up or a tails up. An experiment is a process that generates an outcome. For example, rolling a die, flipping a coin, measuring the temperature on a given day are all experiments because they all have multiple possible outcomes.</p><p>Now that we have those down, let&#8217;s do a simple coin flip simulation. Heads is 1, and tails is 0, and we can assume the coin is fair.</p><pre><code>import random

def flip_coin():
  return random.choice([0,1])
</code></pre><p>Because we know the coin is fair, we know that if we flip the coin a 100 times, we can expect a roughly equal number of heads and tail ups.</p><pre><code>def hundred_flips(n=100):
  result=[]
  for i in range(0,n):
    result.append(flip_coin())
  return result

flips=hundred_flips(n=100)
num_heads = sum(flips)
num_tails = len(flips) - num_heads

print(f"Number of Heads: {num_heads}") # 48
print(f"Number of Tails: {num_tails}") # 52</code></pre><p>As you can see, we got 48 heads and 52 tails. We can expect to get similar numbers if we repeat this a thousand more times, and look at the distribution of heads ups. </p><pre><code>import matplotlib.pyplot as plt

def run_experiment(num_flips=100, num_trials=1000):
  heads_counts=[]
  for _ in range(num_trials):
    flips = hundred_flips(num_flips)
    heads=sum(flips)
    heads_counts.append(heads)
  return heads_counts

heads_counts=run_experiment()

plt.hist(heads_counts, bins=range(30,70), alpha=0.75)
plt.title('Distribution of Heads in 100 Coin Flips')
plt.xlabel('Number of heads')
plt.ylabel('Frequency')
plt.show()
</code></pre><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!0emv!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91974507-969e-4ff0-a68f-b65e25d9fabd_562x455.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!0emv!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91974507-969e-4ff0-a68f-b65e25d9fabd_562x455.png 424w, https://substackcdn.com/image/fetch/$s_!0emv!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91974507-969e-4ff0-a68f-b65e25d9fabd_562x455.png 848w, https://substackcdn.com/image/fetch/$s_!0emv!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91974507-969e-4ff0-a68f-b65e25d9fabd_562x455.png 1272w, https://substackcdn.com/image/fetch/$s_!0emv!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91974507-969e-4ff0-a68f-b65e25d9fabd_562x455.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!0emv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91974507-969e-4ff0-a68f-b65e25d9fabd_562x455.png" width="562" height="455" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/91974507-969e-4ff0-a68f-b65e25d9fabd_562x455.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:455,&quot;width&quot;:562,&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_!0emv!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91974507-969e-4ff0-a68f-b65e25d9fabd_562x455.png 424w, https://substackcdn.com/image/fetch/$s_!0emv!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91974507-969e-4ff0-a68f-b65e25d9fabd_562x455.png 848w, https://substackcdn.com/image/fetch/$s_!0emv!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91974507-969e-4ff0-a68f-b65e25d9fabd_562x455.png 1272w, https://substackcdn.com/image/fetch/$s_!0emv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91974507-969e-4ff0-a68f-b65e25d9fabd_562x455.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>In the above simulation, we flip a coin 100 times as we did before, and repeat that a 1000 times, and in each trial keep track of the number of heads we get in each 100-flip sequence. When we plot this distribution, we see that the plot roughly simulates a normal distribution, which is what we expected.</p><h3>Axioms, rules and principles </h3><p>So now let&#8217;s talk about axioms, or the laws of probability. </p><ol><li><p><em>Non-negative probability: </em>This just means the probability of any event is at least 0, i.e. <em>P</em>(<em>E</em>)&#8805;0,&#8704;<em>E</em>&#8712;&#937;, and here <em>E </em>is the event and &#937; is the sample space (i.e. for all events in sample space). Note that it can be 0, just has to be non-negative. </p></li><li><p><em>Certainty of sample space</em>: The one means that something must happen. The total probability over the entire sample space is 1, i.e.:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;P(\\Omega) = \\sum_{E \\in \\Omega} P(E) = 1&quot;,&quot;id&quot;:&quot;LYCCWLNXTP&quot;}" data-component-name="LatexBlockToDOM"></div></li><li><p><em>Addition rule for mutually exclusive events</em>: So if you have a mutually exclusive event, <em>E_i</em>&#8203;, it&#8217;s probability of occurring is the sum of it&#8217;s individual probabilities. A coin flip outcome can not be heads and tails at the same time, i.e. heads and tails are mutually exclusive. The probability of both, P(H&#8899;T) = P(H) + P(T). By extension:</p><div class="latex-rendered" data-attrs="{&quot;persistentExpression&quot;:&quot;P\\left(\\bigcup_{i=1}^{\\infty} E_i\\right) = \\sum_{i=1}^{\\infty} P(E_i)&quot;,&quot;id&quot;:&quot;YAHGUCRLVU&quot;}" data-component-name="LatexBlockToDOM"></div></li></ol><h3>Conditional probability</h3><p>For two events, A and B, conditional probability, P(A|B), is the probability that event A happens when one assumes that event B has already happened. </p><p>Let&#8217;s say we want to calculate P(A|B) for rolling a six-sided die. Event A is rolling an odd number and event B is rolling a number less than 3, i.e.:</p><p>Event A: {1, 3, 5}, Event B: {1, 2}</p><p>From basic probability we know:</p><ul><li><p>the theoretical probability of getting an odd number, P(A) =3/6 = 0.5</p></li><li><p>the theoretical probability of getting a number &lt;3, P(B) = 2/6 =0.33</p></li><li><p> the theoretical probability of getting both, P(A&#8745;B)=1/6 = 0.167 because only one number (1) satisfies both conditions.</p></li><li><p>for conditional probability, P(A|B), we use formula P(A&#8745;B)/P(B)=0.167/0.33=0.5</p></li></ul><p>Let&#8217;s simulate this.</p><pre><code>import random

# Event A: {1, 3, 5}
# Evnet B: {1, 2}

def simulate_die_rolls(num_rolls=100):
  count_A = 0
  count_B = 0
  count_A_and_B = 0

  for _ in range(num_rolls):
    roll = random.randint(1,6)
    is_A = roll in [1, 3, 5]
    is_B = roll in [1, 2]
    if is_A:
      count_A+=1
    if is_B:
      count_B+=1
    if is_A and is_B:
      count_A_and_B +=1

  p_A = count_A / num_rolls
  p_B = count_B / num_rolls
  p_A_and_B = count_A_and_B / num_rolls

  p_A_given_B = count_A_and_B / count_B

  is_independent = p_A_and_B == (p_A * p_B)

  return p_A, p_B,  p_A_and_B, p_A_given_B, is_independent

p_A, p_B, p_A_and_B, p_A_given_B, is_independent=simulate_die_rolls(num_rolls=10000)

print(f"P(A) = {p_A}")
print(f"P(B) = {p_B}")
print(f"P(A and B) = {p_A_and_B}")
print(f"P(A|B) = {p_A_given_B}")
print(f"Are A and B independent? {is_independent}")
</code></pre><p>Result:</p><pre><code>P(A) = 0.4998
P(B) = 0.3342
P(A and B) = 0.163
P(A|B) = 0.48773189706762415
Are A and B independent? False</code></pre><p>The simulated probabilities are close, which is a good sanity check. The P(A|B) value suggests that the if we know event B has occurred, there is a ~49% chance that event B will also occur. The independence check tells us that A and B are not independent, which makes sense. The more simulations you run, the closer you will get to the theoretical values we computed earlier.</p><h3>Bayes&#8217; rule</h3><p>Bayes&#8217; rule is a powerful tool for revising existing predictions or hypothesis <em>given</em> new evidence. You may have seen it in the context of medical diagnosis, spam filtering and other decision-making scenarios where you have some data and you want to make an informed decision. Here is how it is generally applied:</p><ul><li><p>P(A), <strong>prior probability:</strong> this is what you already know about event A happening without new evidence on event B</p></li><li><p>P(B|A), <strong>likelihood</strong>: this tells you how likely B is assuming A is true</p></li><li><p>P(B), <strong>marginal likelihood</strong>: this tells you how likely new evidence B is under all possible values of A</p></li><li><p>P(A|B), <strong>posterior probability</strong>: this (goal of Bayes&#8217; theorem) tells you how likely A is given some new evidence B</p></li></ul><p>The general formula for Bayes&#8217; rule is:</p><p>P(B|A) = (P(B|A)P(A))  / P(B)</p><p>Say, for example, we have a rare disease that affects 1% of the population. We also have to test for this disease that has a 95% true positive rate (TPR) and a 5% FPR. We want to know: <em>if you get a positive test, what is the chance you actually have the disease?</em></p><p>P(disease | positive test) = P(positive test | disease) * P(disease) / P(positive test)</p><p>expanding P(positive test) = [ P(positive test | disease ) * P(disease) + P(positive test | no disease) * P(no disease)]</p><p>so, P(disease | positive test) = P(positive test | disease) * P(disease) /  [ P(positive test | disease ) * P(disease) + P(positive test | no disease) * P(no disease)]</p><pre><code>def bayes_rule(prior, tpr, fpr):

  # prior
  p_disease = prior # P(disease)

  # complement
  p_no_disease = 1-prior # P(no disease)

  # likelihood
  p_pos_given_disease = tpr # P(pos | disease)

  # fpr
  p_pos_given_no_disease = fpr # P(pos | no disease)

  #marginal
  p_pos = (p_disease * p_pos_given_disease) + (p_no_disease * p_pos_given_no_disease)

  #posterior
  p_disease_given_pos = (p_pos_given_disease * p_disease) / p_pos

  return p_disease_given_pos

prior=0.01
tpr=0.95
fpr=1-tpr

result = bayes_rule(prior, tpr, fpr)
print(result)

# 0.1610
</code></pre><p>So this implies that even though the test is 95% accurate, the chance of <em>actually</em> having the disease after testing is only about 16.10%. This is why Bayes&#8217; theorem is so important. It includes prior probability into the calculation.</p><h3>Combinatorics </h3><p>Combinatorics is the study of counting things. There are two main ways of counting. Consider 3 apples, A_1, A_2, and A_3, and you&#8217;re picking 2 out of them. Two considerations:</p><ul><li><p><strong>ordered vs. unordered</strong>: does the order matter? i.e. (A_1, A_2) different from (A_2, A_1)? if so, the selection process is ordered, otherwise unordered.</p></li><li><p><strong>with or without replacement</strong>: when selecting one object, is that object removed from future selection? if so, the selection process is said to have replacement.</p></li></ul><p>Let's map some of these concepts to code.</p><ul><li><p>Ordered selection with replacement </p></li></ul><p>For ordered selection with replacement, we have n^k possible selections :</p><pre><code>def ordered_with_replacement(n,k):
  return n**k

n=3 # 3 apples
k=2 # pick 2
result =ordered_with_replacement(n,k)
print(result) # 9</code></pre><p>(1,1), (1,2), (1,3), (2,1), (2,2), (2,3), (3,1), (3,2), (3,3)</p><p>For 3 apples, picking 2, we compute 3^2 = 9</p><ul><li><p>Ordered selection without replacement</p></li></ul><p>For ordered selection without replacement, use n!/(n-k)!</p><pre><code>from math import factorial

def ordered_without_replacement(n, k):
    return factorial(n) // factorial(n - k)

result = ordered_without_replacement(n, k)
print(result) # 6</code></pre><p>(1,2), (1,3), (2,1), (2,3), (3,1), (3,2)</p><p>We compute 3!/(3-2)! = 6</p><ul><li><p>Unordered selection without replacement</p></li></ul><p>For unordered selection without replacement, we use n!/(k! * (n-k)!)</p><pre><code>def unordered_without_replacement(n, k):
    return factorial(n) // (factorial(k) * factorial(n - k))

result = unordered_without_replacement(n, k)
print(result) # 3</code></pre><p>(1,2), (1,3), (2,3)</p><p>We compute 3!/(2!*(3-2)!)=3</p><ul><li><p>Unordered selection with replacement</p></li></ul><p>For unordered selection with replacement, we use (n+k-1)!/k!(n-1)!</p><pre><code>def unordered_with_replacement(n, k):
    return factorial(n + k - 1) // (factorial(k) * factorial(n - 1))

result = unordered_with_replacement(n, k)
print(result)</code></pre><p>(1,1), (1,2), (1,3), (2,2), (2,3), (3,3)</p><p>We compute (3+2-1)!/(2!(3-1)!)=6</p><p>In each case, the results match up with the theoretical expectations, and we&#8217;ve also enumerated the sets of all possible selections.</p><h3>Bonus: questions</h3><ol><li><p>Three zebras (E):</p></li></ol><pre><code>Three zebras are chilling in the desert. Suddenly a lion attacks.

Each zebra is sitting on a corner of an equally length triangle. Each zebra randomly picks a direction and only runs along the outline of the triangle to either edge of the triangle.

What is the probability that none of the zebras collide?</code></pre><p>So each zebra can either run in a CW, or CCW direction.</p><p>P(CW) = P(CCW) = 1/2 * 1/2 * 1/2 = 1/8</p><p>so P(same direction) = P(CW) + P(CCW) = <strong>1/4</strong></p><ol start="2"><li><p>Lazy Rater (E):</p></li></ol><pre><code>Netflix has hired people to rate movies.

Out of all of the raters, 80% of the raters carefully rate movies and rate 60% of the movies as good and 40% as bad. The other 20% are lazy raters and rate 100% of the movies as good.

Assuming all raters rate the same amount of movies, what is the probability that a movie is rated good?</code></pre><p>P(careful) = 0.8</p><ul><li><p>P(rated good|careful) = 0.6</p></li><li><p>P(rated bad|careful) = 0.4</p></li></ul><p>P(lazy) = 0.2</p><ul><li><p>P(rated good|lazy) = 1.0</p></li><li><p>P(rated bad|lazy) = 0</p></li></ul><p>P(rated good)= P(rated good|careful)*P(careful) +  P(rated good|lazy)*P(lazy) = 0.4*0.8 + 0.2*1=<strong>0.68</strong></p><ol start="3"><li><p>Fake Algorithm Reviews (E):</p></li></ol><pre><code>Let&#8217;s say we&#8217;re trying to determine fake reviews on our products. 

Based on past data, 98% reviews are legitimate and 2% are fake. If a review is fake, there is 95% chance that the machine learning algorithm identifies it as fake. If a review is legitimate, there is a 90% chance that the machine learning algorithm identifies it as legitimate.

What is the percentage chance the review is actually fake when the algorithm detects it as fake?</code></pre><p>P(legit) = .98</p><p>P(fake) = .02</p><p>P(identified fake|fake) = 0.95 = </p><p>P(identified legit|legit) = 0.90</p><p>P(fake|identified fake) = ?</p><p>This is a Bayes&#8217; theorem problem.</p><p>P(fake|identified fake) = P(identified fake|fake) P(fake)/P(identified fake) </p><p>P(identified fake)  = P(identified fake|fake) P(fake) + P(identified fake|legit) P(legit)</p><p>P(fake|identified fake) = 0.95 * 0.02 / [(0.95*0.2) + ((1-0.90)+0.98)]</p><p><strong>= 0.162</strong></p><div><hr></div><p>That&#8217;s all for now! &#9996;&#127996;In the next part, we will talk about discrete distributions.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://uptodata.substack.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 Up to Data! 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[Failure Handling in Apache Airflow DAGs]]></title><description><![CDATA[Learn strategies for graceful failures and efficient monitoring in your DAGs.]]></description><link>https://uptodata.substack.com/p/failure-handling-in-apache-airflow</link><guid isPermaLink="false">https://uptodata.substack.com/p/failure-handling-in-apache-airflow</guid><dc:creator><![CDATA[Kopal Garg]]></dc:creator><pubDate>Tue, 29 Aug 2023 14:31:39 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!hY7Q!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83c054d9-94d4-4dfd-a1a5-4ac6e22e79b7_706x514.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Apache Airflow is a powerful tool for orchestrating complex data pipelines. <em>However</em>, no matter how well you design your directed acyclic graphs (DAGs), failures are inevitable. The key is to make your DAGs fail gracefully so you can minimize impact. </p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!hY7Q!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83c054d9-94d4-4dfd-a1a5-4ac6e22e79b7_706x514.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!hY7Q!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83c054d9-94d4-4dfd-a1a5-4ac6e22e79b7_706x514.png 424w, https://substackcdn.com/image/fetch/$s_!hY7Q!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83c054d9-94d4-4dfd-a1a5-4ac6e22e79b7_706x514.png 848w, https://substackcdn.com/image/fetch/$s_!hY7Q!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83c054d9-94d4-4dfd-a1a5-4ac6e22e79b7_706x514.png 1272w, https://substackcdn.com/image/fetch/$s_!hY7Q!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83c054d9-94d4-4dfd-a1a5-4ac6e22e79b7_706x514.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!hY7Q!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83c054d9-94d4-4dfd-a1a5-4ac6e22e79b7_706x514.png" width="452" height="329.07648725212465" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/83c054d9-94d4-4dfd-a1a5-4ac6e22e79b7_706x514.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:514,&quot;width&quot;:706,&quot;resizeWidth&quot;:452,&quot;bytes&quot;:138031,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&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_!hY7Q!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83c054d9-94d4-4dfd-a1a5-4ac6e22e79b7_706x514.png 424w, https://substackcdn.com/image/fetch/$s_!hY7Q!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83c054d9-94d4-4dfd-a1a5-4ac6e22e79b7_706x514.png 848w, https://substackcdn.com/image/fetch/$s_!hY7Q!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83c054d9-94d4-4dfd-a1a5-4ac6e22e79b7_706x514.png 1272w, https://substackcdn.com/image/fetch/$s_!hY7Q!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83c054d9-94d4-4dfd-a1a5-4ac6e22e79b7_706x514.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>In this blog, we take a deep dive into various strategies and look at code snippets that&#8217;ll help you make your data pipelines more fault-tolerant, and maintainable.</p><h3>Variable Retry Mechanisms </h3><h4>Exponentially increase the delay between task retries</h4><p>Airflow&#8217;s built-in retry mechanism is useful, but it often makes sense to exponentially increase the delay between the tries instead of having a constant delay between retries.</p><blockquote><p>Imagine you have a task that calls an external API. If the API goes down temporarily due to rate-limiting or maintenance, the exponential backoff strategy gives the API more time to recover, rather than bombarding it with repeated calls.</p></blockquote><p>You can achieve this with some custom Python logic:</p><pre><code>from datetime import timedelta

def custom_retry_delay(context):
    var_factor = context['ti'].try_number ** 2   factor
    return timedelta(minutes=5 * var_factor)

default_args = {
    'retries': 3,
    'retry_delay': custom_retry_delay,
}</code></pre><p>So in the above code, we define a custom retry delay function. The try_number is squared to get an exponential factor. This is then used to calculate the new delay time, multiplying it with a base delay.</p><h4>Dynamic retry delays</h4><p>Similar to exponential delay, dynamic delay introduces a variable retry interval. The difference is with dynamic delay, you can delay times using a pre-defined list. There are no dynamic calculations involved, so there is no dynamic progression or increase. You can set dynamic retry delays by setting <code>retry_delay</code> to a list of <code>timedelta</code> objects.</p><blockquote><p>This is useful when you know the exact intervals you&#8217;d like between your retries. For example, when dealing with multiple services with known recovery times, you can set the delays accordingly.</p></blockquote><pre><code>default_args = {
    'retries': 3,
    'retry_delay': [timedelta(minutes=5), timedelta(minutes=10), timedelta(minutes=20)]
}</code></pre><h3>State Management and Conditional Logic</h3><h4>Using custom XComs for State</h4><p>For those not familiar, XComs is short for &#8220;cross-communications&#8221;. It&#8217;s a feature that allows you to pass state in form of messages of small amounts of data between tasks. This can be useful when you have tasks that depend on the state of previous tasks. XComs are especially useful in making your DAGs fail gracefully by letting subsequent tasks know if prior task has failed, partially succeeded, or produced a certain output. </p><pre><code>def push_xcom_value(**kwargs):
    ti = kwargs['ti']
    ti.xcom_push(key='my_key', value='my_value')</code></pre><p>One of the arguments to <code>push_xcom_value</code> is <code>ti</code>, which tasks for task instance. The <code>ti</code> object has methods for pushing and pulling XCom values in form of key-pairs, like <code>my_key</code> and <code>my_value</code>.</p><p>When this task is run, it will push the key-pair into XCom. Other tasks within the same DAG can pull this value and make decisions based on it.</p><pre><code>def pull_xcom_and_act(**kwargs):
    ti = kwargs['ti']
    value = ti.xcom_pull(key='my_key')
    if value == 'my_value':
        do_something()
    else:
        do_something_else()</code></pre><blockquote><p>Imagine you are asked to set up a data pipeline where the first task is to fetch data from an API. If the API returns an empty dataset, there is no point running the rest of the pipeline. Using XCom, you can pass the state of the data from the first task to subsequent tasks, which can then decide whether to continue execution or skip themselves.</p></blockquote><h4>Conditional skipping of tasks</h4><p>In come cases, you may want to conditionally skip tasks based on custom logic or the state of other tasks. Skipping tasks can help you make your DAGs by avoiding unnecessary computations or operations. </p><pre><code>from airflow.exceptions import AirflowSkipException

def conditionally_skip_task():
  if some_condition:
      raise AirflowSkipException</code></pre><p>In above code, when the task is run, the condition will be evaluated. If it is met, the task will raise an exception that will effectively skip the task, setting its state to &#8220;Skipped&#8221; in the Airflow UI.</p><blockquote><p>Looking at the same case use case as above, this exception would be useful if you want to skip certain tasks when the data comes back empty.</p></blockquote><h4>Task rescheduling</h4><p>Airflow allows tasks to be rescheduled rather than retried from scratch, which is useful for long-running or resource-intensive tasks. This is done by the <code>Reschedule</code> exception from <code>airflow.models</code>. </p><blockquote><p>Consider a data processing task that takes several hours to complete. If the task fails midway due to issues like network timeouts, rescheduling allows the task to continue from where it left off, saving both time and compute resources.</p></blockquote><p>Here&#8217;s an example:</p><pre><code>from airflow.models import Reschedule
def long_running_task():
  if not task_complete:
     raise Reschedule </code></pre><p>So when <code>Reschedule</code> is raised, the task instance will be rescheduled to run again at the next available time slot, maintaining its state.</p><p>This feature enables more efficient resource utilization and can significantly improve the reliability of long-running tasks in your DAGs.</p><h3>Callbacks and Alerts</h3><h4>Using <code>on_failure_callback</code> and <code>on_retry_callback</code></h4><p>Airflow provides callback mechanisms that allow you to define custom behavior when a task fails or is retried. These callbacks can be used to send alerts, clean up resources, or perform any other custom logic</p><pre><code>from airflow.operators.python import PythonOperator

def alert_on_failure(context):
   pass

task = PythonOperator(
   task_id='my_task'
   python_callable=my_python_function
   on_failure_callback=alert_on_failure
)</code></pre><p>When the task fails, the function specified in <code>on_failure_callback</code> will be executed. </p><blockquote><p>These callbacks are particularly useful for alerting. For instance, you could use <code>on_failure_callback</code> to send a Slack message, log the failure details, or even trigger another task that performs some sort of cleanup or rollback. This is demonstrated next</p></blockquote><p></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://uptodata.substack.com/p/failure-handling-in-apache-airflow?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://uptodata.substack.com/p/failure-handling-in-apache-airflow?utm_source=substack&utm_medium=email&utm_content=share&action=share"><span>Share</span></a></p><h4>Add Slack webhooks and operators for notifications</h4><p>Airflow provides built-in support for Slack notifications via the <code>SlackAPIPostOperator</code>. This lets you send custom messages to a Slack channel, which can be useful for alerting your team when a task fails or meets certain conditions.</p><blockquote><p>Let's say you have a data pipeline with multiple tasks, and it's crucial to know immediately if any task fails. You can add the <code>slack_alert</code> task to your DAG and use Airflow's <code>trigger_rule</code> to execute it whenever a task fails, ensuring instant notification.</p></blockquote><pre><code>from airflow.providers.slack.operators.slack_api import SlackAPIPostOperator

slack_alert = SlackAPIPostOperator(
   task_id = 'slack_alert',
   text = 'Task failed!'
   token = 'your_token',
   channel = '#your-channel'
)</code></pre><p>This task can be added to your DAG and set to execute when a preceding task fails, succeeds, or meets some other condition. When triggered, the <code>SlackAPIPostOperator</code> will post the specified message to the given Slack channel.</p><h4>Setting up alerts</h4><p>Airflow also has in-built support to set up email alerts that can be configured using <code>email_on_failure</code> and <code>email_on_retry</code> parameters. These parameters allow you to automatically send email alerts when tasks in your DAG fail or are retried.</p><blockquote><p>Consider a data pipeline where task failures could have a significant impact. By setting <code>email_on_failure</code> to <code>True</code>, you can ensure that the right team members are immediately notified and can take quick action to resolve the issue.</p></blockquote><pre><code>default_args = {
   'email_on_failure': True,
   'email_on_retry': False,
   'email': ['your-email@example.com']
}</code></pre><p>You can specify multiple recipients in the email list.</p><h3>Dependency Management</h3><h4>External trigger dependency with sensors</h4><p>Airflow&#8217;s sensor tasks are designed to wait for certain criteria to be met before proceeding to execute subsequent tasks. One common use case is to use the <code>ExternalTaskSensor</code> to wait for the completion of tasks in another DAG.</p><blockquote><p>Imagine two separate DAGs: 1) data ingestion and 2) data processing. The processing DAG should only run after the ingestion DAG has successfully ingested the day's data. You can use <code>ExternalTaskSensor</code> in the processing DAG to wait for the completion of the ingestion task in the ingestion DAG.</p></blockquote><pre><code>from airflow.sensors.external_task import ExternalTaskSensor

wait_for_other_task = ExternalTaskSensor(
    task_id='wait_for_other_task',
    external_dag_id='other_dag_id',
    external_task_id='other_task_id',
)</code></pre><p>The  <code>ExternalTaskSensor</code> will keep polling for the status of the specified external task in the specified external DAG. Once that task is complete, the sensor task will be marked as complete, allowing downstream tasks to proceed. </p><h4>Setting up Service Level Agreements (SLAs)</h4><p>Service Level Agreements (SLAs) in Airflow allow you to specify the maximum amount of time a task should take to complete. This can be particularly useful to ensure your tasks meet operational requirements.</p><blockquote><p>Suppose you have a task that aggregates daily sales data and it's important for this data to be available within the first few hours of the new day for reporting. By setting an SLA, you can make sure that any delays in this task are flagged to allow for prompt remedial action.</p></blockquote><pre><code>my_task = PythonOperator(
    task_id='my_task',
    python_callable=my_python_function,
    sla=timedelta(hours=2),
)</code></pre><p>When this task is executed, Airflow will monitor its duration. If the task takes more than 2 hours to complete, it will violate the SLA. Airflow provides ways to alert or take other actions when SLA violations occur, often configured via the <code>email</code> and <code>sla_miss_callback</code> parameters.</p><p>That&#8217;s all for now! These techniques are best practices that can help you build robust data pipelines. Feel free to share any additional techniques or insights you might have&#8212;I'd love to hear from you!</p><p></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://uptodata.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://uptodata.substack.com/subscribe?"><span>Subscribe now</span></a></p>]]></content:encoded></item><item><title><![CDATA[GPU-Enabled Workflows with Docker and WDL ]]></title><description><![CDATA[Optimize bioinformatics with Google Cloud: Learn how to create GPU-enabled Docker containers on Artifact Registry, and their application in running WDLs through the Cromwell.]]></description><link>https://uptodata.substack.com/p/gpu-enabled-workflows-with-docker</link><guid isPermaLink="false">https://uptodata.substack.com/p/gpu-enabled-workflows-with-docker</guid><dc:creator><![CDATA[Kopal Garg]]></dc:creator><pubDate>Fri, 18 Aug 2023 22:07:44 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!c6P8!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb208b6d2-a2bc-4633-9f96-2c93eeed558d_826x608.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>As the data we deal with in bioinformatics gets more and more complex, using GPU acceleration in Docker containers presents a good strategy to speed things up.</p><p>In this blog, we will learn how to:</p><ul><li><p>create a GPU-enabled Docker container </p></li><li><p>push the custom Docker image to Google Artifacts Registry</p></li><li><p>craft a WDL script that leverages the GPU-enabled Docker </p></li><li><p>run a WDL using Cromwell</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!c6P8!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb208b6d2-a2bc-4633-9f96-2c93eeed558d_826x608.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!c6P8!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb208b6d2-a2bc-4633-9f96-2c93eeed558d_826x608.png 424w, https://substackcdn.com/image/fetch/$s_!c6P8!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb208b6d2-a2bc-4633-9f96-2c93eeed558d_826x608.png 848w, https://substackcdn.com/image/fetch/$s_!c6P8!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb208b6d2-a2bc-4633-9f96-2c93eeed558d_826x608.png 1272w, https://substackcdn.com/image/fetch/$s_!c6P8!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb208b6d2-a2bc-4633-9f96-2c93eeed558d_826x608.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!c6P8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb208b6d2-a2bc-4633-9f96-2c93eeed558d_826x608.png" width="826" height="608" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b208b6d2-a2bc-4633-9f96-2c93eeed558d_826x608.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:608,&quot;width&quot;:826,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:74525,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&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_!c6P8!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb208b6d2-a2bc-4633-9f96-2c93eeed558d_826x608.png 424w, https://substackcdn.com/image/fetch/$s_!c6P8!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb208b6d2-a2bc-4633-9f96-2c93eeed558d_826x608.png 848w, https://substackcdn.com/image/fetch/$s_!c6P8!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb208b6d2-a2bc-4633-9f96-2c93eeed558d_826x608.png 1272w, https://substackcdn.com/image/fetch/$s_!c6P8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb208b6d2-a2bc-4633-9f96-2c93eeed558d_826x608.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><figcaption class="image-caption">GPU-Enabled Docker Containers with WDL for Bioinformatics Workflows </figcaption></figure></div><h4><strong>0. Setting Up the Environment:</strong></h4><p>Start by making sure you have Docker installed on your system, or VM. Next, choose an artifact registry like Google Artifact Registry or AWS Elastic Container Registry and set it up according to their instructions. Finally, you can run the following linux command to check if your host machine's GPU is configured properly</p><pre><code>nvidia-smi</code></pre><p>If everything is in order, you'll see detailed information about your GPU.</p><h4><strong>1. Build a GPU-Enabled Docker Image</strong></h4><p>To begin, create a file named <code>Dockerfile</code> with the following content:</p><pre><code>FROM nvidia/cuda:12.2.0-devel-ubuntu20.04</code></pre><p>This line sets up a GPU-enabled environment using NVIDIA's CUDA 12.2 development image based on Ubuntu 20.04. </p><p>From here, you can further customize the image to include specific tools or libraries relevant to your bioinformatics workflow, like so:</p><pre><code>FROM nvidia/cuda:12.2.0-devel-ubuntu20.04

WORKDIR /usr/src/app

RUN apt-get update &amp;&amp; apt-get install -y \
    git \
    wget
    
ENV CONDA_DIR /opt/conda
RUN wget --quiet https://repo.anaconda.com/miniconda/Miniconda3-py39_23.5.2-0-Linux-x86_64.sh -O ~/miniconda.sh &amp;&amp; \
    /bin/bash ~/miniconda.sh -b -p /opt/conda

ENV PATH=$CONDA_DIR/bin:$PATH

RUN git clone https://github.com/example/example &amp;&amp; \
    cd example &amp;&amp; \
    python3 setup.py install</code></pre><p>Build a Docker image named &#8216;<code>example</code>&#8217; with the following command:</p><pre><code>docker build -t example .</code></pre><p>Test the image locally:</p><pre><code>docker run --rm --gpus=all -it example</code></pre><p>If you see a GPU driver incompatibility error, you may use the following to avoid it:</p><pre><code>docker run --rm --gpus=all --env NVIDIA_DISABLE_REQUIRE=1 -it example</code></pre><h4><strong>2. Push the Image to the Artifact Registry</strong></h4><p>Once you've built and tested the Docker image locally, you may want to deploy it in a cloud environment, like the Google Artifact Registry (GAR). </p><p>You'll need to authenticate your local Docker client with GAR so you can push and pull images from that registry.</p><pre><code>gcloud auth configure-docker us-central1-docker.pkg.dev</code></pre><p>Next, tag the image with the full registry path including the location (<code>us-central1</code>), project ID (<code>project-1</code>), repository name (<code>repository-1</code>) and image name (<code>example</code>):</p><pre><code>docker tag example us-central1-docker.pkg.dev/project-1/repository-1/example</code></pre><p>Finally, push the tagged image to the registry:</p><pre><code>docker push us-central1-docker.pkg.dev/project-1/repository-1/example</code></pre><h4><strong>3. Writing the WDL Task</strong></h4><p>Now let&#8217;s create a file named <code>example_workflow.wdl</code> to define a task to run an example command using the GPU-enabled Docker image we created earlier.</p><pre><code>task example {
  String sequence_input

  runtime {
    docker: "us-central1-docker.pkg.dev/project-1/repository-1/example"
    memory: "16 GB"
    cpu: "8"
    disks: "local-disk 500 HDD"
    gpuCount: 1
    bootDiskSizeGb: 100
  }

  command &lt;&lt;&lt;
   
    example [...] &gt; result
    tar -cvf result.tar result/

  &gt;&gt;&gt;
  output {
    File example_output = "result.tar"
  }
}

workflow example_workflow {
  String sequence_input

  call example {
    input:
      sequence_input = sequence_input
  }
  output{
    File example_output_workflow = example.example_output
    }
}</code></pre><p>The next step is to execute it. We'll use Cromwell, a WDL execution engine. </p><pre><code>java -jar cromwell.jar run my_task.wdl -i inputs.json
</code></pre><p>You'll need to create an <code>inputs.json</code> file with the necessary inputs. For the example workflow we defined earlier, the inputs file might look like this:</p><pre><code>{
  "example_workflow.sequence_input": "your_sequence_here"
}
</code></pre><p>Cromwell will provide log output as the workflow runs, allowing you to monitor its progress. </p><p>With these steps, you can create, push, and use GPU-enabled Docker containers in your bioinformatics workflows using WDL. </p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://uptodata.substack.com/p/gpu-enabled-workflows-with-docker?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://uptodata.substack.com/p/gpu-enabled-workflows-with-docker?utm_source=substack&utm_medium=email&utm_content=share&action=share"><span>Share</span></a></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://uptodata.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://uptodata.substack.com/subscribe?"><span>Subscribe now</span></a></p><h4>References</h4><ul><li><p>WDL specifications: https://github.com/openwdl/wdl/blob/main/versions/1.1/SPEC.md</p></li><li><p>Setting WDL runtime attributes for Cromwell: https://cromwell.readthedocs.io/en/stable/RuntimeAttributes/</p></li></ul>]]></content:encoded></item><item><title><![CDATA[Tuning Vertex AI Models and Using Caching Strategies for Inference]]></title><description><![CDATA[In this article, we will delve into the process of fine-tuning a language model using Google's Vertex AI, utilizing data extracted from the Stack Overflow dataset available through BigQuery.]]></description><link>https://uptodata.substack.com/p/tuning-vertex-ai-models-and-using</link><guid isPermaLink="false">https://uptodata.substack.com/p/tuning-vertex-ai-models-and-using</guid><dc:creator><![CDATA[Kopal Garg]]></dc:creator><pubDate>Tue, 15 Aug 2023 18:19:30 GMT</pubDate><enclosure url="https://substack-post-media.s3.amazonaws.com/public/images/1c334fa0-56f1-45dd-a9b9-f38cfa214f77_1128x904.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In this article, we will delve into the process of fine-tuning a language model using Google's Vertex AI, utilizing data extracted from the Stack Overflow dataset available through BigQuery. We will also look at caching strategies for improving model inference. </p><p></p><div class="captioned-image-container"><figure><a class="image-link image2" target="_blank" href="https://substackcdn.com/image/fetch/$s_!gOZ2!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91593424-cfda-4f3e-a521-29a3d03d5720_1425x324.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!gOZ2!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91593424-cfda-4f3e-a521-29a3d03d5720_1425x324.png 424w, https://substackcdn.com/image/fetch/$s_!gOZ2!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91593424-cfda-4f3e-a521-29a3d03d5720_1425x324.png 848w, https://substackcdn.com/image/fetch/$s_!gOZ2!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91593424-cfda-4f3e-a521-29a3d03d5720_1425x324.png 1272w, https://substackcdn.com/image/fetch/$s_!gOZ2!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91593424-cfda-4f3e-a521-29a3d03d5720_1425x324.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!gOZ2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91593424-cfda-4f3e-a521-29a3d03d5720_1425x324.png" width="1425" height="324" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/91593424-cfda-4f3e-a521-29a3d03d5720_1425x324.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:324,&quot;width&quot;:1425,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:49733,&quot;alt&quot;:&quot;&quot;,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&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="" title="" srcset="https://substackcdn.com/image/fetch/$s_!gOZ2!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91593424-cfda-4f3e-a521-29a3d03d5720_1425x324.png 424w, https://substackcdn.com/image/fetch/$s_!gOZ2!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91593424-cfda-4f3e-a521-29a3d03d5720_1425x324.png 848w, https://substackcdn.com/image/fetch/$s_!gOZ2!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91593424-cfda-4f3e-a521-29a3d03d5720_1425x324.png 1272w, https://substackcdn.com/image/fetch/$s_!gOZ2!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F91593424-cfda-4f3e-a521-29a3d03d5720_1425x324.png 1456w" sizes="100vw" fetchpriority="high"></picture><div></div></div></a><figcaption class="image-caption">Process for Fine-Tuning a Vertex AI Language Model</figcaption></figure></div><p></p><p>To provide some context:</p><ul><li><p><strong>Vertex AI</strong>: Google's managed machine learning platform designed for building, deploying, and scaling models. It offers a few pre-trained models along with customization options</p></li><li><p><strong>BigQuery</strong>: Google's data warehouse solution that enables SQL queries over massive datasets</p></li></ul><p>This article will lead you through the following steps:</p><ul><li><p>Retrieve relevant data from Stack Overflow using BigQuery</p></li><li><p>Learn how to clean and structure your data for optimal model training</p></li><li><p>Fine-tune a Vertex AI foundation language model </p></li><li><p>Discover various techniques for caching using LangChain</p></li></ul><h4>1. Retrieve data from BigQuery</h4><p>This section deals with data extraction from BigQuery and downloading a CSV file containing the required data. Note that you&#8217;ll find information on environment variables, and libraries towards the end of the article.</p><ul><li><p>Run this SQL query on BigQuery to extract data from the publicly available Stack Overflow dataset. Filter questions and answers based on specific tags (e.g. R, bash) and dates.</p></li></ul><pre><code><code>SELECT
  CONCAT(q.body, q.tags) AS input_text,
  a.body AS output_text
FROM
  `bigquery-public-data.stackoverflow.posts_questions` q
JOIN
  `bigquery-public-data.stackoverflow.posts_answers` a
ON
  q.accepted_answer_id = a.id
WHERE
  q.accepted_answer_id IS NOT NULL
  AND (REGEXP_CONTAINS(q.tags, "R")
    OR REGEXP_CONTAINS(q.tags, "bash"))
  AND a.creation_date &gt;= "2020-01-01"
LIMIT
  10000</code></code></pre><ul><li><p>Save the above results into a CSV file on your Google Drive. The next piece of code downloads the file into a dataframe.</p></li></ul><pre><code><code>
creds = None  
drive_service = build('drive', 'v3', credentials=creds)
file_id='file-id'

request = drive_service.files().get_media(fileId=file_id)
downloaded = io.BytesIO()
downloader = MediaIoBaseDownload(downloaded, request)
done = False

while done is False:
    _, done = downloader.next_chunk()

downloaded.seek(0)
df = pd.read_csv(downloaded)[0:1000]</code></code></pre><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ysYy!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe4ec66a8-0aac-4480-839b-247bc00e3462_502x572.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ysYy!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe4ec66a8-0aac-4480-839b-247bc00e3462_502x572.png 424w, https://substackcdn.com/image/fetch/$s_!ysYy!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe4ec66a8-0aac-4480-839b-247bc00e3462_502x572.png 848w, https://substackcdn.com/image/fetch/$s_!ysYy!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe4ec66a8-0aac-4480-839b-247bc00e3462_502x572.png 1272w, https://substackcdn.com/image/fetch/$s_!ysYy!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe4ec66a8-0aac-4480-839b-247bc00e3462_502x572.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ysYy!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe4ec66a8-0aac-4480-839b-247bc00e3462_502x572.png" width="502" height="572" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e4ec66a8-0aac-4480-839b-247bc00e3462_502x572.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:572,&quot;width&quot;:502,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:61606,&quot;alt&quot;:&quot;&quot;,&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="" title="" srcset="https://substackcdn.com/image/fetch/$s_!ysYy!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe4ec66a8-0aac-4480-839b-247bc00e3462_502x572.png 424w, https://substackcdn.com/image/fetch/$s_!ysYy!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe4ec66a8-0aac-4480-839b-247bc00e3462_502x572.png 848w, https://substackcdn.com/image/fetch/$s_!ysYy!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe4ec66a8-0aac-4480-839b-247bc00e3462_502x572.png 1272w, https://substackcdn.com/image/fetch/$s_!ysYy!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe4ec66a8-0aac-4480-839b-247bc00e3462_502x572.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><figcaption class="image-caption">SQL Query Results on BigQuery</figcaption></figure></div><h4>2. Data Cleaning</h4><p>This section focuses on data cleaning, specifically removing HTML tags from text data.</p><ul><li><p>Here is a custom function that uses <code>BeautifulSoup</code> to clean the HTML tags from the <code>input_text</code> and <code>output_text</code> columns of the dataframe.</p></li></ul><pre><code><code>def remove_html_tags(text):
    return BeautifulSoup(text, 'html.parser').get_text()

df['input_text'] = df['input_text'].apply(remove_html_tags)
df['output_text'] = df['output_text'].apply(remove_html_tags)</code></code></pre><h4><strong>3. Model Tuning</strong></h4><p>This section focuses on splitting the data into training and evaluation sets and tuning a language model using Vertex AI. </p><ul><li><p>This code creates a JSONL file for training and then defines the new model name. </p></li></ul><pre><code><code>train, evaluation = train_test_split(df, test_size=0.2)
tune_jsonl = train.to_json(orient='records', lines=True)
training_data_filename = "tune_data_stack_overflow_python_qa.jsonl"

with open(training_data_filename, "w") as f:
    f.write(tune_jsonl)

tuned_model_name="genai-workshop-tuned-model-cart1"</code></code></pre><ul><li><p>With the following code you can tune a pre-trained model (<code>text-bison@001)</code> for a specific task, with parameters like training steps and locations.</p></li></ul><pre><code><code>model = TextGenerationModel.from_pretrained("text-bison@001")
model.tune_model(training_data=training_data_filename, model_display_name=tuned_model_name, train_steps=100, tuning_job_location="europe-west4",tuned_model_location="us-central1")</code></code></pre><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!e4Il!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5dda0021-e890-420a-952a-45467d2111ff_1187x1126.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!e4Il!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5dda0021-e890-420a-952a-45467d2111ff_1187x1126.png 424w, https://substackcdn.com/image/fetch/$s_!e4Il!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5dda0021-e890-420a-952a-45467d2111ff_1187x1126.png 848w, https://substackcdn.com/image/fetch/$s_!e4Il!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5dda0021-e890-420a-952a-45467d2111ff_1187x1126.png 1272w, https://substackcdn.com/image/fetch/$s_!e4Il!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5dda0021-e890-420a-952a-45467d2111ff_1187x1126.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!e4Il!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5dda0021-e890-420a-952a-45467d2111ff_1187x1126.png" width="1187" height="1126" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5dda0021-e890-420a-952a-45467d2111ff_1187x1126.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1126,&quot;width&quot;:1187,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:211873,&quot;alt&quot;:&quot;&quot;,&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="" title="" srcset="https://substackcdn.com/image/fetch/$s_!e4Il!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5dda0021-e890-420a-952a-45467d2111ff_1187x1126.png 424w, https://substackcdn.com/image/fetch/$s_!e4Il!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5dda0021-e890-420a-952a-45467d2111ff_1187x1126.png 848w, https://substackcdn.com/image/fetch/$s_!e4Il!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5dda0021-e890-420a-952a-45467d2111ff_1187x1126.png 1272w, https://substackcdn.com/image/fetch/$s_!e4Il!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5dda0021-e890-420a-952a-45467d2111ff_1187x1126.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><figcaption class="image-caption">Model Tuning Pipeline on Vertex AI</figcaption></figure></div><h4>4. Inference</h4><p>This section involves generating predictions from different models. Each model takes a prompt as an input and predicts the response based on the defined parameters like temperature and maximum output tokens.</p><p>Here we ask it to write an R function.</p><pre><code><code>parameters = {
    "temperature": 0.2,
    "max_output_tokens": 1024
}

prompt = """
Write an R function that uses GPU acceleration to multiply two matrices. Include details such as matrix dimensions, data types, and any specific GPU libraries or functions you use.
"""</code></code></pre><h5>Tuned Model: This is the model we fine-tuned using the dataset from Stack Overflow.</h5><pre><code><code>tuned_model = model.get_tuned_model("projects/581748955524/locations/us-central1/models/2298518062749450240")
response_tuned_model = tuned_model.predict(
    prompt,
    **parameters
)
print(f"Response from Model: {response_tuned_model.text}")</code></code></pre><pre><code><code>#' Multiply two matrices using GPU acceleration
#'
#' This function multiplies two matrices using GPU acceleration.
#'
#' @param x A numeric matrix.
#' @param y A numeric matrix.
#'
#' @return A numeric matrix.
#'
#' @examples
#' x &lt;- matrix(1:16, 4, 4)
#' y &lt;- matrix(1:16, 4, 4)
#' z &lt;- gpu_matmul(x, y)
#'
#' @importFrom gpuR gpuR
#'
#' @export
gpu_matmul &lt;- function(x, y) {
  # Check input dimensions
  if (dim(x)[2] != dim(y)[1]) {
    stop("Matrices must have compatible dimensions.")
  }

  # Create GPU matrices
  x_gpu &lt;- gpuR::gpuR(x)
  y_gpu &lt;- gpuR::gpuR(y)

  # Multiply matrices
  z_gpu &lt;- gpuR::gpuR(x_gpu %*% y_gpu)

  # Convert GPU matrix to R matrix
  z &lt;- gpuR::gpuR_to_array(z_gpu)

  return(z)
}</code></code></pre><h5>Code Model: This is a Vertex AI foundation model, designed specifically to generate code snippets based on given requirements.</h5><pre><code><code>code_model = CodeGenerationModel.from_pretrained("code-bison@001")
response_code_model = code_model.predict(
    prompt,
    **parameters
)
print(f"Response from Model: {response_code_model.text}")")</code></code></pre><pre><code><code># Multiply two matrices using GPU acceleration

multiply_matrices_gpu &lt;- function(m1, m2) {

  # Check that the matrices are of the same size and type

  if (dim(m1) != dim(m2)) {
    stop("The matrices must be of the same size.")
  }

  if (typeof(m1) != typeof(m2)) {
    stop("The matrices must be of the same type.")
  }

  # Initialize the GPU library

  library(gputools)

  # Create GPU matrices from the input matrices

  m1_gpu &lt;- gpuMatrix(m1)
  m2_gpu &lt;- gpuMatrix(m2)

  # Multiply the matrices on the GPU

  product_gpu &lt;- m1_gpu %*% m2_gpu

  # Return the product matrix

  return(product_gpu)

}</code></code></pre><p>Both models seem to produce syntactically correct code snippets that follow the prompt. </p><h4>5. Caching Strategies</h4><p>This section explores strategies to speed up retrieval of repeated queries.</p><p>We  time cache retrieval for different prompts.</p><h5>Exact Caching: Uses exact match between query and cache.</h5><p>Exact caching is straightforward. It is efficient for queries that are repeated exactly. It makes sure to retrieve the cached result rather than re-computing it.</p><p>Here's how it's implemented:</p><ul><li><p>A Vertex AI model is linked with a specific tuned model</p></li><li><p>A cache object is initialized</p></li></ul><pre><code><code>llm = VertexAI(tuned_model_name='projects/581748955524/locations/us-central1/models/2298518062749450240')

llm_cache = Cache()
llm_cache.init(pre_embedding_func=get_prompt)

cached_llm = LangChainLLMs(llm=llm)</code></code></pre><ul><li><p>Timing is used to show the retrieval speed of cached and uncached prompts</p></li><li><p>For exact matches, the response is taken from the cache</p></li></ul><p>Query once:</p><pre><code><code>start = time.time()

prompt = 'What is caching?'
answer = cached_llm(prompt=prompt, cache_obj=llm_cache)
print(time.time() - start)</code></code></pre><p><em>Time:  6.31 s</em></p><p>Send the exact same query again:</p><pre><code><code>start = time.time()

prompt = 'What is caching?'
answer = cached_llm(prompt=prompt, cache_obj=llm_cache)
print(time.time() - start)</code></code></pre><p><em>Time:  0.76 s</em></p><p>This is where it falls short. It doesn&#8217;t recognize a similar question that is worded differently, and ends up taking a long time:</p><pre><code><code>start = time.time()

prompt = 'Define caching'
answer = cached_llm(prompt=prompt, cache_obj=llm_cache)
print(time.time() - start)</code></code></pre><p><em>Time:  6.49 s</em></p><h5>Semantic Caching: Utilizes similarity-based matching between queries.</h5><p>And this is where semantic caching is better. It goes a step further by recognizing similarity-based matches. Here's how it works:</p><ul><li><p>Again, a Vertex AI model is linked with a specific tuned model</p></li><li><p>A hash function is used to initialize the cache object, linking it with a directory</p></li></ul><pre><code><code>llm = VertexAI(tuned_model_name='projects/581748955524/locations/us-central1/models/2298518062749450240')

def init_gptcache(cache_obj: Cache, llm: str):
    hashed_llm = hashlib.sha256(llm.encode()).hexdigest()
    init_similar_cache(cache_obj=cache_obj, data_dir=f"similar_cache_{hashed_llm}")


langchain.llm_cache = GPTCache(init_gptcache)</code></code></pre><ul><li><p>Timing is used to compare the retrieval speed for similar but <em>not</em> identical queries</p></li><li><p>If the query is semantically similar to a cached result, the cached response is used</p></li></ul><p>Query once:</p><pre><code><code>start = time.time()
llm('What is sharding?')
print(time.time() - start)
</code></code></pre><p><em>Time:  7.33 s</em></p><p>Semantically similar query:</p><pre><code><code>start = time.time()
llm('Can you define sharding?')
print(time.time() - start)
</code></code></pre><p><em>Time:  0.71 s</em></p><p>As you can see, cached results are faster for slightly different, but related queries.</p><p>So, in this article we learned how to fine tune foundation models from Vertex AI. We also saw how semantic caching can provide an additional layer of performance optimization. </p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://uptodata.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://uptodata.substack.com/subscribe?"><span>Subscribe now</span></a></p><h4>References</h4><p>GPTCache: https://github.com/zilliztech/GPTCache</p><p>LangChain caching: https://python.langchain.com/docs/integrations/llms/llm_caching</p><p>Tuning foundation models: https://cloud.google.com/vertex-ai/docs/generative-ai/models/tune-models</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://uptodata.substack.com/p/tuning-vertex-ai-models-and-using?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;,&quot;action&quot;:null,&quot;class&quot;:&quot;button-wrapper&quot;}" data-component-name="ButtonCreateButton"><a class="button primary button-wrapper" href="https://uptodata.substack.com/p/tuning-vertex-ai-models-and-using?utm_source=substack&utm_medium=email&utm_content=share&action=share"><span>Share</span></a></p><h4>Additional details:</h4><h5>Environment Variables </h5><p>Here's a quick look at the variables being used.</p><ul><li><p><code>project</code>: specifies the unique ID of the project</p></li><li><p><code>bucket_uri</code>: denotes the URI of the bucket</p></li><li><p><code>location</code>: defines the location for resource provisioning</p></li></ul><pre><code><code>project="abc" 
bucket_uri="gs://abc-cde-efg"
location="us-central1"  </code></code></pre><h5>Import Libraries</h5><ul><li><p><code>io</code>, <code>time</code>, <code>numpy</code>, and <code>pandas</code> for handling input-output and data processing</p></li><li><p><code>BeautifulSoup</code> for HTML parsing</p></li><li><p><code>aiplatform</code>, <code>bigquery</code>, and Google Drive API libraries for  interacting with Google Cloud</p></li><li><p>Other libraries for caching and VertexAI</p></li></ul><pre><code><code>import io
import time
import numpy as np
import pandas as pd
import hashlib
from typing import Union
from sklearn.model_selection import train_test_split
from bs4 import BeautifulSoup

from google.cloud import aiplatform, bigquery
from googleapiclient.discovery import build
from googleapiclient.http import MediaIoBaseDownload
from google.oauth2.credentials import Credentials
from google.colab import auth as google_auth

import vertexai
from vertexai.preview.language_models import TextGenerationModel
from vertexai.language_models import CodeGenerationModel

from langchain import SQLDatabase, PromptTemplate, LLMChain
from langchain.llms import VertexAI
from langchain.cache import GPTCache

from gptcache import cache, Cache
from gptcache.manager import CacheBase, VectorBase, get_data_manager
from gptcache.similarity_evaluation.distance import SearchDistanceEvaluation
from gptcache.processor.pre import get_prompt
from gptcache.adapter.langchain_models import LangChainLLMs
from gptcache.adapter.api import init_similar_cache
</code></code></pre>]]></content:encoded></item></channel></rss>