# Overview of RAG Approaches with Vector Databases

<mark style="color:green;">Significance in Customisation for Specific Use Cases:</mark> RAG is particularly useful for customising language models to specific organisational needs and datasets, making it a common initial use case in many companies.

<mark style="color:green;">Complexity in Retrieval Augmented Generation:</mark> The retrieval process in RAG involves identifying the most relevant document segments to be fed into the model for generating responses. This process is complicated by the need to balance the context size – the amount of data the LLM can process at once – and the specificity of the information retrieved.

<mark style="color:green;">Experimentation and Adaptation:</mark> The process of chunking, embedding generation, and retrieval requires experimentation and adaptation to the specific document set and application requirements. This is a trial-and-error process, where the chunk size and the method of handling overlaps are fine-tuned for optimal retrieval and context representation.

<mark style="color:green;">Challenges in Document Parsing:</mark> A significant challenge in implementing RAG is the parsing of documents. The context and structure of the documents are crucial. For example, <mark style="color:yellow;">policies and regulations have hierarchical structures that need to be retained in the chunks for the context to remain intact.</mark> Misrepresenting or losing this structure in parsing can lead to incomplete or incorrect context, affecting the response accuracy.

<mark style="color:green;">File Parsing Techniques and Challenges:</mark> Parsing various document formats (like PDFs, Word documents, or Excel spreadsheets) into a data structure that preserves meaning while facilitating  model operations is a major challenge.&#x20;

<mark style="color:green;">Practical Challenges and Techniques:</mark> The real-world application of RAG involves *<mark style="color:yellow;">**dealing with messy, diverse data**</mark>*<mark style="color:yellow;">.</mark> Standard libraries and tools may not always be sufficient. Customised solutions might be required to handle specific data types, formats, and structures effectively.

<mark style="color:green;">Data Diversity and Complexity:</mark> In real-world applications, data sources are not limited to plain text documents.  They include various formats like PDFs, Word documents, SharePoint sites, and even complex elements like tables. This *<mark style="color:yellow;">**diversity adds complexity to the process of converting these various formats into a uniform structure suitable for embedding and retrieval**</mark>*<mark style="color:yellow;">.</mark>

<mark style="color:green;">Optimising Embedding Generation:</mark> Generating embeddings for appropriately sized chunks of text is crucial.  Too small chunks may miss context, while too large ones may contain excessive information. Finding the right balance is key to creating useful embeddings that represent meaningful text segments for effective retrieval.

<mark style="color:green;">Handling Overlapping Contexts:</mark> To ensure comprehensive context coverage without missing crucial information, chunks or windows of text for embedding generation might need to overlap. This approach allows for a broader context to be captured, improving the effectiveness of the retrieval process.

<mark style="color:green;">Importance of Document Hierarchy and Context:</mark> Maintaining the hierarchical structure of documents is crucial for understanding the context. For example, a *<mark style="color:yellow;">**rule stated in one section of a document might have exceptions listed in another section**</mark>*. Ignoring this hierarchy can lead to misunderstanding or misinterpreting the information.

<mark style="color:green;">Document summary method:</mark> <mark style="color:yellow;">Documents are summarised, and these summaries are embedded and stored in the vector database.</mark> For generation, the original full document is used instead of the summary, providing rich context to the language model.

<mark style="color:green;">Knowledge Graph-Aided RAG:</mark>  Knowledge graphs, which store data in nodes and edges rather than traditional rows and columns, are particularly effective for data with complex relationships.  In RAG, <mark style="color:yellow;">knowledge graphs can be used instead of chunking documents.</mark> Nodes in the knowledge graph are embedded and stored in a vector database.

<mark style="color:green;">Large Language Models Writing Graph Queries:</mark> Large language models can be used to transcribe natural language into graph queries, effectively querying knowledge graphs and retrieving relevant information.

<mark style="color:green;">Impact of Larger Context Windows in RAG:</mark> With advancements like GPT-4's 128k token context window, more data can be provided to the large language model, offering richer context.

<mark style="color:green;">Structured Retrieval as an Optimisation Method:</mark>  Structured retrieval involves using metadata to assist the retrieval process.  This method can be particularly useful when dealing with databases with additional contextual information, like movies with metadata such as genre, director, or ratings.

<mark style="color:green;">Large language models can be trained to understand and use metadata schemas,</mark> allowing them to automatically tag incoming queries with relevant metadata for more precise filtering.  This approach is particularly useful for <mark style="color:yellow;">time-based retrieval, where embeddings are tagged with date and time metadata,</mark> allowing searches within specific time frames or for the most recent data.

<mark style="color:green;">Advanced Structured Retrieval:</mark>  The concept of structured retrieval is where <mark style="color:yellow;">metadata is used to pre-filter the data before conducting a similarity search</mark>. This method enhances efficiency by narrowing down the search space.

<mark style="color:green;">Application in Real-World Scenarios:</mark>  using metadata for time-based retrieval and handling different types of documents separately shows the practical applicability of RAG in diverse real-world scenarios, such as financial analysis, news aggregation, and historical data analysis.

<mark style="color:green;">Handling Hierarchical Data in RAG:</mark> For very large documents that don't fit into a single context window, a hierarchical chunking approach can be used. Documents are chunked into larger segments, and these segments are further broken down into smaller chunks.

<mark style="color:green;">Document-Specific Agents and Recursive Retrieval:</mark> In scenarios where documents are similar but should be treated separately (e.g., financial documents from different companies), *<mark style="color:yellow;">**document-specific agents can be used.**</mark>*  Each document has its own retriever and summary, and an agent decides which path to take based on the user query.

<mark style="color:green;">Reranking for Improved Retrieval Accuracy:</mark>  Reranking is a method to improve the retrieval of chunks in RAG.   After the initial embedding and similarity search, a *<mark style="color:yellow;">**reranking process further refines the selection of relevant chunks.**</mark>*  This process addresses the potential information loss that might occur during the embedding phase, enhancing the precision of the retrieved data.

<mark style="color:green;">Reranking for Enhanced Retrieval Accuracy:</mark>  Reranking involves using a transformer or deep learning model after an initial vector search to refine and prioritise the most relevant chunks.   This process addresses the potential inaccuracies of similarity searches in vector spaces, ensuring that the best possible chunks are retrieved for generation.  <mark style="color:yellow;">Although reranking is more accurate, it's also more resource-intensive</mark>, which is why it's not feasible to use it for the entire dataset.

<mark style="color:green;">Sentence text windows:</mark> Documents are chunked at the sentence level for retrieval, but a larger text window is provided for generation to ensure adequate context.

<mark style="color:green;">Parent Document Retriever:</mark>  Similar to the document summary method, chunks are linked back to their parent document. Relevant chunks lead to the entire parent document being passed to the language model, offering comprehensive context.

<mark style="color:green;">Hierarchical Chunking:</mark>  This approach involves *<mark style="color:yellow;">c</mark><mark style="color:yellow;">**hunking large documents into progressively smaller units, such as page-size chunks, then paragraph-size, and finally sentence-size chunks**</mark>*<mark style="color:yellow;">.</mark>  This method balances the granularity of smaller chunks with the context provided by larger chunks, improving the relevance and accuracy of retrieval.

<mark style="color:green;">Prompt Engineering for Response Length and Relevance:</mark> The issue of generating responses of varying lengths depending on the chunk size and use case is more related to prompt engineering rather than chunk size.   Prompt engineering involves crafting the input to the language model in a way that guides the length and style of the response.

<mark style="color:green;">Enhancing Language Model Contextual Understanding:</mark> By *<mark style="color:yellow;">**linking summaries or specific chunks to their parent documents and using these in the generation process**</mark>*, RAG techniques demonstrate a commitment to enhancing the contextual understanding of language models.  This approach ensures that responses generated are not just relevant but also contextually rich and accurate.

<mark style="color:green;">Retaining Document Structure in ML Models:</mark> there's often a need to flatten hierarchical document structures into a format more amenable to machine learning models. This involves *<mark style="color:yellow;">**transforming documents into a flat representation like a list or dictionary, where each element is tagged with keys representing the hierarchical structure**</mark>* (e.g., file name, chapter, subsection, paragraph).

<mark style="color:green;">Application-Specific Retrieval and Design:</mark> The relevance of retrieved information is highly application-specific and depends on careful design and user feedback. This process is integral to the RAG system's effectiveness and underlines the ongoing need for skilled data scientists and programmers in AI development.
