<?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 IVAN ILIN on Medium]]></title>
        <description><![CDATA[Stories by IVAN ILIN on Medium]]></description>
        <link>https://medium.com/@ivanilin_iki?source=rss-4a0803226977------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*pbZ10EIUtVt6qu2DNdP9dg.jpeg</url>
            <title>Stories by IVAN ILIN on Medium</title>
            <link>https://medium.com/@ivanilin_iki?source=rss-4a0803226977------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 19 Jun 2026 13:12:09 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ivanilin_iki/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[Through knowledge sharing to singularity, accelerated by LLMs]]></title>
            <link>https://pub.towardsai.net/through-knowledge-sharing-to-singularity-accelerated-by-llms-db87cb516b79?source=rss-4a0803226977------2</link>
            <guid isPermaLink="false">https://medium.com/p/db87cb516b79</guid>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[knowledge]]></category>
            <category><![CDATA[productivity]]></category>
            <category><![CDATA[future]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[IVAN ILIN]]></dc:creator>
            <pubDate>Tue, 27 Feb 2024 13:05:33 GMT</pubDate>
            <atom:updated>2024-02-27T13:05:33.854Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZBbISWgnAMYw-RlWkn2JTA.png" /></figure><h3>Through Knowledge Sharing to Singularity, Accelerated By LLMs</h3><h3>LLMs are one of the pinnacles of human knowledge that has transformative potential comparable to the Internet. How did we come to that? Why do knowledge sharing and knowledge flow play a crucial role in our world’s acceleration, and why are LLMs the lapis philosophorum that could bring us to the singularity?</h3><p><em>I’ll briefly outline some knowledge-sharing history milestones, the effects open-source had on knowledge accumulation, and the way it brought us to LLMs. <br>Then we’ll stop at the current point to reflect on the effects LLMs will have in tech, science, and society, touching on recent techno-optimism e/acc philosophy that ushers humanity into the bright singularity future.</em></p><blockquote>We believe intelligence is in an upward spiral [1] — Marc Andreessen</blockquote><h3>Some history</h3><p><strong>The ability to share knowledge might be the key distinction that allowed humankind to evolve into such a complex civilization and to become the dominant species on our planet.</strong></p><p>Since the beginning of the times, the collective efforts to explore the unknown and find meaning in the unexplainable have driven our evolution, propelling humanity forward on a relentless quest for knowledge and understanding. Spurred by this inherent curiosity, our species’ development has been distinguished from others by the accumulation of wisdom passed down through generations.</p><p><strong>Early humans, driven by the need to collaborate and share experiences, developed language to communicate, describe their surroundings, and transmit knowledge.</strong> This cognitive leap laid the foundation for the complex societies that would emerge over time and the foundation of science later on as the collective understanding of the world became a shared endeavor.</p><p><strong>From 15,000 BC cave drawings to Wikipedia, all iterations of knowledge sharing tools were an invitation for collaboration.</strong> While documentation techniques have continued to get more and more sophisticated, the goal has stayed the same — to find meanings and <strong>share the findings</strong>. It is <a href="https://core.ac.uk/download/pdf/249335744.pdf">scientifically proven</a> that teamwork has been a key factor in the progress, evolution, and survival of humanity.</p><p>The obvious milestones in knowledge sharing are the invention of writing around 3400 B.C. in the Schumer area and the Gutenberg press almost five thousand years later. The first one allowed to capture knowledge while the second provided its distribution to the masses grounding the <a href="https://en.wikipedia.org/wiki/Scientific_Revolution">Scientific Revolution</a> resulting in the <a href="https://en.wikipedia.org/wiki/Industrial_Revolution">Industrial Revolution</a>.</p><p>The quest for understanding the meaning, truth, and unraveling the mysteries of existence has compelled us to cultivate an environment where the exchange of ideas and the advancement of knowledge are paramount. During the 17th century, the majority of European countries established their Academies of Sciences, thus accelerating the exchange and validation of knowledge.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WFtT4bS8qtI9ANxWwhlHHg.png" /><figcaption>The time axis here has a logarithmic scale due to our world’s exponential acceleration</figcaption></figure><h3><strong>Internet era &amp; open source</strong></h3><p>Long story short — a hundred years after the invention of the telephone and the radio we finally came up with the Internet. The first message was sent on October 29, 1969, from UCLA to the Stanford Research Institute — the universities were the first organizations involved in information sharing technology, though under the military supervision of (D)ARPA.</p><p>The Internet brought us to the globalization era with the vast availability of knowledge, and then people started sharing their solutions to popular problems so the <a href="https://en.wikipedia.org/wiki/History_of_free_and_open-source_software">open source</a> was sparked. Engineers shared the source code of their projects even before the Internet, but for sure it sped things up dramatically.</p><p>Apart from sharing code people started sharing opinions, ideas, and facts on the Internet — speaking of knowledge sharing it is impossible to pass by Wikipedia&#39;s launch in 2001. Looking ahead, it has served the ML community a lot while building different Natural Language Understanding tools and models as a high-quality curated corpus of information.</p><p><strong>The open-source movement gained hold with the rise of the Internet, and it has since grown into a vibrant scene with many contributors and projects. </strong>Fast forward to 2008, and we see the Github launch, providing developers with a platform to collaborate on their projects online. <strong>Since that time the open-source approach has become a solid ground for the tech scene’s exponential growth and allowed society to not only avoid a good chunk of duplicative efforts but to build a shared foundation for our current and future innovations. </strong>More than 99% of Fortune 500 companies use open-source code [2].</p><p>The whole machine learning industry since the early days was growing on open source solutions like <a href="https://scikit-learn.org/stable/">scikit learn</a> (2007) and then deep learning frameworks —<a href="https://www.tensorflow.org"> TensorFlow</a> (2015) and <a href="https://pytorch.org">PyTorch</a> (2016). Later on in the 2020s people started sharing pre-trained models weights on <a href="https://huggingface.co">huggingface</a> and referencing the <a href="https://arxiv.org">arXiv</a> papers to their implementations on <a href="https://paperswithcode.com/">paperswithcode</a>.</p><p><strong>These were all the building blocks of the knowledge-sharing culture, community, and tech, particularly in machine learning, that paved the way for ChatGPT and other LLMs to appear. Without that, we would never have developed AI to its state in just a decade.</strong></p><p>This sharing culture dramatically increased our progress pace by multiplying contributions, almost instantly sharing the current state-of-the-art tech with the world and <strong>allowing every engineer to start his journey right from the current pinnacle.</strong></p><p><strong>In fact, the whole open source culture is a collaborative learning environment and we are all working in the </strong><a href="https://en.wikipedia.org/wiki/Zone_of_proximal_development"><strong>zone of proximal development</strong></a> according to Lev Vygotsky.</p><h3><strong>LLMs and knowledge — a symbiotic relationship</strong></h3><h4>Transformers invention and training</h4><p><strong>Additionally, this knowledge sharing process left plenty of publicly available data</strong> — first in the form of texts, then in code, and lately as public models and datasets. <strong>This data abundance is crucial to the LLMs training process — </strong>LLMs thrive on large datasets, which expand and thrive thanks to the collective knowledge accumulated and shared on the Internet through open-source platforms — human-generated texts from scriptures to Instagram posts, Reddit (recent $60M/y training <a href="https://www.theverge.com/2024/2/22/24080165/google-reddit-ai-training-data">deal</a> with Google), massive Github and StackOverflow codebase, etc.</p><p>Let’s dig a bit into the LLMs training process.<strong> </strong>First, the GPT family models are decentralized parts of the industry-revolutionizing transformer architecture introduced in the seminal paper <a href="https://arxiv.org/abs/1706.03762">Attention Is All You Need</a>. Many novel ideas were implemented at once, resulting in a complete replacement of the previous state-of-the-art variations of Recurrent Neural Networks by this new architecture.<strong> One of the pivotal ideas besides self-attention was the application of </strong><a href="https://en.wikipedia.org/wiki/Self-supervised_learning"><strong>self-supervised learning</strong></a><strong> to model pretraining.</strong> Transformer models had to solve two core tasks while pre-training — masked language modeling — prediction of the randomly masked word given the context and the next sentence prediction task — given two sentences model has to classify if they are consecutive or not.</p><p><strong>This approach eliminated the need to create a specific dataset to train models on</strong> by manually labeling texts, allowing researchers to increase the training data size dramatically without any human work involved and <strong>to use any human texts of reasonable quality to train Transformer models — </strong>first high quality curated corpora like Wikipedia and later the whole Internet.</p><p>The invention of Transformers led to a new cycle in Natural Language Understanding bringing us to the era of the first capable chat-bots with retrieval and generative parts, high-quality semantic search, and multimodal generative models.</p><h4>LLMs are born</h4><p>A few years later OpenAI applied <a href="https://openai.com/research/learning-from-human-preferences">reinforcement learning from human feedback (RLHF)</a> to train <a href="https://openai.com/research/instruction-following">InstructGPT</a> and make it safer, more helpful, and more aligned — labelers provided demonstrations of the desired model behavior, ranked several outputs from the models, and then used this data to fine-tune GPT-3, eventually giving birth to ChatGPT in 2022.</p><p><strong>That’s how LLM reasoning abilities emerged — we may speculate that InstructGPT</strong>, having demonstrated a much better ability to follow human instructions and more coherent responses, <strong>learned this reasoning and logic from humans</strong>. And let’s step back a bit to remind ourselves that <strong>models have been learning in natural language, so our knowledge sharing mechanism worked for a machine learning model too.</strong></p><p>Here lies a thin line between the reproduction of coherent sequences and the actual “understanding” of information encoded by those sequences. Understanding means the model’s capability not only to capture relations but also to demonstrate general logic and common knowledge[3]. <br>GPT-4 reasoning capabilities are extensively tested in the <a href="https://cdn.openai.com/papers/gpt-4.pdf">OpenAI tech report</a>, but if you’d like to briefly get the idea, here is my 8-month-old (yes, THAT old) <a href="https://hackernoon.com/scratching-the-singularity-surface-the-past-present-and-mysterious-future-of-llms">post</a> reflecting on LLM’s reasoning potential.</p><p>So we got LLMs as the reasoning engine, being capable of executing logical operations, which is already a very promising thing. But while <strong>LLM weights contain human-level reasoning capabilities</strong> they may lack relevant information — that’s where <a href="https://pub.towardsai.net/advanced-rag-techniques-an-illustrated-overview-04d193d8fec6">RAG, standing for retrieval augmented generation</a>, comes to the stage. <strong>RAG is the most popular architecture of LLM applications providing information injection into the LLM prompt</strong>, <strong>giving LLM the knowledge to reason upon.</strong></p><h4>LLMs are the new interface for information</h4><p>This RAG approach brings immense possibilities for creating knowledge assistants, capable of answering complex logical questions and fetching any data needed from multiple sources, creating reports, preparing analytical notes, comparative analysis, writing long and short texts, or even working as your thinking assistant. <strong>We are building such an</strong> <strong>intelligent knowledge interface in </strong><a href="https://iki.ai"><strong>iki.ai</strong></a><strong> — </strong>an assistant and a second brain for professionals &amp; teams, but there are many more solutions in the market, focusing on specific use cases — perplexity.ai is fighting with Google for the mainstream information discovery tool market, Arc browser is going a step further and wants not to just merge various information pieces from different sources with an LLM, but to build a whole new interface for web aimed at information aggregation and structuring (as far as I got it from their <a href="https://www.youtube.com/watch?v=WIeJF3kL5ng">recent video</a>).</p><p><strong>This ability of LLMs to generate a coherent and logically correct text provided some information is what changes the whole knowledge sharing paradigm — it is now possible to merge and transform information according to a specific request — the operation that required a human expert before.</strong></p><p><strong>Those who are building truly disruptive LLM-powered products are reshaping the interfaces to information.</strong></p><p><strong>That is the paradigm shift in the knowledge creation process</strong> as we know it — <strong>before the dots were connected only in human brains</strong>. Scientists studied, adopted thinking patterns, and research methods, and consumed vast amounts of information to come up with an innovation — adding a new layer of ideas to the existing knowledge.</p><p>Now an LLM can do that for you. And obviously, it does not have cognitive load threshold or memory limit issues. <strong>A research agent, augmenting a scientist’s intelligence,</strong> now can have some tools like access to scientific databases and the Internet, a goal, and some human-in-the-loop guidance. That’s not a futuristic idea, that’s how research would be done this year — there already are products implementing early prototypes of research assistants.</p><p>This would boost knowledge creation aka tech and scientific progress, speeding up ideation loops and research cycles dramatically plus granting almost instant access to information, thus ushering in the age of singularity — arguably we are entering it now.</p><p>Most of the knowledge workers may delegate some part of their daily responsibilities to LLMs right now — I am speaking of analysts, lawyers, researchers, and experts in general. <strong>One of the obvious problems we are taсkling in iki.ai is professional information overload — you may ask your knowledge assistant to distill key ideas or connect the dots from multiple contexts for you. </strong>The cognitive load, especially in the field of Machine Learning, has now reached unprecedented levels and our brains are not made to withstand this constant influx of information<em> </em>so having some kind of software to store your knowledge and query it in various ways becomes a necessity.</p><p><strong>The next big cognitive frontier once solely attributed to humans is creativity — </strong>It could be interpreted as the ability to connect previously unrelated dots. Now these dots, or ideas, may be extracted from a context by an LLM and then connected in various ways until it clicks with the user — that gives birth to the first true second brains emerging.</p><p>I’ve already mentioned <strong>agents — LLMs capable of using external tools to complete a task — that’s a whole next paradigm. </strong>Proactive assistants, software, automating whole pipelines, automated interactions with GUI to integrate agentic systems with the current generation of software, and who knows what comes next.</p><p>The coming few years will change many processes and tools we got used to in the previous decade and be sure that the best minds in the industry are now working on that huge business opportunity.</p><h3><strong>Societal effects</strong></h3><p>Such drastic technological changes cannot happen without affecting society, especially in the informational world we live in after the <a href="https://www.weforum.org/agenda/2016/01/the-fourth-industrial-revolution-what-it-means-and-how-to-respond/">fourth industrial revolution</a>.</p><p>One of the philosophies, outlining a fairly positive view on this major tech shift, is <strong>Effective Accelerationism, or e/acc</strong>, <a href="https://web.archive.org/web/20220602164803/https://swarthy.substack.com/p/effective-accelerationism-eacc?s=r">coined</a> in 2022 and substantially developed by Marc Andreessen in his <a href="https://a16z.com/the-techno-optimist-manifesto/">Techno Optimist Manifesto</a> several months ago. The text is remarkable and I recommend reading the original, but let’s cite some core ideas:</p><blockquote>We believe intelligence is the ultimate engine of progress. Intelligence makes everything better. Smart people and smart societies outperform less smart ones on virtually every metric we can measure. Intelligence is the birthright of humanity; we should expand it as fully and broadly as we possibly can.</blockquote><blockquote>Ray Kurzweil defines his Law of Accelerating Returns: Technological advances tend to feed on themselves, increasing the rate of further advance.</blockquote><blockquote>We believe in accelerationism — the conscious and deliberate propulsion of technological development — to ensure the fulfillment of the Law of Accelerating Returns. To ensure the techno-capital upward spiral continues forever.</blockquote><p>Although a bit one-sided e/acc movement is popular in the California tech community and on Twitter — the new AI tech creates a lot of possibilities in the market for tech entrepreneurs, engineers, and investors, and then for a lot of data-related businesses. Capitalism is an economic system based on growth, so for me, e/acc looks more like a technocratic approach with some sparkling singularity touch.</p><p>AI and LLMs are the new tech revolution, and those with tech capabilities, capital, and some audience are positioned much better to handle this new opportunity — successful VCs &amp; entrepreneurs will become richer, and the measure of merits from the new tech will be proportional to the tech ownership. The least qualified employees will be the first ones to be replaced, so I do not see a better world for everyone immediately, more likely a continuous series of layoffs and turbulence due to the disrupting tech adoption. The upside is that eventually, more tedious tasks will be gone along with the low-paid, less-qualified jobs, while more companies are created, more cases are solved, and more successful VC stories happen.</p><p>At a higher level of abstraction, <strong>knowledge can fast-track happiness.</strong> Adopting a motivation-driven learning approach enriches personal and professional journeys, aiding in discovering your <a href="https://en.wikipedia.org/wiki/Ikigai">ikigai</a> — the essence of your existence. Your “second brain” can help you accelerate the iteration loop of ideation, creation, feedback collection, and refinement, allowing for a more steep and successful learning curve.</p><p>Despite all the positive things about knowledge sharing and tech advancements that humanity fostered over the last 300–400 years, <strong>there also are a few problems caused by this unprecedented acceleration we are experiencing: psychological overload, insane rivalry in the markets, and tech knowledge getting obsolete</strong> within a month now.</p><p>The current pace of advancements in AI technology resembles an explosion. Humanity operates as a cohesive entity, exchanging information, resources, and responsibilities. The rapidity of changes mirrors that of a shock wave and it remains uncertain how human society and individual psyches will adjust to this acceleration, or if adaptation is even feasible for the majority.</p><p>Another thing is that while the unprecedented acceleration equals the opportunities a bit it creates an extreme rush and rivalry. <strong>By no means you can stop learning in such a world.</strong> While some may accept this, others simply are not prepared for it.</p><p>As our psyche is not designed for such a pace of changes, they may cause a general feeling of insecurity and uncertainty. The speed of tech growth levered by VC money leaves little chance for the common people to grasp that new world, adopt it, and adapt. The psychological effects of living in this unstable and less predictable world are far from beneficial, but no one is going to slow down; remember <a href="https://futureoflife.org/open-letter/pause-giant-ai-experiments/">the letter</a> we had last year?</p><h3>Conclusion</h3><p><strong>Knowledge sharing has been the key factor in humanity&#39;s progress. </strong><br>LLMs are the current pinnacle of this progress, accelerating information sharing, knowledge creation, and the economic growth spiral. That’s because <strong>LLMs are the new generation of interfaces to information</strong>, providing its transformation according to user queries and tasks, thus untapping instant knowledge sharing. Add agents, capable of completing complex tasks and pipelines, and you’ll get an <strong>augmented intelligence reality that we are facing</strong>.</p><p>The bright future of humanity in the e/acc paradigm is defined by this free knowledge flow and AI-enhanced intellectual work, accelerating tech progress and ushering us to the fabulous singularity. <br><strong>LLM-powered knowledge assistants are a crucial part of this acceleration.</strong></p><p>The world has entered a new phase, and we have to adapt faster than ever before. Better off with an assistant to help :)</p><p>Just curious — is that singularity yet?</p><p><em>Find me on </em><a href="https://www.linkedin.com/in/ivan-ilin-/"><em>LinkedIn</em></a><em> or </em><a href="https://twitter.com/ivanilin9"><em>Twitter</em></a><em> to challenge the opinions &amp; ideas shared above!</em></p><p><em>The main references are collected in my knowledge base, there is a co-pilot to chat with this set of documents: </em><a href="https://app.iki.ai/playlist/393"><em>https://app.iki.ai/playlist/393</em></a><em>.</em></p><p><strong><em>References</em></strong></p><p>[1] <a href="https://a16z.com/the-techno-optimist-manifesto/">https://a16z.com/the-techno-optimist-manifesto/</a></p><p>[2] <a href="https://a16z.com/open-source-from-community-to-commercialization/">https://a16z.com/open-source-from-community-to-commercialization/</a></p><p>[3] <a href="https://hackernoon.com/scratching-the-singularity-surface-the-past-present-and-mysterious-future-of-llms">https://hackernoon.com/scratching-the-singularity-surface-the-past-present-and-mysterious-future-of-llms</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=db87cb516b79" width="1" height="1" alt=""><hr><p><a href="https://pub.towardsai.net/through-knowledge-sharing-to-singularity-accelerated-by-llms-db87cb516b79">Through knowledge sharing to singularity, accelerated by LLMs</a> was originally published in <a href="https://pub.towardsai.net">Towards AI</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Advanced RAG Techniques: an Illustrated Overview]]></title>
            <link>https://pub.towardsai.net/advanced-rag-techniques-an-illustrated-overview-04d193d8fec6?source=rss-4a0803226977------2</link>
            <guid isPermaLink="false">https://medium.com/p/04d193d8fec6</guid>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[mls]]></category>
            <category><![CDATA[retrieval-augmented]]></category>
            <category><![CDATA[llm]]></category>
            <dc:creator><![CDATA[IVAN ILIN]]></dc:creator>
            <pubDate>Sun, 17 Dec 2023 12:02:51 GMT</pubDate>
            <atom:updated>2024-01-03T23:36:36.472Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1fgXZ3jvzFYVY0tU_f53cw.jpeg" /><figcaption>Groningen, Martinitoren, where the article was composed in the peace of the Noorderplatsoen</figcaption></figure><h3>A comprehensive study of the advanced retrieval augmented generation techniques and algorithms, systemising various approaches. The article comes with a collection of links in my knowledge base referencing various implementations and studies mentioned.</h3><p><em>Since the goal of the post is to make an overview &amp; explanation of avaliable RAG algorithms and techniques, I won’t dive into implementations details in code, just referencing them and leaving it to the vast </em><a href="https://app.iki.ai/playlist/236"><em>documentation &amp; tutorials</em></a><em> available.</em></p><h3>Intro</h3><p><em>If you are familiar with the RAG concept, please skip to the Advanced RAG part.</em></p><p>Retrieval Augmented Generation, aka RAG, provides LLMs with the information retrieved from some data source to ground its generated answer on. <strong>Basically RAG is Search + LLM prompting</strong>, where you ask the model to answer the query provided the information found with the search algorithm as a context. Both the query and the retrieved context are injected into the prompt that is sent to the LLM.</p><p>RAG is the most popular architecture of the LLM based systems in 2023. There are many products build almost solely on RAG — from Question Answering services combining web search engines with LLMs to hundreds of chat-with-your-data apps.</p><p>Even the <strong>vector search</strong> area got pumped by that hype although embedding based search engines were made with <a href="https://faiss.ai">faiss</a> back in 2019. Vector database startups like <a href="https://github.com/chroma-core/chroma">chroma</a>, <a href="https://weaviate.io">weavaite.io</a> and <a href="https://www.pinecone.io">pinecone</a> have been built upon existing open source search indices — mainly faiss and <a href="https://github.com/nmslib/nmslib">nmslib</a> — and added an extra storage for the input texts plus some other tooling lately.</p><p><strong>There are two most prominent open source libraries for LLM-based pipelines &amp; applications</strong> — <a href="https://python.langchain.com/docs/get_started/introduction">LangChain</a> and <a href="https://docs.llamaindex.ai/en/stable/">LlamaIndex</a>, founded with a month difference in October and November 2022, respectfully, inspired by the ChatGPT launch and having gained massive adoption in 2023.</p><blockquote><em>The purpose of this article is to systemise the key advanced RAG techniques with references to their implementations — mostly in the LlamaIndex — in order to facilitate other developers’ dive into the technology.</em></blockquote><blockquote><em>The problem is that most of the tutorials cherry-pick one or several techniques and explain in details how to implement them rather than decribing the full variety of the avaliable tools .</em></blockquote><blockquote><em>Another thing is that both LlamaIndex and LangChain are amazing open source projects, developing at such a pace that their documentation is already thicker than a machine learning textbook in 2016.</em></blockquote><h3>Naive RAG</h3><p>The starting point of the RAG pipeline in this article would be a corpus of text documents — we skip everything before that point, leaving it to the amazing open source data loaders connecting to any imaginable source from Youtube to Notion.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Ko_ihY8ecAukf2g1.png" /><figcaption>A scheme by author, as well all the schemes further in the text</figcaption></figure><p><strong>Vanilla RAG case</strong> in brief looks the following way: you split your texts into chunks, then you embed these chunks into vectors with some Transformer Encoder model, you put all those vectors into an index and finally you create a prompt for an LLM that tells the model to answers user’s query given the context we found on the search step. <br>In the runtime we vectorise user’s query with the same Encoder model and then execute search of this query vector against the index, find the top-k results, retrieve the corresponding text chunks from our database and feed them into the LLM prompt as context.</p><p>The prompt can look like that:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/30b7701044f1c52a209404dde05c8f9b/href">https://medium.com/media/30b7701044f1c52a209404dde05c8f9b/href</a></iframe><p><a href="https://docs.llamaindex.ai/en/latest/examples/prompts/prompts_rag.html"><strong>Prompt engineering</strong></a> is the cheapest thing you can try to improve your RAG pipeline. Make sure you’ve checked a quite comprehensive OpenAI <a href="https://platform.openai.com/docs/guides/prompt-engineering/strategy-write-clear-instructions">prompt engineering guide</a>.</p><p>Obviously despite OpenAI being the market leader as an LLM provider there is a number of alternatives such as <a href="https://www.anthropic.com/product">Claude</a> from Anthropic, recent trendy smaller but very capable models like <a href="https://mistral.ai/news/mixtral-of-experts/">Mixtral</a> form Mistral, <a href="https://www.microsoft.com/en-us/research/blog/phi-2-the-surprising-power-of-small-language-models/">Phi-2</a> from Microsoft and many open source options like <a href="https://huggingface.co/blog/llama2">Llama2</a>, <a href="https://huggingface.co/openlm-research">OpenLLaMA</a>, <a href="https://huggingface.co/tiiuae">Falcon</a>, so you have a choice of the brain for your RAG pipeline.</p><h3>Advanced RAG</h3><p>Now we’ll dive into the overview of the advanced RAG techniques.<br>Here is a scheme depicting core steps and algorithms involved. <br>Some logic loops and complex multistep agentic behaviours are omitted to keep the scheme readable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Gr_JqzdpHu7enWG9.png" /><figcaption>Some key components of an advanced RAG architecture. It’s more a choice of available instruments than a blueprint.</figcaption></figure><p>The green elements on the scheme are the core RAG techniques discussed further, the blue ones are texts. Not all the advanced RAG ideas are easily visualised on a single scheme, for example, various context enlarging approaches are omitted — we’ll dive into that on the way.</p><h4>1. Chunking &amp; vectorisation</h4><p>First of all we want to create an index of vectors, representing our document contents and then in the runtime to search for the least cosine distance between all these vectors and the query vector which corresponds to the closest semantic meaning.</p><p><strong>1.1 Chunking </strong><br>Transformer models have fixed input sequence length and even if the input context window is large, the vector of a sentence or a few better represents their semantic meaning than a vector averaged over a few pages of text (depends on the model too, but true in general), so <strong>chunk your data</strong> — split the initial documents in chunks of some size without loosing their meaning (splitting your text in sentences or in paragraphs, not cutting a single sentence in two parts). There are various text splitter implementations capable of this task.</p><p><strong>The size of the chunk is a parameter to think of </strong>— it depends on the embedding model you use and its capacity in tokens, standard transformer Encoder models like BERT-based Sentence Transformers take 512 tokens at most, OpenAI ada-002 is capable of handling longer sequences like 8191 tokens, but <strong>the compromise here is enough context for the LLM to reason upon vs specific enough text embedding in order to efficiently execute search upon</strong>. <a href="https://www.pinecone.io/learn/chunking-strategies/">Here</a> you can find a research illustrating chunk size selection concerns. In LlamaIndex this is covered by the <a href="https://docs.llamaindex.ai/en/stable/api_reference/service_context/node_parser.html">NodeParser class</a> with some advanced options as defining your own text splitter, metadata, nodes / chunks relations, etc.</p><p><strong>1.2 Vectorisation</strong><br>The next step is to choose a <strong>model to embed our chunks</strong> — there are quite some options, I go with the <strong>search optimised models</strong> like <a href="https://huggingface.co/BAAI/bge-large-en-v1.5">bge-large</a> or <a href="https://huggingface.co/intfloat/multilingual-e5-large">E5</a> embeddings family — just check the <a href="https://huggingface.co/spaces/mteb/leaderboard">MTEB leaderboard</a> for the latest updates.</p><p>For an end2end implementation of the chunking &amp; vectorisation step check an <a href="https://docs.llamaindex.ai/en/latest/module_guides/loading/ingestion_pipeline/root.html#">example</a> of a full data ingestion pipeline in LlamaIndex.</p><h4>2. Search index<br>2.1 Vector store index</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*fCxtcFf8gIgnaJfE.png" /><figcaption>In this scheme and everywhere further in the text I omit the Encoder block and send our query straight to the index for the scheme simplicity. The query always gets vectorised first of course. Same with the top k cunks — index retrieves top k vectors, not chunks, but I replace them with chunks as fetching them is a trivial step.</figcaption></figure><p><strong>The crucial part of the RAG pipeline is the search index</strong>, storing your vectorised content we got in the previous step. The most naive implementation uses a flat index — a brute force distance calculation between the query vector and all the chunks’ vectors.</p><p><strong>A proper search index, optimised for efficient retrieval</strong> on 10000+ elements scales <strong>is a vector index</strong> like <a href="https://faiss.ai">faiss</a>, <a href="https://github.com/nmslib/nmslib">nmslib</a> or <a href="https://github.com/spotify/annoy">annoy</a>, using some Approximate Nearest Neighbours implementation like clustring, trees or <a href="https://www.pinecone.io/learn/series/faiss/hnsw/">HNSW</a> algorithm.</p><p>There are also managed solutions like OpenSearch or ElasticSearch and vector databases, taking care of the data ingestion pipeline described in step 1 under the hood, like <a href="https://www.pinecone.io">Pinecone</a>, <a href="https://weaviate.io">Weaviate</a> or <a href="https://www.trychroma.com">Chroma</a>.</p><p>Depending on your index choice, data and search needs <strong>you can also store metadata along with vectors</strong> and then use <strong>metadata filters</strong> to search for information within some dates or sources for example.</p><p>LlamaIndex supports lots of <a href="https://docs.llamaindex.ai/en/latest/community/integrations/vector_stores.html">vector store indices</a> but there are also other simpler index implementations supported like list index, tree index, and keyword table index — we’ll talk about the latter in the Fusion retrieval part.</p><h4>2. 2 Hierarchical indices</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*nDwj0Jgpyk2qc_qJ.png" /></figure><p>In case you have many documents to retrieve from, you need to be able to efficiently search inside them, find relevant information and synthesise it in a single answer with references to the sources. An efficient way to do that in case of a large database is to <strong>create two indices — one composed of summaries and the other one composed of document chunks</strong>, and to search in two steps, first filtering out the relevant docs by summaries and then searching just inside this relevant group.</p><h4>2.3 Hypothetical Questions and HyDE</h4><p>Another approach is to ask an LLM to <strong>generate a question for each chunk and embed these questions in vectors</strong>, at runtime performing query search against this index of question vectors (replacing chunks vectors with questions vectors in our index) and then after retrieval route to original text chunks and send them as the context for the LLM to get an answer.<br>This approach improves search quality due to a <strong>higher semantic similarity between query and hypothetical question</strong> compared to what we’d have for an actual chunk.</p><p>There is also the reversed logic apporach called <a href="http://boston.lti.cs.cmu.edu/luyug/HyDE/HyDE.pdf"><strong>HyDE</strong></a><strong> </strong>— you ask an LLM to generate a hypothetical response given the query and then use its vector along with the query vector to enhance search quality.</p><h4>2.4 Context enrichment</h4><p><strong>The concept here is to retrieve smaller chunks for better search quality</strong>, <strong>but add up surrounding context for LLM to reason upon</strong>.<br>There are two options — to expand context by sentences around the smaller retrieved chunk or to split documents recursively into a number of larger parent chunks, containing smaller child chunks.</p><p><a href="https://docs.llamaindex.ai/en/stable/examples/node_postprocessor/MetadataReplacementDemo.html"><strong>2.4.1 Sentence Window Retrieval</strong></a><br>In this scheme each sentence in a document is embedded separately which provides great accuracy of the query to context cosine distance search.<br>In order to better reason upon the found context after fetching the most relevant single sentence we extend the context window by <em>k</em> sentences before and after the retrieved sentence and then send this extended context to LLM.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*JKZ9m_c6jyIKqCWu.png" /></figure><p>The green part is the sentence embedding found while search in index, and the whole black + green paragraph is fed to the LLM to enlarge its context while reasoning upon the provided query</p><p><strong>2.4.2 </strong><a href="https://docs.llamaindex.ai/en/latest/examples/retrievers/auto_merging_retriever.html"><strong>Auto-merging Retriever</strong></a><strong> (aka </strong><a href="https://python.langchain.com/docs/modules/data_connection/retrievers/parent_document_retriever"><strong>Parent Document Retriever</strong></a><strong>)</strong></p><p>The idea here is pretty much similar to Sentence Window Retriever — to search for more granular pieces of information and then to extend the context window before feeding said context to an LLM for reasoning. Documents are split into smaller child chunks referring to larger parent chunks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*x4rMd50GP99OSDuo.png" /><figcaption>Documents are splitted into an hierarchy of chunks and then the smallest leaf chunks are sent to index. At the retrieval time we retrieve k leaf chunks, and if there is n chunks referring to the same parent chunk, we replace them with this parent chunk and send it to LLM for answer generation.</figcaption></figure><p>Fetch smaller chunks during retrieval first, then if more than <em>n</em> chunks in top <em>k</em> retrieved chunks are linked to the same parent node (larger chunk), we replace the context fed to the LLM by this parent node — works like auto merging a few retrieved chunks into a larger parent chunk, hence the method name. Just to note — search is performed just within the child nodes index. Check the LlamaIndex tutorial on <a href="https://docs.llamaindex.ai/en/stable/examples/retrievers/recursive_retriever_nodes.html">Recursive Retriever + Node References</a> for a deeper dive.</p><h4>2.5 Fusion retrieval or hybrid search</h4><p>A relatively old idea that you could <strong>take the best from both worlds — keyword-based old school search </strong>— sparse retrieval algorithms like <a href="https://en.wikipedia.org/wiki/Tf–idf">tf-idf</a> or search industry standard <a href="https://en.wikipedia.org/wiki/Okapi_BM25">BM25</a> — <strong>and modern </strong>semantic or<strong> vector search and combine it in one retrieval result.</strong> <br>The only trick here is to properly combine the retrieved results with different similarity scores — this problem is usually solved with the help of the <a href="https://plg.uwaterloo.ca/~gvcormac/cormacksigir09-rrf.pdf">Reciprocal Rank Fusion</a> algorithm, reranking the retrieved results for the final output.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*0pQbhBEez7U-2knd.png" /></figure><p>In LangChain this is implemented in the <a href="https://python.langchain.com/docs/modules/data_connection/retrievers/ensemble">Ensemble Retriever</a> class, combining a list of retrievers you define, for example a faiss vector index and a BM25 based retriever and using RRF for reranking.</p><p>In LlamaIndex this is <a href="https://docs.llamaindex.ai/en/stable/examples/retrievers/reciprocal_rerank_fusion.html">done</a> in a pretty similar fashion.</p><p>Hybrid or fusion search usually provides better retrieval results as two complementary search algorithms are combined, taking into account both semantic similarity and keyword matching between the query and the stored documents.</p><h4>3. Reranking &amp; filtering</h4><p>So we got our retrieval results with any of the algorithms described above, now it is time to refine them through filtering, re-ranking or some transformation. In LlamaIndex there is a variety of available <a href="https://docs.llamaindex.ai/en/stable/module_guides/querying/node_postprocessors/root.html"><strong>Postprocessors</strong></a>, <strong>filtering out results based on similarity score, keywords, metadata or reranking them with other models </strong>like an LLM, <br><a href="https://www.sbert.net/examples/applications/cross-encoder/README.html">sentence-transformer cross-encoder</a>, Cohere reranking <a href="https://txt.cohere.com/rerank/">endpoint</a> <br>or based on metadata like date recency — basically, all you could imagine.</p><p>This is the final step before feeding our retrieved context to LLM in order to get the resulting answer.</p><p>Now it is time to get to the more sophisticated RAG techniques like Query transformation and Routing, both involving LLMs and thus representing <strong>agentic behaviour — some complex logic involving LLM reasoning within our RAG pipeline.</strong></p><h4>4. Query transformations</h4><p><strong>Query transformations are a family of techniques using an LLM as a reasoning engine to modify user input in order to improve retrieval quality. </strong>There are different options to do that.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*DP6RrSA2OkcHnWIV.png" /><figcaption>Query transformation principles illustrated</figcaption></figure><p><strong>If the query is complex, LLM can decompose it into several sub queries.</strong> For examle, if you ask: <br><em> — “What framework has more stars on Github, Langchain or LlamaIndex?”,<br></em>and it is unlikely that we’ll find a direct comparison in some text in our corpus so it makes sense to decompose this question in two sub-queries presupposing simpler and more concrete information retrieval: <br><em> — “How many stars does Langchain have on Github?” <br> — “How many stars does Llamaindex have on Github?”<br></em>They would be executed in parallel and then the retrieved context would be combined in a single prompt for LLM to synthesize a final answer to the initial query. Both libraries have this functional implemented — as a <a href="https://python.langchain.com/docs/modules/data_connection/retrievers/MultiQueryRetriever?ref=blog.langchain.dev">Multi Query Retriever</a> in Langchain and as a <a href="https://docs.llamaindex.ai/en/stable/examples/query_engine/sub_question_query_engine.html">Sub Question Query Engine</a> in Llamaindex.</p><ol><li><a href="https://arxiv.org/pdf/2310.06117.pdf?ref=blog.langchain.dev"><strong>Step-back prompting</strong></a><strong> uses LLM to generate a more general query</strong>, retrieving for which we obtain a more general or high-level context useful to ground the answer to our original query on. <br>Retrieval for the original query is also performed and both contexts are fed to the LLM on the final answer generation step. <br>Here is a LangChain <a href="https://github.com/langchain-ai/langchain/blob/master/cookbook/stepback-qa.ipynb?ref=blog.langchain.dev">implementation</a>.</li><li><strong>Query re-writing uses LLM to reformulate initial query</strong> in order to improve retrieval. Both <a href="https://github.com/langchain-ai/langchain/blob/master/cookbook/rewrite.ipynb?ref=blog.langchain.dev">LangChain</a> and <a href="https://llamahub.ai/l/llama_packs-fusion_retriever-query_rewrite">LlamaIndex</a> have implementations, tough a bit different, I find LlamaIndex solution being more powerful here.</li></ol><h4>Reference citations</h4><p>This one goes without a number as this is more an instrument than a retrieval improvement technique, although a very important one. <br><strong>If we’ve used multiple sources to generate an answer</strong> either due to the initial query complexity (we had to execute multiple subqueries and then to combine retrieved context in one answer), or because we found relevant context for a single query in various documents, the question rises if we could <strong>accurately back reference our sources</strong>.</p><p>There are a couple of ways to do that:</p><ol><li><strong>Insert this referencing task into our prompt</strong> and ask LLM to mention ids of the used sources.</li><li><strong>Match the parts of generated response to the original text chunks</strong> in our index — llamaindex offers an efficient <a href="https://github.com/run-llama/llama-hub/tree/main/llama_hub/llama_packs/fuzzy_citation">fuzzy matching based solution</a> for this case. In case you have not heard of fuzzy matching, this is an <a href="https://towardsdatascience.com/fuzzy-matching-at-scale-84f2bfd0c536">incredibly powerful string matching technique</a>.</li></ol><h4>5. <a href="https://docs.llamaindex.ai/en/stable/module_guides/deploying/chat_engines/root.html">Chat Engine</a></h4><p>The next big thing about building a nice RAG system that can work more than once for a single query is the <strong>chat logic, taking into account the dialogue context</strong>, same as in the classic chat bots in the pre-LLM era.<br>This is needed to support follow up questions, anaphora, or arbitrary user commands relating to the previous dialogue context. It is solved by <strong>query compression technique, taking chat context into account </strong>along with the user query.</p><p>As always, there are several approaches to said context compression — <br>a popular and relatively simple <a href="https://docs.llamaindex.ai/en/stable/examples/chat_engine/chat_engine_context.html">ContextChatEngine</a>, first retrieving context relevant to user’s query and then sending it to LLM along with chat history from the <em>memory</em> buffer for LLM to be aware of the previous context while generating the next answer.</p><p>A bit more sophisticated case is <a href="https://docs.llamaindex.ai/en/stable/examples/chat_engine/chat_engine_condense_plus_context.html">CondensePlusContextMode</a> — there in each interaction the chat history and last message are condensed into a new query, then this query goes to the index and the retrieved context is passed to the LLM along with the original user message to generate an answer.</p><p>It’s important to note that there is also support for <a href="https://docs.llamaindex.ai/en/stable/examples/chat_engine/chat_engine_openai.html">OpenAI agents based Chat Engine</a> in LlamaIndex providing a more flexible chat mode and Langchain also <a href="https://python.langchain.com/docs/modules/agents/agent_types/openai_multi_functions_agent">supports</a> OpenAI functional API.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*9cxhMMkUf8veRnRB.png" /><figcaption>An illustration of different Chat Engine types and principles</figcaption></figure><p>There are other Chat engine types like <a href="https://docs.llamaindex.ai/en/stable/examples/chat_engine/chat_engine_react.html">ReAct Agent</a>, but let’s skip to Agents themselves in section 7.</p><h4>6. Query Routing</h4><p><strong>Query routing is the step of LLM-powered decision making upon what to do next given the user query </strong>— the options usually are to summarise, to perform search against some data index or to try a number of different routes and then to synthesise their output in a single answer.</p><p>Query routers are also used to select an index, or, broader, data store, where to send user query — either you have multiple sources of data, for example, a classic vector store and a graph database or a relational DB, or you have an hierarchy of indices — for a multi-document storage a pretty classic case would be an index of summaries and another index of document chunks vectors for example.</p><p><strong>Defining the query router includes setting up the choices it can make.</strong> <br>The selection of a routing option is performed with an LLM call, returning its result in a predefined format, used to route the query to the given index, or, if we are taking of the agnatic behaviour, to sub-chains or even other agents as shown in the <strong>Multi documents agent scheme</strong> below.</p><p>Both <a href="https://docs.llamaindex.ai/en/stable/module_guides/querying/router/root.html">LlamaIndex</a> and <a href="https://python.langchain.com/docs/expression_language/how_to/routing?ref=blog.langchain.dev">LangChain</a> have support for query routers.</p><h4>7. Agents in RAG</h4><p>Agents (supported both by <a href="https://python.langchain.com/docs/modules/agents/">Langchain</a> and <a href="https://docs.llamaindex.ai/en/latest/use_cases/agents.html#">LlamaIndex</a>) have been around almost since the first LLM API has been released — <strong>the idea was to provide an LLM, capable of reasoning, with a set of tools and a task to be completed</strong>. The tools might include some deterministic functions like any code function or an external API or even other agents — this LLM chaining idea is where LangChain got its name from.</p><p>Agents are a huge thing itself and it’s impossible to make a deep enough dive into the topic inside a RAG overview, so I’ll just continue with the agent-based multi document retrieval case, making a short stop at the OpenAI Assistants station<strong> </strong>as it’s a relatively new thing, presented at the <a href="https://openai.com/blog/new-models-and-developer-products-announced-at-devday">recent OpenAI dev conference as GPTs</a>, and working under the hood of the RAG system described below.</p><p><a href="https://platform.openai.com/docs/assistants/overview"><strong>OpenAI Assistants</strong></a> basically have implemented a lot of tools needed around an LLM that we previously had in open source — a chat history, a knowledge storage, a document uploading interface and, maybe most important, <a href="https://platform.openai.com/docs/assistants/tools/function-calling"><strong>function calling API</strong></a>. This latter provides capabilities to <strong>convert natural language into API calls to external tools or database queries.</strong></p><p>In LlamaIndex there is an <a href="https://docs.llamaindex.ai/en/stable/examples/agent/openai_agent.html">OpenAIAgent</a> class marrying this advanced logic with the ChatEngine and QueryEngine classes, providing knowledge-based and context aware chatting along with the ability of multiple OpenAI functions calls in one conversation turn, which really brings the smart agentic behaviour.</p><p>Let’s take a look at the <a href="https://docs.llamaindex.ai/en/stable/examples/agent/multi_document_agents.html"><strong>Multi-Document Agents</strong></a><strong> scheme </strong>— a pretty sophisticated setting, involving initialisation of an <strong>agent</strong> (<a href="https://docs.llamaindex.ai/en/stable/examples/agent/openai_agent.html">OpenAIAgent</a>) <strong>upon each document</strong>, capable of doc summarisation and the classic QA mechanics, <strong>and a top agent</strong>, responsible for queries routing to doc agents and for the final answer synthesis.</p><p>Each document agent has two tools — a vector store index and a summary index, and based on the routed query it decides which one to use. <br>And for the top agent, all document agents are tools respectfully.</p><p>This scheme illustrates an advanced RAG architecture with a lot of routing decisions made by each involved agent. <strong>The benefit of such approach is the ability to compare different solutions or entities, described in different documents and their summaries</strong> along with the classic single doc summarisation and QA mechanics — this basically covers the most frequent chat-with-collection-of-docs usecases.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*FZp2J2NyHHBXPtii.png" /><figcaption>A scheme illustrating multi document agents, involving both query routing and agentic behavior patterns.</figcaption></figure><p>The drawback of such a complex scheme can be guessed from the picture — it’s a bit slow due to multiple back and forth iterations with the LLMs inside our agents. Just in case, an LLM call is always the longest operation in a RAG pipeline — search is optimised for speed by design. So for a large multi document storage I’d recommed to think of some simplifications to this scheme making it scalable.</p><h4>8. Response synthesiser</h4><p>This is the final step of any RAG pipeline — generate an answer based on all the context we carefully retrieved and on the initial user query. <br>The simplest approach would be just to concatenate and feed all the fetched context (above some relevance threshold) along with the query to an LLM at once. <br>But, as always, there are other more sophisticated options involving multiple LLM calls to refine retrieved context and generate a better answer.</p><p><strong>The main approaches to response synthesis are:<br></strong>1. <strong>iteratively refine the answer</strong> by sending retrieved context to LLM chunk by chunk <strong><br></strong>2. <strong>summarise the retrieved context</strong> to fit into the prompt<strong><br></strong>3. <strong>generate multiple answers</strong> based on different context chunks and then to concatenate or <strong>summarise them</strong>. <br>For more details please check the <a href="https://docs.llamaindex.ai/en/stable/module_guides/querying/response_synthesizers/root.html">Response synthesizer module docs</a>.</p><h3>Encoder and LLM fine-tuning</h3><p>This approach involves fine-tuning of some of the two DL models involved in our RAG pipeline — either the Transformer<strong> Encoder, resposible for embeddings quality and thus context retrieval quality</strong> or an <strong>LLM, responsible for the best usage of the provided context to answer user query</strong> — luckily, the latter is a good few shot learner.</p><p>One big advantage nowadays is the availability of high-end LLMs like GPT-4 to generate high quality synthetic datasets. <br>But you should always be aware that taking an open-source model trained by professional research teams on carefully collected, cleaned and validated large datasets and making a quick tuning using small synthetic dataset might narrow down the model’s capabilities in general.</p><h4>Encoder fine-tuning</h4><p>I’ve also been a bit skeptical about the Encoder funetuning approach as the latest Transformer Encoders optimised for search are pretty efficient. <br>So I have tested the performance increase provided by finetuning of <a href="https://huggingface.co/BAAI/bge-large-en-v1.5">bge-large-en-v1.5</a> (top 4 of the <a href="https://huggingface.co/spaces/mteb/leaderboard">MTEB leaderboard</a> at the time of writing) in the <a href="https://docs.llamaindex.ai/en/stable/examples/finetuning/embeddings/finetune_embedding.html">LlamaIndex notebook</a> setting, and it demonstrated a 2% retrieval quality increase. Nothing dramatic but it is nice to be aware of that option, especially if you have a narrow domain dataset you’re building RAG for.</p><h4>Ranker fine-tuning</h4><p><strong>The other good old option is to have a cross-encoder for reranking your retrieved results</strong> if you dont trust your base Encoder completely. <br>It works the following way — you pass the query and each of the top k retrieved text chunks to the cross-encoder, separated by a SEP token, and fine-tune it to output 1 for relevant chunks and 0 for non-relevant. <br>A good example of such tuning process could be found <a href="https://docs.llamaindex.ai/en/latest/examples/finetuning/cross_encoder_finetuning/cross_encoder_finetuning.html#">here</a>, the results say the pairwise score was improved by 4% by cross-encoder finetuning.</p><h4>LLM fine-tuning</h4><p>Recently OpenAI started providing LLM finetuning <a href="https://platform.openai.com/docs/guides/fine-tuning">API</a> and LlamaIndex has a <a href="https://docs.llamaindex.ai/en/stable/examples/finetuning/openai_fine_tuning.html">tutorial</a> on <strong>finetuning GPT-3.5-turbo in RAG setting </strong>to “distill” some of the GPT-4 knowledge. The idea here is to take a document, generate a number of questions with GPT-3.5-turbo, then use GPT-4 to generate answers to these questions based on the document contents (build a GPT4-powered RAG pipeline) and then to fine-tune GPT-3.5-turbo on that dataset of question-answer pairs. The <a href="https://docs.ragas.io/en/latest/index.html">ragas</a> framework used for the RAG pipeline evaluation shows a <strong>5% increase in the faithfulness metrics, meaning the fine-tuned GPT 3.5-turbo model made a better use of the provided context</strong> to generate its answer, than the original one.</p><p>A bit more sophisticated approach is demonstrated in the recent <a href="https://arxiv.org/pdf/2310.01352.pdf">paper</a> <strong>RA-DIT: Retrieval Augmented Dual Instruction Tuning</strong> by Meta AI Research, <strong>suggesting a technique to tune both the LLM and the Retriever</strong> <br>(a Dual Encoder in the original paper) <strong>on triplets of query, context and answer</strong>. For the implementations details please refer to this <a href="https://docs.llamaindex.ai/en/stable/examples/finetuning/knowledge/finetune_retrieval_aug.html#fine-tuning-with-retrieval-augmentation">guide</a>. <br>This technique was used both to fine-tune OpenAI LLMs through the fine-tuning API and Llama2 open-source model (in the original paper), resulting in ~5% increase in knowledge-intense tasks metrics (compared to Llama2 65B with RAG) and a couple percent increase in common sense reasoning tasks.</p><p><em>In case you know better approaches to LLM finetuning for RAG, please share your expertise in the comments section, especially if they are applied to the smaller open source LLMs.</em></p><h3>Evaluation</h3><p>There are <strong>several frameworks for RAG systems performance evaluation </strong>sharing the idea of having a few separate metrics like overall <strong>answer relevance, answer groundedness, faithfulness and retrieved context relevance</strong>.</p><p><a href="https://docs.ragas.io/en/latest/index.html">Ragas</a>, mentioned in the previous section, uses <a href="https://docs.ragas.io/en/latest/concepts/metrics/faithfulness.html">faithfulness</a> and <a href="https://docs.ragas.io/en/latest/concepts/metrics/answer_relevance.html">answer relevance</a> as the generated answer quality metrics and classic context <a href="https://docs.ragas.io/en/latest/concepts/metrics/context_precision.html">precision</a> and <a href="https://docs.ragas.io/en/latest/concepts/metrics/context_recall.html">recall</a> for the retrieval part of the RAG scheme.</p><p>In a recently released great short course <a href="https://learn.deeplearning.ai/building-evaluating-advanced-rag/">Building and Evaluating Advanced RAG</a> by Andrew NG, LlamaIndex and the evaluation framework <a href="https://github.com/truera/trulens/tree/main">Truelens</a>, they suggest the <strong>RAG triad</strong> — <strong>retrieved context relevance</strong> to the query, <strong>groundedness </strong>(how much the LLM answer is supported by the provided context) and <strong>answer relevance</strong> to the query.</p><p>The key and the most controllable metric is the <strong>retrieved context relevance</strong> — basically parts 1–7 of the advanced RAG pipeline described above plus the Encoder and Ranker fine-tuning sections are meant to improve this metric, while part 8 and LLM fine-tuning are focusing on answer relevance and groundedness.</p><p>A good example of a pretty simple retriever evaluation pipeline could be found <a href="https://github.com/run-llama/finetune-embedding/blob/main/evaluate.ipynb">here</a> and it was applied in the Encoder fine-tuning section. <br>A bit more advanced approach taking into account not only the <strong>hit rate</strong>, but the <strong>Mean Reciprocal Rank</strong>, a common search engine metric, and also generated answer metrics such as faithfulness abd relevance, is demonstrated in the OpenAI <a href="https://github.com/openai/openai-cookbook/blob/main/examples/evaluation/Evaluate_RAG_with_LlamaIndex.ipynb">cookbook.</a></p><p>LangChain has a pretty advanced evaluation framework <a href="https://docs.smith.langchain.com">LangSmith</a> where custom evaluators may be implemented plus it monitors the traces running inside your RAG pipeline in order to make your system more transparent.</p><p>In case you are building with LlamaIndex, there is a <a href="https://github.com/run-llama/llama-hub/tree/dac193254456df699b4c73dd98cdbab3d1dc89b0/llama_hub/llama_packs/rag_evaluator">rag_evaluator llama pack</a>, providing a quick tool to evaluate your pipeline with a public dataset.</p><h3>Conclusion</h3><p>I tried to outline the core algorithmic approaches to RAG and to illustrate some of them in hopes this might spark some novel ideas to try in your RAG pipeline, or bring some system to the vast variety of tecniques that have been invented this year — for me 2023 was the most exciting year in ML so far.</p><p><strong>There are many more</strong> <strong>other things to consider</strong> like <strong>web search based RAG </strong>(<a href="https://github.com/run-llama/rags">RAGs</a> by LlamaIndex, <a href="https://blog.langchain.dev/weblangchain/">webLangChain</a>, etc), taking a deeper dive into <strong>agentic architectures</strong> (and the recent <a href="https://blog.langchain.dev/openais-bet-on-a-cognitive-architecture/">OpenAI stake</a> in this game) and some ideas on <a href="https://blog.langchain.dev/adding-long-term-memory-to-opengpts/"><strong>LLMs Long-term memory</strong></a>.</p><p><strong>The main production challenge for RAG systems besides answer relevance and faithfulness is speed</strong>, especially if you are into the more flexible agent-based schemes, but that’s a thing for another post. This streaming feature ChatGPT and most other assistants use is not a random cyberpunk style, but merely a way to shorten the perceived answer generation time. <br>That is why I see a very bright future for the smaller LLMs and recent releases of Mixtral and Phi-2 are leading us in this direction.</p><p><em>Thank you very much for reading this long post!</em></p><p><em>The main references are collected in my knowledge base, there is a co-pilot to chat with this set of documents: </em><a href="https://app.iki.ai/playlist/236"><em>https://app.iki.ai/playlist/236</em></a><em>.</em></p><p><em>Find me on </em><a href="https://www.linkedin.com/in/ivan-ilin-/"><em>LinkedIn</em></a><em> or </em><a href="https://twitter.com/ivanilin9"><em>Twitter</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=04d193d8fec6" width="1" height="1" alt=""><hr><p><a href="https://pub.towardsai.net/advanced-rag-techniques-an-illustrated-overview-04d193d8fec6">Advanced RAG Techniques: an Illustrated Overview</a> was originally published in <a href="https://pub.towardsai.net">Towards AI</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building a news aggregator from scratch: news filtering, classification, grouping in threads and…]]></title>
            <link>https://medium.com/data-science/building-a-news-aggregator-from-scratch-news-filtering-classification-grouping-in-threads-and-7b0bbf619b68?source=rss-4a0803226977------2</link>
            <guid isPermaLink="false">https://medium.com/p/7b0bbf619b68</guid>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[search]]></category>
            <category><![CDATA[natural-language]]></category>
            <category><![CDATA[attention]]></category>
            <category><![CDATA[aggregation]]></category>
            <dc:creator><![CDATA[IVAN ILIN]]></dc:creator>
            <pubDate>Mon, 27 Jan 2020 13:19:32 GMT</pubDate>
            <atom:updated>2020-01-27T13:19:32.855Z</atom:updated>
            <content:encoded><![CDATA[<h3>Building a <strong>news aggregator</strong> from scratch: news filtering, classification, grouping in threads and ranking</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KcTklW8jd5q_6ktz_ee3Dw.jpeg" /><figcaption>Fake news headlines from <a href="https://www.designboom.com/design/the-fake-newsstand-tbwa-chiat-day-columbia-journalism-review-11-05-2018/">https://www.designboom.com/design/the-fake-newsstand-tbwa-chiat-day-columbia-journalism-review-11-05-2018/</a></figcaption></figure><blockquote>The idea behind this post is to show a reasonably simple approach one can implement in a couple of weeks to solve a real-world problem of creating a news aggregator like <a href="https://news.google.com/">Google news</a> or <a href="https://yandex.com/news">Yandex news</a> showing the top news threads out of millions of news scraped all over the web.</blockquote><h3>Problem statement and restrictions</h3><p>So this is another post about NLP where I shall describe a few algorithms for texts filtering, classification, grouping and ranking developed during the <a href="https://contest.com/docs/data_clustering">Telegram Data Clustering contest</a>. The motivation behind this post is to demonstrate that you can build a decent texts processing system and run it on your laptop without even a GPU.</p><p>The contest included five tasks — detecting news languages, filtering news from other texts (such as encyclopedic articles, some random entertainment posts, blogposts, etc), news classification in one of seven categories (Society, Economy, Sports, Science, Technologies, Entertainment and Other), grouping news in threads and ranking these threads by importance.<em> </em>The full contest rules are avaliable <a href="https://contest.com/docs/data_clustering">here</a>.</p><p>Selection of the particular algorithms and instruments was largely dependant on the the contest rules putting some constraints on the implementation — each task had to be executed within 60 sec per 1000 articles on a Debian machine with 8 CPUs and 16 Gb of RAM, there should be no external services or APIs used, the algorithm even sould not assume there is an internet connection (to download some pretrained models, for example). Hence no SOTA Transformer models like BERT, ALBERT or GPT-2 should have been involved.</p><p>The high-level solution architecture described in this article looks the following way:</p><ol><li>Text preprocessing and vectorization</li><li>Texts classification with a custom Deep Neural Network (DNN) with a LSTM and an Attention layers</li><li>Grouping texts in threads with the nearest neighbors search algorithm controlled by the Levenshtein distance within each group.</li><li>Ranking news threads by importance</li></ol><p>Note: all the code and ideas described in this post have been developed during the contest period (2 weeks), though some fine-tuning of the grouping algorithm as well as code styling have been performed afterwards during the New Year holidays.</p><h4>Raw data parsing</h4><p>Contest participants have been provided with hundreds of thousands of publications saved as html files containing news title, text, sometimes an image, a publication date, an author and media source. I used the <a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/">Beatifulsoup </a>library as a handy html parser to extract all the needed data into a pandas dataframe.</p><h4>Language detection</h4><p>This part was pretty straightforward — I used a fast <a href="https://pypi.org/project/langdetect/">langdetect</a> implementation as language detector and feeded it with titles by default to speed up detection — language detection on texts with 10x larger average length is slower.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b3d2c71943f7ae2ea8fd99f1de791944/href">https://medium.com/media/b3d2c71943f7ae2ea8fd99f1de791944/href</a></iframe><p>This step took 12 sec<strong> </strong>per 1000 files, the detection accuracy is over 99%. The resulting data looked the following way:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e1464b62c14b3d9bede74a20e87fb911/href">https://medium.com/media/e1464b62c14b3d9bede74a20e87fb911/href</a></iframe><h4>Text preprocessing logic</h4><p>Text vectorisation logic is one of the core algorithm decisions contest participants had to come up with. We had a large and variative enough corpus of around 1M articles to use pretrained word embeddings instead of the basic TF-IDF approach. But first we had to perform common tokenization and stemming procedures. I used stopwords list and Porter Stemmer from the <a href="http://www.nltk.org">nltk</a> library.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c82add3ec392d3cdbe863cf52129aeae/href">https://medium.com/media/c82add3ec392d3cdbe863cf52129aeae/href</a></iframe><p>After this step each text was represented by a list of word tokens.</p><p>The next step was the replacement of each token in a list with a vector from one of the pretrained language models — <a href="https://nlp.stanford.edu/projects/glove/">Glove</a> or <a href="https://fasttext.cc">fasttext</a>.</p><p>The result of this operation was that each text was now represented by a list of semantically rich word vectors. I have put a restriction on the maximum length of the list — 50 words, the headline was concatenated with the beginning of the article’s body.</p><p>In order to equalise the lenght of all vectors the padding operation has been performed. We’ve now got the feature tensor of our text corpus, each row represents a sequence of pretrained word vectors of the chosen dimension.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1d46c8a7124e2ca305e4eb018abdf63a/href">https://medium.com/media/1d46c8a7124e2ca305e4eb018abdf63a/href</a></iframe><h3>Deep neural network architecture</h3><p>Since we had quite enough data for a neural network training I decided to use a deep neural network (DNN) classifier in order to tell news from not news and to define news topics.</p><p>Taking into account it was a algorithm contest an obvious choice maximising the solution’s accuracy would be a SOTA NLP model architecture, namely some kind of large Transformer like BERT, but as I have mentioned before, these kinds of models would be too large and too slow to pass the restrictions imposed on hardware and on text processing speed. The other drawback is that such model’s training would take a few days leaving me no time for model’s fine-tuning.</p><p>I had to come up with a simpler architecture so I implemented a lightweight neural net with an RNN (LSTM) layer taking the sequence of words embeddings representing texts and an Attention layer on top of it. The output of the network should be the class probabilities (binary classification for news filtering step and multiclass for news categories detection), so the upper part of the network was comprised by a set of fully connected layers.</p><h4>News topic detection — multiclass classification</h4><p>I shall be explaining the selection of particular DNN architecture using the multiclass classifier (detecting news categories) as an example because its objective is more challenging and its performance is independent of the threshold value used in the binary classifier to draw the margin between positive and negative classes.</p><p>The upper part of our neural net was made up of three <strong>Dense layers</strong>, the output layer had 7 units corresponding to the number of classes with the softmax<strong> </strong>activation function and categorical crossentropy<strong> </strong>as a loss function.</p><p>To train it I used the <a href="https://www.kaggle.com/rmisra/news-category-dataset">News_Category_Dataset</a> and applied a mapping logic in order to fit the initial 31 news categories into one of 7 categories: Society, Economy, Sports, Technology, Entertainment, Science and Other to comply with the contest objectives.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dbddc68d0a5d550b96d50e42ca88307a/href">https://medium.com/media/dbddc68d0a5d550b96d50e42ca88307a/href</a></iframe><p>I would like to share the logic behind the NN model’s architecture selection and hyperparameters fine-tuning.</p><ol><li>The <strong>Dropout layers</strong> increase the generalisation ability of our model and prevent it from overfitting (Dropout layer randomly zeroes out the given percentage of weights at each update of the training phase). Model’s overfitting without the Dropout layers is clearly demonstrated by the learning curves — the accuracy on the training set grows up to 95% while the accuracy on the validation dataset barely grows during training.</li><li>A <strong>BatchNormalisation layer</strong> applied on top of the LSTM-Attention construction normalized the activations after the dropout at each batch keeping the activation mean close to 0 and the activation standard deviation close to 1. It tends to increase test accuracy on the larger batch sizes and to decrease it on the smaller ones.</li><li><strong>Regularization </strong>applied to Dense layers penalizes the extreme values of layer parameters.</li><li><strong>Batch size</strong> selection can also affect a model’s performance. The larger the size is the faster your model trains and the more precisely the gradient vector is calculated on each step. This results in noise reduction which makes the model more prone to converging to a local minimum so the batch size selection is usually a tradeoff between speed, memory consumption and model’s performance. Values between 32 and 256 are the common choice, in our case the model showed top accuracy with batch size 64. Increasing batch size up to 512 or 1024 significantly decreases model’s accuracy (by 2% and 4% accordingly)</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/753/1*GeOx8TZPDg6qW7OjfQX6CQ.png" /><figcaption>Model’s performance depending on architecture and hyperparameters</figcaption></figure><h4>Attention layer explained</h4><p>It is worth saying a few words about the attention mechanism used in our model. The theory behind the applied approach is described in the <a href="https://arxiv.org/abs/1512.08756">arXiv paper by Raffel et al.</a> This is a simplified model of attention for feed-forward neural networks addressing the RNN information flow problem for long sequences. Particularly, the attention layer provides an optimal transition to the fully connected layer, creating a <strong>context vector</strong> (an embedding for the sequence of input word vectors) <strong>as the weighted average of the hidden states of the input sequence</strong> with the weights representing the importance of the elements of the sequence. The explicit notation looks the following way:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AtKvTMuA4c8uDHcLcEejsA.png" /></figure><p>where T is the length of sequence and <strong>a</strong> is a learnable function, namely a single hidden layer feed-forward network with the tanh activation function, jointly trained with the global model.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d52c0fdd34305efe5950f2e949434ba9/href">https://medium.com/media/d52c0fdd34305efe5950f2e949434ba9/href</a></iframe><p>The drawback of this model is that it does not take the order within an input sequence into account but for our task of news headlines vectorization this is not as significant as for some sequence-to-sequence problems such as phrase translation.</p><p>There is a number of publications describing the more complicated attention mechanisms designed for seqence-to-sequence problems, a good start would be the <a href="https://www.tensorflow.org/tutorials/text/nmt_with_attention#write_the_encoder_and_decoder_model">Neural Machine Translation with Attention</a> tutorial by Google Research, I would also recommend to check the <a href="https://lilianweng.github.io/lil-log/2018/06/24/attention-attention.html">Attention? Attention!</a> post by Lilian Weng with an overview of attention mechanisms and their evolution and make sure you have read the original <a href="https://arxiv.org/pdf/1409.0473.pdf">Bahdanau et al, 2015</a> paper. Among the more up-to-date LSTM + Attention papers of the post-Transformer era I would recommend reading recent <a href="https://arxiv.org/abs/1911.11423">Single-headed attention RNN</a> by Stephen Merity, proving that the huge Transformers are not the only possible approach.</p><p>P.S. Do not forget to add a saving config in the Attention class as it will enable seamless loading of the saved model with the custom layer.</p><p>An alternative simpler transition to the Dense layers would be the Flatten layer used for tensor reshaping.</p><p><strong>Other ideas &amp; results</strong></p><p>In order to increase the classifier quality one could take advantage<em> </em>of the other information provided like the publication source and the author and add them to the network as one-hot encoded features, but the contest rules explicitly said that the algorithms would be evaluated on different datasets with other specific source list.</p><p>To speed up the training phase I ran the code in a <a href="https://colab.research.google.com/notebooks/welcome.ipynb">colab</a> notebook with TeslaK80 GPU runtime.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/977/1*KpBQ9G8mnnD4SU--xczj8g.png" /><figcaption>Typical model’s learning curves</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ac8799a214b8c27d8e09d89b177ceb28/href">https://medium.com/media/ac8799a214b8c27d8e09d89b177ceb28/href</a></iframe><p>The classification step takes 13 sec per 1000 texts.</p><h4>News / not news — binary classification</h4><p>To filter only news from the given dataset of publications we had to implement a binary classifier. Actually this step has been performed before news categories classification. The binary calssifier had pretty much similar architecture with the obvious difference in the output layer — the last layer had 1 unit with the sigmoid activation function and binary crossentropy<strong> </strong>as a loss function.</p><p>The data provided by contest organizers had over 90% of news publications in it so I decided to filter 100% news by source (The Guardian, Bloomberg, CNN, etc) and then to use an english <a href="https://www.kaggle.com/urbanbricks/wikipedia-promotional-articles#good.csv">Wikipedia atricles</a> (the good ones, not promotional) to represent the NOT NEWS class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*BMykJvS9BQBvBvZNiefTLg.png" /><figcaption>Typical binary model’s learning curves</figcaption></figure><p>The trained binary classifier model outputs the probability of the object being a positive (1) class, in order to interpret these probabilities we needed to impose a treshold wich would tell news from not news. This selection has been performed manually after a long careful look on the marginal cases has been taken.</p><p>The classification step takes 14 sec per 1000 texts. The classification results can be observed in the previous table.</p><h3>Grouping news in threads</h3><blockquote>The news grouping task was solved by building a Ball Tree on the text embedding vectors and then searching this tree with an adaptive search radius controlled by the normalized Levenshtein distance within each group of neighbors.</blockquote><p><strong>Text vectorization</strong></p><p>In order to group news first we should introduce some kind on metrics on the dataset. Since we used DNNs for texts processing and classification the most obvious way to get a text’s vector would be taking the embedding obtained by passing the text through the pretrained multiclass DNN without the last layer. I used the 128 unit dense layer as the output to create texts embedding vectors.</p><p>As I found later this approach was not the optimal one — a significantly better performance in news grouping was demonstrated when I switched to a simpler TF-IDF vectorization. This is quite explainable — while in news category classification we needed to generalize the whole news semantic meaning regardless of particular politician’s names, gadget, celebrities, and even particular circumstances, the sequence of pretrained word vectors fed to the DNN was a suitable choice for the task. In news grouping we are dealing with a different setting — each thread should be describing a very particular event — something has happened to somebody, the characters names, even verbs and adverbial modifiers should be the same across the whole news thread, so the classic TF-IDF (calculating the vector of n-gram frequencies in each text normalized by the n-gram frequencies in the whole corpus) is the approach loosing less valuable information. To fight the TF-IDF output matrix sparsity I have applied an SVD decomposition compressing each text’s vector to the selected dimension (1000 in our case).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dde83ae5e777a232d1e946de3ba9424c/href">https://medium.com/media/dde83ae5e777a232d1e946de3ba9424c/href</a></iframe><p><strong>A fast nearest neighbor search</strong></p><p>The next step is actually texts grouping. The basic unsupervised approach for this task would be clustering, but since we are unlikely to get a perfect clustering from the first shot our algorithm requires an iterative approach and the clustering of the whole dataset is expensive. Besides that we do not have a proper metrics of grouping quality to run an automated hyperparameters tuning procedure, neither we should tune them by hands since it is highly possible that we’ll need a different set of hyperparameters for different news datasets.</p><p>I decided that <strong>a more precise and flexible approach would be the construction of a fast search index on texts embedding vectors and then querying this index. </strong>The efficient approach for fast high-dimensional nearest-neighbor search is to build a binary space partitioning tree, namely a <a href="https://en.wikipedia.org/wiki/Ball_tree">Ball Tree</a> or a <a href="https://en.wikipedia.org/wiki/K-d_tree">KD Tree</a> (a k-dimensional binary search tree is a space-partitioning data structure for organizing points in a k-dimensional space) on the text embeddings without explicitly calculating all the distances within a dataset. These algorithms scale with O(N log(N)) complexity for tree construction. My particular choice was a <a href="https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.BallTree.html#sklearn.neighbors.BallTree">scikit-learn Ball Tree implementation</a> as it has lower query time O(D log(N)) than a KD Tree in high dimensions and we had to optimize query times as far as we intend to perform iterative searches with various search radius for each element in our dataset. For more details on the differences between KDtree and BallTree data structures and the performance benchmarking please refer <a href="https://jakevdp.github.io/blog/2013/04/29/benchmarking-nearest-neighbor-searches-in-python/">to this great post</a> by Jake VanderPlasof, scikit-learn contributor.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/55eadb9ac6a89d789866d08e9318961d/href">https://medium.com/media/55eadb9ac6a89d789866d08e9318961d/href</a></iframe><p><strong>The grouping algorithm</strong></p><p>Ok, now we’ve finally have got an index of all papers and can group them in threads by distance between different samples.</p><p>I did not have enough time to reflect on some more sophisticated approaches and just created <strong>a cycle over all papers in each news category</strong> (we can take advantage of the pervious algorithm step to partition our dataset by news categories) <strong>checking for their neighbors in radius r_start</strong>, r_start was selected empirically in such a way that it took a little more papers than the actual thread contained. Then <strong>I calculated an empirical functional variative_criterium_norm — the normalized Levenstein distance between the texts within the group — and decreased the search radius r_curr iteratively until this functional became less than the empirically found constraint or the search radius size hit r_min constraint</strong>. If there were no news in the given area, I increased the search radius until we found some neighbors and then switched to the radius decreasing branch. The idea behind this iterative search was that similar news have partly similar headlines (some entities like subject, object and sometimes verbs are invariant over the news thread). The other details of the developed algoritms are easier to see from the code snippet.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/46b81adbff9668e0109cefd8cf94dcbb/href">https://medium.com/media/46b81adbff9668e0109cefd8cf94dcbb/href</a></iframe><p>There are 7 hyperparameters controlling the algorithm: r_min, r_max — these control the size of the query area, r_start, r_step — controlling the query area dynamics and vc_min, vc_max, delta_max, controlling the value of normalized Levenshtein distance within a group—this defines the variance of news headlines in a group. These hyperparameters should be tuned after you have chosen the vectorization parameters like the final text embedding size (n_components in SVD) and the range of n_grams used (I used 1-3).</p><p>It is not really obvious how we can estimate a “proper” news grouping, so I did not spend too much time playing with hyperparameters after I got a reasonable grouping result — my intention was to suggest a working approach to solve the problem with the given constraints. Clearly there is some space for improvement like checking if we can merge some of the groups and filtering out some occasional noise. Actually in order to get a reasonable number and density of groups the grouping hyperparameters should be fine tuned to each dataset, so there could be an outer cycle implementing a kind of Randomized search on them. One of the ways to get an idea of the particular grouping we got and to estimate its quality is to check the groups size distribution histogram.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W1YZRhSkd18AATn7Nkoieg.png" /><figcaption>Groups size distribution histogram obtained on a test dataset</figcaption></figure><p>In fact, the described approach could be regarded as a relative of the DBSCAN clustering.</p><p>Execution time varies depending on the hyperparameters chosen for the dataset and the structure of data, the typical values are from 8.5 sec / 1000 papers to 25 sec / 1000 papers including the vectorization time defined by the expensive SVD operation.</p><p>Sorry for the long listing, here are the <a href="https://gist.github.com/isvanilin/3ea2fc6dc43162855c59bdc281ea642f#file-news_groups-txt">full results for the SOCIETY category</a> ranked by groups size</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a3162d70881279caa6044aa41e52777c/href">https://medium.com/media/a3162d70881279caa6044aa41e52777c/href</a></iframe><h3>Threads ranking</h3><p>Actually there are lots of features to use when it comes to threads ranking, the problem is that this ranking may be quite subjective depending on the region and the interests of a particular user. We may agree that there are global and local news, some speculations/opinions on ploitical issues and mere facts, but some new information about Trump’s impeachment hearings may outweigh the global but distant tragedies like forest fires in Australia — <strong>the importance of news is a perceived, not an objective characteristic</strong>.</p><p>In the view of the foregoing I shall just describe the approach to handle this task: the most obvious features for threads ranking would be the number of publications in a thread, the ranking of the sources (in the <em>Society</em> category for example Bloomberg and Financial Times are the most respectable sources) and the semantic meaning of the thread like ‘international politics’, ‘global economy’, ‘war’, ‘global accident’, ‘local accident/crime’ etc. This approach presupposes manual introduction and ranking of these semantic categories and manual ranking of the news sources. If we do not want to create features manually, another possible approach would be to manually rank some selection of threads, to calculate their mean semantic vectors (average any vectorization we have used) and then to use these vectors as the predictors to train out ranking model (a kind of regression model) based on the scores we have assigned to threads importance.</p><p>In fact, a quite reasonable ranking can be obtained just by sorting the threads by size. Given this fact and the fact that I have not implemented the ranking part during the contest, I shall leave the enthusiastic ones free to try out any of the approaches discussed above.</p><p>One important issue is that the contest presupposed a static ranking — we are given all news for the same date, but in reality news are time-dependent and loose their novelty as time passes, this relevance decay could be fairly enough described with the exp(-t) function.</p><p>Thank you for reading this long post, I hope you have found some ideas to use in your NLP projects.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7b0bbf619b68" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/building-a-news-aggregator-from-scratch-news-filtering-classification-grouping-in-threads-and-7b0bbf619b68">Building a news aggregator from scratch: news filtering, classification, grouping in threads and…</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Check out iki.ai, a new professional growth ecosystem]]></title>
            <link>https://medium.com/@ivanilin_iki/https-medium-com-iki-ai-professional-growth-ecosystem-38fb47875434?source=rss-4a0803226977------2</link>
            <guid isPermaLink="false">https://medium.com/p/38fb47875434</guid>
            <category><![CDATA[product-launch]]></category>
            <category><![CDATA[professional-development]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[continuous-learning]]></category>
            <dc:creator><![CDATA[IVAN ILIN]]></dc:creator>
            <pubDate>Wed, 21 Feb 2018 12:50:05 GMT</pubDate>
            <atom:updated>2023-07-06T17:32:00.417Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gi84DFyriCI5yKf0XxVFbA.jpeg" /></figure><p><em>Value of information decreases as its volume grows, and time is more precious than ever so we have the ambition to provide the most efficient ways of spending your time for professional growth.</em></p><p><strong>The future is here</strong></p><p>Technology progress comes at an exponential rate. The humankind now stands at the threshold of the fourth industrial revolution which will bring together achievements of biotechnology, physics, robotics and IT. Artificial intelligence, the blockchain, quantum computing, biohacking, next-generation human-computer interfaces will cause disruptive changes in world economy. These global processes change labor market. Today cycles of technology trends come faster than a classical academy course. When you were entering university there was little demand for AI scientists, AR designers or blockchain engineers. In order to keep being a competitive professional, you have to live in the lifelong education paradigm. People will be changing several industries during their lifespan and globalisation is enhancing competition, so you better get ready for this marathon.</p><p>Education has always defined person’s life path and his social class. Education has always been a very powerful social elevator. Today this elevator has opened its’ doors for everyone. World’s top level academy courses and the foremost technologies are accessible for everyone having access to the Internet by means of such educational platforms as Coursera, EdX, Udacity, etc, or technology resources such as Github or StackOverflow. This counts up to 3 bln people around the globe. Even language barriers are obliterated with recent advances in speech recognition and Natural Language Understanding.</p><p>There are only two main barriers left — the excessive amount of information available and lack of personal motivation and self-confidence to step on the right track.</p><p><strong>Ikigai philosophy</strong></p><p>But why the education track is the right one? Maybe there is too much competition — the higher you get, the higher the stakes are, with management consultants and investment bankers on top working 16 hours a day, sleeping in their office or Silicon Valley engineers spending their lives looking at the blue screens 70% of their awake time?</p><p>The answer is quite simple — because you have to find your ikigai — the reason for your being. This is a concept from Japanese philosophy (to be more exact, from Okinawa), meaning the reason to wake up every morning.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ob_mVVGyPSgfxlSVuFZJ_A.png" /></figure><p>Ikigai is the intersection of the things that you love, you are good at, of things that world needs and of the ones that are paid for. In order to find it, you need to try various things in life — traveling, going in for sports, reading and starting relations. But the main point is trying to create something, be that a painting, a computer program or a new business process. I would call this concept “modern” or “western” ikigai.</p><p><strong>iki.ai</strong> service was invented to help people enroll in this process of trials and errors, to push barriers of their knowledge and skills in a more systematic and engaging way than people usually do. iki.ai is your personal career advisor powered by machine learning and artificial intelligence related technologies.</p><p><strong>Professional growth algorithm</strong></p><p>iki.ai service has two main use cases. First is helping a young professional or a student to navigate through various career options, set a career goal and providing him with all the tools needed to achieve it. This case also includes professionals willing to make a major pivot in their career such as switching industry — it will be much easier to make the decision having clear understanding of needed skills and career options in the new field. The second scenario is for experienced professionals willing to excel — they get carefully filtered content and news feed from the selected areas of professional expertise.</p><p>Let’s go a little deeper and see how exactly this works and what value is delivered.</p><p>All data analysis is based on your professional experience provided by Linkedin login or CV upload along with a single screens’ tags selection mechanic resembling Apple Music experience.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zfl2n9n9ghIm4M4bG3jOEw.png" /><figcaption>Career graph for IT industry with path from intern to product VP (left). Personal development plan leading to portfolio manager position in Financial industry (right)</figcaption></figure><p><strong>In the first scenario</strong>, user explores career graph — typical industry positions with the routing function using real-world career statistics. Once one of the positions is set as career goal the route is broken down into a list of career steps with key skills and responsibilities at each position. iki.ai shall calculate your personal route from a junior developer to the head of a research group or to the managing partner of a hedge fund. Your personal development plan will include recommendations of the most relevant online courses and trainings, platforms and local communities. Personal news feed covering topics of interest and needed skills powered by a custom recommending system will be with you on the chosen track, fetching carefully filtered fresh blogposts, news and publications in selected professional areas on daily basis.</p><p>Motivation cannot be underestimated, that is why we unite people on the same career track in a community. Within this group members will see each other’s progress, feedback on content and will share achievements. We shall be tracking your progress and showing your peer’s successes. Your peers will be your team — share your insights and help each other to improve.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lBw-jUiPu8GunawnaQFipw.png" /><figcaption>Social line showing your peers and their most recent achievements with Y coordinate representing each user’s progress (left). Achievement card (position change) shown in user’s feed (right).</figcaption></figure><p><strong>The second scenario</strong> is designed for seasoned professionals looking more for insights than for motivation — these guys are provided with content feed directly without any career goal setting.</p><p>Our feed is smart — you can chat with it and specify your particular interest, goal, expertise level or type of the content fetched for you from the web. Our intention here is to save user’s time by cutting informational noize and secondary publications with careful information aggregation, filtering, and ranking both with ML algorithms and user ratings.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eg7uOcB3ldeAzT7goqLQjw.png" /></figure><p><strong>Featured technologies &amp; innovation</strong></p><p>AI now is the hot trend, machine learning and data science becoming a casual approach. All major features of iki.ai service are empowered with machine learning algorithms starting from user’s experience processing (vectorizing text data) and classification in one of the points on the career graph (which is built by clustering of a large amount of career data within each industry to form position clusters with similar job description) continuing with machine learning ranked courses recommendations and the smart content feed. Daily feed is generated via automated web data collection and processing with an advanced ih-house developed deep learning recommending system on top. A chat-bot mechanic will be integrated for a more interactive feeling and precise recommendations. Technology stack and UX design ideas will be described in more details in further publications.</p><p><strong>Conclusion</strong></p><p>Our motivation to build this product was quite simple — both founders missed an egocentric interactive service focused on user’s professional growth, bringing together education, career goals, and personal development. Ivan was a little puzzled trying to find his way in the variety of open possibilities after defending his Ph.D. in Applied Mathematics. Max was really busy working as CEO and Creative Director of <a href="https://en.ony.ru">ONY agency</a> which he founded 17 years ago, having little time left for self-improvement. We both felt that knowledge delivery job could be optimized and automated, that now there is little time left for scrolling Facebook newsfeed as well as for checking dozens of good quality blogs every couple of days. We also presumed that automatically formed new social groups of professionals with similar aspirations pushing each others expertise would make that painful self-improvement process more engaging and meaningful for each member.</p><p>Due to the past industrial revolutions, people got more free time and the unlimited access to information. We have the ambition to help you use this information in order to find your ikigai and to fulfill your potential. To help people move at modern world’s pace, running with technology’s exponential acceleration.</p><p>A public beta will be released soon, please keep in touch to be among the first adopters. Check out our web page <a href="https://iki.ai">iki.ai</a> and subscribe to updates on Facebook: <a href="https://www.facebook.com/ikiservice/">fb.me/ikiservice</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=38fb47875434" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>