File size: 9,061 Bytes
4deadc0
99c3958
4deadc0
 
 
99c3958
 
 
4deadc0
99c3958
4deadc0
99c3958
4deadc0
99c3958
4deadc0
99c3958
4deadc0
 
 
99c3958
4deadc0
99c3958
4deadc0
99c3958
4deadc0
99c3958
4deadc0
99c3958
4deadc0
99c3958
4deadc0
 
 
 
 
 
99c3958
4deadc0
 
99c3958
 
 
4deadc0
99c3958
4deadc0
99c3958
4deadc0
99c3958
4deadc0
 
 
 
 
99c3958
 
4deadc0
99c3958
 
 
4deadc0
99c3958
4deadc0
99c3958
4deadc0
99c3958
4deadc0
99c3958
4deadc0
99c3958
4deadc0
99c3958
4deadc0
99c3958
4deadc0
 
 
 
99c3958
4deadc0
99c3958
4deadc0
99c3958
4deadc0
99c3958
4deadc0
99c3958
4deadc0
 
 
99c3958
 
4deadc0
99c3958
4deadc0
99c3958
4deadc0
99c3958
4deadc0
99c3958
4deadc0
99c3958
4deadc0
 
 
 
 
 
 
 
99c3958
4deadc0
99c3958
4deadc0
99c3958
4deadc0
99c3958
4deadc0
99c3958
4deadc0
 
 
 
 
99c3958
4deadc0
99c3958
4deadc0
99c3958
 
 
4deadc0
99c3958
4deadc0
99c3958
4deadc0
99c3958
4deadc0
 
 
 
 
 
99c3958
4deadc0
99c3958
4deadc0
99c3958
4deadc0
99c3958
4deadc0
 
 
99c3958
4deadc0
99c3958
4deadc0
 
 
99c3958
4deadc0
99c3958
4deadc0
 
 
99c3958
4deadc0
99c3958
4deadc0
 
 
99c3958
4deadc0
99c3958
4deadc0
 
99c3958
4deadc0
99c3958
4deadc0
99c3958
4deadc0
 
 
 
 
99c3958
4deadc0
99c3958
4deadc0
99c3958
 
 
4deadc0
99c3958
4deadc0
99c3958
4deadc0
 
 
99c3958
 
4deadc0
99c3958
4deadc0
99c3958
4deadc0
 
 
99c3958
 
4deadc0
99c3958
4deadc0
 
 
99c3958
 
4deadc0
99c3958
4deadc0
 
99c3958
 
 
4deadc0
99c3958
4deadc0
99c3958
4deadc0
 
99c3958
 
4deadc0
99c3958
4deadc0
99c3958
4deadc0
 
99c3958
4deadc0
 
99c3958
4deadc0
 
99c3958
4deadc0
99c3958
4deadc0
99c3958
 
4deadc0
99c3958
4deadc0
99c3958
4deadc0
99c3958
4deadc0
 
99c3958
 
4deadc0
99c3958
4deadc0
99c3958
4deadc0
99c3958
4deadc0
99c3958
4deadc0
99c3958
4deadc0
99c3958
4deadc0
99c3958
4deadc0
 
99c3958
 
4deadc0
99c3958
4deadc0
 
99c3958
 
4deadc0
99c3958
4deadc0
 
 
 
 
 
 
 
 
 
 
99c3958
 
 
 
4deadc0
99c3958
4deadc0
99c3958
4deadc0
99c3958
4deadc0
 
99c3958
 
4deadc0
99c3958
4deadc0
 
99c3958
 
4deadc0
99c3958
4deadc0
99c3958
 
4deadc0
 
 
99c3958
4deadc0
 
 
 
99c3958
4deadc0
 
 
99c3958
 
 
 
 
 
 
4deadc0
 
99c3958
 
 
 
4deadc0
 
 
 
 
99c3958
 
 
 
 
 
4deadc0
99c3958
4deadc0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99c3958
 
 
 
4deadc0
99c3958
4deadc0
 
 
 
 
 
 
 
 
 
99c3958
 
 
4deadc0
99c3958
4deadc0
 
 
 
 
 
99c3958
 
 
4deadc0
99c3958
4deadc0
99c3958
4deadc0
 
 
 
 
 
 
 
 
 
 
99c3958
 
 
4deadc0
99c3958
4deadc0
99c3958
4deadc0
99c3958
4deadc0
99c3958
4deadc0
99c3958
4deadc0
99c3958
 
4deadc0
99c3958
4deadc0
99c3958
4deadc0
 
 
99c3958
4deadc0
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# AI Codebase Assistant

An AI-powered developer tool that lets you interact with an entire code repository using **RAG (Retrieval-Augmented Generation)** and LLMs.

It can answer questions about your codebase, detect potential bugs, analyze code complexity, explain functions, generate documentation, create new files, and interact with GitHub through the GitHub MCP API.

---

## Features

### Codebase Question Answering

Ask natural-language questions about your repository.

**Example:**

```text
How does authentication work?
```

The assistant searches the relevant parts of the codebase and generates an answer with source references.

---

### Bug Detection

Analyze a source file and identify potential issues using an LLM-powered code review.

Example:

```text
[HIGH] Line 34
SQL query uses string concatenation

Recommendation:
Use parameterized queries to prevent SQL injection.
```

The system returns the issue severity, location, and recommendation.

---

### Cyclomatic Complexity Analysis

Analyze the complexity of Python functions using **Radon**.

Example:

```text
[A] process_order        complexity = 2
[B] validate_cart        complexity = 5
[C] apply_discounts     complexity = 8
[F] handle_edge_cases   complexity = 18
```

This helps identify functions that may be difficult to maintain or test.

---

### Function Explanation

Select a function and get a plain-English explanation of what it does.

For Python code, the project uses the `ast` module to extract the function and provides relevant repository context to the LLM.

---

### Documentation Generation

Generate documentation automatically for your codebase.

The assistant can generate:

- Module documentation
- Function explanations
- Docstrings
- Repository README files

---

### AI File Generation

Describe the file you want to create and let the AI generate it.

Example:

```text
Create a Rectangle class in JavaScript
that calculates area and perimeter.
```

The generated file is shown before it is written to the repository, allowing the user to approve it first.

---

### GitHub MCP Integration

The project integrates with the **GitHub Copilot MCP API**.

It currently supports access to **44 GitHub MCP tools**, including:

- `search_code`
- `search_repositories`
- `get_file_contents`
- `list_issues`
- `create_branch`
- `create_pull_request`
- `push_files`
- `fork_repository`

---

## RAG-Based Code Search

The project uses **Retrieval-Augmented Generation (RAG)** to work with repository-level code.

Repository files are:

1. Loaded and filtered
2. Split into smaller chunks
3. Converted into embeddings
4. Stored in ChromaDB
5. Retrieved based on semantic similarity when a question is asked

The retrieved code is then provided to the LLM as context.

Source metadata such as file path and line range is preserved during this process.

---

## Supported Languages

The repository ingestion system currently supports:

| Extension | Language |
|---|---|
| `.py` | Python |
| `.js` | JavaScript |
| `.ts` | TypeScript |
| `.java` | Java |
| `.go` | Go |
| `.md` | Markdown |

---

## Tech Stack

### AI / LLM

- Groq
- Llama 3.3 70B
- Google Gemini

### RAG

- LangChain
- Google Gemini Embeddings
- ChromaDB

### Backend

- Python
- FastAPI
- Pydantic Settings

### Code Analysis

- Python AST
- Radon
- LLM-based code analysis

### Integration

- GitHub MCP
- GitHub Copilot MCP API

---

## LLM Providers

| Provider | Model | Usage |
|---|---|---|
| Groq | `llama-3.3-70b-versatile` | LLM generation |
| Gemini | Configurable | LLM generation |
| Gemini Embeddings | `models/gemini-embedding-001` | Code embeddings |

The project supports switching between Groq and Gemini for LLM generation.

Gemini Embeddings are used for semantic retrieval.

---

# Installation

## 1. Clone the Repository

```bash
git clone <your-repository-url>
cd "Codebase Assistant"
```

## 2. Create a Virtual Environment

### Windows

```bash
python -m venv .venv
.venv\Scripts\Activate.ps1
```

### Linux / macOS

```bash
python3 -m venv .venv
source .venv/bin/activate
```

## 3. Install Dependencies

```bash
pip install -r requirements.txt
```

---
## πŸ—οΈ Architecture

![AI Codebase Assistant Architecture](./architecture.png)

The system combines a RAG pipeline, LLM providers, code analysis services,
FastAPI, and GitHub MCP integration to provide repository-level AI assistance.


# Configuration

Create a `.env` file in the project root.

```env
LLM_PROVIDER=groq

GROQ_API_KEY=your_groq_api_key
GROQ_MODEL=llama-3.3-70b-versatile

GEMINI_API_KEY=your_gemini_api_key
EMBEDDING_MODEL=models/gemini-embedding-001

VECTOR_DB_PATH=./vector_db

GITHUB_MCP_TOKEN=your_github_token
```

### Required API Keys

**Groq**

Used for LLM generation when:

```env
LLM_PROVIDER=groq
```

**Gemini**

Required for embeddings because the project uses Gemini Embeddings for repository indexing.

**GitHub Token**

Required for GitHub MCP functionality.

---

# Running the CLI

Start the interactive CLI:

```bash
python cli.py
```

The application will ask for the repository you want to analyze.

```text
β–Ά Enter the path to the repository you want to analyse:
```

After the repository is indexed, you can choose from:

```text
1  Ask a question about the codebase
2  Detect bugs in a file
3  Cyclomatic complexity analysis
4  Explain a function
5  Generate module documentation
6  Generate README
7  Propose & create a new file
8  List GitHub MCP tools
9  Re-ingest repository
0  Exit
```

---

# REST API

The project also provides a FastAPI REST API.

Start the server:

```bash
python -m uvicorn app:app --reload --port 8000
```

Open the interactive API documentation:

```text
http://localhost:8000/docs
```

## API Endpoints

| Method | Endpoint | Description |
|---|---|---|
| `GET` | `/health` | Health check |
| `POST` | `/api/query` | Ask questions about the codebase |
| `POST` | `/api/analyze/bugs` | Detect potential bugs |
| `POST` | `/api/analyze/complexity` | Analyze cyclomatic complexity |
| `POST` | `/api/analyze/explain` | Explain a function |
| `POST` | `/api/docs/module` | Generate module documentation |
| `POST` | `/api/docs/readme` | Generate README |
| `POST` | `/api/files/propose` | Generate a file proposal |
| `POST` | `/api/files/approve` | Write an approved file |

---

## Example API Request

```bash
curl -X POST http://localhost:8000/api/query \
  -H "Content-Type: application/json" \
  -d '{"query": "How does authentication work?", "k": 5}'
```

Example response:

```json
{
  "answer": "Authentication is handled by ...",
  "sources": [
    {
      "file_path": "src/auth.py",
      "start_line": 10,
      "end_line": 45
    }
  ]
}
```

---

# πŸ“ Project Structure

```text
Codebase Assistant/
β”‚
β”œβ”€β”€ cli.py
β”œβ”€β”€ app.py
β”œβ”€β”€ config.py
β”œβ”€β”€ llm.py
β”œβ”€β”€ mcp_client.py
β”œβ”€β”€ requirements.txt
β”‚
β”œβ”€β”€ rag/
β”‚   β”œβ”€β”€ repository_loader.py
β”‚   β”œβ”€β”€ splitter.py
β”‚   β”œβ”€β”€ embedding.py
β”‚   β”œβ”€β”€ retriever.py
β”‚   └── rag_chain.py
β”‚
β”œβ”€β”€ services/
β”‚   β”œβ”€β”€ code_analysis.py
β”‚   β”œβ”€β”€ documentation.py
β”‚   └── file_creator.py
β”‚
β”œβ”€β”€ api/
β”‚   └── routes.py
β”‚
└── vector_db/
    └── chroma.sqlite3
```

---

# Configuration Options

| Setting | Default | Description |
|---|---|---|
| `LLM_PROVIDER` | `groq` | LLM provider |
| `GROQ_MODEL` | `llama-3.3-70b-versatile` | Groq model |
| `EMBEDDING_MODEL` | `models/gemini-embedding-001` | Embedding model |
| `VECTOR_DB_PATH` | `./vector_db` | ChromaDB storage |
| `CHUNK_SIZE` | `1200` | Chunk size |
| `CHUNK_OVERLAP` | `100` | Chunk overlap |
| `MAX_FILE_SIZE_KB` | `1042` | Maximum file size |
| `ALLOWED_EXTENSIONS` | `.py,.js,.ts,.go,.java,.md` | Supported files |

---

# Limitations

- Repository ingestion currently runs locally.
- External API keys are required for LLM and embedding services.
- Gemini embedding quotas may limit large repositories.
- Only the currently supported file types are indexed.
- LLM-generated code and bug reports should be reviewed before use.
- The vector database currently focuses on the actively ingested repository.

---

# Future Improvements

Planned or possible improvements include:

- More programming language support
- Incremental repository indexing
- GitHub repository ingestion
- AST-based code chunking
- Hybrid keyword + vector search
- Retrieval reranking
- Dependency graph analysis
- Automated test generation
- Automated pull request review
- Multi-repository search
- Web-based developer interface

---

# Project Goal

The goal of this project is to build an AI developer assistant that can understand and interact with an entire codebase rather than only individual code snippets.

It combines:

**RAG + LLMs + Vector Search + Code Analysis + FastAPI + MCP**

into a single developer-focused tool.

---


# Author

**Armaan Alam**

AI Engineer & Software Developer

Interested in:

- Generative AI
- RAG Systems
- Backend Engineering
- LLM Applications
- AI Developer Tools
- Machine Learning