$npx -y skills add giuseppe-trisciuoglio/developer-kit --skill ragImplements document chunking, embedding generation, vector storage, and retrieval pipelines for Retrieval-Augmented Generation systems. Use when building RAG applications, creating document Q&A systems, or integrating AI with knowledge bases.
| 1 | # RAG Implementation |
| 2 | |
| 3 | Build Retrieval-Augmented Generation systems that extend AI capabilities with external knowledge sources. |
| 4 | |
| 5 | ## Overview |
| 6 | |
| 7 | This skill covers: document processing, embedding generation, vector storage, retrieval configuration, and RAG pipeline implementation. |
| 8 | |
| 9 | ## When to Use |
| 10 | |
| 11 | - Building Q&A systems over proprietary documents |
| 12 | - Creating chatbots with factual information from knowledge bases |
| 13 | - Implementing semantic search with natural language queries |
| 14 | - Reducing hallucinations with grounded, sourced responses |
| 15 | - Building documentation assistants and research tools |
| 16 | - Enabling AI systems to access domain-specific knowledge |
| 17 | |
| 18 | ## Instructions |
| 19 | |
| 20 | ### Step 1: Choose Vector Database |
| 21 | |
| 22 | Select based on your requirements: |
| 23 | |
| 24 | | Requirement | Recommended | |
| 25 | |-------------|-------------| |
| 26 | | Production scalability | Pinecone, Milvus | |
| 27 | | Open-source | Weaviate, Qdrant | |
| 28 | | Local development | Chroma, FAISS | |
| 29 | | Hybrid search | Weaviate with BM25 | |
| 30 | |
| 31 | ### Step 2: Select Embedding Model |
| 32 | |
| 33 | | Use Case | Model | |
| 34 | |----------|-------| |
| 35 | | General purpose | text-embedding-ada-002 | |
| 36 | | Fast and lightweight | all-MiniLM-L6-v2 | |
| 37 | | Multilingual | e5-large-v2 | |
| 38 | | Best performance | bge-large-en-v1.5 | |
| 39 | |
| 40 | ### Step 3: Implement Document Processing Pipeline |
| 41 | |
| 42 | 1. Load documents from source (file system, database, API) |
| 43 | 2. Clean and preprocess (remove formatting, normalize text) |
| 44 | 3. Split documents into chunks with appropriate strategy |
| 45 | 4. Generate embeddings for each chunk |
| 46 | 5. Store embeddings in vector database with metadata |
| 47 | |
| 48 | **Validation**: Verify embeddings were generated successfully: |
| 49 | ```java |
| 50 | List<Embedding> embeddings = embeddingModel.embedAll(segments); |
| 51 | if (embeddings.isEmpty() || embeddings.get(0).dimension() != expectedDim) { |
| 52 | throw new IllegalStateException("Embedding generation failed"); |
| 53 | } |
| 54 | ``` |
| 55 | |
| 56 | ### Step 4: Configure Retrieval Strategy |
| 57 | |
| 58 | Choose the appropriate strategy: |
| 59 | |
| 60 | - **Dense Retrieval**: Semantic similarity via embeddings (default for most cases) |
| 61 | - **Hybrid Search**: Dense + sparse retrieval for better coverage |
| 62 | - **Metadata Filtering**: Filter by document attributes |
| 63 | - **Reranking**: Cross-encoder reranking for high-precision requirements |
| 64 | |
| 65 | ### Step 5: Build RAG Pipeline |
| 66 | |
| 67 | 1. Create content retriever with your embedding store |
| 68 | 2. Configure AI service with retriever and chat memory |
| 69 | 3. Implement prompt template with context injection |
| 70 | 4. Add response validation and grounding checks |
| 71 | |
| 72 | **Validation**: Test with known queries to verify context injection works correctly. |
| 73 | |
| 74 | **Error Handling**: For batch ingestion, wrap in retry logic: |
| 75 | ```java |
| 76 | for (Document doc : documents) { |
| 77 | int attempts = 0; |
| 78 | while (attempts < 3) { |
| 79 | try { |
| 80 | store.add(embeddingModel.embed(doc).content(), doc.toTextSegment()); |
| 81 | break; |
| 82 | } catch (EmbeddingException e) { |
| 83 | attempts++; |
| 84 | if (attempts == 3) throw new RuntimeException("Failed after 3 retries", e); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | ``` |
| 89 | |
| 90 | ### Step 6: Evaluate and Optimize |
| 91 | |
| 92 | 1. Measure retrieval metrics: precision@k, recall@k, MRR |
| 93 | 2. Evaluate answer quality: faithfulness, relevance |
| 94 | 3. Monitor performance and user feedback |
| 95 | 4. Iterate on chunking, retrieval, and prompt parameters |
| 96 | |
| 97 | ## Examples |
| 98 | |
| 99 | ### Example 1: Basic Document Q&A |
| 100 | |
| 101 | ```java |
| 102 | List<Document> documents = FileSystemDocumentLoader.loadDocuments("/docs"); |
| 103 | |
| 104 | InMemoryEmbeddingStore<TextSegment> store = new InMemoryEmbeddingStore<>(); |
| 105 | EmbeddingStoreIngestor.ingest(documents, store); |
| 106 | |
| 107 | DocumentAssistant assistant = AiServices.builder(DocumentAssistant.class) |
| 108 | .chatModel(chatModel) |
| 109 | .contentRetriever(EmbeddingStoreContentRetriever.from(store)) |
| 110 | .build(); |
| 111 | |
| 112 | String answer = assistant.answer("What is the company policy on remote work?"); |
| 113 | ``` |
| 114 | |
| 115 | ### Example 2: Metadata-Filtered Retrieval |
| 116 | |
| 117 | ```java |
| 118 | EmbeddingStoreContentRetriever retriever = EmbeddingStoreContentRetriever.builder() |
| 119 | .embeddingStore(store) |
| 120 | .embeddingModel(embeddingModel) |
| 121 | .maxResults(5) |
| 122 | .minScore(0.7) |
| 123 | .filter(metadataKey("category").isEqualTo("technical")) |
| 124 | .build(); |
| 125 | ``` |
| 126 | |
| 127 | ### Example 3: Multi-Source RAG Pipeline |
| 128 | |
| 129 | ```java |
| 130 | ContentRetriever webRetriever = EmbeddingStoreContentRetriever.from(webStore); |
| 131 | ContentRetriever docRetriever = EmbeddingStoreContentRetriever.from(docStore); |
| 132 | |
| 133 | List<Content> results = new ArrayList<>(); |
| 134 | results.addAll(webRetriever.retrieve(query)); |
| 135 | results.addAll(docRetriever.retrieve(query)); |
| 136 | |
| 137 | List<Content> topResults = reranker.reorder(query, results).subList(0, 5); |
| 138 | ``` |
| 139 | |
| 140 | ### Example 4: RAG with Chat Memory |
| 141 | |
| 142 | ```java |
| 143 | Assistant assistant = AiServices.builder(Assistant.class) |
| 144 | .chatModel(chatModel) |
| 145 | .chatMemory(MessageWindowChatMemory.withMaxMessages(10)) |
| 146 | .contentRetriever(retriever) |
| 147 | .build(); |
| 148 | |
| 149 | assistant.chat("Tell |