Skip to content

CodeWise 2.0 — RAG pipeline rework

Choose a tag to compare

@shekharsanskar9 shekharsanskar9 released this 27 Sep 15:15
· 5 commits to main since this release

CodeWise 2.0 rebuilds the backend's retrieval pipeline so answers are grounded in the right code, cite real line numbers, and stay inside the model's context window. The API is unchanged apart from the additions listed below, so the existing React frontend works without modification.

Highlights

Better retrieval

  • Structure-aware chunking. Python is split by AST (functions, classes, and methods of large classes, with decorators and comments attached). JavaScript, TypeScript, Java, Go, Rust, C and C++ are split by top-level brace blocks, and Markdown by headings. The old fixed-size windows cut functions in half.
  • Code-aware embeddings. Uses nomic-embed-text via Ollama, and embeds each chunk together with a File / Symbol / Lines header. If the model isn't installed, it falls back to MiniLM with a warning.
  • Hybrid search. Vector search plus BM25 keyword search that understands identifiers (snake_case and camelCase are split), fused with Reciprocal Rank Fusion. Files named in the question are boosted, and weak matches are dropped.
  • Follow-up questions. "How does it handle errors?" is rewritten into a standalone search query using the chat history.
  • Less noise. Lockfiles, minified bundles, binaries, and folders such as node_modules/, dist/ and venv/ are rejected at upload.

Correctness fixes

  • Identical content in two files (licence headers, empty __init__.py) no longer collides onto one chunk ID.
  • Re-uploading a file replaces its old chunks instead of mixing old and new code.
  • Line numbers start at 1, and every line in the prompt is numbered, so citations point at real lines.
  • Project metadata is stored in SQLite (codewise.db) and survives restarts. Existing Chroma collections are adopted automatically.
  • Relative paths are kept, so src/utils.py and tests/utils.py stay distinct.

Prompting

  • num_ctx is set explicitly (default 8192), and retrieved code and history are fitted to a token budget. Ollama's small default window used to cut the start of the prompt without warning.
  • Clients can no longer inject system messages through the chat history.
  • When no relevant code is found, the model is told so, and it answers "not in the code" instead of guessing.
  • /analyze now uses the file tree, README, dependency manifests and entry points. For large projects it summarises files first and then combines the summaries (map-reduce).

API changes

  • POST /api/projects/<id>/upload returns 200 when all files were indexed, 207 for partial success and 400 when none were. Unknown project IDs return 404.
  • POST /api/projects/<id>/ask sources now include symbol, score, similarity and matched_by, and the response includes search_query and context_found.
  • New: POST /api/projects/<id>/ask/stream streams the answer as Server-Sent Events (sources, token, done).
  • GET /api/projects/<id>/analyze also returns files_analyzed and mode (full or map_reduce).
  • Errors come back as JSON with sensible status codes: 400, 404, 409 (embedding model mismatch), 413 and 502 (model unavailable).

Upgrading from 1.0

  1. pip install -r requirements.txt
  2. ollama pull nomic-embed-text
  3. Create a new project and upload your code again. Projects indexed with 1.0's embedding model return 409 when you ask questions, because vectors from different models can't be mixed.
  4. Settings are now environment variables (see the README), for example CODEWISE_CHAT_MODEL, CODEWISE_NUM_CTX and CODEWISE_TOP_K.
  5. Flask debug mode is off by default (CODEWISE_DEBUG=1 turns it on), and CORS only allows http://localhost:5173. Set CODEWISE_CORS_ORIGINS if your frontend runs elsewhere.

Under the hood

  • backend.py is now a thin entry point. The logic lives in the codewise/ package (config, files, chunking, embeddings, index, retrieval, llm, store, service, app), so each part can be tested and swapped on its own.
  • 45 tests (pytest) cover chunking, file filtering, BM25, prompt building and the API. They use fake models, so Ollama isn't needed.
  • Retrieval evaluation (python eval/retrieval_eval.py) reports hit@k and MRR for vector, keyword and hybrid search. On CodeWise's own backend (21 questions, MiniLM fallback embeddings), hybrid search reached 100% hit@5 with an MRR of 0.806, against 0.756 for vector-only and 0.754 for keyword-only. The question set is small and was written by the developer, so treat these numbers as a regression check, not a benchmark.
  • Generated files (chroma_data/, node_modules/, local databases, caches) are no longer tracked.

Full diff: e3f6d2b...v2.0