<?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 Lokesh Kum on Medium]]></title>
        <description><![CDATA[Stories by Lokesh Kum on Medium]]></description>
        <link>https://medium.com/@codeloki?source=rss-314cfae202af------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*3T6npUsOaFMlPZxqXFyAfQ.jpeg</url>
            <title>Stories by Lokesh Kum on Medium</title>
            <link>https://medium.com/@codeloki?source=rss-314cfae202af------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 04:19:50 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@codeloki/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[Build ‘Agents’ that search web, does math and write python with Mixtral 8x-7B, Langchain and Groq.]]></title>
            <link>https://codeloki.medium.com/lets-build-agents-with-mistral-8x-7b-and-groq-3f364ca81221?source=rss-314cfae202af------2</link>
            <guid isPermaLink="false">https://medium.com/p/3f364ca81221</guid>
            <dc:creator><![CDATA[Lokesh Kum]]></dc:creator>
            <pubDate>Thu, 29 Feb 2024 15:24:34 GMT</pubDate>
            <atom:updated>2024-03-18T06:28:27.495Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hYzo5NE-9VNYdcth-CpxFw.png" /><figcaption>Created with AI — segmind/SSD-1B</figcaption></figure><p><strong>Introduction</strong><br>In the realm of cutting-edge technology, Langchain agents stand out as versatile and powerful tools, offering a myriad of applications across various domains. In this blog, we’ll delve into the fundamentals of Langchain agents, exploring what they are, how they operate, and why they are indispensable in today’s technological landscape. Additionally, we’ll highlight the utilization of Mistral 8x-7B, an exceptional open-source model, in the development and implementation of these agents.</p><p>Lets understand agents {quick summary} -</p><p><strong>What are they?<br>How do they work?<br>Why do we need them?</strong></p><p><strong>What are Agents?</strong></p><p>Agents are like digital helpers that can think for themselves (well, sort of!). They’re designed to tackle tasks and make decisions, but in a virtual world. Instead of following a fixed set of rules like a robot, agents use LLM to derive certain sequence of activities {which is termed as reasoning} — to determine which actions to take and in which order. <br>For example- When a question is framed infront of the LLM it tries to answer this with the data it has been trained on. In agents the concept is same {LLM will respond} but the order of actions will define the answer.</p><p><strong>How Do They Work?</strong></p><p>Agents function through a series of steps that involve passing information to the Language Model (LM) and receiving responses. Let’s break it down with an example:</p><p>Imagine you’re asking a question about cars. Initially, you might just ask, “What is a car?” But as you refine your query with specific roles or goals, the LM’s responses adapt accordingly:</p><ol><li><strong>Basic Question</strong>: “What is a car?”</li><li><strong>Question with Role</strong>: “As a Research Analyst, what is a car?”</li><li><strong>Question with Role and Goal</strong>: “As a Research Analyst, tasked with providing market analysis, what is a car?”</li><li><strong>Question with Role, Goal, and Backstory</strong>: “As a Research Analyst, with expertise in market trends, what is a car?”</li></ol><p>These prompts showcase how the LM’s responses evolve based on the context provided.</p><p>Now, let’s explore this process further with three hypothetical agents:</p><ol><li><strong>Agent Math</strong>: Handles mathematical questions.</li><li><strong>Agent Web</strong>: Browses the internet.</li><li><strong>Agent Wikipedia</strong>: Fact-checks information.</li></ol><p>When you ask a question, the LM responds using its pre-existing knowledge. However, this might not always provide the exact answer you’re looking for.</p><p>Within the agents framework:</p><ol><li><strong>Question Breakdown</strong>: The LM breaks down your question into actions.</li><li><strong>Action Assignment</strong>: These actions are then assigned to the appropriate agents, determined by the LM.</li><li><strong>Agent Action</strong>: Each agent, equipped with its specific functions, carries out its assigned task.</li><li><strong>Evaluation</strong>: The results from the agents are collected on a scratch pad for the LM to review.</li><li><strong>Response Generation</strong>: Finally, the LM synthesizes the information gathered from the agents to generate a comprehensive response.</li></ol><p>In essence, agents facilitate a dynamic exchange where questions are parsed, actions are executed, and responses are synthesized, all orchestrated by the LM to provide tailored and relevant information.</p><p><strong>Why do we need them?</strong><br>We need retrievers to process information and get to the correct answers.</p><p><strong>Prompting Techniques </strong>- Here you can ask questions to LLM and evaluate the LLM response based on the pre-training data. Here the scope to get the correct answer is very low. These are out of box model and can only generate back the data it is trained on.</p><p><strong>Finetuing your LLM </strong>- One of the approach to retrieve correct answers, is Fine tuning you LLM on curated data. In this approach we have to create a dataset. Also we have come up with a fine tuning strategty like <a href="https://huggingface.co/docs/trl/en/sft_trainer">SFT</a>, <a href="https://arxiv.org/pdf/2305.14314.pdf">QLoRa</a>, <a href="https://arxiv.org/pdf/2305.18290.pdf">DPO</a>. <em>{My humble request,please go through the research papers}</em></p><p><strong>Naive/Advanced RAG </strong>- With Vector db,SQL DB and NoSql, we try to retrieve information from these data sources.<br>Either we get the query created by LLM and execute on sql to retrive results or we create a vector db for text data to do similarity search for the documents.</p><p><strong>Agents</strong> — Imagine agents as smart helpers with specific roles, goals, and a memory of past interactions <em>{conversational}</em>. They break down all the information they receive into smaller, actionable tasks. These tasks trigger specific functions to work on them, retrieving necessary information. Once these tasks are completed, the Language and Logic Model (LLM) steps in to compile all the gathered information and craft a final answer.</p><p>In essence, agents are like organized assistants who process information, perform tasks, and then use their intelligence to give you the best possible answer.</p><p>Lets build it with langchain and Mistral 8x-7B &amp; <a href="https://wow.groq.com/">Groq</a><br>Groq provides good inference at very low cost/prices. As of now they are providing API access for free for 10 days.</p><pre>!pip install langchain-groq #langchain module<br>from langchain_core.prompts import ChatPromptTemplate<br>from langchain_groq import ChatGroq<br>llm = ChatGroq(temperature=0.5, #If the temperature is set to a lower value, the model tends to produce more deterministic response<br>               groq_api_key=&quot;your-groq-key&quot;,<br>               model_name=&quot;mixtral-8x7b-32768&quot;,<br>               max_tokens=20000) #Mistral context window is 32768 tokens, however we are limiting that to 20K tokens.</pre><p>Let’s explore how these functions operate independently. To start, we’ll examine the Wikipedia API. In simpler terms, we’ll take a look at how each of these processes works on its own.</p><pre>from langchain.utilities import WikipediaAPIWrapper<br>wikipedia = WikipediaAPIWrapper()<br>wikipedia.run(&#39;Langchain&#39;)</pre><p>PythonREPL is a convenient way to prototype ideas, debug code, or quickly verify syntax. Whether you’re debugging a tricky algorithm or exploring the behavior of a specific Python module, PythonREPL gives you the flexibility to experiment and iterate rapidly</p><pre>from langchain.utilities import PythonREPL<br>python_repl = PythonREPL()<br>python_repl.run(&quot;print(17*2)&quot;)</pre><pre>&#39;34\n&#39;</pre><p><em>DuckDuck Go API</em> offers a convenient way to browse the web and obtain quick responses on demand. It’s like having a search engine at your fingertips wherever you go.</p><p>Alternatively, <em>SerpAPI</em> and <em>Brave API </em>are also effective options, but they often come with costs and certain restrictions. These alternatives provide similar functionalities, but users may need to consider factors like pricing and usage limits before making a choice.</p><pre>from langchain.tools import DuckDuckGoSearchRun<br>search = DuckDuckGoSearchRun()<br>search.run(&quot;what is bitcoin price for today in INR?&quot;)</pre><pre>## Response<br>Bitcoin INR Bitcoin to Indian Rupee Data The BTC to INR conversion rate today is ₹4,616,107.90 and has increased by 8.17% in the last 24 hours. <br>Our converter updates in real time giving you accurate data every time you use it to make a conversion. <br>The current price direction of Bitcoin is increasing because BTC is up 32.27% in the last 30 days. <br>BTC/INR: Convert Bitcoin (BTC) to Indian Rupee (INR) 1 Bitcoin equals ₹4,546,842.15 Indian Rupee Buy Bitcoin BTC Last update: 5:39 PM, February 26, 2024 Refresh Bitcoin Markets Bitcoin is climbing this week. <br>The current price of Bitcoin is ₹4,546,842.15 per BTC. INR Last update: 9:15 PM, February 26, 2024 Refresh Bitcoin Markets Bitcoin is climbing this week. <br>The current price of Bitcoin is ₹4,630,611.03 per BTC. With a circulating supply of 19,638,837 BTC, it means that Bitcoin has a total market cap of ₹90,868,629,233,432.38. <br>Bitcoin Price Index (XBX) Ether Price Index (ETX) Basic Attention Token Price Index (BTX) ... <br>Convert BTC to INR: Bitcoin to India Rupee. To FIAT. To Crypto. 1 Bitcoin equals India Rupee. BTC. INR. <br>The live price of Bitcoin is $ 51,066.09 per (BTC / USD) with a current market cap of $ 1,002.63B USD. 24-hour trading volume is $ 28.74B USD. BTC to USD price is updated in real-time. <br>Bitcoin is -0.41% in the last 24 hours with a circulating supply of 19.63M. BTC Price History USD BTC Price Information 24h Low &amp; High Low: $ 50,773.21</pre><p>Having understood the mechanics of each function independently, let’s now merge them together and observe the outcomes.</p><pre>from langchain.agents import Tool<br><br>tools = [<br>    Tool(<br>        name = &quot;python repl&quot;,<br>        func=python_repl.run,<br>        description=&quot;useful for when you need to use python to answer a question. You should input python code&quot;<br>    )<br>]</pre><p>Let’s establish a clear definition for these tools to integrate them into the Agent framework.</p><pre>wikipedia_tool = Tool(<br>    name=&#39;wikipedia&#39;,<br>    func= wikipedia.run,<br>    description=&quot;Useful for when you need to look up a topic, country or person on wikipedia is the best website for fact check and any other details on any subject.&quot;<br>)<br><br>duckduckgo_tool = Tool(<br>    name=&#39;DuckDuckGo Search&#39;,<br>    func= search.run,<br>    description=&quot;Useful for when you need to do a search on the internet to find information that another tool can&#39;t find. Always be specific with your input.&quot;<br>)</pre><p>Lets define math tool, Here, an instance of the LLMMathChain class is created, with llm being passed as a parameter. This suggests that llm_math is an object representing a mathematical chain within the LangChain framework.</p><pre>from langchain.chains import LLMMathChain<br>from langchain.agents import Tool<br><br>llm_math = LLMMathChain(llm=llm)<br><br># initialize the math tool<br>math_tool = Tool(<br>    name=&#39;Calculator&#39;,<br>    func=llm_math.run,<br>    description=&#39;Useful for when you need to answer questions about math. Or you want to calculate something.&#39;<br>)<br></pre><p>Appending all the individuals functions to create one big tool</p><pre>tools.append(duckduckgo_tool)<br>tools.append(wikipedia_tool)<br>tools.append(math_tool)</pre><p>Initializing Agent type, there are various agent types and you can choose them as per your requirements. For<br>“zero-shot-react-description” agents can perform zero-shot tasks on an input, meaning the agent only considers the current interaction and has no memory. This agent type is optimized for chat models and can invoke tools that have multiple inputs. It’s different from other agent types, such as conversational ReAct agents, which have memory to remember previous conversation interactions</p><pre>from langchain.agents import initialize_agent<br><br>zero_shot_agent = initialize_agent(<br>    agent=&quot;zero-shot-react-description&quot;,<br>    tools=tools,<br>    llm=llm,<br>    verbose=True,<br>    max_iterations=10,<br>)</pre><p>Initializing functon call</p><pre>final_response = zero_shot_agent.run(&quot;Give me the best cars between a range of 40-60 lakhs in india.\<br>Also tell me how pricy other cars are. calculate the difference\<br>Also give me the working links about the information.\<br>I want minimum three links, one for text, one for video, and one for images&quot;)</pre><p>Intermediate Response</p><pre>## Intermediate Response<br><br>&gt; Entering new AgentExecutor chain...<br>This question involves finding the best cars in a certain price range in India, comparing their prices, and finding relevant links with different types of media. I will use a combination of DuckDuckGo Search and Wikipedia to find the information.<br><br>Action: DuckDuckGo Search<br>Action Input: &quot;best cars in india between 40-60 lakhs&quot;<br><br>Observation: The price for the best cars under 50 Lakh in India are: Toyota Fortuner price is Rs. 33.43 Lakh, Mercedes-Benz A-Class Limousine price is Rs. 42.8 Lakh, BMW X1 price is Rs. 49.5 Lakh and MINI ... With its impressive off-road capabilities, the new Toyota Fortuner is an ideal choice for adventure seekers and off-road enthusiasts, making it one of the best SUVs under 40 lakh in India. Engine capacity: 2.8-litre diesel/2.7-litre petrol engine. 2. Jeep Meridian. This is a curated list of the best cars under 40 lakhs in India. The list includes all body styles such as MPVs, SUVs, sedans and even pickups like the Toyota Hilux ... Rs 33.60 - 39.66 lakh. BYD Atto 3. Mid-size electric SUV. Rs 33.99 - 34.49 lakh. Citroen C5 Aircross. Mid-size SUV. Rs 36.91 - 37.67 lakh. MG Gloster. Full-size SUV. Hyundai Tucson. Introducing the Hyundai Tucson, a stylish and versatile SUV that offers a delightful combination of modern design and a commanding presence on the road. With a starting price of Rs. 28.63 Lakh and going up to Rs. 35.46 Lakh (Avg. ex-showroom), the Tucson is available in 8 variants to suit your preferences. Coupled with the zippy DCT transmission option, the Skoda Kushaq clocks 0-100 kmph in a mere 8.6 seconds. The three drive modes - normal, eco, and sport further amplify the overall driving experience. Adding to that, the Kushaq is one of the safest cars in India, with a commendable 5-star Global NCAP rating.<br>Thought:It seems like the search result provides a list of some of the best cars in India within the price range of 40-60 lakhs. However, the user also asked for a comparison of prices and the difference between these cars and others. To answer this question accurately, I need to gather more specific information about the prices of each car and calculate the difference.<br><br>Action: DuckDuckGo Search<br>Action Input: &quot;prices of cars in india 40-60 lakhs&quot;<br><br>Observation: ₹46.40 Lakh: BMW 3 Series: Petrol: Sedan: 15.3 - 16.3 km/l ₹43.60 Lakh - ₹53.90 Lakh ... Depending on the variant and customization options picked, the price of the Audi A6 in India ranges from about ₹60 lakhs to ₹68 lakhs. Key Specifications. Key Specifications: Details: Maximum Power ... Best 10 Made In India Cars - Price ... With its impressive off-road capabilities, the new Toyota Fortuner is an ideal choice for adventure seekers and off-road enthusiasts, making it one of the best SUVs under 40 lakh in India. Engine capacity: 2.8-litre diesel/2.7-litre petrol engine. 2. Jeep Meridian. The price for the best cars under 50 Lakh in India are: Toyota Fortuner price is Rs. 33.43 Lakh, Mercedes-Benz A-Class Limousine price is Rs. 42.8 Lakh, BMW X1 price is Rs. 49.5 Lakh and MINI ... Check List of New Cars in India 2022 New Car Brands New SUVs and Sedans Cars in India. ... New Kia cars in India 14. Kia Carens (Price: Rs. 9.60 lakh to Rs. 16.60 lakh) The Kia Carens is a Multi Utility Vehicle (MUV) with a SUVish design approach. ... (Price: Rs. 37.50 lakh to Rs. 40 lakh) The Skoda Kodiaq is a full-size SUV and looks premium ... This is a curated list of the best cars under 40 lakhs in India. The list includes all body styles such as MPVs, SUVs, sedans and even pickups like the Toyota Hilux ... Rs 33.60 - 39.66 lakh. BYD Atto 3. Mid-size electric SUV. Rs 33.99 - 34.49 lakh. ... most variants are available under this price point. Also, under 40 lakh you can own high ...<br>Thought:I now have a better understanding of the prices of cars in the 40-60 lakh price range in India. I can start comparing the prices of the cars mentioned earlier and calculate the difference.<br><br>Action: Calculator<br>Action Input: 5390000 - 4280000<br>Observation: Answer: 1110000<br>Thought:The price difference between the most expensive car mentioned (Audi A6 with a price of 53.90 lakhs) and the second most expensive one (Mercedes-Benz A-Class Limousine with a price of 42.8 lakhs) is 11.10 lakhs.<br><br>Thought: I have answered the user&#39;s question about the price difference between the two most expensive cars in the list. However, I still need to provide the user with information about the prices and features of other cars in this price range.<br><br>Final Answer: The prices of some of the best cars in India within the price range of 40-60 lakhs are as follows: Toyota Fortuner (33.43 lakhs), Mercedes-Benz A-Class Limousine (42.8 lakhs), BMW X1 (49.5 lakhs), Audi A6 (53.9 lakhs), and MINI (prices not mentioned). The price difference between the two most expensive cars mentioned (Audi A6 and Mercedes-Benz A-Class Limousine) is 11.10 lakhs.<br><br>Regarding the user&#39;s request for links, here are some relevant ones:<br><br>* Text link: &lt;https://www.carwale.com/best-cars-under-50-lakhs-in-india/&gt;<br>* Video link: &lt;https://www.youtube.com/watch?v=3vxvgCZ1o-Y&gt;<br>* Image link: &lt;https://www.google.com/search?q=best+cars+under+50+lakhs+in+india&amp;rlz=1C1GCEU_enIN836IN836&amp;source=lnms&amp;tbm=isch&amp;sa=X&amp;ved=0ahUKEwiB9Z6E5rX3AhXQg3MBHQz-CWQQ_AUIDCgD&gt;<br><br>&gt; Finished chain.</pre><p>Final Response</p><pre># Final Response<br><br>The prices of some of the best cars in India within the price range of 40-60 lakhs are as follows: <br>Toyota Fortuner (33.43 lakhs), <br>Mercedes-Benz A-Class Limousine (42.8 lakhs), <br>BMW X1 (49.5 lakhs), <br>Audi A6 (53.9 lakhs), <br>and MINI (prices not mentioned). <br>The price difference between the two most expensive cars mentioned (Audi A6 and Mercedes-Benz A-Class Limousine) is 11.10 lakhs.<br>Regarding the user&#39;s request for links, here are some relevant ones:<br>Text link: &lt;https://www.carwale.com/best-cars-under-50-lakhs-in-india/&gt;<br>Video link: &lt;https://www.youtube.com/watch?v=3vxvgCZ1o-Y&gt;<br>Image link: &lt;https://www.google.com/search?q=best+cars+under+50+lakhs+in+india&amp;rlz=1C1GCEU_enIN836IN836&amp;source=lnms&amp;tbm=isch&amp;sa=X&amp;ved=0ahUKEwiB9Z6E5rX3AhXQg3MBHQz-CWQQ_AUIDCgD&gt;</pre><p>Next time we will build an Agent that will do RAG and self rectify the responses for better accuracy.</p><p>Please share your thoughts, comments and do support me so that I can bring such articles to you apart from my work projects.</p><h3>Thanks, <br>LK</h3><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3f364ca81221" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Unveiling RAG: Revolutionizing Information Retrieval and Content Generation.]]></title>
            <link>https://codeloki.medium.com/unveiling-rag-revolutionizing-information-retrieval-and-content-generation-a230b95b234d?source=rss-314cfae202af------2</link>
            <guid isPermaLink="false">https://medium.com/p/a230b95b234d</guid>
            <dc:creator><![CDATA[Lokesh Kum]]></dc:creator>
            <pubDate>Tue, 13 Feb 2024 13:06:00 GMT</pubDate>
            <atom:updated>2024-02-13T13:06:00.395Z</atom:updated>
            <content:encoded><![CDATA[<h3>{Retrieval Augmented Generation}</h3><h3><strong>Section -1</strong></h3><p>LLMs, or large language models, serve as powerful “generator models” capable of generating text based on provided input. Whether you’re seeking creative output, business ideas, information retrieval, or code generation, LLMs are versatile tools ready to assist. Here’s how they work:</p><ul><li>You can ask LLM to generate poem or rap about a topic</li><li>You can ask LLM to generate business ideas</li><li>You can ask LLM to find out something from the huge chunk of text that you have provided</li><li>You can provide a scenarios and ask LLM to generate python code or SQL code or any other code.</li></ul><p>In all the above situation you will be doing the below mentioned things-</p><p>1.<strong>Prompt </strong>— What you want LLM to do {Rap about science like Jesse Pinkman from Breaking Bad}</p><p>2. <strong>Instructions </strong>— How you want LLM to respond back while working on your prompt follow while generating the text {Treat each line as one element of a list and avoid rapping about Gus Fring.}</p><p>3. <strong>Additional Information</strong> — You can provide LLM with extra context or references to aid its generation process. For example, you might provide Jesse Pinkman’s dialogue as “Ground Truth” for LLM’s reference.</p><h3><strong>Section -2</strong></h3><p>Technical Approach to Document Question Answering</p><p>When faced with the challenge of extracting information from a vast corpus of text, particularly in the context of mergers where details are spread across numerous documents, a systematic and technical approach is essential. Here’s how I would tackle this problem:</p><p>1.<strong>Text Chunking</strong>:</p><ul><li>Break down the 10,000 pages of information into manageable chunks. This can be done at various levels such as character-level, page-level, paragraph-level, or any other suitable granularity.</li><li>Develop code to automate this process, ensuring efficiency and accuracy in chunk segmentation.</li></ul><p>2.<strong>Text Embedding</strong>:</p><ul><li>Once the text is chunked, each chunk needs to be converted into a numerical representation for computational analysis. This is where embedding models come into play.</li><li>Utilize an embedding model to transform the textual data into vector spaces or numerical representations. Embedding models capture semantic information and relationships between words or phrases.</li><li>These numerical representations enable efficient comparison, similarity analysis, and retrieval of relevant information.</li></ul><p>3.<strong>Question Answering Model</strong>:</p><ul><li>Implement a question answering model capable of processing the numeric representations of both the question and the chunks of text.</li><li>This model should be trained on question-answer pairs or utilize techniques like BERT (Bidirectional Encoder Representations from Transformers) for contextual understanding.</li><li>The model should effectively retrieve relevant chunks of text based on the query and provide the corresponding answers.</li></ul><p>4.<strong>Scalability and Optimization</strong>:</p><ul><li>Consider scalability and optimization strategies to handle large volumes of text data efficiently.</li><li>Techniques such as parallel processing, distributed computing, or using pre-trained models can enhance performance and reduce computational overhead.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yNTetHzquQNYvVinNGWdCA.png" /><figcaption>Fig.1 Embedding model returning stream of vectors based on inputs</figcaption></figure><p>After segmenting 10,000 pages into individual chunks, you’ve fed them into an embedding model for conversion. The model has then produced a continuous stream of vectors, each representing a unique aspect of the text. These vectors were diligently captured and deposited into a specialized database known as vectorDB. Unlike conventional databases, vectorDB is tailored specifically to accommodate and manage high-dimensional vector data efficiently. Further insights into vectorDB will be provided in the subsequent chapter. In the meantime, refer to the accompanying image for a brief overview of vectorDB’s functionalities.</p><p>Normal Text</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/762/1*QdyRL1VD8e0oVCkDVz2ylw.png" /></figure><p>Tokens</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/931/1*EWWYPD90Atttz45AVs_9Dw.png" /></figure><p>Tokenization</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/954/1*h9v5pEsZuYvNAFIDobYaBQ.png" /></figure><p>Embedding or vector representation in “n” dimension</p><p>“Yo”: [0.21144, 0.63053, -0.020552, …] (N-dimensional vector)<br>“,”: [0.418, 0.24968, -0.41242, …]<br>“yo”: [0.48271, -0.31877, 0.5071, …]<br>“check”: [0.024074, 0.20902, 0.26508, …]<br>“it”: [0.60993, 0.67703, -0.01279, …]<br>“out”: [0.022701, 0.26177, 0.26458, …]<br>“I”: [0.41055, 0.89837, 0.11374, …]<br>“‘m”: [0.011283, 0.21698, 0.011683, …]<br>“breaking”: [0.086786, -0.19157, 0.11083, …]<br>“bad”: [-0.14788, 0.5065, -0.03543, …]<br>“with”: [0.009877, 0.42413, 0.046038, …]<br>“the”: [0.70627, 0.28223, 0.0071297, …]<br>“knowledge”: [0.12835, -0.42525, -0.16186, …]<br>“I”: [0.41055, 0.89837, 0.11374, …]<br>“possess”: [0.072617, -0.10663, 0.052415, …]<br>“,”: [0.418, 0.24968, -0.41242, …]<br>“Cooking”: [0.14478, 0.20488, 0.49475, …]<br>“up”: [0.12293, 0.80297, 0.42082, …]<br>“rhymes”: [-0.33935, 0.20912, 0.46348, …]<br>“hotter”: [-0.16856, 0.19461, -0.31518, …]<br>“than”: [0.27062, 0.045789, -0.6916, …]<br>“Walter”: [0.16942, 0.79715, -0.11892, …]<br>“White”: [0.059205, 0.111, -0.046535, …]<br>“‘s”: [0.42744, 0.16559, 0.75444, …]<br>“meth”: [-0.21734, 0.46515, 0.25789, …]<br>“success”: [-0.074976, -0.061078, 0.22443, …]<br>“.”: [0.43251, -0.1061, 0.012001, …]</p><h3><strong>RAG</strong></h3><p>Now, you’ve stored this comprehensive dataset in a specialized vector database (Vec DB), equipped to handle high-dimensional vector data efficiently. The process unfolds as follows:</p><p>Step 1: User queries the system with a question.</p><p>Step 2: Utilizing advanced similarity search algorithms, the system conducts a thorough comparison across all 10,000 documents, ranking them based on the similarity between the tokens present in the question and those within the documents.</p><p>Step 3: Documents are further prioritized based on their relevance, selecting the top 5 or 10 documents from the sorted list.</p><p>Step 4: These top-ranked documents are then fed into an LLM (Large Language Model) along with the original question. The LLM meticulously analyzes the text within these documents and generates a nuanced response tailored to the user’s query.</p><p>This intricate process ensures that users receive precise and informative answers drawn from the most relevant documents within the dataset, facilitated by the seamless integration of advanced search algorithms, vector database technology, and cutting-edge language models.</p><p>And that’s how you create a Simple RAG application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PlPKO_Cop4hwoAUbmku_kQ.png" /><figcaption>Created RAG Pipeline with Draw.io.</figcaption></figure><p>Our observations reveal several key insights into the capabilities of LLMs:</p><ol><li><strong>Text Generation</strong>: Leveraging LLMs for text generation, we integrated organizational data to produce contextually relevant outputs. This fusion of text generation with organizational data harnesses the LLM’s prowess in generating informative content tailored to specific contexts.</li><li><strong>Broad Knowledge Base</strong>: LLMs, having been trained on vast repositories of internet data, possess extensive knowledge encompassing movies, TV series, characters, and their dialogues. This rich understanding of cultural references empowers LLMs to infuse generated text with nuanced insights and references.</li><li><strong>Next Token Predictio</strong>n: Through its training process, LLMs have acquired the ability to discern patterns and anticipate subsequent tokens in a given sequence. This predictive capability enables them to generate coherent and contextually relevant text with remarkable accuracy.</li></ol><p>In Practice:</p><p>To demonstrate the utility of LLMs, we developed a rudimentary application based on the Retrieval-Augmented Generation (RAG) framework, facilitating question-and-answer interactions with datasets. In the subsequent chapter, we delve deeper into various strategies and methodologies for implementing RAG applications, exploring diverse approaches to leverage the full potential of this innovative framework.</p><p><strong>Kindly provide your feedback</strong>. Feel free to share your doubts and questions, and I’ll do my best to provide clear and concise explanations or solutions. Your understanding is my priority, so don’t hesitate to ask for clarification or assistance on any topic. Let’s work together to resolve any queries you may have!</p><ul><li><a href="https://huggingface.co/docs/transformers/main/en/tasks/prompting">LLM prompting guide</a></li><li><a href="https://www.pinecone.io/learn/vector-embeddings/">What are Vector Embeddings | Pinecone</a></li><li><a href="https://www.pinecone.io/learn/vector-database/">What is a Vector Database &amp; How Does it Work? Use Cases + Examples | Pinecone</a></li><li><a href="https://huggingface.co/learn/nlp-course/en/chapter6/5">Byte-Pair Encoding tokenization - Hugging Face NLP Course</a></li></ul><p>Thanks</p><p>LK</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a230b95b234d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Automating ML model for re-training and re-evaluation in AWS studio.]]></title>
            <link>https://codeloki.medium.com/automating-ml-model-for-re-training-and-re-evaluation-in-aws-studio-bbfb7b8f4608?source=rss-314cfae202af------2</link>
            <guid isPermaLink="false">https://medium.com/p/bbfb7b8f4608</guid>
            <dc:creator><![CDATA[Lokesh Kum]]></dc:creator>
            <pubDate>Fri, 23 Apr 2021 09:54:11 GMT</pubDate>
            <atom:updated>2021-04-23T10:15:44.175Z</atom:updated>
            <content:encoded><![CDATA[<p>This story will answer below questions.</p><ol><li>How to re-train your model on every week’s or 14 days data file?</li><li>What is lambda function(AWS) how to use it in ML projects?</li><li>Alternatives of this approach.</li></ol><p>So let’s understand that we created one ML model but now business decide to get this model on production and so we need couple of things before we move it out. This answer one very important question (and if you read all my other articles) that we are creating a statistical model and creating an application out of it which will provide insight/business to us.</p><p>Assuming you have created one ML model with good performance and now you wanted this model to get re-train every time you get new data .(frequency of the data file can be 1,2,3 or 4 week anything).</p><p><strong>Step1</strong>.Get your file on S3 and create a trigger on it.</p><p><strong>Step2</strong>.This will trigger the lambda function and the lambda will in-turn trigger the pipeline execution .</p><p><strong>Step3.</strong>Here you will provide the pipeline details.</p><p>Once your pipeline get executed the you can wait for the model to run and give you a evaluation value to understand it form . (You can refer to my other article where I shared, how we were able to provide the custom performance matrix to the model build . )</p><p>This is the idea now lets visualize it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2NxZEqPDf6FghUXuBEwdjA.png" /><figcaption>High Level arch</figcaption></figure><p>Lamda function here will get a notification from S3 and will trigger the pipeline .</p><p>Let’s look @ the implementation of S3 notification.</p><p><strong>Step4</strong>. Go to S3 and open and any bucket . On top you will see {Object, Properties, Permission..} → Click on properties and scroll down to ‘<strong>Create event notification</strong>’. Click on this and enter the needed details as shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dvuiJsTw6YK7Yo0Vz3lw-A.png" /></figure><p>In event type enter the details like when should this notification occur. If some file gets uploaded or copied to the bucket or deleted from bucket.. As of now we will choose put, post, copy. For any file coming from glue job will get copied to S3 bucket will initiate a event notification.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/949/1*2qBAumeqwo6WLIkAyzsX-A.png" /></figure><p>Now choose destination, as in what this notification will do. Over here when we choose lambda it will run the lambda as soon as the file get copied on the location. Save Changes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/988/1*-9HMyc-9tOSfHWMI97GCnQ.png" /></figure><p><strong>Step5</strong>. Go to Lambda function in AWS. Click on Create a lambda function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-Rfe6Jw01JY0mgEL39Au-g.png" /><figcaption>Choose python3.X as per your need.</figcaption></figure><p>After the function got created select the “Add trigger ” event and add the trigger . It will show you to enter the trigger config → Enter S3 there and then fill in the bucket details where we have created the event notification.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1017/1*_FtZLleGCcdwylbXBiBjfA.png" /></figure><p>After filling in all these details “Add” the trigger.</p><p>you can make changes to the below code as per your need.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RaOHnqQKpNvkVbGHsNEfOg.png" /></figure><p>Here the lambda function should have pipeline execution role which your cloud / solution architect will know how to add or refer to the aws articles on how to add the execution role .</p><p>Once this is done check it manually but uploading the file to S3 bucket, these couple of ways . Upload from pc or sagemaker notebook. Check the cloud watch or cloud console for the data points to get reflected.</p><p><strong>Scenario1</strong>. No data points are getting reflected under cloud watch . This means the event notification is not correctly setup . Reconfigure it and upload the sample file to get the data points here.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/572/1*TKgLGkf1GiZZ0ASQ722mLQ.png" /><figcaption>Data invocation points will be shown here.</figcaption></figure><p><strong>Scenario2</strong>. Data points are getting reflected but the pipeline is not getting executed. Check the name of the pipeline and execution role.</p><p>Once the pipeline gets executed and the model gets trained on the new file its time to check the evaluation score. Here the pre-defined metrics will show up the scores. Evaluate the model score with the last good model and take decision to approve or reject the model. see below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*21xp7KqxAIPMc9WbwC3iBA.png" /><figcaption>Custom metrics</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_xXLA466sEfeg3d0bXyDzQ.png" /><figcaption>Once the metrics looks good update the status of the model</figcaption></figure><p>Once you update the status of the model it will get implemented replacing the last good model so the endpoint can get the new model and hence we don&#39;t have to make changes to endpoint.</p><p>Alternative approach can be using step function of AWS and source code can be on our sagemaker notebook.</p><p>Thanks for your time</p><p>Lokesh</p><p>Data Scientist @Useready</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bbfb7b8f4608" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding AWS SageMaker studio with model building, training and deployment along with the…]]></title>
            <link>https://codeloki.medium.com/understanding-aws-sagemaker-studio-with-model-building-training-and-deployment-along-with-the-12287ac56188?source=rss-314cfae202af------2</link>
            <guid isPermaLink="false">https://medium.com/p/12287ac56188</guid>
            <dc:creator><![CDATA[Lokesh Kum]]></dc:creator>
            <pubDate>Mon, 19 Apr 2021 13:46:23 GMT</pubDate>
            <atom:updated>2021-04-23T10:14:22.230Z</atom:updated>
            <content:encoded><![CDATA[<h3>AWS SageMaker studio with statistical model building, training and deployment along with the hyperparameter tuning .</h3><blockquote>Implementation Runbook for sagemaker studio build, train and deploy.</blockquote><p>While working on a project, I was requested to build a statistical model on sagemaker studio. If you are someone who wants to know how or you are also facing the same situation than don&#39;t worry, I am here to guide you through it and by the end of this story you will be able to do the same . I hope that you guys already know, how to do statistical modelling, What are IAM roles, VPC, S3 buckets, how to make make and use them, what is your region when you are working in AWS. And please don’t mind my snips.</p><p><strong>Step1</strong>. Please sign to the AWS console and click on “Amazon SageMaker” or you can search the same in the search bar.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6UC8GzWzoYpIMUNLWv0Xsw.png" /><figcaption>*AWS console when you sign-in , these are mostly used services that’s why I am getting these many options*</figcaption></figure><p><strong>Step2. </strong>Click on the Amazon Sagemaker Studio.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vsvICqZ_hvQqICkePQcrUg.png" /></figure><p><strong>Step3. </strong>Add a user and name, click on the created user and go for ‘Open Studio’ .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1TtP_f2j8VzR11zQJegtvg.png" /></figure><p><strong>Step4</strong>. Create a project if you wanted to , or if already created</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/458/1*Z54KMmqjFZ54e_rG-5yFTA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X315dz6mEkdELqgwMHU9GQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/904/1*k1uT-IqaaPH2G6I1IRz-RQ.png" /><figcaption>Please Clone both build and deploy repository.</figcaption></figure><p>Choose the last one for build, train &amp; deployment. And provide relevant details to it like name and description of the project that your are working on.</p><p><strong>Step5</strong>. Once the project is build you can go to ‘Sagemaker Components and registries’ &amp; ‘File Browser’ on the extreme left and check your project .</p><p><strong>Step6</strong>. Go to ‘File Browser’ and click on your project . There you will see build and deploy modules created for your project. These are default ones from the AWS to build ‘<strong>model build</strong>’ pipeline and ‘<strong>model deploy</strong>’ pipeline . The default models are working(have build and deploy) and you will change these details with your project one get your project on Studio.</p><p><strong>Step7</strong>.Under Build you will see couple of files and folder structure lets explore them one by one.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/772/1*BJc-XE8iRugnwDO0kXJPTQ.png" /></figure><p>The codebuild yml file have details about the sequence of execution . Here you will see build commands and then “run-pipepline” which will run your pipeline , the default name of the pipeline project will be ‘abalone’ but you can edit this with the project name you wanted to run , as shown below. After this some of the roles are provided , then comes the region , pipeline and the model artifacts which will be saved when the code build will run .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Bzk2Y8iQ92I9bSswtBW_Pw.png" /></figure><p>Then the files ‘README’, ‘LICENSE’ , ‘CONTRIBUTING’, setup config and py and tox.ini will be later used. (we will not discuss these here.)</p><p><strong>Step8</strong>. Now lets explore the folder/dir pipelines. Here you will see below details</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/497/1*geOyA27zda400Ip1Rvf7FA.png" /></figure><p>And in abalone you will see the below files , the untitled one is created by me to see the working of any specific code if they fails .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/495/1*JBqrIYMRVyk5KMP2MfgZuA.png" /></figure><p>Lets talk about the python files inside the abalone directory .</p><p>preprocess.py is responsible for preprocessing of the code and evaluate.py is evaluating the abalone model performance. pipeline.py is the backbone or base for these code files to simultaneously . These are default one and have default code in it. What we will do is when we have a notebook code which has all the details from data ingestion , data wrangling, data preprocessing, model building and training with hyperparameter tuning, we will import these details from notebook to make the project on AWS.</p><p><strong>Step9</strong>. Open preprocess.py and check the code over there, the needed library are imported for the preprocessing of the data. To get the data from S3 bucket also if you are getting <strong>s3fs</strong> error so you can implement the below code as well.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/656/1*b4HDY2fHCZ5LE-I0Q-DgTQ.png" /><figcaption>Snapshot from preprocess.py</figcaption></figure><p>After this the preprocessing code starts, data ingestion from the pipeline.py. The input data below is the same data that is passed from the pipeline.py , here we can pass multiple files to preprocessing code if needed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/745/1*zCybThZWCo_2oHnoKglQ6Q.png" /><figcaption>Snapshot from preprocess.py</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TSKHA2HxfKYmXKG_ki-C6w.png" /><figcaption>Snapshot from pipeline.py</figcaption></figure><p>Here, in pipeline.py we can provide “<strong>ProcessingInstanceType</strong>” , “<strong>TrainingInstanceType</strong>” as the computation and also provide the address (INputDataUrl is the name assigned to it and default_value is the address)for the S3 bucket where the data files are kept. We will assign them here and will use it in preprocess.py.</p><p>Once the data is downloaded , you can start the preprocessing as per your code and requirements. once you are done with preprocessing, feature engineering and all the steps needed to build your final data for model building .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1022/1*550iVLj2nAepYrJSkhdupA.png" /><figcaption>Snapshot from preprocess.py</figcaption></figure><p>First the data is split in X_pre and y_pre and then got concatenated and again got split in train, validation and test and saved in the model artifact bucket on S3. Now the data is split and saved and will be used for model training but here we need hyperparameter tuning before the trained model get saved. I have tried the hyperparameter tuning from “jobs” as well outside the studio but that also didn&#39;t worked as the whole pipeline would not know whether the tuning is working or not, also we can hung the internal process for some time but that is also not a good solution for a running business as you can’t let your client know that every time your model is getting trained the model will be hung for some time. So we will tune the hyperparameter in studio only with the below code &amp; file adjustment.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/595/1*IfOSWaZHLHLm0bbejrQZuA.png" /></figure><p><strong>Step10</strong>. Create a file with any name, I have chosen parameter-tuning.py you can name it any thing , then open your pipeline.py and check the code where the training starts, join the parameter-tuning.py in the pipeline.py and now when the model spilt the data in train, test and validation we will first tune the hyperparameter and then pass it to model training .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1021/1*MMYAOUSrpCPFyPXNUqC42g.png" /><figcaption>Pipeline.py code where the training steps are defined</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CIMTrMLAah2TXJ5T_LmYUg.png" /><figcaption>Pipeline.py code where parameter-tuning.py is joined</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/863/1*snKvWE44TvQVkp_H4K9S9A.png" /><figcaption>Code for parameter-tuning.py</figcaption></figure><p><strong>Step11</strong>. Import the needed libraries for parameter tuning. Define a fn for hyperparameter tuning . The below code is for multiclass classification but you can make you own as per your need.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/776/1*PzyMEP5cYWvRFf1xWfT-Aw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1001/1*HBgaT63EcJmFaKQ8iLz1KQ.png" /><figcaption>Specify the grid search details.</figcaption></figure><p>Pull the data from S3. Read your training and validation data. Split in X and y . Provide the params</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/811/1*7-VAZKfx6VVpZG6IfgzY9Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/702/1*heyllj6J18gr2FD_81hO9Q.png" /></figure><p>Assign the values to the best parameter and get them in logger info for best practices and to check in the tuning which parameters are used .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/799/1*Swe2ZUUMEhy75d1NzyInHA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/673/1*z3566dYHgeuzcYoumC7Juw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/623/1*3BaErnrAdROwoxbpS8BOeg.png" /><figcaption>Save the model to the S3</figcaption></figure><p>One this is done check the pipeline execution will evaluate the model build and show us the evaluation score . The below snapshot is from the Components and registries → Pipelines</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/442/1*eDeFZi9yZFVtsnRMHRjloQ.png" /><figcaption>The screen shot from the execution where the model is evaluated and registered .</figcaption></figure><p>The logs for the successful execution will be checked on the cloud watch.</p><p>I will add more details here and upload all the code github and will share the link .Till the time please write to <strong>codelokiyt@gmail.com</strong> for any questions.</p><p>And yes you can follow J<a href="https://www.youtube.com/user/juliensimonfr">ulien </a>simon , <a href="https://www.youtube.com/user/AmazonWebServices">Amazon </a>web series , <a href="https://www.youtube.com/channel/UCGWZY-0pONnKmF98dhZy9CQ">Stephane </a>Maarek and <a href="https://www.youtube.com/watch?v=7QSsysGX14w">Emily </a>webber . However they haven&#39;t included this part in any of there videos. So I thought to share this.</p><p>Hope you’ll like it. Thanks</p><p>Lokesh <br>Data Scientist @Useready</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=12287ac56188" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>