<?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 Senapathi Thirumurugan on Medium]]></title>
        <description><![CDATA[Stories by Senapathi Thirumurugan on Medium]]></description>
        <link>https://medium.com/@senapathithirumurugan?source=rss-acd2a9a42361------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*wZ1MUN1E2VutOHIFXIBIJw.jpeg</url>
            <title>Stories by Senapathi Thirumurugan on Medium</title>
            <link>https://medium.com/@senapathithirumurugan?source=rss-acd2a9a42361------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 03:51:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@senapathithirumurugan/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[Empowering Your Apps with LLM Models: Building Your First App]]></title>
            <link>https://medium.com/@senapathithirumurugan/empowering-your-apps-with-llm-models-building-your-first-app-2748a828aba9?source=rss-acd2a9a42361------2</link>
            <guid isPermaLink="false">https://medium.com/p/2748a828aba9</guid>
            <category><![CDATA[openai]]></category>
            <category><![CDATA[chatbots]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[pinecone]]></category>
            <dc:creator><![CDATA[Senapathi Thirumurugan]]></dc:creator>
            <pubDate>Fri, 06 Oct 2023 16:59:15 GMT</pubDate>
            <atom:updated>2023-10-06T16:59:15.111Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>In this post, I will teach you how to write your first application using LLM. If you are new to LLM check my other blog <a href="https://medium.com/@senapathithirumurugan/empowering-your-apps-with-llm-models-a-beginners-guide-to-crafting-ai-generated-magic-79f752edd55b">Empowering Your Apps with LLM Models: A Beginner’s Guide to Crafting AI-Generated Magic</a> where I de-mistify various LLM concepts and walk through the architectural approach to building an LLM-based application</p><p>Here I will be giving a walkthrough of how to build an LLM application that answers based on the content present in the document that you provide</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ckPaFq1y5d3HZGs_.png" /></figure><h3>Prerequisites</h3><ol><li>Python 3.8 or above, I will be using a jupyter notebook as an IDE</li><li>Open AI API Key: If you are creating an account for the first time Open AI gives you free credits to play with. Once you have created an account visit <a href="https://platform.openai.com/account/api-keys">https://platform.openai.com/account/api-keys</a> to generate your API key. Remember you will not be able to retrieve the key again. Please regenerate a new one if you have lost the previous key and delete the old key for security purposes.</li><li>A Pinecone API Key: Login into your <a href="https://www.pinecone.io/">pinecone</a> account. Navigate to “API Keys” section &gt; Click “Create API Key” &gt; Give a name for your API Key &gt; Click “Create Key”. Store the generated key in some secured place</li></ol><p>Download the product data and place it in your working directory</p><p><a href="https://github.com/shopifypartners/product-csvs/blob/master/apparel.csv">product-csvs/apparel.csv at master · shopifypartners/product-csvs</a></p><p>We will be using the above data as the test dataset for building our application.</p><p>Install the following packages</p><pre>pip install --upgrade langchain openai pinecone-client<br>pip install unstructured tiktoken</pre><h3>Ingestion Phase</h3><p>Now that we have all the pre-requisites ready, it is time to prepare and populate the vector database for consumption</p><p><strong>Step 1:</strong> Import the necessary libraries</p><pre>import os<br>import pinecone<br>import openai<br>from langchain.vectorstores import Pinecone<br>from langchain.document_loaders.csv_loader import CSVLoader<br>from langchain.embeddings.openai import OpenAIEmbeddings<br>from langchain.text_splitter import RecursiveCharacterTextSplitter<br>from langchain.embeddings.openai import OpenAIEmbeddings<br>from langchain.chains.question_answering import load_qa_chain<br>from langchain.llms import OpenAI</pre><p><strong>Step 2:</strong> Initialise OpenAPI and Pinecone and Vector Embedding Model</p><pre># You need to get the key from the safe place where you saved it earlier<br>os.environ[&quot;OPENAI_API_KEY&quot;] = &quot;&lt;YOUR OPEN API KEY&gt;&quot;<br><br># You may find the API Key and Environment value in API Keys section of pinecone dashboard<br>pinecone.init(      <br>    api_key=&#39;&lt;YOUR PINECONE API KEY&gt;&#39;,      <br>    environment=&#39;&lt;ENVIRONMENT&gt;&#39; <br>)    <br><br>embeddings = OpenAIEmbeddings(model_kwargs={&quot;model_name&quot;:&quot;ada&quot;})</pre><p>We have used the “ada” Embedding model from OpenAI</p><p><strong>Step 3:</strong> It&#39;s time to create your pinecone index. An index is the highest level of organisational unit in Pinecone. The data in one index is isolated from data in other indexes.</p><pre># Name of the pinecone index<br>index_name = &quot;product-index&quot;<br># Number of dimensions in the vector that will be stored in this index<br># Since we are using &quot;ada&quot; Embedding Model from OpenAI, That model gives an output of 1536 dimensions. This can be found in their documentation<br>index_dimension = 1536<br># Distance metric will be used to find the similarity between two vector data<br># There are multiple metrics available in pinecone. You can choose any of &quot;cosine&quot;, &quot;dotproduct&quot;, &quot;euclidean&quot;. <br>distance_metric - &quot;cosine&quot;<br>pinecone.create_index(name=index_name, dimension=index_dimension, metric=distance_metric)</pre><p>Once you run the above code you should be able to notice the newly created index in the Pinecone console</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kXk34Hwxqf-YDcG3_WVTDQ.png" /></figure><p>Congratulations! You have created your first vector database</p><p><strong>Step 4:</strong> Read the CSV file</p><p>There are multiple ways to read the CSV file, You can use the ‘csv’ library available in Python but I will be using modules from the langchain library</p><p>For starters hearing the langchain library for the first time, it is called the “AI Developers Best Friend”. Langchain abstracts a lot of repeatedly performed tasks while building an LLM application and gives us easy, re-usable functions for us to work with</p><pre>loader = CSVLoader(file_path=&quot;./apparel.csv&quot;)<br>csv_data = loader.load()</pre><p><strong>Step 5: </strong>Populate the document in the pinecone index</p><p>Now that we have read the CSV file, I will be using another function in Langchain to insert the document into the Pinecone index that we created earlier</p><pre>index = Pinecone.from_documents(csv_data, embeddings, index_name=index_name)</pre><p>After you run the above code, Check the data by clicking on the index name in the Pinecone console.</p><p>Tada !! You can now see that the data is successfully inserted</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oGtou7M9Lq8laqadZtBe_g.png" /></figure><p>But WAIT, The data from my CSV is present in the metadata section and I am seeing an array of decimal numbers in the place of value. What is happening?</p><p>This is what I call the magic of Langchain, it does a series of steps under the hood before inserting the data into the index</p><ol><li>First, It reads data from CSV row by row</li><li>Uses the embedding model we passed as an input to the function to convert the row data into vector embeddings. This is the array of numerical values that you find in the console. Also if you check the length of the array, it will be 1536. This was the dimension value we mentioned while creating the index</li><li>Inserts the vector embedding into the pinecone index as a document (it&#39;s the least organisational unit of pinecone) with a unique id and metadata about the document (Usually consists of source and text value. Here we have row number as well since it is a CSV document)</li></ol><p>Now our ingestion stage is complete and is ready to be used for querying</p><p><strong>Optional Step:</strong> Chunk the document</p><p>If the input document is too large we need to split the document into smaller pieces. There is another function in Langchain to achieve this</p><pre># Here the first argument is the actual document that need to be chunked<br># chunk_size refers to the number of tokens to have per chunk<br># chunk_overlap refers to the number of tokens to overlap from adjacent chunks to estabilish some relativity between the chunks<br>def split_docs(documents, chunk_size=1000, chunk_overlap=20):<br>  text_splitter = RecursiveCharacterTextSplitter(chunk_size=chunk_size, chunk_overlap=chunk_overlap)<br>  docs = text_splitter.split_documents(documents)<br>  return docs<br><br>docs = split_docs(csv_data)</pre><h3>Application Phase</h3><p>Now that we have successfully populated the database. We can build an application where we can ask questions about the products we just populated.</p><p>Let&#39;s initialize the LLM Model</p><pre>model_name = &quot;gpt-4&quot;<br>llm = OpenAI(model_name=model_name)<br>chain = load_qa_chain(llm, chain_type=&quot;stuff&quot;)</pre><p>Now let&#39;s create helper functions to get similar documents based on the user query from the vector database</p><pre>def get_similiar_docs(query, k=2, score=False):<br>  if score:<br>    similar_docs = index.similarity_search_with_score(query, k=k)<br>  else:<br>    similar_docs = index.similarity_search(query, k=k)<br>  return similar_docs</pre><p>Here k = 2 means the function will return the top 2 results that are similar to the user query. Next, let&#39;s create a function to answer the user query</p><pre>def get_answer(query):<br>  similar_docs = get_similiar_docs(query)<br>  answer = chain.run(input_documents=similar_docs, question=query)<br>  return answer</pre><p>That’s all my friends It is time for the finale, Now let&#39;s ask a question about a random product and see if the model is able to answer it</p><pre>query = &quot;What is the price of ocean blue shirt?&quot;<br>answer = get_answer(query)<br>print(answer)<br><br># The price of the ocean blue shirt is 50.</pre><p>Yay! It&#39;s time to celebrate. We have written our first LLM application. This code just serves as the initial step, leading to a realm of limitless possibilities fueled by your imagination.</p><p>If you love this content do follow my medium account for more such upcoming blogs and connect with me on <a href="https://www.linkedin.com/in/senapathi-t/">LinkedIn</a> where I share regular updates about my work.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2748a828aba9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Empowering Your Apps with LLM Models: A Beginner’s Guide to Crafting AI-Generated Magic]]></title>
            <link>https://medium.com/@senapathithirumurugan/empowering-your-apps-with-llm-models-a-beginners-guide-to-crafting-ai-generated-magic-79f752edd55b?source=rss-acd2a9a42361------2</link>
            <guid isPermaLink="false">https://medium.com/p/79f752edd55b</guid>
            <category><![CDATA[openai]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[vector-database]]></category>
            <category><![CDATA[chatgpt]]></category>
            <dc:creator><![CDATA[Senapathi Thirumurugan]]></dc:creator>
            <pubDate>Fri, 06 Oct 2023 16:56:55 GMT</pubDate>
            <atom:updated>2023-10-06T16:56:55.545Z</atom:updated>
            <content:encoded><![CDATA[<p>Building applications using large language models (LLMs) has been a trending topic in the field of artificial intelligence and software development. In this post, I will help you de-mistify the elements that contribute to building applications using LLM and how one can get started in the amazing world of Generative AI</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*OcOARmpH6zsMK-W7" /></figure><h3>What is an LLM Model?</h3><p>Large Language Models (LLMs) are a class of artificial intelligence models designed to understand and generate human language.</p><p>LLMs are typically pre-trained on extensive and diverse text data, which can include books, articles, websites, and social media posts. During this pre-training phase, the models learn the statistical relationships and linguistic structures present in the data.</p><p>Some of the LLM models available in the market are</p><ul><li><a href="https://openai.com/gpt-4">GPT-4</a></li><li><a href="https://openai.com/blog/chatgpt">GPT 3.5</a></li><li><a href="https://www.anthropic.com/index/introducing-claude">Claude v1</a></li><li><a href="https://falconllm.tii.ae/">Falcon</a></li></ul><p><a href="https://openai.com/blog/chatgpt">GPT 3.5</a> and <a href="https://openai.com/gpt-4">GPT-4</a> are some of my favourites and it is easy to get started to build your own application</p><h3>Where to use LLM Models?</h3><p>LLMs are not a solution to all the problems we face. They excel at natural language understanding, which means they can comprehend and generate human-like text. This capability makes them valuable for applications that require text-based interactions, such as</p><ol><li>Content Generation</li><li>Translation and Localization</li><li>Code Generation</li><li>Personalization</li><li>Sentiment Analysis</li><li>Data Analysis</li></ol><h3>Glossary:</h3><p>Before we proceed it is ideal to understand and familiarise ourselves more with the terms frequently used while talking about LLM models</p><ol><li><strong>Prompt</strong>: A prompt for an LLM is like a question or sentence that tells the AI what you want to talk about or get information on.</li><li><strong>Token</strong>: In natural language processing, a “token” is the smallest unit of text that a model reads. In English, a token can be as short as a single character or as long as a word. For example, the sentence “Hello World !!” consists of six tokens: [“Hello”, “ ”, “World”, “ ”, “!”, “!”]. Models often count tokens to keep track of the length of the input and output, as token limits can affect the cost, time, and performance of model interactions.</li><li><strong>Temperature</strong>: It is a hyperparameter value that controls the randomness of the model&#39;s response. The value of 0 means that the model will always output the highest probability token.</li></ol><h3>Typical LLM Application Architecture</h3><p>Since the LLM models are already trained on vast amounts of data, it eliminates the huge amount of work involved in model training and deployment but at the same time, nobody wants to deploy a clone of ChatGPT in their application. Businesses are looking to integrate these LLM models in such a way that they use the organisations&#39; existing data knowledge and generate content using it.</p><p>There are 2 ways to achieve this</p><ol><li><strong>Prompt Engineering:</strong> This is probably the easiest way to get started, All you have to do is ask the right question and make the LLM models provide you with the expected answer. During this time the models are provided with additional context to answer your question.</li><li><strong>Fine-Tuning:</strong> As the name says, fine-tuning is all about further training the underlying model itself by modifying the set parameters and training it to respond differently. While fine-tuning gives control over the behaviour, This approach is resource-intensive and requires deep ML skills.</li></ol><p>Both prompt engineering and fine-tuning play vital roles in improving the effectiveness of large language models in producing accurate and customized responses. Nevertheless, prompt engineering stands out as the preferred approach. This preference arises from its direct impact on input context, resulting in more controlled and contextually relevant outputs. Additionally, users don&#39;t need to be technically strong and can acquire prompt engineering skills faster.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*f2f6mJrFvkYxd_UD" /><figcaption>Prompt Engineering Workflow</figcaption></figure><p>Most prompt engineering applications consist of 2 layers</p><ol><li>Ingestion layer: In this layer large amounts of data are ingested into the system. The data could be anything (Structured, Semi-Structured or Unstructured). Most widely used data are unstructured, especially text data like Docs, PDF, Web Scrapping Data etc. The data is chunked into smaller bits and converted to vectors using embedding models and is stored in a vector database</li><li>Application layer: This layer interacts with the end-users, where they place a request. The request query is then converted to vectors and is compared with the data in the vector database for similarity. The output from the vector database is passed along with the user query to the LLM model to get an answer</li></ol><h3>What is vector embedding and why should we do it?</h3><p>Vector embedding is a mathematical representation of a word, phrase, sentence, or any piece of text. Embeddings are used to convert textual data into a format that machine learning models can work with more effectively.</p><p>There are different mathematical formulas that are used to calculate the distance metrics in vector embedding and finding similarity but let me explain on a high level what these numbers mean.</p><ul><li>For simplicity, let’s focus on two words: “dog” and “cat.” These words are likely to be similar in meaning because they are both related to pets.</li><li>Let’s assume that “dog” is represented by the vector [0.8, 0.4, 0.6] and “cat” by the vector [0.7, 0.45, 0.55].</li><li>In this case, if the distance metric is calculated to be 0.92, it suggests a high degree of similarity between “dog” and “cat.”</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*HrnQhzX1Cm0Mdngm.png" /><figcaption>Vectors in Multi Dimension Space [<a href="https://weaviate.io/blog/distance-metrics-in-vector-search">source</a>]</figcaption></figure><p><strong>Note:</strong></p><ul><li>A distance metric value close to 1 indicates high similarity and a value close to -1 indicates dissimilarity.</li><li>The vector values for each word are just an example and are usually generated by a model that is trained on a large corpus of text data, which results in the creation of meaningful vector representations for words based on the context in which they appear.</li></ul><p>There are a lot of embedding models available in the market. I found that the embedding model provided by <a href="https://platform.openai.com/docs/guides/embeddings/what-are-embeddings">OpenAI</a> is easy to use and has a lot of integrations with third-party frameworks</p><h3>Why chunk documents?</h3><p>Most LLM models developed by various companies are released as a platform as a service, Where developers and businesses can build products on top of it. In order to prevent abuse there are some rate limits enforced on the amount of input that one user may provide at a time.</p><p>Due to these limits, we will not be able to pass a large text input and seek answers from it. That is why large data is split into chunks of smaller data and stored.</p><p>Whenever the user asks a question, the chunks that are related to the question are retrieved from a vector database and passed to the LLM model along with the user query</p><h3>Why do we need vector databases?</h3><p>Vector Database is a database system designed to efficiently store, manage, and query data in the form of vectors or vectorized data. In a vector database, data is represented as vectors, which are arrays of numerical values or other data types</p><p>Well, I had a question if vectors are nothing but arrays of numbers why not store them in a conventional database, Most of the SQL databases support array datatype and if that is not a right fit, we can always store them in NoSQL databases. On further research, I understood that the problem was not how data can be stored but how it can be retrieved in milliseconds based on similarity.</p><p>Vector Databases use the Approximate Nearest Neighbor (ANN) algorithm to index data based on vectors. ANN algorithms organize indexes so that the vectors that are closely related are stored next to each other and hence retrieved at a faster speed.</p><p>Thus vector databases are particularly well-suited for applications that require similarity search, machine learning, and analytics on high-dimensional data.</p><p>Some of the vector databases available in the market are</p><ul><li><a href="https://www.pinecone.io/">Pinecone</a></li><li><a href="https://www.pinecone.io/">Chroma</a></li><li><a href="https://aws.amazon.com/blogs/big-data/amazon-opensearch-services-vector-database-capabilities-explained/">OpenSearch</a></li><li><a href="https://milvus.io/">Milvus</a></li><li><a href="https://milvus.io/">Weaviate</a></li></ul><h3><strong>Conclusion</strong></h3><p>I hope you found my beginner’s guide to creating LLM applications helpful. Do watch out for my next blog where I help you in writing your first application using LLM.</p><p>Please feel free to connect with me via <a href="https://www.linkedin.com/in/senapathi-t/">LinkedIn</a></p><h3>References</h3><ol><li><a href="https://weaviate.io/blog/distance-metrics-in-vector-search#distance-metrics">https://weaviate.io/blog/distance-metrics-in-vector-search#distance-metrics</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=79f752edd55b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>