Skip to content

Custom Code RAG

Retrieval-Augmented Generation (RAG) is a technique that enhances AI models by retrieving relevant information before generating responses. ByteBuddy's Custom Code RAG feature allows you to optimize how the AI understands and interacts with your codebase.

Understanding Code RAG

What is RAG?

RAG combines two components:

  1. Retrieval: Finding relevant information from your codebase
  2. Generation: Using that information to generate better responses

How Code RAG Works in ByteBuddy

ByteBuddy's Code RAG process:

  1. Context Analysis: Analyze the current code context
  2. Relevant Code Retrieval: Find related code segments
  3. Documentation Integration: Include relevant documentation
  4. Enhanced Response Generation: Generate informed responses

Basic RAG Configuration

Enabling Code RAG

Enable RAG in your configuration:

yaml
# .bytebuddy/config.yaml
rag:
  enabled: true
  codeRag:
    enabled: true
    maxContextTokens: 8192
    similarityThreshold: 0.7

Simple Code RAG Setup

Basic configuration for getting started:

yaml
rag:
  enabled: true
  codeRag:
    enabled: true
    sources:
      - type: "localFiles"
        paths:
          - "src/**/*.js"
          - "src/**/*.ts"
        exclude:
          - "node_modules/**"
          - "dist/**"
          - "*.test.*"

Advanced RAG Configuration

Multiple Data Sources

Configure multiple sources for RAG:

yaml
rag:
  enabled: true
  codeRag:
    enabled: true
    sources:
      # Local source code
      - type: "localFiles"
        name: "source-code"
        paths:
          - "src/**/*.js"
          - "src/**/*.ts"
        exclude:
          - "node_modules/**"
          - "dist/**"
        weight: 0.8 # Higher priority

      # Documentation files
      - type: "localFiles"
        name: "documentation"
        paths:
          - "docs/**/*.md"
          - "README.md"
        weight: 0.6

      # Test files
      - type: "localFiles"
        name: "tests"
        paths:
          - "tests/**/*.js"
          - "tests/**/*.ts"
        weight: 0.4

      # External API documentation
      - type: "http"
        name: "api-docs"
        url: "https://api.company.com/docs"
        format: "json"
        weight: 0.5

Custom Retrieval Strategies

Define how code is retrieved and ranked:

yaml
rag:
  enabled: true
  codeRag:
    enabled: true
    retrievalStrategy:
      type: "hybrid"
      components:
        - type: "semantic"
          weight: 0.7
          model: "embeddings"

        - type: "lexical"
          weight: 0.3
          algorithm: "bm25"

    ranking:
      algorithm: "rrf" # Reciprocal Rank Fusion
      weights:
        semantic: 0.7
        lexical: 0.3

Semantic Code Indexing

Embedding Models

Configure embedding models for semantic search:

yaml
rag:
  enabled: true
  codeRag:
    enabled: true
    embeddings:
      model: "openai/text-embedding-ada-002"
      apiKey: "${OPENAI_API_KEY}"
      batchSize: 100
      cacheEnabled: true

    # Alternative: local embeddings
    # embeddings:
    #   model: "sentence-transformers/all-MiniLM-L6-v2"
    #   type: "local"

Indexing Configuration

Control how code is indexed:

yaml
rag:
  enabled: true
  codeRag:
    enabled: true
    indexing:
      chunkSize: 1000
      overlap: 200
      maxFileSize: "1MB"
      updateFrequency: "1h"

    preprocessing:
      removeComments: false
      extractFunctions: true
      extractClasses: true
      extractVariables: false

Custom RAG Pipelines

Defining Pipelines

Create custom RAG pipelines for specific use cases:

yaml
rag:
  enabled: true
  codeRag:
    enabled: true
    pipelines:
      - name: "code-completion"
        description: "Pipeline for code completion tasks"
        stages:
          - name: "context-extraction"
            action: "extractContext"
            parameters:
              includeSurroundingLines: 50

          - name: "relevant-code-retrieval"
            action: "retrieveRelevantCode"
            parameters:
              maxResults: 10
              similarityThreshold: 0.8

          - name: "documentation-retrieval"
            action: "retrieveDocumentation"
            parameters:
              maxResults: 5

          - name: "context-assembly"
            action: "assembleContext"
            parameters:
              maxTokens: 4096

Pipeline Routing

Route different queries to different pipelines:

yaml
rag:
  enabled: true
  codeRag:
    enabled: true
    pipelineRouting:
      - condition:
          queryType: "code-completion"
        pipeline: "code-completion"

      - condition:
          queryType: "bug-fixing"
        pipeline: "bug-fixing-pipeline"

      - condition:
          queryType: "refactoring"
        pipeline: "refactoring-pipeline"

      - condition:
          default: true
        pipeline: "general-purpose"

Performance Optimization

Caching Strategies

Optimize RAG performance with caching:

yaml
rag:
  enabled: true
  codeRag:
    enabled: true
    caching:
      enabled: true
      ttl: "24h"
      maxSize: "1GB"

      layers:
        - type: "memory"
          maxSize: "100MB"

        - type: "disk"
          path: "~/.bytebuddy/cache/rag"
          maxSize: "1GB"

        - type: "remote"
          url: "redis://localhost:6379"
          enabled: false

Resource Management

Control resource usage:

yaml
rag:
  enabled: true
  codeRag:
    enabled: true
    resources:
      maxConcurrency: 4
      timeout: 30
      memoryLimit: "512MB"

    rateLimiting:
      enabled: true
      requestsPerMinute: 60
      burstLimit: 10

Integration with Other Features

RAG and Continuous AI

Combine RAG with Continuous AI:

yaml
continuousAI:
  enabled: true
  workflows:
    - name: "smart-code-review"
      actions:
        - action: "reviewCode"
          useRag: true
          ragPipeline: "code-review-pipeline"

rag:
  enabled: true
  codeRag:
    enabled: true
    pipelines:
      - name: "code-review-pipeline"
        stages:
          - name: "similar-code-finding"
            action: "findSimilarCode"

          - name: "best-practices-retrieval"
            action: "retrieveBestPractices"

          - name: "anti-patterns-check"
            action: "checkAntiPatterns"

RAG and Rules

Enhance rules with RAG context:

yaml
rules:
  - name: "security-review"
    prompt: |
      Review this code for security issues, considering:
      {{rag_context.security_patterns}}

      Common vulnerabilities to check:
      {{rag_context.common_vulnerabilities}}

    useRag: true
    ragContext:
      - "security_patterns"
      - "common_vulnerabilities"
      - "framework_security_guidelines"

Custom Processors

Pre-processing

Add custom pre-processing steps:

yaml
rag:
  enabled: true
  codeRag:
    enabled: true
    preprocessors:
      - name: "code-normalizer"
        type: "custom"
        script: "./scripts/normalize-code.js"

      - name: "comment-remover"
        type: "builtin"
        action: "removeComments"

      - name: "framework-identifier"
        type: "custom"
        script: "./scripts/identify-framework.js"

Post-processing

Add custom post-processing steps:

yaml
rag:
  enabled: true
  codeRag:
    enabled: true
    postprocessors:
      - name: "relevance-filter"
        type: "custom"
        script: "./scripts/filter-by-relevance.js"
        parameters:
          minScore: 0.7

      - name: "duplicate-remover"
        type: "builtin"
        action: "removeDuplicates"

      - name: "format-enforcer"
        type: "custom"
        script: "./scripts/enforce-format.js"

Example Configurations

Web Development RAG Setup

yaml
rag:
  enabled: true
  codeRag:
    enabled: true
    sources:
      - type: "localFiles"
        name: "frontend-code"
        paths:
          - "src/components/**/*.jsx"
          - "src/pages/**/*.jsx"
          - "src/hooks/**/*.js"
        exclude:
          - "node_modules/**"
          - "build/**"
        weight: 0.9

      - type: "localFiles"
        name: "backend-code"
        paths:
          - "src/controllers/**/*.js"
          - "src/models/**/*.js"
          - "src/routes/**/*.js"
        weight: 0.8

      - type: "localFiles"
        name: "documentation"
        paths:
          - "docs/**/*.md"
          - "README.md"
          - "SPECIFICATION.md"
        weight: 0.7

    embeddings:
      model: "openai/text-embedding-ada-002"
      apiKey: "${OPENAI_API_KEY}"

    indexing:
      chunkSize: 800
      overlap: 100

    pipelines:
      - name: "react-development"
        stages:
          - name: "component-finder"
            action: "findSimilarComponents"

          - name: "hook-recommender"
            action: "recommendHooks"

          - name: "best-practices"
            action: "retrieveReactBestPractices"

Data Science RAG Setup

yaml
rag:
  enabled: true
  codeRag:
    enabled: true
    sources:
      - type: "localFiles"
        name: "notebooks"
        paths:
          - "notebooks/**/*.ipynb"
          - "scripts/**/*.py"
        weight: 0.9

      - type: "localFiles"
        name: "data-pipelines"
        paths:
          - "src/data/**/*.py"
          - "src/models/**/*.py"
        weight: 0.8

      - type: "http"
        name: "ml-documentation"
        url: "https://scikit-learn.org/stable/modules/"
        format: "html"
        weight: 0.6

    embeddings:
      model: "sentence-transformers/all-MiniLM-L6-v2"
      type: "local"

    preprocessing:
      extractFunctions: true
      extractClasses: true
      extractDocstrings: true

    pipelines:
      - name: "ml-model-development"
        stages:
          - name: "algorithm-finder"
            action: "findSimilarAlgorithms"

          - name: "parameter-suggestions"
            action: "suggestParameters"

          - name: "evaluation-methods"
            action: "retrieveEvaluationMethods"

Monitoring and Analytics

RAG Metrics

Track RAG performance:

yaml
rag:
  enabled: true
  codeRag:
    enabled: true
    analytics:
      enabled: true
      metrics:
        - "retrievalAccuracy"
        - "generationQuality"
        - "responseTime"
        - "cacheHitRate"

      logging:
        level: "info"
        outputFile: "~/.bytebuddy/logs/rag.log"

      dashboards:
        enabled: true
        port: 8080

Performance Profiling

Profile RAG performance:

bash
# Profile RAG operations
bytebuddy profile --rag

# View detailed metrics
bytebuddy metrics --rag

# Generate performance report
bytebuddy report --rag --output rag-performance.md

Troubleshooting

Common Issues

Poor Retrieval Quality

  1. Check embedding model quality
  2. Adjust similarity thresholds
  3. Review chunking strategy
  4. Verify data sources are properly indexed

Slow Performance

  1. Enable caching
  2. Reduce max results
  3. Optimize chunk sizes
  4. Check resource limits

Missing Context

  1. Expand data sources
  2. Adjust retrieval strategies
  3. Fine-tune preprocessing
  4. Review exclusion patterns

Debugging Commands

bash
# Test RAG retrieval
bytebuddy rag test --query "how to handle user authentication"

# View indexed documents
bytebuddy rag index list

# Rebuild index
bytebuddy rag index rebuild

# Check embedding quality
bytebuddy rag embeddings test

Best Practices

Indexing Strategy

  1. Chunk Appropriately: Balance chunk size for context vs. specificity
  2. Update Regularly: Keep indices fresh with regular updates
  3. Exclude Noise: Remove irrelevant files from indexing
  4. Prioritize Sources: Weight important sources higher

Performance Optimization

  1. Use Caching: Enable multi-layer caching
  2. Limit Results: Retrieve only necessary information
  3. Preprocess Data: Clean and normalize data before indexing
  4. Monitor Resources: Keep track of memory and CPU usage

Quality Assurance

  1. Test Retrieval: Regularly test retrieval accuracy
  2. Review Results: Manually review generated responses
  3. Gather Feedback: Collect user feedback on RAG quality
  4. Iterate Improvements: Continuously refine RAG configuration

Next Steps

After configuring Custom Code RAG, explore these related guides: