{"id":9662,"date":"2024-02-19T06:03:14","date_gmt":"2024-02-19T11:03:14","guid":{"rendered":"https:\/\/blogs.pacasmayo.com\/felipe\/?p=9662"},"modified":"2024-02-19T06:50:28","modified_gmt":"2024-02-19T11:50:28","slug":"demystifying-retrieval-augmented-generation-rag-a-step-by-step-guide-to-chatgpt-with-context","status":"publish","type":"post","link":"https:\/\/blogs.pacasmayo.com\/felipe\/2024\/02\/19\/demystifying-retrieval-augmented-generation-rag-a-step-by-step-guide-to-chatgpt-with-context\/","title":{"rendered":"Demystifying Retrieval-Augmented Generation (RAG): A Step-by-Step Guide to ChatGPT with Context"},"content":{"rendered":"\n<p id=\"b515\">Large Language Models (LLM), especially the ones from the \u201cgenerative\u201d kind, have taken the world by storm. And, Retrieval-Augmented-Generation (RAG) has become very popular as the predominant technique for LLMs to answer questions based on our custom information.<\/p>\n\n\n\n<p id=\"393c\">I understand there are plenty of tools in the market today to help setup RAG in minutes, but before using the fancy tools that hide all the implementation details, you might want to understand what is really going on under the surface. And, there is no better way to learn how the wheels turn, than to write the code yourself. So, here is a step by step, with thorough explanations.<\/p>\n\n\n\n<p id=\"750c\">I will be using the OpenAI python library.<\/p>\n\n\n\n<p id=\"d281\">Requirements:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Docker (or a python environment with langchain, openai, pandas, and numpy)<\/li>\n\n\n\n<li>OpenAI API access<\/li>\n\n\n\n<li>Interest in understanding how things work<\/li>\n<\/ul>\n\n\n\n<p id=\"07cf\">If using the Docker route, you can use this Dockerfile:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/catsdev\/aa61704b5e8b0f9826612cfa485a80d0.js\"><\/script>\n\n\n\n<p id=\"f140\">Build and run it mounting the local folder where you will write your python code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker build . --tag rag<br>docker run --rm -it -v $(pwd):\/src rag bash<\/code><\/pre>\n\n\n\n<p id=\"73d7\">From here on, if you are using the Docker route, all the code writing should be done in the folder outside of Docker, and the running in the shell of the container, Visual Studio Code makes this very easy, but it can be done manually, too.<\/p>\n\n\n\n<p id=\"33f3\">So, the first thing you will need is to setup your variables in the environment. Linux flavors:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export OPENAI_API_KEY=\"xxxx\"<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"ceca\">Interacting with ChatGPT<\/h1>\n\n\n\n<p id=\"2b98\">Perfect, so now we will just chat with ChatGPT via python.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/catsdev\/6668d20225dae35a93a36d0c2e6e9c00.js\"><\/script>\n\n\n\n<p>As you can see, writing code to chat with ChatGPT is extremely simple. But, at this point, ChatGPT is just answering from its own knowledge background (billions of training documents). In order for ChatGPT to use our own knowledge base to answer the questions we need to find a way to inject our data into that conversation. But it is not as easy. ChatGPT cannot be \u201cfed\u201d large amounts of data, so we need to find the most relevant documents ourselves, and provide them to ChatGPT as context to the question. And we would prefer to accomplish it without having to implement yet another LLM, nor having to re-train or fine-tune these models every time our documents change. Let me show you how to do just that.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"c322\">Embedding vectors<\/h1>\n\n\n\n<p id=\"2870\">In order to get ChatGPT to answer the question based on your content, first you need to understand a little bit about embeddings.<br>An embedding is a vector representation of the semantics of a string. It is what enables us to use mathematical concepts, tools and techniques to manipulate and compare strings. Let me show you an example:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/catsdev\/13e50a7fb4d3f34332c332344edf814d.js\"><\/script>\n\n\n\n<p id=\"30b8\">The first thing you will notice is that the content of \u201cresults\u201d is huge. It is a very large array of floating point numbers that represent the text. Those numbers are not an encrypted or codified version of the text, they represent the semantic meaning of the text in high-dimensional vectors.<\/p>\n\n\n\n<p id=\"30b8\">To attempt to understand these, let\u2019s imagine that I want to represent people with numbers. I think I could do this easily with age. I can represent a 2 year old person with a number 2 and a 4 year old with a number 4. Since we only have one dimension, I could represent them as lines and it would look something like this:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"297\" height=\"85\" src=\"https:\/\/blogs.pacasmayo.com\/felipe\/files\/2024\/02\/image-4.png\" alt=\"\" class=\"wp-image-9667\"\/><\/figure>\n\n\n\n<p id=\"66c2\">But then, if I would like to add a height component in feet, the representation would have to add a second dimension and would look like this:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"332\" height=\"218\" src=\"https:\/\/blogs.pacasmayo.com\/felipe\/files\/2024\/02\/image-3.png\" alt=\"\" class=\"wp-image-9666\" srcset=\"https:\/\/blogs.pacasmayo.com\/felipe\/files\/2024\/02\/image-3.png 332w, https:\/\/blogs.pacasmayo.com\/felipe\/files\/2024\/02\/image-3-300x197.png 300w\" sizes=\"auto, (max-width: 332px) 100vw, 332px\" \/><\/figure>\n\n\n\n<p id=\"c8d9\">The information now has two dimensions, and while we could just draw a point in the intersections of the the age and height, we are really more interested in seeing the vector (remember, magnitude and direction). In this case, the 2 year old is represented by a vector of two dimensions:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/catsdev\/dbee82e64a00089d8f65774f44653656.js\"><\/script>\n\n\n\n<p id=\"f6c0\">Things get a tad more complex if I add a third dimension: weight in kilograms. The vector now has a different value in each dimension, and the graph is now tridimensional:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"283\" height=\"193\" src=\"https:\/\/blogs.pacasmayo.com\/felipe\/files\/2024\/02\/image-5.png\" alt=\"\" class=\"wp-image-9668\"\/><\/figure>\n\n\n\n<script src=\"https:\/\/gist.github.com\/catsdev\/80ac7365d7919fe2749545a970385933.js\"><\/script>\n\n\n\n<p id=\"0190\">Notice that direction for a two dimensional vector was represented by the angle of the line, but that is not the case beyond two dimensions. Adding a fourth dimension makes it very difficult to draw in a two dimensional system like our screens, and moves into a realm that is difficult to visualize, yet, the analysis of the description of these two individuals into four different dimensions that can be represented by numbers is what allows us to use existing algorithms and tools to manipulate this non-numeric objects.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/catsdev\/bb31f31e5c1d2ad4f3bec2d6d185bb3f.js\"><\/script>\n\n\n\n<p id=\"08bb\">Now, back to the results we got back from the request to create a vector embedding of our text, Large Language Models (LLM) identify thousands of dimensions that the text is evaluated against and return these large number of coordinates that describe the text in a high-dimensional vector. In the case of the text-embedding-ada-002 model, it returns a vector of 1,536 dimensions.<br>Also, notice that the vectors for both texts have the same amount of dimensions, the length of the text does not affect the amount of dimensions it has been evaluated against. That is key, because it will make comparison of the two vectors possible, they represent the text against the same dimensions.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"b0c2\">Vector similarity<\/h1>\n\n\n\n<p id=\"3217\">Now that we have successfully created embeddings, we need to figure out how they will help us accomplish AI tasks. First of all, let\u2019s notice that what we have done is to create a representation of the semantic meaning of those texts using vectors. Second, vectors are mathematical concepts, and we have mathematical ways to deal with them. For example,<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/catsdev\/c397ea177dab5ce4723263dba121a6ce.js\"><\/script>\n\n\n\n<p id=\"047e\">Notice that I compared two vectors of two dimensions, and that both have the same numbers, two and five, yet, the similarity between them is only 68%.<\/p>\n\n\n\n<p id=\"047e\">Let\u2019s increase the dimensions:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/catsdev\/0155aae02a89a31a46b4b5ab954cb140.js\"><\/script>\n\n\n\n<p id=\"86e4\">The similarity is even lower, these two vectors are definitely going into different directions. Remember that even though we have two tens, two fives, etc., the comparison is by dimension, so the function is comparing 1 to 10 in the first dimension, 2 to 5 in the second, and so forth. The comparison by dimension finds these two vectors to be vastly different.<br>The point will be reinforced if we use totally different numbers in both vectors, but that keep similar direction and magnitude:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/catsdev\/2599071d0f126b17fce4c0a162b92852.js\"><\/script>\n\n\n\n<p id=\"795c\">As you can see, these completely different sets of numbers are actually 98% similar because their direction and magnitude are almost identical.<br>Now let\u2019s compare the values of the 2 and 4 year olds:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/catsdev\/c4b72d3f5fb5d70ba09ce5a26913eb76.js\"><\/script>\n\n\n\n<p id=\"916b\">Wow, greater than 99% similar, what the cosine_similarity function is telling us is that these two vectors represent almost the same thing. If you think about it, both vectors represent a child at different moments of their life, thus the similarity.<br>What about comparing the 2 year old vector to that of an adult? Well, that proves to be very dissimilar:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/catsdev\/c5cdc6e4309ed7303effe6cc3eb3eb86.js\"><\/script>\n\n\n\n<p id=\"9294\">These two individuals are so different that this superficial comparison over only four dimensions considers them to be very far apart.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"01ce\">Embedding similarity<\/h1>\n\n\n\n<p id=\"9703\">Now that we have a better understading of how two vectors can be compared, let\u2019s go back to the two vectors we got from OpenAI\u2019s\u00a0<code>text-embedding-ada-002<\/code>\u00a0and compare them:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/catsdev\/f5587f9d4547e37f809596867b9070b8.js\"><\/script>\n\n\n\n<p id=\"7587\">When comparing embedding vectors for text similarity, even as dissimilar as these were, we will observe that, semantically, their similarity is usually above 70%, so a 72% is low.<\/p>\n\n\n\n<p id=\"9a6d\">So, let\u2019s change the texts to some similar texts:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/catsdev\/c3009b66dc28c87078ab6ebb3899ce17.js\"><\/script>\n\n\n\n<p id=\"e3dd\">Ok, these two texts share three words and are only different in one, yet the similarity is only 86%.<br>How about two sentences that pretty much mean the same thing:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/catsdev\/f857f8f6629d7075d5f985bb7b3f2a16.js\"><\/script>\n\n\n\n<p id=\"1413\">These sentences only share one word, yet they are considered 96% similar, because they basically mean the same thing.<\/p>\n\n\n\n<p id=\"2122\">If we pass two unrelated sentences:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/catsdev\/a3d4e2a570353af79ab2fe6308c273e2.js\"><\/script>\n\n\n\n<p id=\"1332\">As expected, slightly above 70%, but if we add some sort of connection between the two sentences:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/catsdev\/e99d0c09e54315aa0071d34045dc4056.js\"><\/script>\n\n\n\n<p id=\"9940\">The cosine similarity finds a stronger relation between the last two sentences because of the connection to the United States.<\/p>\n\n\n\n<p id=\"5056\">Also, it should be noted that, depending on the model, the semantic vectorization of text can also work across languages:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/catsdev\/a971a2799ba133e6068196cd5fee87e0.js\"><\/script>\n\n\n\n<p id=\"9481\">Some models work better than others across languages, these ones were done using the&nbsp;<code>text-embedding-ada-002<\/code>&nbsp;model from OpenAI.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"3786\">Searching for similar content<\/h1>\n\n\n\n<p id=\"e4f4\">Excellent, now we know how to ask ChatGPT a question, we also know how to create vector embeddings of text, and we now understand how those vectors can be used with mathematical principles to compare their similarity.<br>Now we\u2019ll get into the process of searching for similar content.<br>The process is simple, but let\u2019s consider one more thing. Creating embeddings for a large list of documents is expensive in time and money, so we should find a way to create them only once, or at least only as often as the content changes. That said, the process should be pretty simple:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1605\">Prep<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>go through the list of all of your documents<\/li>\n\n\n\n<li>get an embedding for each document<\/li>\n\n\n\n<li>save the embedding along with a pointer to the corresponding document<\/li>\n\n\n\n<li>persist this list (on a vector database, or a persisted Pandas dataframe)<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"cb29\">On every question<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>get an embedding for the question (or subject)<\/li>\n\n\n\n<li>go through the saved list of embeddings\/documents<\/li>\n\n\n\n<li>compare the embedding of the document with the embedding of the question<\/li>\n\n\n\n<li>save the comparison score with the document<\/li>\n\n\n\n<li>Now, take all the documents and filter them based on some threshold (&gt;75% for example)<\/li>\n\n\n\n<li>Sort the matching ones by the score<\/li>\n\n\n\n<li>Pick the top one\u2026 and the document will probably contain an answer to your question<\/li>\n<\/ul>\n\n\n\n<p id=\"92d1\">For this exercise I won\u2019t be using a vector database, although it is definitely the way to go for a production deployment, instead, I will just go the easy route, and use a pandas dataframe.<\/p>\n\n\n\n<p>Let\u2019s add pandas and numpy to our file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import pandas as pd<\/code><\/pre>\n\n\n\n<p id=\"02eb\">And let\u2019s add<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/catsdev\/94ffaafbe3a894cbe9bb2b44443c8793.js\"><\/script>\n\n\n\n<p id=\"f52c\">So, now we have a Dataframe with one column: texts.<br>Now we will move the creation of the embeddings into a function that we can call repeatedly. This is key at this point, since we are going to be adding a new column to the dataframe called\u00a0<code>embedding<\/code>\u00a0which will store the result of calling the get_embedding function on each text.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/catsdev\/9afbc51cdaee32b541764ad141cfd715.js\"><\/script>\n\n\n\n<p id=\"eced\">As you can see, each row in the dataframe now has the vector that represents the text.<br>Now, we need to get an embedding for the question:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>question = \"Is Peter a musician?\"<br>question_embedding = get_embedding(question)<\/code><\/pre>\n\n\n\n<p id=\"5992\">And then add a new column to the dataset to store the similarity between the vector of the text and the vector of the question.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/catsdev\/e20c0216422c8c2087f87c9678455f0b.js\"><\/script>\n\n\n\n<p id=\"2adb\">Great, so now our dataframe consists of rows with the original text, the vector of the text, and the similarity to the question vector.<br>We can probably just eyeball the list and pick the closest line, but let\u2019s attempt to do it in code and see if it gets us a reasonable answer.<\/p>\n\n\n\n<p id=\"1e78\">The next step is usually to filter the list so that for the next steps we deal with a smaller subset. We do this by filtering the dataframe by similarity greater than certain threshold, in this case we chose 80%.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/catsdev\/8aaac15f875d4a976dc6ae8bced51d05.js\"><\/script>\n\n\n\n<p id=\"4f7f\">We already see that the list got smaller, only 5 rows meet the criteria.<br>The next step is to sort the filtered rows in similarity order and pick the top match:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/catsdev\/ab9466f3f2ed1afcdfd9f91c49ebb480.js\"><\/script>\n\n\n\n<p id=\"a71b\">Excellent, using the embeddings we only needed plain mathematics to find the closest document with content that could be used to answer our question!<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"f92e\">Finally, ask ChatGPT to answer a question based on our Context (RAG)<\/h1>\n\n\n\n<p id=\"4e6d\">Now we have all the elements we need to finally get ChatGPT to use our context to answer the question. By inspecting the statement that math helped pick we can probably guess the answer already. But, in real world cases, this could be a very long document, besides, \u201cPeter plays the clarinet\u201d is not a direct answer to the question. For that we will use the Large Language Model ChatGPT that is not only capable of understanding the question and the context, but also to generate an appropriate response.<\/p>\n\n\n\n<p id=\"b741\">And now, the moment of truth, we will get the value of the top answer that we found matching the question and pass it to the chat call as context:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/catsdev\/630214b989dcb29b50310a6ee697d203.js\"><\/script>\n\n\n\n<p id=\"aeae\">That is what RAG is all about. Augmenting the context for ChatGPT to answer a question by retrieving information using vectorized data.<\/p>\n\n\n\n<p id=\"99db\">Now, of course there is plenty more to create a production-level application using these technologies. The newer embedding models create larger vectors (higher accuracy, but slower search), but also allow for shortening them, which can be used to do a two-pass search by filtering the total set of documents with a less granular vector and then using the higher dimension vector for the smaller set. I have only used strings here, but the same principles would work for larger documents. The documents will need some processing, like cracking them open (extract text from pdfs, etc.), chunking (break the documents in smaller units, potentially overlapping the chunks to avoid chopping the flow), using a vector database, implement hybrid searches (keyword, semantic, etc.).<\/p>\n\n\n\n<p id=\"5053\">So, yes, there is plenty of other work to be done to take this application to production, but hopefully if you have read this far, you have a greater understanding of how RAG works.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Large Language Models (LLM), especially the ones from the \u201cgenerative\u201d kind, have taken the world by storm. And, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":9664,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"","fifu_image_alt":"","footnotes":""},"categories":[38],"tags":[78,85,79,59],"class_list":["post-9662","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology","tag-chatgpt","tag-llm","tag-rag","tag-technology"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Demystifying Retrieval-Augmented Generation (RAG): A Step-by-Step Guide to ChatGPT with Context - El blog de Felipe<\/title>\n<meta name=\"description\" content=\"Empower your AI chatbot with deeper context! This guide explores Retrieval-Augmented Generation (RAG), a technique that unlocks ChatGPT&#039;s potential to answer questions based on relevant information, not just keywords. Dive into step-by-step explanations, code examples, and practical applications, and discover how RAG can revolutionize your chatbot&#039;s interactions. Learn how to leverage ChatGPT&#039;s understanding of language with the power of context retrieval, and take your AI conversations to the next level!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blogs.pacasmayo.com\/felipe\/2024\/02\/19\/demystifying-retrieval-augmented-generation-rag-a-step-by-step-guide-to-chatgpt-with-context\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Demystifying Retrieval-Augmented Generation (RAG): A Step-by-Step Guide to ChatGPT with Context - El blog de Felipe\" \/>\n<meta property=\"og:description\" content=\"Empower your AI chatbot with deeper context! This guide explores Retrieval-Augmented Generation (RAG), a technique that unlocks ChatGPT&#039;s potential to answer questions based on relevant information, not just keywords. Dive into step-by-step explanations, code examples, and practical applications, and discover how RAG can revolutionize your chatbot&#039;s interactions. Learn how to leverage ChatGPT&#039;s understanding of language with the power of context retrieval, and take your AI conversations to the next level!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blogs.pacasmayo.com\/felipe\/2024\/02\/19\/demystifying-retrieval-augmented-generation-rag-a-step-by-step-guide-to-chatgpt-with-context\/\" \/>\n<meta property=\"og:site_name\" content=\"El blog de Felipe\" \/>\n<meta property=\"article:published_time\" content=\"2024-02-19T11:03:14+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-19T11:50:28+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/blogs.pacasmayo.com\/felipe\/files\/2024\/02\/image-2.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t<meta property=\"og:image:height\" content=\"1024\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"AI\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"AI\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/blogs.pacasmayo.com\\\/felipe\\\/2024\\\/02\\\/19\\\/demystifying-retrieval-augmented-generation-rag-a-step-by-step-guide-to-chatgpt-with-context\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blogs.pacasmayo.com\\\/felipe\\\/2024\\\/02\\\/19\\\/demystifying-retrieval-augmented-generation-rag-a-step-by-step-guide-to-chatgpt-with-context\\\/\"},\"author\":{\"name\":\"AI\",\"@id\":\"https:\\\/\\\/blogs.pacasmayo.com\\\/felipe\\\/#\\\/schema\\\/person\\\/e32bfb69f0dcbed869e838474a34d240\"},\"headline\":\"Demystifying Retrieval-Augmented Generation (RAG): A Step-by-Step Guide to ChatGPT with Context\",\"datePublished\":\"2024-02-19T11:03:14+00:00\",\"dateModified\":\"2024-02-19T11:50:28+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/blogs.pacasmayo.com\\\/felipe\\\/2024\\\/02\\\/19\\\/demystifying-retrieval-augmented-generation-rag-a-step-by-step-guide-to-chatgpt-with-context\\\/\"},\"wordCount\":2180,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/blogs.pacasmayo.com\\\/felipe\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/blogs.pacasmayo.com\\\/felipe\\\/2024\\\/02\\\/19\\\/demystifying-retrieval-augmented-generation-rag-a-step-by-step-guide-to-chatgpt-with-context\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/blogs.pacasmayo.com\\\/felipe\\\/files\\\/2024\\\/02\\\/image-2.png\",\"keywords\":[\"ChatGPT\",\"LLM\",\"RAG\",\"technology\"],\"articleSection\":[\"Technology\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/blogs.pacasmayo.com\\\/felipe\\\/2024\\\/02\\\/19\\\/demystifying-retrieval-augmented-generation-rag-a-step-by-step-guide-to-chatgpt-with-context\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/blogs.pacasmayo.com\\\/felipe\\\/2024\\\/02\\\/19\\\/demystifying-retrieval-augmented-generation-rag-a-step-by-step-guide-to-chatgpt-with-context\\\/\",\"url\":\"https:\\\/\\\/blogs.pacasmayo.com\\\/felipe\\\/2024\\\/02\\\/19\\\/demystifying-retrieval-augmented-generation-rag-a-step-by-step-guide-to-chatgpt-with-context\\\/\",\"name\":\"Demystifying Retrieval-Augmented Generation (RAG): A Step-by-Step Guide to ChatGPT with Context - El blog de Felipe\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blogs.pacasmayo.com\\\/felipe\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/blogs.pacasmayo.com\\\/felipe\\\/2024\\\/02\\\/19\\\/demystifying-retrieval-augmented-generation-rag-a-step-by-step-guide-to-chatgpt-with-context\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/blogs.pacasmayo.com\\\/felipe\\\/2024\\\/02\\\/19\\\/demystifying-retrieval-augmented-generation-rag-a-step-by-step-guide-to-chatgpt-with-context\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/blogs.pacasmayo.com\\\/felipe\\\/files\\\/2024\\\/02\\\/image-2.png\",\"datePublished\":\"2024-02-19T11:03:14+00:00\",\"dateModified\":\"2024-02-19T11:50:28+00:00\",\"description\":\"Empower your AI chatbot with deeper context! This guide explores Retrieval-Augmented Generation (RAG), a technique that unlocks ChatGPT's potential to answer questions based on relevant information, not just keywords. Dive into step-by-step explanations, code examples, and practical applications, and discover how RAG can revolutionize your chatbot's interactions. Learn how to leverage ChatGPT's understanding of language with the power of context retrieval, and take your AI conversations to the next level!\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/blogs.pacasmayo.com\\\/felipe\\\/2024\\\/02\\\/19\\\/demystifying-retrieval-augmented-generation-rag-a-step-by-step-guide-to-chatgpt-with-context\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/blogs.pacasmayo.com\\\/felipe\\\/2024\\\/02\\\/19\\\/demystifying-retrieval-augmented-generation-rag-a-step-by-step-guide-to-chatgpt-with-context\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/blogs.pacasmayo.com\\\/felipe\\\/2024\\\/02\\\/19\\\/demystifying-retrieval-augmented-generation-rag-a-step-by-step-guide-to-chatgpt-with-context\\\/#primaryimage\",\"url\":\"https:\\\/\\\/blogs.pacasmayo.com\\\/felipe\\\/files\\\/2024\\\/02\\\/image-2.png\",\"contentUrl\":\"https:\\\/\\\/blogs.pacasmayo.com\\\/felipe\\\/files\\\/2024\\\/02\\\/image-2.png\",\"width\":1024,\"height\":1024},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/blogs.pacasmayo.com\\\/felipe\\\/2024\\\/02\\\/19\\\/demystifying-retrieval-augmented-generation-rag-a-step-by-step-guide-to-chatgpt-with-context\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Portada\",\"item\":\"https:\\\/\\\/blogs.pacasmayo.com\\\/felipe\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Demystifying Retrieval-Augmented Generation (RAG): A Step-by-Step Guide to ChatGPT with Context\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/blogs.pacasmayo.com\\\/felipe\\\/#website\",\"url\":\"https:\\\/\\\/blogs.pacasmayo.com\\\/felipe\\\/\",\"name\":\"El blog de Felipe\",\"description\":\"De la abundancia del coraz\u00f3n...\",\"publisher\":{\"@id\":\"https:\\\/\\\/blogs.pacasmayo.com\\\/felipe\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/blogs.pacasmayo.com\\\/felipe\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/blogs.pacasmayo.com\\\/felipe\\\/#organization\",\"name\":\"El blog de Felipe\",\"url\":\"https:\\\/\\\/blogs.pacasmayo.com\\\/felipe\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/blogs.pacasmayo.com\\\/felipe\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/blogs.pacasmayo.com\\\/felipe\\\/files\\\/2019\\\/11\\\/cropped-caricatura_felipe.jpg\",\"contentUrl\":\"https:\\\/\\\/blogs.pacasmayo.com\\\/felipe\\\/files\\\/2019\\\/11\\\/cropped-caricatura_felipe.jpg\",\"width\":1360,\"height\":598,\"caption\":\"El blog de Felipe\"},\"image\":{\"@id\":\"https:\\\/\\\/blogs.pacasmayo.com\\\/felipe\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/blogs.pacasmayo.com\\\/felipe\\\/#\\\/schema\\\/person\\\/e32bfb69f0dcbed869e838474a34d240\",\"name\":\"AI\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4afeebd1deb1471ed6e7e21e6dc44176da2cb2ef0704b36e720ef8656e34bd65?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4afeebd1deb1471ed6e7e21e6dc44176da2cb2ef0704b36e720ef8656e34bd65?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4afeebd1deb1471ed6e7e21e6dc44176da2cb2ef0704b36e720ef8656e34bd65?s=96&d=mm&r=g\",\"caption\":\"AI\"},\"description\":\"Este art\u00edculo ha sido seleccionado y parcialmente escrito e ilustrado por Inteligencia Artificial (AI) basado en noticias disponibles.\",\"url\":\"https:\\\/\\\/blogs.pacasmayo.com\\\/felipe\\\/author\\\/ai\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Demystifying Retrieval-Augmented Generation (RAG): A Step-by-Step Guide to ChatGPT with Context - El blog de Felipe","description":"Empower your AI chatbot with deeper context! This guide explores Retrieval-Augmented Generation (RAG), a technique that unlocks ChatGPT's potential to answer questions based on relevant information, not just keywords. Dive into step-by-step explanations, code examples, and practical applications, and discover how RAG can revolutionize your chatbot's interactions. Learn how to leverage ChatGPT's understanding of language with the power of context retrieval, and take your AI conversations to the next level!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blogs.pacasmayo.com\/felipe\/2024\/02\/19\/demystifying-retrieval-augmented-generation-rag-a-step-by-step-guide-to-chatgpt-with-context\/","og_locale":"en_US","og_type":"article","og_title":"Demystifying Retrieval-Augmented Generation (RAG): A Step-by-Step Guide to ChatGPT with Context - El blog de Felipe","og_description":"Empower your AI chatbot with deeper context! This guide explores Retrieval-Augmented Generation (RAG), a technique that unlocks ChatGPT's potential to answer questions based on relevant information, not just keywords. Dive into step-by-step explanations, code examples, and practical applications, and discover how RAG can revolutionize your chatbot's interactions. Learn how to leverage ChatGPT's understanding of language with the power of context retrieval, and take your AI conversations to the next level!","og_url":"https:\/\/blogs.pacasmayo.com\/felipe\/2024\/02\/19\/demystifying-retrieval-augmented-generation-rag-a-step-by-step-guide-to-chatgpt-with-context\/","og_site_name":"El blog de Felipe","article_published_time":"2024-02-19T11:03:14+00:00","article_modified_time":"2024-02-19T11:50:28+00:00","og_image":[{"width":1024,"height":1024,"url":"https:\/\/blogs.pacasmayo.com\/felipe\/files\/2024\/02\/image-2.png","type":"image\/png"}],"author":"AI","twitter_card":"summary_large_image","twitter_misc":{"Written by":"AI","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blogs.pacasmayo.com\/felipe\/2024\/02\/19\/demystifying-retrieval-augmented-generation-rag-a-step-by-step-guide-to-chatgpt-with-context\/#article","isPartOf":{"@id":"https:\/\/blogs.pacasmayo.com\/felipe\/2024\/02\/19\/demystifying-retrieval-augmented-generation-rag-a-step-by-step-guide-to-chatgpt-with-context\/"},"author":{"name":"AI","@id":"https:\/\/blogs.pacasmayo.com\/felipe\/#\/schema\/person\/e32bfb69f0dcbed869e838474a34d240"},"headline":"Demystifying Retrieval-Augmented Generation (RAG): A Step-by-Step Guide to ChatGPT with Context","datePublished":"2024-02-19T11:03:14+00:00","dateModified":"2024-02-19T11:50:28+00:00","mainEntityOfPage":{"@id":"https:\/\/blogs.pacasmayo.com\/felipe\/2024\/02\/19\/demystifying-retrieval-augmented-generation-rag-a-step-by-step-guide-to-chatgpt-with-context\/"},"wordCount":2180,"commentCount":0,"publisher":{"@id":"https:\/\/blogs.pacasmayo.com\/felipe\/#organization"},"image":{"@id":"https:\/\/blogs.pacasmayo.com\/felipe\/2024\/02\/19\/demystifying-retrieval-augmented-generation-rag-a-step-by-step-guide-to-chatgpt-with-context\/#primaryimage"},"thumbnailUrl":"https:\/\/blogs.pacasmayo.com\/felipe\/files\/2024\/02\/image-2.png","keywords":["ChatGPT","LLM","RAG","technology"],"articleSection":["Technology"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blogs.pacasmayo.com\/felipe\/2024\/02\/19\/demystifying-retrieval-augmented-generation-rag-a-step-by-step-guide-to-chatgpt-with-context\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blogs.pacasmayo.com\/felipe\/2024\/02\/19\/demystifying-retrieval-augmented-generation-rag-a-step-by-step-guide-to-chatgpt-with-context\/","url":"https:\/\/blogs.pacasmayo.com\/felipe\/2024\/02\/19\/demystifying-retrieval-augmented-generation-rag-a-step-by-step-guide-to-chatgpt-with-context\/","name":"Demystifying Retrieval-Augmented Generation (RAG): A Step-by-Step Guide to ChatGPT with Context - El blog de Felipe","isPartOf":{"@id":"https:\/\/blogs.pacasmayo.com\/felipe\/#website"},"primaryImageOfPage":{"@id":"https:\/\/blogs.pacasmayo.com\/felipe\/2024\/02\/19\/demystifying-retrieval-augmented-generation-rag-a-step-by-step-guide-to-chatgpt-with-context\/#primaryimage"},"image":{"@id":"https:\/\/blogs.pacasmayo.com\/felipe\/2024\/02\/19\/demystifying-retrieval-augmented-generation-rag-a-step-by-step-guide-to-chatgpt-with-context\/#primaryimage"},"thumbnailUrl":"https:\/\/blogs.pacasmayo.com\/felipe\/files\/2024\/02\/image-2.png","datePublished":"2024-02-19T11:03:14+00:00","dateModified":"2024-02-19T11:50:28+00:00","description":"Empower your AI chatbot with deeper context! This guide explores Retrieval-Augmented Generation (RAG), a technique that unlocks ChatGPT's potential to answer questions based on relevant information, not just keywords. Dive into step-by-step explanations, code examples, and practical applications, and discover how RAG can revolutionize your chatbot's interactions. Learn how to leverage ChatGPT's understanding of language with the power of context retrieval, and take your AI conversations to the next level!","breadcrumb":{"@id":"https:\/\/blogs.pacasmayo.com\/felipe\/2024\/02\/19\/demystifying-retrieval-augmented-generation-rag-a-step-by-step-guide-to-chatgpt-with-context\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blogs.pacasmayo.com\/felipe\/2024\/02\/19\/demystifying-retrieval-augmented-generation-rag-a-step-by-step-guide-to-chatgpt-with-context\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blogs.pacasmayo.com\/felipe\/2024\/02\/19\/demystifying-retrieval-augmented-generation-rag-a-step-by-step-guide-to-chatgpt-with-context\/#primaryimage","url":"https:\/\/blogs.pacasmayo.com\/felipe\/files\/2024\/02\/image-2.png","contentUrl":"https:\/\/blogs.pacasmayo.com\/felipe\/files\/2024\/02\/image-2.png","width":1024,"height":1024},{"@type":"BreadcrumbList","@id":"https:\/\/blogs.pacasmayo.com\/felipe\/2024\/02\/19\/demystifying-retrieval-augmented-generation-rag-a-step-by-step-guide-to-chatgpt-with-context\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Portada","item":"https:\/\/blogs.pacasmayo.com\/felipe\/"},{"@type":"ListItem","position":2,"name":"Demystifying Retrieval-Augmented Generation (RAG): A Step-by-Step Guide to ChatGPT with Context"}]},{"@type":"WebSite","@id":"https:\/\/blogs.pacasmayo.com\/felipe\/#website","url":"https:\/\/blogs.pacasmayo.com\/felipe\/","name":"El blog de Felipe","description":"De la abundancia del coraz\u00f3n...","publisher":{"@id":"https:\/\/blogs.pacasmayo.com\/felipe\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blogs.pacasmayo.com\/felipe\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/blogs.pacasmayo.com\/felipe\/#organization","name":"El blog de Felipe","url":"https:\/\/blogs.pacasmayo.com\/felipe\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blogs.pacasmayo.com\/felipe\/#\/schema\/logo\/image\/","url":"https:\/\/blogs.pacasmayo.com\/felipe\/files\/2019\/11\/cropped-caricatura_felipe.jpg","contentUrl":"https:\/\/blogs.pacasmayo.com\/felipe\/files\/2019\/11\/cropped-caricatura_felipe.jpg","width":1360,"height":598,"caption":"El blog de Felipe"},"image":{"@id":"https:\/\/blogs.pacasmayo.com\/felipe\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/blogs.pacasmayo.com\/felipe\/#\/schema\/person\/e32bfb69f0dcbed869e838474a34d240","name":"AI","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4afeebd1deb1471ed6e7e21e6dc44176da2cb2ef0704b36e720ef8656e34bd65?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4afeebd1deb1471ed6e7e21e6dc44176da2cb2ef0704b36e720ef8656e34bd65?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4afeebd1deb1471ed6e7e21e6dc44176da2cb2ef0704b36e720ef8656e34bd65?s=96&d=mm&r=g","caption":"AI"},"description":"Este art\u00edculo ha sido seleccionado y parcialmente escrito e ilustrado por Inteligencia Artificial (AI) basado en noticias disponibles.","url":"https:\/\/blogs.pacasmayo.com\/felipe\/author\/ai\/"}]}},"_links":{"self":[{"href":"https:\/\/blogs.pacasmayo.com\/felipe\/wp-json\/wp\/v2\/posts\/9662","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.pacasmayo.com\/felipe\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.pacasmayo.com\/felipe\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.pacasmayo.com\/felipe\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.pacasmayo.com\/felipe\/wp-json\/wp\/v2\/comments?post=9662"}],"version-history":[{"count":4,"href":"https:\/\/blogs.pacasmayo.com\/felipe\/wp-json\/wp\/v2\/posts\/9662\/revisions"}],"predecessor-version":[{"id":9675,"href":"https:\/\/blogs.pacasmayo.com\/felipe\/wp-json\/wp\/v2\/posts\/9662\/revisions\/9675"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.pacasmayo.com\/felipe\/wp-json\/wp\/v2\/media\/9664"}],"wp:attachment":[{"href":"https:\/\/blogs.pacasmayo.com\/felipe\/wp-json\/wp\/v2\/media?parent=9662"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.pacasmayo.com\/felipe\/wp-json\/wp\/v2\/categories?post=9662"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.pacasmayo.com\/felipe\/wp-json\/wp\/v2\/tags?post=9662"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}