Repository navigation
CodeWise 2.0 — RAG pipeline rework
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-textvia Ollama, and embeds each chunk together with aFile / Symbol / Linesheader. 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_caseandcamelCaseare 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/andvenv/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.pyandtests/utils.pystay distinct.
Prompting
num_ctxis 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
systemmessages 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.
/analyzenow 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>/uploadreturns 200 when all files were indexed, 207 for partial success and 400 when none were. Unknown project IDs return 404.POST /api/projects/<id>/asksources now includesymbol,score,similarityandmatched_by, and the response includessearch_queryandcontext_found.- New:
POST /api/projects/<id>/ask/streamstreams the answer as Server-Sent Events (sources,token,done). GET /api/projects/<id>/analyzealso returnsfiles_analyzedandmode(fullormap_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
pip install -r requirements.txtollama pull nomic-embed-text- 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.
- Settings are now environment variables (see the README), for example
CODEWISE_CHAT_MODEL,CODEWISE_NUM_CTXandCODEWISE_TOP_K. - Flask debug mode is off by default (
CODEWISE_DEBUG=1turns it on), and CORS only allowshttp://localhost:5173. SetCODEWISE_CORS_ORIGINSif your frontend runs elsewhere.
Under the hood
backend.pyis now a thin entry point. The logic lives in thecodewise/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