Skip to content

ahcbsa/rag-context-engineering-examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ RAG Context Engineering System

A complete RAG ecosystem with interactive Chainlit UI, multi-provider LLM support, and specialized context engines.

This repository demonstrates a production-ready Retrieval-Augmented Generation (RAG) system featuring:

  • 🎯 Three specialized context engines for different domains
  • πŸ–₯️ Interactive Chainlit UI for seamless user experience
  • πŸ”„ Multi-provider LLM support (OpenAI, Anthropic, DeepSeek, Ollama)
  • ⚑ Smart provider fallback and automatic configuration
  • πŸ“Š Prometheus metrics and comprehensive testing
  • πŸ—οΈ Microservices architecture with FastAPI

πŸ—οΈ System Architecture

Context Engines

Engine Domain Use Cases
🏒 Enterprise WICS framework, company policies HR questions, policy lookup, process guidance
πŸ’° Financial Compliance Basel III, BACEN, CVM regulations Regulatory compliance, risk assessment
βš™οΈ DevOps SRE practices, troubleshooting Infrastructure issues, operational guidance

Core Components

  • context_engine_core/: Shared RAG pipeline with LangChain + LangGraph
  • app.py: Main Chainlit application with UI
  • config.json: Multi-provider configuration
  • context_quality_monitor/: Prometheus metrics service
  • context_engineering_testing_suite/: Comprehensive test suite

πŸš€ Quick Start

1. Prerequisites

  • Python 3.8+
  • API key for at least one LLM provider

2. Installation

# Clone and navigate to the project
cd rag-context-engineering-examples

# Install dependencies
pip install -r requirements.txt

# Set your API key (choose one)
export OPENAI_API_KEY=sk-your-openai-key
export ANTHROPIC_API_KEY=sk-ant-your-anthropic-key
export DEEPSEEK_API_KEY=sk-your-deepseek-key

3. Launch the Application

Option A: Easy Launch (Recommended)

python launch.py

Option B: Direct Chainlit

chainlit run app.py --host 0.0.0.0 --port 8000

Option C: Python execution

python app.py

4. Access the Interface

Open your browser to: http://localhost:8000

The Chainlit interface will provide:

  • πŸ”§ Engine selection via settings panel
  • πŸ’¬ Interactive chat with all context engines
  • πŸ“± Mobile-friendly responsive design
  • 🎨 Rich markdown formatting

πŸ”§ Configuration

Multi-Provider Setup

The system automatically selects the best available provider. Configure per-engine preferences in config.json:

{
  "engines": {
    "enterprise_context_engine": {
      "llm": {
        "provider": "anthropic",
        "anthropic": {
          "model_name": "claude-3-sonnet-20240229",
          "temperature": 0.1
        }
      }
    },
    "financial_compliance_context_engine": {
      "llm": {
        "provider": "openai", 
        "openai": {
          "model_name": "gpt-4",
          "temperature": 0
        }
      }
    }
  }
}

Supported Providers

Provider Setup Models Best For
OpenAI export OPENAI_API_KEY=sk-... GPT-3.5, GPT-4 Balanced performance
Anthropic export ANTHROPIC_API_KEY=sk-ant-... Claude-3 Sonnet/Opus Complex reasoning
DeepSeek export DEEPSEEK_API_KEY=sk-... DeepSeek Chat/Coder Cost-effective
Ollama Local installation Llama2, Mistral Privacy/offline

πŸ§ͺ Testing & Development

Run Tests

# Full test suite
cd context_engineering_testing_suite
pytest tests/ -v

# Single test
pytest tests/test_rag_flow.py::test_query_returns_string -v

# Test with coverage
pytest tests/ --cov=context_engine_core --cov-report=html

Development Examples

# Run individual examples
cd examples/
python quick_demo.py              # Basic RAG demonstration
python multi_provider_testing.py  # Provider comparison
python provider_demo.py           # Provider switching demo

API Services (Alternative to UI)

# Start individual engines as APIs
uvicorn enterprise_context_engine.src.enterprise_context_engine.api:app --reload --port 8001
uvicorn financial_compliance_context_engine.src.financial_compliance_context_engine.api:app --reload --port 8002
uvicorn devops_context_engine.src.devops_context_engine.api:app --reload --port 8003

# Start metrics service
uvicorn context_quality_monitor.src.context_quality_monitor.api:app --reload --port 8004

πŸ’‘ Usage Examples

Enterprise Context Engine

Q: "What is WICS?"
A: "WICS (Work Integration and Coordination System) is a framework for..."

Q: "What is the remote work policy?"
A: "The remote work policy allows for flexible arrangements..."

Financial Compliance Engine

Q: "What are Basel III requirements?"
A: "Basel III introduces enhanced capital requirements including..."

Q: "Tell me about BACEN regulations"
A: "BACEN (Central Bank of Brazil) regulations cover..."

DevOps Context Engine

Q: "How do I troubleshoot high CPU usage?"
A: "For high CPU usage troubleshooting, follow these steps..."

Q: "What are SRE best practices?"
A: "Site Reliability Engineering best practices include..."

πŸ—οΈ Architecture Deep Dive

RAG Pipeline Flow

graph TD
    A[User Query] --> B[Context Engine]
    B --> C[Document Retrieval]
    C --> D[FAISS Vector Search]
    D --> E[Relevant Documents]
    E --> F[LLM Provider]
    F --> G[Generated Response]
    G --> H[Chainlit UI]
Loading

LangGraph Workflow

  1. Retrieve: Query vector store for relevant documents
  2. Generate: Use LLM to synthesize answer from context
  3. Return: Format response for UI display

Component Interactions

  • BaseContextEngine: Core RAG logic shared across all engines
  • LLMProviders: Abstraction layer for multiple AI providers
  • Chainlit App: User interface with engine switching
  • FastAPI Services: Alternative REST API access
  • Prometheus: Metrics collection and monitoring

πŸ“Š Monitoring & Metrics

Prometheus Metrics

Access metrics at: http://localhost:8004/metrics

Available metrics:

  • rag_queries_total: Total queries per engine
  • rag_query_duration_seconds: Query processing time
  • rag_errors_total: Error count by type

Health Checks

  • UI Health: http://localhost:8000
  • API Health: http://localhost:8001/health (per service)
  • Metrics Health: http://localhost:8004/health

πŸ”„ Development Workflow

Adding New Context Engines

  1. Create Engine Directory

    mkdir new_context_engine/src/new_context_engine/
  2. Implement Engine Class

    from context_engine_core.base_engine import BaseContextEngine
    
    class NewContextEngine(BaseContextEngine):
        def _default_docs(self):
            return ["Your domain-specific documents here"]
  3. Add FastAPI Wrapper

    from fastapi import FastAPI
    app = FastAPI()
    
    @app.post("/query")
    async def query_endpoint(query: str):
        engine = NewContextEngine()
        return {"response": engine.query(query)}
  4. Update Configuration

    {
      "engines": {
        "new_context_engine": {
          "llm": {"provider": "openai"}
        }
      }
    }
  5. Integrate with UI

    # Add to app.py engines dictionary
    engines["new_engine"] = NewContextEngine()

Code Quality

# Linting
flake8 context_engine_core/ --max-line-length=100

# Type checking
mypy context_engine_core/

# Security scan
bandit -r context_engine_core/

🚨 Troubleshooting

Common Issues

❌ "No module named 'context_engine_core'"

# Ensure you're in the project root
cd rag-context-engineering-examples
python app.py

❌ "API key not found"

# Set your API key
export OPENAI_API_KEY=your-key-here
python launch.py  # Will check API keys

❌ "Chainlit not found"

# Install Chainlit
pip install chainlit>=1.0.0

❌ "Port already in use"

# Use different port
chainlit run app.py --port 8001

Debug Mode

# Enable verbose logging
export LANGCHAIN_VERBOSE=true
export LANGCHAIN_TRACING=true
python app.py

πŸ“š Educational Resources

This system demonstrates key RAG concepts:

  • Context Engineering: Beyond prompt engineering to systematic context management
  • Multi-Modal RAG: Different engines for different knowledge domains
  • Provider Abstraction: Flexible LLM provider switching
  • Production Patterns: Monitoring, testing, configuration management
  • User Experience: Interactive UI with real-time engine switching

Learning Path

  1. Start with Examples: Run python examples/quick_demo.py
  2. Explore UI: Use Chainlit interface to understand user experience
  3. Study Architecture: Review context_engine_core/base_engine.py
  4. Extend System: Add your own context engine
  5. Production Deploy: Scale with Docker/Kubernetes

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/amazing-feature
  3. Run tests: pytest tests/
  4. Submit a pull request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


πŸ†˜ Support

  • πŸ“– Documentation: Check the inline code documentation
  • πŸ› Issues: Open GitHub issues for bugs
  • πŸ’‘ Features: Request enhancements via GitHub
  • πŸ“§ Contact: For educational/commercial inquiries

Built with ❀️ for the RAG community. Designed for learning, built for production.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

Β 
Β 
Β 

Contributors

Languages