|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## ⚠️ CRITICAL: Package Management |
| 6 | + |
| 7 | +**ALWAYS use `uv` for ALL dependency management operations. NEVER use `pip` directly.** |
| 8 | + |
| 9 | +This project uses `uv` as its package manager. All Python commands must be run through `uv run`: |
| 10 | + |
| 11 | +```bash |
| 12 | +# ✅ CORRECT - Use uv |
| 13 | +uv sync # Install/sync dependencies |
| 14 | +uv run python script.py # Run Python scripts |
| 15 | +uv run uvicorn app:app --reload # Run servers |
| 16 | +uv add package-name # Add new dependency |
| 17 | +uv remove package-name # Remove dependency |
| 18 | + |
| 19 | +# ❌ WRONG - Do NOT use |
| 20 | +pip install package-name # Don't use pip |
| 21 | +python script.py # Don't run Python directly |
| 22 | +``` |
| 23 | + |
| 24 | +## Project Overview |
| 25 | + |
| 26 | +This is a **RAG (Retrieval-Augmented Generation) system** for course materials. It allows users to query educational content and receive AI-powered responses backed by semantic search across course documents. |
| 27 | + |
| 28 | +**Tech Stack:** |
| 29 | +- Backend: FastAPI + Python 3.13 |
| 30 | +- Vector Database: ChromaDB with sentence-transformers embeddings |
| 31 | +- AI: Anthropic Claude API (claude-sonnet-4-20250514) |
| 32 | +- Frontend: Vanilla HTML/CSS/JavaScript |
| 33 | +- **Package Manager: uv** (not pip) |
| 34 | + |
| 35 | +## Development Commands |
| 36 | + |
| 37 | +### Running the Application |
| 38 | + |
| 39 | +```bash |
| 40 | +# Quick start (recommended) |
| 41 | +./run.sh |
| 42 | + |
| 43 | +# Manual start |
| 44 | +cd backend |
| 45 | +uv run uvicorn app:app --reload --port 8000 |
| 46 | +``` |
| 47 | + |
| 48 | +Application URLs: |
| 49 | +- Frontend: `http://localhost:8000` |
| 50 | +- API docs: `http://localhost:8000/docs` |
| 51 | + |
| 52 | +### Package Management |
| 53 | + |
| 54 | +See **"⚠️ CRITICAL: Package Management"** section at the top of this file. |
| 55 | + |
| 56 | +All dependency operations use `uv` exclusively: |
| 57 | +- Install dependencies: `uv sync` |
| 58 | +- Add packages: `uv add <package>` |
| 59 | +- Remove packages: `uv remove <package>` |
| 60 | +- Run commands: `uv run <command>` |
| 61 | + |
| 62 | +**Adding New Dependencies:** |
| 63 | +When adding new packages to this project, always use `uv add`: |
| 64 | +```bash |
| 65 | +uv add anthropic # Add to project dependencies |
| 66 | +uv add --dev pytest # Add development dependency |
| 67 | +``` |
| 68 | + |
| 69 | +This updates both `pyproject.toml` and `uv.lock` automatically. |
| 70 | + |
| 71 | +### Environment Setup |
| 72 | + |
| 73 | +Create `.env` in project root: |
| 74 | +``` |
| 75 | +ANTHROPIC_API_KEY=your_key_here |
| 76 | +``` |
| 77 | + |
| 78 | +## Architecture |
| 79 | + |
| 80 | +### Core Components (backend/) |
| 81 | + |
| 82 | +The system follows a modular architecture with clear separation of concerns: |
| 83 | + |
| 84 | +1. **app.py** - FastAPI application entry point |
| 85 | + - Serves static frontend files |
| 86 | + - Exposes `/api/query` and `/api/courses` endpoints |
| 87 | + - Initializes RAGSystem and loads documents from `../docs` on startup |
| 88 | + |
| 89 | +2. **rag_system.py** - Main orchestrator |
| 90 | + - Coordinates all components (document processor, vector store, AI generator, session manager) |
| 91 | + - `add_course_document()`: Process and add single course |
| 92 | + - `add_course_folder()`: Batch process all documents in a folder |
| 93 | + - `query()`: Execute RAG query using tool-based search |
| 94 | + |
| 95 | +3. **vector_store.py** - ChromaDB interface |
| 96 | + - Two collections: `course_catalog` (course metadata) and `course_content` (chunked text) |
| 97 | + - `search()`: Unified search interface with course name resolution and content filtering |
| 98 | + - Uses semantic matching for course names (partial matches work) |
| 99 | + |
| 100 | +4. **ai_generator.py** - Claude API integration |
| 101 | + - Uses Anthropic's tool calling for structured search |
| 102 | + - `generate_response()`: Handles tool execution flow |
| 103 | + - Temperature: 0, Max tokens: 800 |
| 104 | + |
| 105 | +5. **document_processor.py** - Course document parsing |
| 106 | + - Expects specific format: Course metadata (title/link/instructor) followed by lessons |
| 107 | + - `chunk_text()`: Sentence-based chunking with configurable overlap |
| 108 | + - Adds contextual prefixes to chunks (e.g., "Course X Lesson Y content:") |
| 109 | + |
| 110 | +6. **search_tools.py** - Tool-based architecture |
| 111 | + - `CourseSearchTool`: Implements semantic search as an Anthropic tool |
| 112 | + - `ToolManager`: Registers and executes tools, tracks sources |
| 113 | + - Follows abstract Tool interface pattern for extensibility |
| 114 | + |
| 115 | +7. **session_manager.py** - Conversation history |
| 116 | + - Tracks user sessions for multi-turn conversations |
| 117 | + - Configurable history length (MAX_HISTORY=2) |
| 118 | + |
| 119 | +### Data Models (models.py) |
| 120 | + |
| 121 | +- **Course**: Container for course metadata and lessons |
| 122 | +- **Lesson**: Individual lesson with number, title, optional link |
| 123 | +- **CourseChunk**: Text chunk with course/lesson metadata for vector storage |
| 124 | + |
| 125 | +### Configuration (config.py) |
| 126 | + |
| 127 | +Key settings in `Config` dataclass: |
| 128 | +- `CHUNK_SIZE=800`, `CHUNK_OVERLAP=100`: Text chunking parameters |
| 129 | +- `MAX_RESULTS=5`: Number of search results returned |
| 130 | +- `MAX_HISTORY=2`: Conversation history length |
| 131 | +- `EMBEDDING_MODEL="all-MiniLM-L6-v2"`: Sentence transformer model |
| 132 | +- `CHROMA_PATH="./chroma_db"`: Vector database location |
| 133 | + |
| 134 | +### Document Format |
| 135 | + |
| 136 | +Course documents in `docs/` folder should follow this structure: |
| 137 | + |
| 138 | +``` |
| 139 | +Course Title: [title] |
| 140 | +Course Link: [url] |
| 141 | +Course Instructor: [name] |
| 142 | +
|
| 143 | +Lesson 0: [lesson title] |
| 144 | +Lesson Link: [optional url] |
| 145 | +[lesson content...] |
| 146 | +
|
| 147 | +Lesson 1: [lesson title] |
| 148 | +... |
| 149 | +``` |
| 150 | + |
| 151 | +Supported formats: `.pdf`, `.docx`, `.txt` |
| 152 | + |
| 153 | +### RAG Query Flow |
| 154 | + |
| 155 | +1. User submits query → FastAPI endpoint |
| 156 | +2. RAGSystem creates session if needed |
| 157 | +3. AI Generator (Claude) receives query + tool definitions |
| 158 | +4. Claude decides whether to use CourseSearchTool |
| 159 | +5. If tool used: VectorStore performs semantic search (course name resolution → content search) |
| 160 | +6. Tool returns formatted results with sources |
| 161 | +7. Claude synthesizes final answer |
| 162 | +8. Response + sources returned to user |
| 163 | + |
| 164 | +### Vector Store Architecture |
| 165 | + |
| 166 | +**Two-collection design:** |
| 167 | +- **course_catalog**: Course-level metadata for course name resolution via semantic search |
| 168 | + - ID: course title |
| 169 | + - Stores: instructor, course_link, lessons (as JSON) |
| 170 | + |
| 171 | +- **course_content**: Chunked course content |
| 172 | + - ID: `{course_title}_{chunk_index}` |
| 173 | + - Metadata: course_title, lesson_number, chunk_index |
| 174 | + - Used for actual content retrieval |
| 175 | + |
| 176 | +This separation enables fuzzy course name matching while maintaining efficient content filtering. |
0 commit comments