<?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:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Aditya Ravi Raj on Medium]]></title>
        <description><![CDATA[Stories by Aditya Ravi Raj on Medium]]></description>
        <link>https://medium.com/@aditya_dev30?source=rss-8f5f8ba622f7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*jkaYy6clMm0UoHOoJ2V_xg.jpeg</url>
            <title>Stories by Aditya Ravi Raj on Medium</title>
            <link>https://medium.com/@aditya_dev30?source=rss-8f5f8ba622f7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 22:49:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@aditya_dev30/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[A Beginner’s Guide to LangGraph]]></title>
            <link>https://medium.com/@aditya_dev30/a-beginners-guide-to-langgraph-45088dc437ce?source=rss-8f5f8ba622f7------2</link>
            <guid isPermaLink="false">https://medium.com/p/45088dc437ce</guid>
            <category><![CDATA[generative-ai-tools]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[large-language-models]]></category>
            <category><![CDATA[ai-agent]]></category>
            <category><![CDATA[genai]]></category>
            <dc:creator><![CDATA[Aditya Ravi Raj]]></dc:creator>
            <pubDate>Sun, 24 Aug 2025 19:49:36 GMT</pubDate>
            <atom:updated>2025-08-24T19:52:05.373Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oQMWuOSYo1tNX_MhG3q9KA.png" /></figure><p>The world of AI is buzzing with “agentic frameworks,” and while there are many to choose from, LangGraph has emerged as a favorite for building complex, production-ready applications. It strikes a powerful balance, offering high-level abstractions for standard tasks (like RAG and ReAct agents) while giving you the freedom to code your own custom logic.</p><p>The learning curve can feel a bit steep compared to other frameworks, but once you master it, LangGraph is an incredibly powerful tool to have in your arsenal.</p><p>Before we dive into LangGraph, let’s quickly demystify what an “agent” really is.</p><h3>What are AI Agents?</h3><p>Stripping away the buzzwords, an <strong>agent</strong> is simply a combination of a Large Language Model (LLM) and a set of tools it can use to interact with the outside world.</p><p>Think of it this way: you can ask an LLM to write an essay, but you can’t ask it to save that essay as a Markdown file on your computer. The LLM is the “brain,” but it lacks “hands.” To give it hands, we create <strong>tools </strong>like a “markdown file writer.” The agent uses the LLM’s reasoning ability to decide <em>when</em> to use that tool to complete a task.</p><p>This core principle powers many of the “smart” features you see today, from GitHub Copilot’s code generation to OpenAI’s function calling.</p><h3>Why are they becoming popular now?</h3><p>An agent is only as smart as the LLM that powers it. Until recently, LLMs weren’t quite capable enough to autonomously reason and choose the correct path in complex situations.</p><p>The landscape shifted dramatically with the arrival of powerful models like GPT-4 and Claude 3 Sonnet. These models are excellent “brains,” capable of making decisions that rival human reasoning. This leap in LLM capability, combined with better hardware and a thriving open-source community, has finally made it practical for developers to build and deploy their own powerful agents.</p><h3>The Lingering Challenges</h3><p>Despite the progress, building a perfectly reliable agent is still a major challenge.</p><ul><li><strong>Hallucinations</strong>: Transformer-based LLMs sometimes “hallucinate” or make things up. This isn’t a bug but a byproduct of their architecture, meaning you can never fully eliminate the uncertainty in their responses. This is why most production-grade agents have a “human-in-the-loop” for oversight.</li><li><strong>Guardrails vs. Latency</strong>: It’s easy to “jailbreak” an LLM without proper safety guardrails. However, adding too many checks and filters can slow the system down, creating a trade-off between safety and performance.</li></ul><p>Now that we have the context, let’s explore how LangGraph helps us tackle these challenges.</p><h3>The Core Components of LangGraph</h3><p>LangGraph is an extension of the popular LangChain library, so if you’re familiar with LangChain, you’ll feel right at home. At its heart, LangGraph models workflows as <strong>graphs</strong>. You build these graphs using four key components.</p><h3>1. State:</h3><p>The <strong>State</strong> is the agent’s shared memory. It’s a central data structure that holds a complete snapshot of the application’s progress at any given moment. Every other component in the graph reads from, writes to, and updates this central State.</p><p>Defining the <strong>State</strong> involves two key components:</p><ol><li><strong>The Schema:</strong> This is the blueprint for your memory. It defines what kind of information can be stored, typically using a Python TypedDict or Pydantic model. This ensures data consistency across your entire graph. This schema serves as the input for every Node and Edge in your graph. It is also possible to define explicit input and output schemas for a graph. In these cases, we define an “internal” schema that contains <em>all</em> keys relevant to graph operations. But, we also define input and output schemas that are subsets of the &quot;internal&quot; schema to constrain the input and output of the graph.</li></ol><p><strong>Example:</strong></p><pre>class InputState(TypedDict):<br>    user_input: str<br><br>class OutputState(TypedDict):<br>    graph_output: str<br><br># Internal Schema<br>class OverallState(TypedDict):<br>    foo: str<br>    user_input: str<br>    graph_output: str<br><br>class PrivateState(TypedDict):<br>    bar: str</pre><ol><li><strong>The Reducer Function:</strong> If the schema is the <em>what</em>, the reducer is the <em>how</em>. A reducer is a function that specifies the rules for updating the state. By default, a node’s output will <strong>overwrite</strong> the corresponding key in the state. However, with a reducer, you can define more intelligent logic, like <strong>appending</strong> to a list instead of replacing it.</li></ol><p><strong>Example 1-Default reducer:</strong><br>In this example, no reducer functions are specified for any key.</p><pre>from typing_extensions import TypedDict<br><br>class State(TypedDict):<br>    foo: int<br>    bar: list[str]</pre><p>Let’s assume the input to the graph is:<br>{&quot;foo&quot;: 1, &quot;bar&quot;: [&quot;hi&quot;]}. Let&#39;s then assume the first Node returns {&quot;foo&quot;: 2}. This is treated as an update to the state. Notice that the Node does not need to return the whole State schema - just an update. After applying this update, the State would then be {&quot;foo&quot;: 2, &quot;bar&quot;: [&quot;hi&quot;]}. If the second node returns {&quot;bar&quot;: [&quot;bye&quot;]} then the State would then be {&quot;foo&quot;: 2, &quot;bar&quot;: [&quot;bye&quot;]}</p><p><strong>Example 2-add reducer:</strong><br>In this example, we’ve used the Annotated type to specify a reducer function (operator.add) for the second key (bar).</p><pre>from typing import Annotated<br>from typing_extensions import TypedDict<br>from operator import add<br><br>class State(TypedDict):<br>    foo: int<br>    bar: Annotated[list[str], add]</pre><p>Let’s assume the input to the graph is {&quot;foo&quot;: 1, &quot;bar&quot;: [&quot;hi&quot;]}. Let&#39;s then assume the first Node returns {&quot;foo&quot;: 2}. This is treated as an update to the state. Notice that the Node does not need to return the whole State schema - just an update. After applying this update, the State would then be {&quot;foo&quot;: 2, &quot;bar&quot;: [&quot;hi&quot;]}. If the second node returns {&quot;bar&quot;: [&quot;bye&quot;]}. The reducer intelligently merges this new information with the existing State, creating the next snapshot. The State would then be {&quot;foo&quot;: 2, &quot;bar&quot;: [&quot;hi&quot;, &quot;bye&quot;]}.</p><p>This cycle is continuous: a Node reads the current <strong>State</strong>, performs an action, and emits an update. The reducer then applies this update, and the process repeats. By defining the <strong>State</strong> first, you create a robust and predictable foundation for your entire agent.</p><h3>2. Nodes:</h3><p>A <strong>Node</strong> is a function or a callable object that performs a unit of work. It takes the current State as input, does something (like calling an LLM, running custom logic, or using a tool), and returns an update for the state. You can think of nodes as the individual steps in your agent&#39;s thought process. Nodes accept the following arguments:</p><ol><li>state: The state of the graph</li><li>config: A RunnableConfig object that contains configuration information like thread_id and tracing information like tags</li><li>runtime: A Runtime object that contains runtime context and other information like store and stream_writer<br>For now we will focus on state.</li></ol><p><strong>Example</strong>:<br>Continuing the example from state definition, we define a flow that processes user input and generates a final output through a series of typed nodes. Each node handles specific state transitions, and collectively they demonstrate how state is passed and transformed across nodes.<br>Node 1: It takes user input and appends the word &quot; name&quot; to it, initializing the foo key in the OverallState.<br>Node 2: It reads from the enriched foo value present in the OverallState and appends &quot; is&quot; to create a new private value (bar) used internally.<br>Node 3: It completes the message by appending &quot; Aditya&quot; to the private state value, forming the final graph output.</p><p><strong>Start Node:</strong> The START Node is a special predefined node provided by LangGraph that represents the node that sends user input to the graph. The main purpose for referencing this node is to determine which nodes should be called first.<br><strong>End Node:</strong> The END Node is a special node that represents a terminal node. This node is referenced when you want to denote which edges have no actions after they are done.</p><pre>class InputState(TypedDict):<br>    user_input: str<br><br>class OutputState(TypedDict):<br>    graph_output: str<br><br>class OverallState(TypedDict):<br>    foo: str<br>    user_input: str<br>    graph_output: str<br><br>class PrivateState(TypedDict):<br>    bar: str<br><br>def node_1(state: InputState) -&gt; OverallState:<br>    # Write to OverallState<br>    return {&quot;foo&quot;: state[&quot;user_input&quot;] + &quot; name&quot;}<br><br>def node_2(state: OverallState) -&gt; PrivateState:<br>    # Read from OverallState, write to PrivateState<br>    return {&quot;bar&quot;: state[&quot;foo&quot;] + &quot; is&quot;}<br><br>def node_3(state: PrivateState) -&gt; OutputState:<br>    # Read from PrivateState, write to OutputState<br>    return {&quot;graph_output&quot;: state[&quot;bar&quot;] + &quot; Aditya&quot;}<br><br>builder = StateGraph(OverallState,<br>                     input_schema=InputState,<br>                     output_schema=OutputState<br>                     )<br>builder.add_node(&quot;node_1&quot;, node_1)<br>builder.add_node(&quot;node_2&quot;, node_2)<br>builder.add_node(&quot;node_3&quot;, node_3)<br># If you add a node to a graph without specifying a name,<br># it will be given a default name equivalent to the function name.</pre><h3><strong>3. Edges:</strong></h3><p><strong>Edges</strong> are the connectors that define the flow of logic between nodes. An edge decides what the agent should do next, which node comes after the current one. A node can have <strong><em>multiple</em></strong> outgoing edges. If a node has multiple outgoing edges, <strong>all</strong> of those destination nodes will be executed in parallel as a part of the next superstep.<br><strong>Normal Edges:</strong> Go directly from one node to the next. You can use the <em>add_edge</em> method directly.<br><strong>Conditional Edges: </strong>These are more powerful. They use a routing function to decide the next step based on the current State. You can use the <em>add_conditional_edges</em> method. This method accepts the name of a node and a “routing function” to call after that node is executed.<br><strong>Example:</strong></p><pre>from langgraph.graph import START<br><br>graph.add_conditional_edges(START, routing_function)<br># OR<br>graph.add_conditional_edges(START, routing_function, {True: &quot;node_b&quot;, False: &quot;node_c&quot;})</pre><p><strong>Example</strong>:<br>Continuing the example from Nodes.</p><pre>builder.add_edge(START, &quot;node_1&quot;)<br>builder.add_edge(&quot;node_1&quot;, &quot;node_2&quot;)<br>builder.add_edge(&quot;node_2&quot;, &quot;node_3&quot;)<br>builder.add_edge(&quot;node_3&quot;, END)</pre><h3>4. StateGraph:</h3><p>The StateGraph is the object that brings everything together. It&#39;s the complete flowchart of your agent, containing the state schema, all the nodes, and all the edges.</p><p>Once you’ve defined your graph, you <strong>compile</strong> it. This step validates the graph’s structure (e.g., checking for orphaned nodes) and makes it executable. After compiling, you can run your agent by calling .invoke() on the graph object.</p><p><strong>Example</strong>:<br>Continuing the example from Edges.</p><pre>graph = builder.compile()<br>graph.invoke({&quot;user_input&quot;:&quot;My&quot;})<br><br># Output:<br># {&#39;graph_output&#39;: &#39;My name is Aditya&#39;}</pre><blockquote>State is the <strong>“agent’s shared memory”.</strong><br>Nodes are the workers that <strong>perform</strong> <strong>the work </strong>based on the state.<br>Edges <strong>choose the path</strong> based on the state.<br>StateGraph combines all these components in a single executable program.</blockquote><h3>Working:</h3><p>Inspired by Google’s <a href="https://research.google/pubs/pregel-a-system-for-large-scale-graph-processing/">Pregel</a> system, the LangGraph program proceeds in discrete “super-steps.” A <strong>super-step</strong> is <strong>one full round</strong> where <strong>all currently active nodes</strong> (not all nodes) run <strong>in parallel</strong>. Each of these nodes receives its <strong>state/message</strong>, does its work, and sends messages to other nodes. After they all finish, the graph moves to the <strong>next super-step</strong>.<br><strong>Example:</strong></p><pre>Let’s say you have 4 nodes: A, B, C, D.<br>Step 0 (Initial State):<br>* Only node A is active (it has received the first message).<br><br>Super-step 1:<br>* Node A runs.<br>* It sends messages to B and C.<br>* B and C are now scheduled for the next step.<br><br>Super-step 2:<br>* Nodes B and C run in parallel.<br>* B sends a message to D, C does nothing.<br>* D is scheduled for next step.<br><br>Super-step 3:<br>* Node D runs.<br>* It sends no messages.<br><br>Super-step 4:<br>* No nodes have received messages. So graph halts.</pre><p>To build your graph, you first define the state, you then add nodes and edges, and then you compile it. Compiling is a pretty simple step. It provides a few basic checks on the structure of your graph (no orphaned nodes, etc). It is also where you can specify runtime args like checkpointers and breakpoints (more on this in a different blog). You compile your graph by just calling the .compile method. Then finally invoke it with the .invokemethod.</p><p>That’s it for the basics, now you are ready for making your own Agents!!!</p><h3>Thanks for reading!</h3><p>I love writing about Generative AI, Data Structures and coding concepts. Feel free to connect with me here on <a href="https://medium.com/@aditya_dev30">Medium </a>or on <a href="https://www.linkedin.com/in/adityadev30">LinkedIn</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=45088dc437ce" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Git Rebase vs Merge: A boon or a Curse?]]></title>
            <link>https://medium.com/@aditya_dev30/github-rebase-vs-merge-a-boon-or-a-curse-890c2d6c3a88?source=rss-8f5f8ba622f7------2</link>
            <guid isPermaLink="false">https://medium.com/p/890c2d6c3a88</guid>
            <category><![CDATA[git]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[software]]></category>
            <category><![CDATA[github]]></category>
            <dc:creator><![CDATA[Aditya Ravi Raj]]></dc:creator>
            <pubDate>Sun, 17 Aug 2025 13:37:04 GMT</pubDate>
            <atom:updated>2025-08-17T13:38:47.788Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b6dTlyaQcJx5BhYg5v38Wg.jpeg" /></figure><p>When i started my journey as an AI Engineer, I was introduced to this fascinating version control System called <strong>Git</strong>. Almost every software team in the industry uses this. But it can be overwhelming for beginners. This blog will help you understand 2 of the most common Git operations(Merge and Rebase) you will be seeing in your journey as a Software Engineer.</p><h3><strong>Prerequisites:</strong></h3><p>This guide assumes you have a basic understanding of Git and version control. If you’re a complete beginner, I recommend checking out some introductory articles on Git basics before diving in.</p><h3>Working with Branches:</h3><p>Whenever we develop our code in a distributed team or multi-member team working on the same repository. We follow certain conventions:</p><ul><li>There is going to be a main branch(previously and still called master by git by default). Which holds the final, stable releases tagged with a version.</li><li>Then from main there can be different branches that can be diverged for different tasks, like dev for developers or test for Automation testers.</li><li>There can also be further branches from dev/test branches for individual features.</li></ul><p>This structure helps manage code quality, and reduces conflicts.<br>For simplicity, in this article we’ll only consider 1 main branch and 1 dev branch.</p><h3>What is Merge?</h3><p>When the work on a branch is complete we need to combine our changes with the main branch. This is where <strong><em>git merge</em></strong> comes in<br>There are two kinds of merge:</p><ol><li><strong>Fast Forward Merge:</strong><br>If the main branch had no new commits since the dev branch was made. Git simply moves the head pointer to dev’s last commit. Hence making a linear history. <br>This is hardly going to be a case in any production environment.</li><li><strong>Three-Way Merge</strong><br>This happens when the main branch has new commits since your branch was created. Git creates a special <strong>merge commit</strong> that ties bot histories together. If there are any conflicts, developers must resolve them for a successful merge. The git history graph will look like Fig 1.</li></ol><pre>// Example: Assuming you are on your dev branch<br>git checkout main<br>git merge dev</pre><blockquote><strong>Pros:<br></strong>- Maintains a clear record of how branches were combined.<br>- Good for preserving context in complex projects.<br><strong>Cons:</strong><br>- This for now seems readable and clean, but when you have a big repository with many devs working together, and each one of them is merging there changes the readability goes for a toss.<br>- This is precisely why rebase is used.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xg6yXR63zuMx64RtVwcb2g.jpeg" /><figcaption>Fig 1: Git merge flow diagram</figcaption></figure><h3>What is Rebase?</h3><p>Rebase is primarily used to maintain a linear history.<br>How does it do that?<br>Instead of creating a merge commit rebase takes all the commits from your current branch and <strong>replays</strong> them onto the tip of the target branch.</p><ol><li>Similar to git merge if the main branch had no new commits since the dev branch was made. Git behaves just like a fast-forward merge.</li><li>But if the main branch has some new commits then the rebase feature actually shows up. If you call git rebase main from dev branch the commits B and C from dev will be rewritten onto the main branch’s latest commit and the final pipeline look something like Fig 2.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Jt2vx6IsCX5ywb04LWl8Tg.jpeg" /><figcaption>Fig 2: Git rebase flow diagram</figcaption></figure><h3><strong>The hidden pitfall of Rebase:</strong></h3><p>For understanding the problem I’m going to be taking an example. Consider there are 3 developers working on the dev branch. Alice, James and Kate.<br>1. Each one of them cloned the repository and started working on there local repositories.<br>2. After some time, Alice finished her work and decided to commit her changes and push it. Since Alice wanted to have a clean history she decides to rebase her commits onto the main branch and <strong>force pushes</strong> the changes to main.<br>3. On the surface, everything looks fine.<br>But here is the problem:<br>- Alice’s rebase rewrote the commit history(the commit hashes changed).<br>- James and Kate’s local history still point to the old commit B.<br>After completing their work when they try to push their code now there is no commit B in remote history. Even though the code content maybe identical, Git doesn’t recognize it because the commit hashes have changed. A pictorial representation is shown in figure 3.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pDE3HzZJ3O_NjQK8yaNfJw.png" /><figcaption>FIg 3: Pitfall with rebase</figcaption></figure><p>To solve this problem both James and Kate will now need to <strong>rebase or reset</strong> their local branches, all of which adds to a lot of complexity. If the code overlaps with Alice’s it might lead to lost commits, conflicts and wasted time.</p><h3>Best Practice:</h3><ol><li>As a general rule of thumb, never rebase a shared branch unless all the people working on it are on the same page and willing for it.</li><li>Always make a feature branch for your individual development from dev and rebase that onto dev for a clean and linear history.</li></ol><h3>Thanks for reading!</h3><p>I love writing about Generative AI, Data Structures and coding concepts. Feel free to connect with me here on <a href="https://medium.com/@aditya_dev30">Medium </a>or on <a href="https://www.linkedin.com/in/adityadev30">LinkedIn</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=890c2d6c3a88" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Getting Started with Google Colab: Your Ultimate Setup Guide for Generative AI Projects]]></title>
            <link>https://medium.com/@aditya_dev30/getting-started-with-google-colab-your-ultimate-setup-guide-for-generative-ai-projects-53fe25f3fc04?source=rss-8f5f8ba622f7------2</link>
            <guid isPermaLink="false">https://medium.com/p/53fe25f3fc04</guid>
            <category><![CDATA[google]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[generative-ai-tools]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[colab]]></category>
            <dc:creator><![CDATA[Aditya Ravi Raj]]></dc:creator>
            <pubDate>Sun, 08 Sep 2024 16:30:03 GMT</pubDate>
            <atom:updated>2024-09-08T16:30:03.624Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Google Colab logo" src="https://cdn-images-1.medium.com/max/1000/0*NOi4Dk3Nf1lu0vvY.jpg" /></figure><p>Google Colab is a cloud-based Jupyter Notebook service that requires no setup and offers free access to powerful computing resources, including GPUs and TPUs. It’s ideal for machine learning, data science, and educational projects. Colab is particularly useful for conducting smaller research experiments that need significant computational power. The free tier provides access to an Nvidia T4 GPU with 15 GB of VRAM and 12 GB of CPU RAM.</p><ol><li><strong>Create a Google Account</strong>: If you don’t have one already, sign up for a Google account.</li><li><strong>Access Colab</strong>: To get started with a Colab notebook, simply <a href="https://colab.research.google.com/">click here</a> and sign in using your Google account.</li><li><strong>Create a New Notebook</strong>: Once logged in, you’ll be greeted by a pop-up window. Click on the “New notebook” icon to create a fresh notebook.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/221/1*5pQjKBQmLlezAGOFlmB66g.png" /></figure><p>4.<strong> Name Your Notebook</strong>: In the new window, rename your file by clicking on the filename in the top left corner to suit your project.</p><p>5. <strong>Choose Runtime Type</strong>: Decide whether you need a GPU for your task. If not, you can directly click the “Connect” button in the top-right corner. Once connected, you’ll see a prompt confirming the active runtime.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*ZyXLp52EdaSaPXbW3jAtOQ.png" /></figure><p>If you need a GPU runtime, you’ll need to adjust the settings by navigating to <strong>Runtime</strong> -&gt; <strong>Change runtime type</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/653/1*tgTC8EOPzsWRw2xvMjLZfw.png" /></figure><p>After selecting a pop-up will appear, where you can select your preferred hardware accelerator, such as a T4 GPU or TPU, based on your needs. If you’re unsure which to choose, opt for the T4 GPU, as NVIDIA GPUs are widely supported by most generative AI libraries. After selecting your preferred option, confirm the change by clicking “OK” on the warning message, and then hit “Save.”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/693/1*gqNuIPOVVw-J3xG8q73Qcg.png" /></figure><p>You are going to see below changes to your resource tracker.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/709/1*pw5FkamqwHAqsO0AeT0a8w.png" /></figure><p>Keep in mind that each time you change the runtime, all your code blocks will be reset, and you’ll need to re-execute them. This ensures that the new runtime settings are applied across your entire notebook. you can do this easily from <strong>Runtime</strong> -&gt; <strong>Run all</strong>.</p><p>6. To add a secret key, click on the <strong>key icon</strong> in the left panel. Since it’s your first time, you won’t have any secrets listed yet. Click on <strong>“Add new secret”</strong>. As a best practice, give your secret a name using all uppercase letters and underscores (e.g., API_KEY_NAME). Then, paste your API key into the value field.<br>For google you can get a free api key from <a href="https://ai.google.dev/">here</a> or by going to the <strong>“Create Gnemini API Key”</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/607/1*xC1Xf6Kxvz3V0ugw2Arvwg.png" /></figure><p><strong>Note:</strong> The free tier of Colab comes with limitations on GPU runtime sessions, which you can check <a href="https://research.google.com/colaboratory/faq.html#idle-timeouts">here</a>. Due to these restrictions, I typically write and test my code in a CPU environment before switching to a GPU runtime to execute all the code blocks of the final version.<br>Alternatively, you can opt for a Colab Pro subscription, which provides access to more powerful GPUs, such as the NVIDIA A100, along with longer and more stable runtime sessions. This can be especially useful for larger-scale projects that require extended compute resources.</p><h3>Thanks for reading!</h3><p>I love writing about Generative AI, Data Structures and coding concepts. Feel free to connect with me here on <a href="https://medium.com/@aditya_dev30">Medium </a>or on <a href="https://www.linkedin.com/in/adityadev30">LinkedIn</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=53fe25f3fc04" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building a RAG from Scratch: How to Create Your Own Retrieval-Augmented Generation System Without…]]></title>
            <link>https://medium.com/@aditya_dev30/building-a-rag-from-scratch-how-to-create-your-own-retrieval-augmented-generation-system-without-75f892351455?source=rss-8f5f8ba622f7------2</link>
            <guid isPermaLink="false">https://medium.com/p/75f892351455</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[generative-ai-tools]]></category>
            <category><![CDATA[generative-ai-use-cases]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Aditya Ravi Raj]]></dc:creator>
            <pubDate>Mon, 26 Aug 2024 19:34:21 GMT</pubDate>
            <atom:updated>2024-09-08T16:40:43.192Z</atom:updated>
            <content:encoded><![CDATA[<h3>Building a RAG from Scratch: How to Create Your Own Retrieval-Augmented Generation System Without Any Generative AI Libraries</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NlQX3MXYJBjtWAVYsTvGFQ.jpeg" /></figure><p>This article is intended as a guide for beginners who need a fundamental understanding of the process, rather than the abstracted methodologies found in popular Genrative AI libraries like Langchain and LlamaIndex. Those libraries are essential for large-scale projects and provide necessary abstractions for advanced tasks. However, when I want to understand something deeply, I believe it’s crucial to start with a small, hands-on implementation. This approach helps in grasping the basics and optimizing more complex structures when needed. To build a basic RAG pipeline, all you really need is Python and some modules, an embedding model, and an LLM.</p><h3>What is Retrieval-Augmented Generation (RAG)?</h3><p>In traditional language models, responses are generated based solely on their pre-trained knowledge and data. This usually leads to potentially outdated or inaccurate responses which might lack depth or specific knowledge. RAG overcomes this by granting LLMs access to external information sources, ensuring accurate and up-to-date answers.<br>By combining this extra knowledge with its own language skills, the model can generate text that is more accurate, up-to-date, and relevant to your specific needs. A basic RAG pipeline consists of two major phases: the Knowledge Base creation phase and the Querying phase. Each of these phases includes several sub-components, which we will explore further in the blog.</p><figure><img alt="Basic Retrieval-Augmented Generation (RAG) WorkFlow" src="https://cdn-images-1.medium.com/max/1024/1*o9vq7wV9mYXcWB34yaHYaQ.jpeg" /><figcaption><a href="https://github.com/StellarPhantasm30/generative-ai/blob/main/RAG/Basic_RAG_WorkFlow.JPG">Basic Retrieval-Augmented Generation (RAG) WorkFlow</a></figcaption></figure><h3>Knowledge Base Creation:</h3><p>All the codes for this blog can be found on my github <a href="https://github.com/StellarPhantasm30/generative-ai/blob/main/RAG/RAG_from_Scratch.ipynb">here</a>. Also you will require a free google api key for using the Gemini API. If you don’t have it you can get one from <a href="https://ai.google.dev/">here</a> or if you have any other api handy you can make changes to the code accordingly. For more details on how to setup the api key in a new colab instance check my article below.</p><p><a href="https://medium.com/@aditya_dev30/getting-started-with-google-colab-your-ultimate-setup-guide-for-generative-ai-projects-53fe25f3fc04">Getting Started with Google Colab: Your Ultimate Setup Guide for Generative AI Projects</a></p><ol><li><strong>First lets import the packages required:</strong></li></ol><pre>!pip install -q sentence-transformers<br>!pip install -q wikipedia-api<br>!pip install -q numpy<br>!pip install -q scipy<br>!pip install -q flash_attn</pre><p>I’ll be using an open-source embedding model, but you can also use Google’s free model if you prefer. For this, I’m utilizing the Sentence Transformers library. The knowledge base can vary depending on the use case — it could include PDFs, text files, Excel sheets, images, data streams, code, etc. To keep things simple, though, I’ll be using a Wikipedia page. Numpy will handle the advanced mathematical calculations. And with that, we’re all set to begin.</p><p><strong>2. Loading the model:</strong></p><pre>from sentence_transformers import SentenceTransformer<br>query_prompt_name = &quot;s2p_query&quot;<br>model = SentenceTransformer(&quot;dunzhang/stella_en_1.5B_v5&quot;, trust_remote_code=True).cuda()</pre><p>I won’t be going into the specifics of what embedding models do and how they work — that’s a topic for another discussion. At a high level, embedding models are designed to capture the meanings of words, sentences, and paragraphs, converting them into high-dimensional matrices. These dimension might vary according to models.<br>I will be using the <strong><em>Stella 1.5Billion</em></strong> version created by <a href="https://github.com/DunZhang">DunZhang</a>. At the time of writing this article it is the second ranked embedding model On the <a href="https://huggingface.co/spaces/mteb/leaderboard"><strong>Massive Text Embedding Benchmark (MTEB) Leaderboard</strong></a><strong>.</strong> You can choose other opensource or commerical models depending on leaderboard and convenience. Stella supports two prompts: &quot;s2p_query&quot; and &quot;s2s_query&quot; for sentence-to-passage and sentence-to-sentence tasks, respectively. For our usecase we are going to use s2p.</p><p><strong>3. Making the Dataset:</strong></p><pre>from wikipediaapi import Wikipedia<br>wiki = Wikipedia(&#39;RAGDemo/0.1&#39;, &#39;en&#39;)<br>doc = wiki.page(&#39;Masashi_Kishimoto&#39;).text<br>paragraphs = doc.split(&#39;\n\n&#39;) # chunking according to paragraphs<br><br># Displaying all the chunks<br>import textwrap<br>for i, paragraph in enumerate(paragraphs):<br>  wrapped_text = textwrap.fill(paragraph, width=100)<br>  print(&quot;-&quot; * 100)<br>  print(wrapped_text)<br>  print(&quot;-&quot; * 100)</pre><p>I’m utilizing the Wikipedia API to extract the full text from the specified wikipedia article, then splitting it into different chunks based on double newline characters or paragraphs. The textwrap module is used to neatly format the text for display in the terminal.</p><p><strong>4. Embedding the Document:</strong></p><pre>embedded_docs = model.encode(paragraphs, normalize_embeddings=True) # working as an in memory Vector Database<br>shape = embedded_docs.shape<br>print(&quot;Number of chunks: &quot; + str(shape[0]) + &quot; Embedding Dimension: &quot; + str(shape[1]))<br>embedded_docs[0] </pre><p>Now, we’ll use our downloaded model to generate embeddings for each chunk and store them in memory as a 2D NumPy array. In this array, each row corresponds to a chunk, and each column represents a dimension of the embedding vector. The embedding dimension will depend on the specific model being used.</p><h3>Querying:</h3><p><strong>5. Embedding The Query:</strong></p><pre>query = &quot;What is the most famous creation of Masashi Kishimoto&quot;<br>embedded_query = model.encode(query, normalize_embeddings=True)<br>embedded_query.shape</pre><p>Now we will embed the user’s query. So that we can do a similarity search on the database we have created.</p><p><strong>6. Similarity Search</strong></p><pre>import numpy as np<br>similarities = np.dot(embedded_docs, embedded_query.T)<br>similarities.shape<br>similarities</pre><p>To find the chunks most similar to the query, we calculate the dot product between the two matrices. There are other advanced methods for calculating similarity scores that include Cosine Similarity, Manhattan Distance, Euclidean Distance, and more, but for the scope of this article we will stick with most basic one. embedded_docs is a 2D matrix, and embedded_query is a 1D vector. Since the columns of the first matrix match the dimensions of the second vector, we can directly perform the dot product. In some cases, if the dimensions don&#39;t align, we would need to transpose the second matrix.</p><p><strong>7. Finding Top K most similar chunks:</strong></p><pre>top_3_idx = np.argsort(similarities)[-3:][::-1].tolist()<br>top_3_idx</pre><p>We can sort the similarity array obtained from the dot product calculation and then slice it to select only the top K chunks with the highest scores (here, I’ve chosen 3). Including more chunks can provide additional context for the LLM, but it can also introduce ambiguity if there’s a significant disparity in similarity scores.</p><p>The .argsort() function sorts the indices of the similarity array in ascending order, meaning it returns the positions of the elements from smallest to largest. After sorting, we slice the last 3 indices, as they correspond to the most similar chunks, and then reverse their order to prioritize the highest similarity first.</p><p><strong>8. Making the Context:</strong></p><pre>CONTEXT = &quot;&quot;<br>for idx in top_3_idx:<br>    wrapped_text = textwrap.fill(paragraphs[idx], width=100)<br>    print(&quot;-&quot; * 100)<br>    print(wrapped_text)<br>    print(&quot;-&quot; * 100)<br>    CONTEXT += wrapped_text + &quot;\n\n&quot;</pre><p>Now we iterate over the paragraphs list and select the top 3 most similar chunks. These chunks are then concatenated into a single string, providing our LLM with a contextual summary for the user query.</p><p><strong>9. Define a Prompt Template:</strong></p><pre>prompt = f&quot;&quot;&quot;<br>use the following CONTEXT to answer the QUESTION at the end.<br>If you don&#39;t know the answer, just say that you don&#39;t know, don&#39;t try to make up an answer.<br><br>CONTEXT: {CONTEXT}<br>QUESTION: {query}<br><br>&quot;&quot;&quot;</pre><p>Let’s create a comprehensive prompt template that effectively merges our instructions, the provided context, and the user’s query into a coherent format. This structured prompt will then be sent to our language model for generating a relevant and informative response.</p><p><strong>10. Defining API for LLM</strong></p><pre>!pip install -q -U google-generativeai</pre><p>Install the dependencies required for using the Gemini api.</p><pre>import google.generativeai as genai<br># Used to securely store your API key<br>from google.colab import userdata<br><br># Or use `os.getenv(&#39;GOOGLE_API_KEY&#39;)` to fetch an environment variable.<br>GOOGLE_API_KEY=userdata.get(&#39;GOOGLE_API_KEY&#39;)<br><br>genai.configure(api_key=GOOGLE_API_KEY)<br><br># Displays all the available models that are free to use, though they are subject to rate limits.<br>for m in genai.list_models():<br>    print(m.name, m.supported_generation_methods)<br><br>model = genai.GenerativeModel(&#39;gemini-1.5-flash&#39;)<br># Calculates total number of tokens used for the api call<br>model.count_tokens(prompt)</pre><p>With our API connection established and our model selected, we can now proceed to generate the response using our RAG pipeline.</p><pre>response = model.generate_content(prompt)<br>print(response.text)</pre><p>This will generate the response from our pipeline.<br>With this we can conclude this article. I hope you found it helpful!</p><h3><strong>Thanks for reading!</strong></h3><p>I love writing about Generative AI, Data Structures and coding concepts. Feel free to connect with me here on <a href="https://medium.com/@aditya_dev30">Medium </a>or on <a href="https://www.linkedin.com/in/adityadev30">LinkedIn</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=75f892351455" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>