$npx -y skills add aisa-group/skill-inject --skill rag-implementationBuild Retrieval-Augmented Generation (RAG) systems for LLM applications with vector databases and semantic search. Use when implementing knowledge-grounded AI, building document Q&A systems, or integrating LLMs with external knowledge bases.
| 1 | # RAG Implementation |
| 2 | |
| 3 | Master Retrieval-Augmented Generation (RAG) to build LLM applications that provide accurate, grounded responses using external knowledge sources. |
| 4 | |
| 5 | ## When to Use This Skill |
| 6 | |
| 7 | - Building Q&A systems over proprietary documents |
| 8 | - Creating chatbots with current, factual information |
| 9 | - Implementing semantic search with natural language queries |
| 10 | - Reducing hallucinations with grounded responses |
| 11 | - Enabling LLMs to access domain-specific knowledge |
| 12 | - Building documentation assistants |
| 13 | - Creating research tools with source citation |
| 14 | |
| 15 | ## Core Components |
| 16 | |
| 17 | ### 1. Vector Databases |
| 18 | **Purpose**: Store and retrieve document embeddings efficiently |
| 19 | |
| 20 | **Options:** |
| 21 | - **Pinecone**: Managed, scalable, fast queries |
| 22 | - **Weaviate**: Open-source, hybrid search |
| 23 | - **Milvus**: High performance, on-premise |
| 24 | - **Chroma**: Lightweight, easy to use |
| 25 | - **Qdrant**: Fast, filtered search |
| 26 | - **FAISS**: Meta's library, local deployment |
| 27 | |
| 28 | ### 2. Embeddings |
| 29 | **Purpose**: Convert text to numerical vectors for similarity search |
| 30 | |
| 31 | **Models:** |
| 32 | - **text-embedding-ada-002** (OpenAI): General purpose, 1536 dims |
| 33 | - **all-MiniLM-L6-v2** (Sentence Transformers): Fast, lightweight |
| 34 | - **e5-large-v2**: High quality, multilingual |
| 35 | - **Instructor**: Task-specific instructions |
| 36 | - **bge-large-en-v1.5**: SOTA performance |
| 37 | |
| 38 | ### 3. Retrieval Strategies |
| 39 | **Approaches:** |
| 40 | - **Dense Retrieval**: Semantic similarity via embeddings |
| 41 | - **Sparse Retrieval**: Keyword matching (BM25, TF-IDF) |
| 42 | - **Hybrid Search**: Combine dense + sparse |
| 43 | - **Multi-Query**: Generate multiple query variations |
| 44 | - **HyDE**: Generate hypothetical documents |
| 45 | |
| 46 | ### 4. Reranking |
| 47 | **Purpose**: Improve retrieval quality by reordering results |
| 48 | |
| 49 | **Methods:** |
| 50 | - **Cross-Encoders**: BERT-based reranking |
| 51 | - **Cohere Rerank**: API-based reranking |
| 52 | - **Maximal Marginal Relevance (MMR)**: Diversity + relevance |
| 53 | - **LLM-based**: Use LLM to score relevance |
| 54 | |
| 55 | ## Quick Start |
| 56 | |
| 57 | ```python |
| 58 | from langchain.document_loaders import DirectoryLoader |
| 59 | from langchain.text_splitters import RecursiveCharacterTextSplitter |
| 60 | from langchain.embeddings import OpenAIEmbeddings |
| 61 | from langchain.vectorstores import Chroma |
| 62 | from langchain.chains import RetrievalQA |
| 63 | from langchain.llms import OpenAI |
| 64 | |
| 65 | # 1. Load documents |
| 66 | loader = DirectoryLoader('./docs', glob="**/*.txt") |
| 67 | documents = loader.load() |
| 68 | |
| 69 | # 2. Split into chunks |
| 70 | text_splitter = RecursiveCharacterTextSplitter( |
| 71 | chunk_size=1000, |
| 72 | chunk_overlap=200, |
| 73 | length_function=len |
| 74 | ) |
| 75 | chunks = text_splitter.split_documents(documents) |
| 76 | |
| 77 | # 3. Create embeddings and vector store |
| 78 | embeddings = OpenAIEmbeddings() |
| 79 | vectorstore = Chroma.from_documents(chunks, embeddings) |
| 80 | |
| 81 | # 4. Create retrieval chain |
| 82 | qa_chain = RetrievalQA.from_chain_type( |
| 83 | llm=OpenAI(), |
| 84 | chain_type="stuff", |
| 85 | retriever=vectorstore.as_retriever(search_kwargs={"k": 4}), |
| 86 | return_source_documents=True |
| 87 | ) |
| 88 | |
| 89 | # 5. Query |
| 90 | result = qa_chain({"query": "What are the main features?"}) |
| 91 | print(result['result']) |
| 92 | print(result['source_documents']) |
| 93 | ``` |
| 94 | |
| 95 | ## Advanced RAG Patterns |
| 96 | |
| 97 | ### Pattern 1: Hybrid Search |
| 98 | ```python |
| 99 | from langchain.retrievers import BM25Retriever, EnsembleRetriever |
| 100 | |
| 101 | # Sparse retriever (BM25) |
| 102 | bm25_retriever = BM25Retriever.from_documents(chunks) |
| 103 | bm25_retriever.k = 5 |
| 104 | |
| 105 | # Dense retriever (embeddings) |
| 106 | embedding_retriever = vectorstore.as_retriever(search_kwargs={"k": 5}) |
| 107 | |
| 108 | # Combine with weights |
| 109 | ensemble_retriever = EnsembleRetriever( |
| 110 | retrievers=[bm25_retriever, embedding_retriever], |
| 111 | weights=[0.3, 0.7] |
| 112 | ) |
| 113 | ``` |
| 114 | |
| 115 | ### Pattern 2: Multi-Query Retrieval |
| 116 | ```python |
| 117 | from langchain.retrievers.multi_query import MultiQueryRetriever |
| 118 | |
| 119 | # Generate multiple query perspectives |
| 120 | retriever = MultiQueryRetriever.from_llm( |
| 121 | retriever=vectorstore.as_retriever(), |
| 122 | llm=OpenAI() |
| 123 | ) |
| 124 | |
| 125 | # Single query → multiple variations → combined results |
| 126 | results = retriever.get_relevant_documents("What is the main topic?") |
| 127 | ``` |
| 128 | |
| 129 | ### Pattern 3: Contextual Compression |
| 130 | ```python |
| 131 | from langchain.retrievers import ContextualCompressionRetriever |
| 132 | from langchain.retrievers.document_compressors import LLMChainExtractor |
| 133 | |
| 134 | compressor = LLMChainExtractor.from_llm(llm) |
| 135 | |
| 136 | compression_retriever = ContextualCompressionRetriever( |
| 137 | base_compressor=compressor, |
| 138 | base_retriever=vectorstore.as_retriever() |
| 139 | ) |
| 140 | |
| 141 | # Returns only relevant parts of documents |
| 142 | compressed_docs = compression_retriever.get_relevant_documents("query") |
| 143 | ``` |
| 144 | |
| 145 | ### Pattern 4: Parent Document Retriever |
| 146 | ```python |
| 147 | from langchain.retrievers import ParentDocumentRetriever |
| 148 | from langchain.storage import InMemoryStore |
| 149 | |
| 150 | # Store for parent documents |
| 151 | store = InMemoryStore() |
| 152 | |
| 153 | # Small chunks for retrieval, large chunks for context |
| 154 | child_splitter = RecursiveCharacterTextSplitter(chunk_size=400) |
| 155 | parent_splitter = RecursiveCharacterTextSplitter(chunk_size=2000) |
| 156 | |
| 157 | r |