Phase 2 adds the core AI feature: intelligent repository question-answering using:
- RAG (Retrieval-Augmented Generation)
- OpenAI Embeddings
- FAISS Vector Search
- LangChain Integration
CodeChunk- Code chunk with metadataChunkMetadata- File path, lines, languageEmbeddingRequest- Request to generate embeddingsEmbeddingProgress- Progress trackingChatQuery- Question requestChatResponse- Answer with sourcesSourceReference- Code source attributionVectorStoreInfo- Index information
- Smart code splitting by structure
- Language-aware chunking
- Preserves file context
- Configurable chunk size (1000 chars) and overlap (200 chars)
- Handles code, markdown, and structured files differently
- OpenAI embeddings integration
- Async batch processing
- Progress tracking
- Rate limiting
- Cost estimation
- Graceful fallback if API key missing
- Persistent local storage
- Repository-specific indexes
- Similarity search
- Save/load functionality
- Metadata management
- RAG-based question answering
- Context retrieval
- LangChain integration
- Source attribution
- Suggested questions
POST /api/chat/prepare/{repo_id}- Generate embeddingsPOST /api/chat/query- Ask questionsGET /api/chat/status/{repo_id}- Check statusGET /api/chat/suggestions/{repo_id}- Get suggestionsDELETE /api/chat/index/{repo_id}- Delete indexGET /api/chat/list- List all indexesGET /api/chat/health- Health check
- OpenAI API Key Required
# Edit .env file
OPENAI_API_KEY=sk-your-actual-key-here- Restart Backend
uvicorn backend.main:app --reload- Verify AI Features Available
curl http://localhost:8000/api/chat/healthExpected response:
{
"status": "healthy",
"embeddings_service": "available",
"chat_service": "available"
}curl -X POST http://localhost:8000/api/repository/analyze \
-H "Content-Type: application/json" \
-d '{
"url": "https://github.com/pallets/flask",
"branch": "main",
"include_content": true
}'Important: Set include_content: true to enable AI features!
Response includes repo_id (e.g., pallets_flask_abc123)
curl -X POST http://localhost:8000/api/chat/prepare/pallets_flask_abc123This will:
- Chunk the repository files (✂️)
- Generate embeddings via OpenAI (🧠)
- Create FAISS index (📊)
- Save to disk (💾)
Response:
{
"success": true,
"message": "Embeddings generated successfully",
"info": {
"repo_id": "pallets_flask_abc123",
"exists": true,
"total_vectors": 245,
"dimension": 1536,
"created_at": "2024-01-16T12:00:00"
},
"statistics": {
"total_chunks": 245,
"total_files": 98,
"languages": {"Python": 200, "JavaScript": 30},
"avg_chunk_size": 850
}
}curl -X POST http://localhost:8000/api/chat/query \
-H "Content-Type: application/json" \
-d '{
"repo_id": "pallets_flask_abc123",
"question": "Where is authentication implemented?",
"max_results": 5,
"include_sources": true
}'Response:
{
"success": true,
"answer": "Authentication in Flask is primarily implemented through the session management system...",
"sources": [
{
"file_path": "src/flask/sessions.py",
"start_line": 45,
"end_line": 95,
"relevance_score": 0.92,
"content_preview": "class SecureCookieSession...",
"language": "Python"
}
],
"repo_id": "pallets_flask_abc123",
"question": "Where is authentication implemented?",
"processing_time_ms": 1250.5
}"What is the main purpose of this repository?"
"What is the project structure?"
"Which files are the entry points?"
"Where is authentication implemented?"
"How is the database configured?"
"What testing framework is used?"
"How are environment variables handled?"
"Which files handle routing?"
"Where is error handling implemented?"
"How is logging configured?"
"What are the main API endpoints?"
"Explain the API flow"
"How is the application structured?"
"What design patterns are used?"
"How do components communicate?"
Endpoint: POST /api/chat/prepare/{repo_id}
Parameters:
repo_id(path): Repository IDforce_regenerate(query, optional): Force regenerationmax_chunks(query, optional): Limit chunks
Response:
{
"success": true,
"message": "Embeddings generated successfully",
"info": {...},
"statistics": {...}
}Time: ~30-60 seconds for small repos, 2-5 minutes for large repos
Cost: ~$0.01-0.10 depending on repository size
Endpoint: POST /api/chat/query
Request Body:
{
"repo_id": "string",
"question": "string",
"max_results": 5,
"include_sources": true
}Response:
{
"success": true,
"answer": "string",
"sources": [...],
"repo_id": "string",
"question": "string",
"processing_time_ms": 1250.5
}Time: ~1-3 seconds per query
Cost: ~$0.001-0.01 per query
Endpoint: GET /api/chat/status/{repo_id}
Response:
{
"success": true,
"info": {
"repo_id": "string",
"exists": true,
"total_vectors": 245,
"dimension": 1536
},
"statistics": {...}
}Endpoint: GET /api/chat/suggestions/{repo_id}
Response:
{
"success": true,
"repo_id": "string",
"suggestions": [
"What is the main purpose of this repository?",
"Where is authentication implemented?",
...
]
}Endpoint: DELETE /api/chat/index/{repo_id}
Response:
{
"success": true,
"message": "Vector index deleted for repository {repo_id}"
}Endpoint: GET /api/chat/list
Response:
{
"success": true,
"total": 3,
"stores": [...]
}import requests
import json
BASE_URL = "http://localhost:8000"
# Step 1: Analyze repository
print("Step 1: Analyzing repository...")
response = requests.post(
f"{BASE_URL}/api/repository/analyze",
json={
"url": "https://github.com/pallets/flask",
"branch": "main",
"include_content": True # Required for AI!
}
)
data = response.json()
repo_id = data["data"]["metadata"]["repo_id"]
print(f"✓ Repository analyzed: {repo_id}")
# Step 2: Prepare embeddings
print("\nStep 2: Preparing embeddings...")
response = requests.post(
f"{BASE_URL}/api/chat/prepare/{repo_id}"
)
data = response.json()
print(f"✓ Embeddings created: {data['info']['total_vectors']} vectors")
# Step 3: Ask questions
print("\nStep 3: Asking questions...")
questions = [
"Where is authentication implemented?",
"How is routing handled?",
"What testing framework is used?"
]
for question in questions:
print(f"\nQ: {question}")
response = requests.post(
f"{BASE_URL}/api/chat/query",
json={
"repo_id": repo_id,
"question": question,
"max_results": 3,
"include_sources": True
}
)
data = response.json()
print(f"A: {data['answer'][:200]}...")
print(f"Sources: {len(data['sources'])} files")
for source in data['sources'][:2]:
print(f" - {source['file_path']} (score: {source['relevance_score']:.2f})")import asyncio
import aiohttp
async def ask_repository(repo_id: str, question: str):
async with aiohttp.ClientSession() as session:
async with session.post(
f"http://localhost:8000/api/chat/query",
json={
"repo_id": repo_id,
"question": question
}
) as response:
return await response.json()
# Usage
answer = asyncio.run(ask_repository(
"pallets_flask_abc123",
"Where is authentication implemented?"
))
print(answer["answer"])User Question
↓
1. Generate Query Embedding (OpenAI)
↓
2. Search Vector Store (FAISS)
↓
3. Retrieve Top-K Similar Chunks
↓
4. Build Context from Chunks
↓
5. Generate Answer (LangChain + GPT-4)
↓
6. Return Answer + Sources
backend/
├── ai/
│ ├── __init__.py
│ ├── chunker.py # Intelligent code chunking
│ ├── embeddings.py # OpenAI embeddings
│ ├── vector_store.py # FAISS integration
│ └── chat_service.py # RAG chat service
├── api/
│ ├── chat.py # Chat endpoints
│ └── repository.py # Repository endpoints
└── models/
├── chat.py # Chat models
└── repository.py # Repository models
data/
└── vector_stores/
└── {repo_id}/
├── index.faiss # FAISS index
├── metadata.pkl # Chunk metadata
└── info.pkl # Store info
- Small repo (50 files): ~$0.01-0.05
- Medium repo (200 files): ~$0.05-0.20
- Large repo (500+ files): ~$0.20-0.50
- Per query: ~$0.001-0.01
- 100 queries: ~$0.10-1.00
- 3 repositories: ~$0.50
- 50 queries: ~$0.50
- Total: ~$1.00
Edit backend/ai/chunker.py:
chunker = CodeChunker(
chunk_size=1000, # Characters per chunk
chunk_overlap=200, # Overlap between chunks
max_chunk_size=2000 # Maximum chunk size
)Edit shared/config.py:
openai_embedding_model = "text-embedding-ada-002"
embedding_dimension = 1536
max_embeddings_per_batch = 100Edit shared/config.py:
openai_model = "gpt-4"
temperature = 0.7
max_tokens = 4096Solution:
# Check .env file
cat .env | grep OPENAI_API_KEY
# Should show:
OPENAI_API_KEY=sk-...
# If not, add it:
echo "OPENAI_API_KEY=sk-your-key" >> .env
# Restart server
uvicorn backend.main:app --reloadSolution:
# Check if embeddings were generated
curl http://localhost:8000/api/chat/status/{repo_id}
# If not, generate them:
curl -X POST http://localhost:8000/api/chat/prepare/{repo_id}Solution:
# Re-analyze with include_content=true
curl -X POST http://localhost:8000/api/repository/analyze \
-H "Content-Type: application/json" \
-d '{
"url": "https://github.com/user/repo",
"include_content": true
}'Solution:
- Reduce
max_chunksparameter - Use smaller repositories for testing
- Check internet connection
- Verify OpenAI API status
Solutions:
- Increase
max_resultsin query (more context) - Adjust
chunk_size(larger chunks = more context) - Try different questions (be specific)
- Check if relevant files were indexed
# For code-heavy repos
chunk_size = 1500 # Larger chunks
# For documentation-heavy repos
chunk_size = 800 # Smaller chunks# Process multiple questions
questions = [...]
for q in questions:
answer = await chat_service.query(...)- Vector stores are cached on disk
- Reuse embeddings across sessions
- No need to regenerate unless code changes
# Index only important files
max_chunks = 200 # Limit total chunks- OpenAI API key configured
- Backend starts successfully
- Chat health endpoint returns "healthy"
- Repository analyzed with
include_content=true - Embeddings generated successfully
- Vector store exists
- Query returns relevant answer
- Sources are attributed correctly
- Multiple queries work
- Suggested questions returned
# 1. Show repository analysis (30 sec)
curl -X POST .../api/repository/analyze -d '{"url": "..."}'
# 2. Generate embeddings (1 min)
curl -X POST .../api/chat/prepare/{repo_id}
# 3. Ask 3 questions (2 min)
# Q1: "Where is authentication implemented?"
# Q2: "How is routing handled?"
# Q3: "What testing framework is used?"
# 4. Show sources (1 min)
# Highlight file paths and relevance scores
# 5. Show suggested questions (30 sec)
curl .../api/chat/suggestions/{repo_id}After Phase 2:
- Phase 3: Documentation generation
- Phase 4: PR analysis
- Phase 5: Code suggestions
- Phase 6: Streamlit UI
Made with ❤️ by Bob
Phase 2: "Ask My Repository" - Complete! 🎉