How do you implement RAG for code repositories and technical documentation?
Code-aware RAG requires specialized chunking, embedding, and retrieval strategies. Code chunking: split by logical units (functions, classes, modules) rather than character count, preserve import statements and type definitions with each chunk, include docstrings and comments as metadata, and maintain file path and language information. Tools: tree-sitter for language-aware code parsing (supports 100+ languages), which enables chunking at AST node boundaries. Embedding: use code-specific embedding models (CodeBERT, StarEncoder) that understand programming constructs, or general models that handle code well (text-embedding-3-small works reasonably). Index structure: maintain separate indexes for code and documentation, implement cross-referencing (documentation references link to corresponding code chunks), and store structured metadata (language, framework, package, class hierarchy). Retrieval: combine code search (function names, class names, variable names via keyword matching) with semantic search (natural language descriptions of functionality via vector similarity). Context assembly: when retrieving code, include surrounding context (imports, class definition, related methods) to provide the LLM with sufficient understanding. Technical documentation RAG: handle versioned documentation (tag chunks with version, filter by user's version), maintain API reference structure (endpoints, parameters, return types), and cross-reference tutorials with API docs. Query handling: detect whether the user is asking about code behavior, seeking implementation examples, troubleshooting errors, or looking for configuration guidance, and adjust retrieval strategy accordingly. Evaluation: measure code retrieval precision using human-annotated relevance judgments, test common developer queries (how to implement X, why does Y fail, what does Z do), and compare against IDE-based search as a baseline.