Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

27 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿง  CodeMind โ€” Chat With Your Codebase (RAG + ML)

Next.js FastAPI PostgreSQL scikit-learn Gemini Cost

An AI-powered semantic search and Q&A tool for GitHub codebases โ€” plus an ML-based issue auto-triage system. Ask natural-language questions about any repo and get grounded answers with file/line citations.


๐Ÿ“– Table of Contents


๐ŸŽฏ What This Is

Two features in one project:

  1. RAG-powered code Q&A โ€” index any GitHub repo's Python files (parsed by function/class using the ast module), embed them, store in pgvector, and answer natural-language questions grounded in the actual code with citations.
  2. ML-based issue auto-triage โ€” a scikit-learn classifier (TF-IDF + Logistic Regression) trained on historical labeled GitHub issues, automatically labels new issues as bug, feature_request, question, or duplicate via a webhook.

๐Ÿ”€ Architecture

flowchart TD
    A["๐Ÿ“ฆ GitHub Repo"] --> B["โœ‚๏ธ AST parser<br/>(function/class chunks)"]
    B --> C["๐Ÿ”ข sentence-transformers<br/>embeddings"]
    C --> D["๐Ÿ—„๏ธ pgvector (PostgreSQL)"]

    E["๐Ÿ’ฌ User question"] --> F["๐Ÿ” Embed + search pgvector"]
    F -->|"top-5 chunks"| G["โœจ Gemini LLM"]
    G -->|"grounded answer + citations"| E

    H["๐Ÿ› New GitHub Issue"] --> I["๐Ÿท๏ธ TF-IDF + LogisticRegression"]
    I -->|"category + confidence"| J["๐Ÿค– Auto-label via GitHub API"]

    style A fill:#181717,color:#fff
    style B fill:#64748B,color:#fff
    style C fill:#4285F4,color:#fff
    style D fill:#336791,color:#fff
    style E fill:#0EA5E9,color:#fff
    style F fill:#4285F4,color:#fff
    style G fill:#4285F4,color:#fff
    style H fill:#181717,color:#fff
    style I fill:#F59E0B,color:#000
    style J fill:#181717,color:#fff
Loading

๐Ÿ“ Folder Structure

codemind/
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ frontend/                          # Next.js app
โ”‚   โ”œโ”€โ”€ package.json
โ”‚   โ”œโ”€โ”€ next.config.js
โ”‚   โ”œโ”€โ”€ tailwind.config.js
โ”‚   โ”œโ”€โ”€ postcss.config.js
โ”‚   โ”œโ”€โ”€ tsconfig.json
โ”‚   โ”œโ”€โ”€ .env.local.example
โ”‚   โ””โ”€โ”€ app/
โ”‚       โ”œโ”€โ”€ layout.tsx
โ”‚       โ”œโ”€โ”€ globals.css
โ”‚       โ”œโ”€โ”€ page.tsx                   # Connect/index a repo
โ”‚       โ””โ”€โ”€ chat/page.tsx              # Chat UI
โ””โ”€โ”€ backend/                           # Python FastAPI app
    โ”œโ”€โ”€ requirements.txt
    โ”œโ”€โ”€ .env.example
    โ”œโ”€โ”€ main.py                        # API routes
    โ”œโ”€โ”€ db.py                          # Postgres/pgvector connection
    โ”œโ”€โ”€ schema.sql                     # Table definitions
    โ”œโ”€โ”€ indexer.py                     # Clone, AST parse, chunk, embed
    โ”œโ”€โ”€ chat.py                        # Retrieval + LLM generation
    โ”œโ”€โ”€ webhook.py                     # Issue auto-triage webhook
    โ””โ”€โ”€ classifier/
        โ”œโ”€โ”€ fetch_training_data.py     # Pull labeled issues from GitHub
        โ”œโ”€โ”€ train.py                   # Train TF-IDF + LogisticRegression
        โ””โ”€โ”€ predict.py                 # Load model, predict category

โœ… Prerequisites

  • Node.js 18+ and Python 3.10+
  • PostgreSQL with the ability to install extensions (local install, or a free-tier managed Postgres from Render/Railway/Supabase โ€” all support pgvector)
  • Free Gemini API key from ai.google.dev
  • GitHub Personal Access Token
  • Git installed locally

Part 1 โ€” API Keys & Accounts

Gemini API key:

  1. Go to ai.google.dev โ†’ Get API key โ†’ sign in โ†’ Create API key โ†’ copy it

GitHub Personal Access Token:

  1. GitHub โ†’ Settings โ†’ Developer settings โ†’ Personal access tokens โ†’ Fine-grained tokens โ†’ Generate new token
  2. Permissions needed: Issues: Read & write, Contents: Read-only
  3. Copy the token

GitHub Webhook Secret: just generate any random string yourself (used to verify webhook authenticity):

openssl rand -hex 20

Part 2 โ€” Database Setup (pgvector)

Option A โ€” Local Postgres:

# Install pgvector extension (Mac via Homebrew)
brew install pgvector

# Create the database
createdb codemind

Option B โ€” Free managed Postgres (Render, Railway, or Supabase โ€” all support pgvector):

  1. Create a free Postgres instance on any of these
  2. Copy the connection string they give you

Either way, once you have a DATABASE_URL, run the schema:

cd backend
python db.py

This enables the vector extension and creates all tables (repos, code_chunks, issue_predictions).


Part 3 โ€” Backend Setup

cd backend
python -m venv venv
source venv/bin/activate      # Windows: venv\Scripts\activate
pip install -r requirements.txt

cp .env.example .env
# Fill in DATABASE_URL, GEMINI_API_KEY, GITHUB_TOKEN, GITHUB_WEBHOOK_SECRET

Run the API server:

uvicorn main:app --reload --port 8000

Visit http://localhost:8000 โ€” you should see {"status": "ok", "service": "CodeMind API"}.


Part 4 โ€” Frontend Setup

cd frontend
npm install
cp .env.local.example .env.local
# Set NEXT_PUBLIC_API_URL=http://localhost:8000
npm run dev

Visit http://localhost:3000 โ€” enter a repo like psf/requests (a smaller, well-known Python repo, good for testing) and click Index Repository.


Part 5 โ€” Training the Issue Classifier

cd backend/classifier
python fetch_training_data.py    # pulls labeled issues from a public repo
python train.py                  # trains and saves issue_classifier.joblib

You'll see a classification_report printed showing precision/recall per category โ€” this tells you how well the model performs before trusting it in production. The trained model is saved as issue_classifier.joblib in the classifier/ folder, which predict.py loads automatically.

To train on a different repo's issues, edit the repo name at the bottom of fetch_training_data.py.


Part 6 โ€” GitHub Webhook Setup

To enable auto-triage on your own repo:

  1. Go to your repo โ†’ Settings โ†’ Webhooks โ†’ Add webhook
  2. Payload URL: https://your-backend-url.com/webhook/issues
  3. Content type: application/json
  4. Secret: the same value you set as GITHUB_WEBHOOK_SECRET
  5. Which events: select Issues only
  6. Save

Now every new issue opened on that repo will trigger the classifier and auto-apply a label if confidence is high enough.


Part 7 โ€” Running Everything Locally

# Terminal 1 โ€” backend
cd backend
source venv/bin/activate
uvicorn main:app --reload --port 8000

# Terminal 2 โ€” frontend
cd frontend
npm run dev

Test flow:

  1. Visit localhost:3000 โ†’ index a repo (e.g., psf/requests)
  2. Wait for indexing to complete โ†’ redirected to chat
  3. Ask: "How does this library handle retries?"
  4. See a grounded answer with file/line citations

Test the webhook locally (needs a public URL โ€” use ngrok):

ngrok http 8000

Use the ngrok HTTPS URL as your webhook's Payload URL temporarily while testing.


Part 8 โ€” Deployment

Backend โ†’ Render or Railway (free tier)

  1. Push the backend/ folder to a GitHub repo
  2. Create a new Web Service on Render/Railway, point it at your repo
  3. Set build command: pip install -r requirements.txt
  4. Set start command: uvicorn main:app --host 0.0.0.0 --port $PORT
  5. Add all environment variables from .env in the platform's dashboard
  6. Deploy โ€” you'll get a live URL like https://codemind-backend.onrender.com

Database โ†’ Render/Railway/Supabase managed Postgres

Use their free-tier Postgres, enable the vector extension via their SQL console, then run schema.sql against it.

Frontend โ†’ Vercel

cd frontend
npm install -g vercel
vercel login
vercel

Set NEXT_PUBLIC_API_URL to your deployed backend URL in Vercel's environment variables, then:

vercel --prod

Update your GitHub webhook

Change the Payload URL to your real deployed backend: https://your-backend.onrender.com/webhook/issues


๐Ÿ’ฐ Cost Breakdown

Resource Free Tier Cost
Vercel (frontend) 100GB bandwidth/mo $0
Render/Railway (backend + Postgres) Free tier available $0
Gemini API Free tier $0
sentence-transformers Runs locally, no API $0
scikit-learn Local training, no API $0

Total: $0/month for personal/demo use.


๐ŸŽค Interview Talking Points

  • Why AST parsing instead of naive text chunking: code has structure โ€” chunking by function/class (not arbitrary word counts) keeps each embedding semantically coherent and enables precise file/line citations.
  • Why pgvector instead of a dedicated vector DB: one database handles both structured data and vector search โ€” simpler infra, no extra service to manage.
  • Why a classical ML model (not an LLM) for issue triage: it's a well-defined, small-output classification task โ€” TF-IDF + Logistic Regression trains in seconds, costs nothing per prediction, and is easy to retrain as label definitions evolve. Using an LLM here would be slower and costlier for no real accuracy benefit.
  • Where training data came from: pulled real historical labeled issues via the GitHub API โ€” no manual labeling needed, a production-realistic way to bootstrap a dataset.
  • Confidence thresholding: the webhook only auto-applies a label above a confidence threshold, otherwise leaves it for a human โ€” avoiding confidently wrong auto-labels.

๐Ÿ”ฎ Future Improvements

  • Support multi-language parsing via tree-sitter (currently Python-only via ast)
  • Add streaming responses in the chat UI
  • Add a feedback mechanism (๐Ÿ‘/๐Ÿ‘Ž) to track answer quality
  • Re-train the classifier periodically as new labeled issues accumulate
  • Add authentication so multiple users can index their own private repos

Built as a developer-focused RAG + ML project combining semantic code search with automated issue triage.

About

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages