-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontext_loader.py
More file actions
169 lines (140 loc) · 5.3 KB
/
context_loader.py
File metadata and controls
169 lines (140 loc) · 5.3 KB
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
#!/usr/bin/env python3
"""
AI Code Assistant - Real implementation with LangChain
Index and understand your codebase
"""
import os
import json
from pathlib import Path
from typing import List, Dict, Optional
from dotenv import load_dotenv
try:
from langchain_community.document_loaders import TextLoader, DirectoryLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import Chroma
LANGCHAIN_AVAILABLE = True
except ImportError:
LANGCHAIN_AVAILABLE = False
load_dotenv()
# Supported code extensions
CODE_EXTENSIONS = {
'.py': 'Python',
'.js': 'JavaScript',
'.ts': 'TypeScript',
'.java': 'Java',
'.go': 'Go',
'.rs': 'Rust',
'.cpp': 'C++',
'.c': 'C',
'.cs': 'C#',
'.rb': 'Ruby',
'.php': 'PHP',
'.swift': 'Swift',
'.kt': 'Kotlin',
'.scala': 'Scala',
'.md': 'Markdown',
'.json': 'JSON',
'.yaml': 'YAML',
'.yml': 'YAML',
'.sh': 'Shell',
'.sql': 'SQL',
}
class CodebaseIndexer:
"""Index codebase for AI understanding"""
def __init__(self, persist_dir: str = "./code_index"):
self.persist_dir = persist_dir
self.embeddings = None
self.vectorstore = None
if LANGCHAIN_AVAILABLE:
api_key = os.getenv("OPENAI_API_KEY")
if api_key:
self.embeddings = OpenAIEmbeddings(openai_api_key=api_key)
def load_codebase(self, path: str) -> List[Dict]:
"""Load all code files from directory"""
path_obj = Path(path)
files = []
if not path_obj.exists():
return []
for ext in CODE_EXTENSIONS.keys():
for file_path in path_obj.rglob(f"*{ext}"):
# Skip common ignored directories
if any(ignored in str(file_path) for ignored in ['node_modules', '.git', '__pycache__', 'venv', '.venv', 'build', 'dist']):
continue
try:
relative = file_path.relative_to(path_obj)
language = CODE_EXTENSIONS.get(file_path.suffix, 'Unknown')
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
content = f.read()
files.append({
'path': str(relative),
'language': language,
'content': content,
'size': len(content),
'lines': len(content.splitlines())
})
except Exception as e:
continue
return files
def index_codebase(self, path: str) -> Dict:
"""Index codebase into vector store"""
if not LANGCHAIN_AVAILABLE:
return {"status": "needs_langchain", "files": 0}
if not self.embeddings:
return {"status": "needs_api_key", "files": 0}
files = self.load_codebase(path)
if not files:
return {"status": "no_files", "files": 0}
print(f"📂 Loading {len(files)} code files...")
# Create documents
documents = []
for file in files:
from langchain.schema import Document
doc = Document(
page_content=f"File: {file['path']}\nLanguage: {file['language']}\n\n{file['content'][:5000]}",
metadata={"source": file['path'], "language": file['language']}
)
documents.append(doc)
# Split into chunks
splitter = RecursiveCharacterTextSplitter(
chunk_size=2000,
chunk_overlap=200,
length_function=len
)
chunks = splitter.split_documents(documents)
# Create vector store
self.vectorstore = Chroma.from_documents(
documents=chunks,
embedding=self.embeddings,
persist_directory=self.persist_dir
)
self.vectorstore.persist()
return {
"status": "success",
"files": len(files),
"chunks": len(chunks),
"languages": list(set(f['language'] for f in files))
}
def get_context(self, query: str, top_k: int = 5) -> List[Dict]:
"""Get relevant code context for a query"""
if not self.vectorstore:
return []
docs = self.vectorstore.similarity_search(query, k=top_k)
return [{"content": doc.page_content, "source": doc.metadata.get("source", "unknown")} for doc in docs]
def main():
import argparse
parser = argparse.ArgumentParser(description="Codebase Indexer")
parser.add_argument("--path", "-p", default=".", help="Path to codebase")
parser.add_argument("--query", "-q", help="Query to search")
args = parser.parse_args()
indexer = CodebaseIndexer()
if args.path:
result = indexer.index_codebase(args.path)
print(json.dumps(result, indent=2))
if args.query:
results = indexer.get_context(args.query)
print(f"\n🔍 Found {len(results)} relevant files:")
for r in results:
print(f" 📄 {r['source']}")
if __name__ == "__main__":
main()