-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
112 lines (92 loc) · 3.16 KB
/
api.py
File metadata and controls
112 lines (92 loc) · 3.16 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
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import List, Optional
import uvicorn
app = FastAPI(
title="CodeLens API",
description="AI-powered codebase intelligence",
version="1.0.0"
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
# Global state
state = {
"retriever": None,
"generator": None,
"reranker": None,
"intelligence": None,
"indexed": False,
}
class IndexRequest(BaseModel):
repo_url: str
class QueryRequest(BaseModel):
query: str
top_k: int = 5
class QueryResponse(BaseModel):
answer: str
sources: List[dict]
time_ms: float
@app.get("/")
def root():
return {"name": "CodeLens API", "status": "running"}
@app.get("/health")
def health():
return {"status": "healthy", "indexed": state["indexed"]}
@app.post("/index")
def index_repo(request: IndexRequest):
from src.ingestion import GitHubLoader
from src.chunking import ASTChunker
from src.retrieval import HybridRetriever, LightweightReranker
from src.generation import CodeGenerator, CodeIntelligence
try:
loader = GitHubLoader()
files = loader.clone_repo(request.repo_url)
chunker = ASTChunker()
chunks = chunker.chunk_files(files)
state["retriever"] = HybridRetriever()
state["generator"] = CodeGenerator()
state["reranker"] = LightweightReranker()
state["retriever"].index(chunks, files)
state["intelligence"] = CodeIntelligence(state["retriever"], state["generator"])
state["indexed"] = True
return {
"success": True,
"files": len(files),
"chunks": len(chunks)
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/query", response_model=QueryResponse)
def query(request: QueryRequest):
import time
if not state["indexed"]:
raise HTTPException(status_code=400, detail="No repository indexed")
try:
start = time.time()
results = state["retriever"].search(request.query, top_k=request.top_k * 2)
results = state["reranker"].rerank(request.query, results, top_k=request.top_k)
answer = state["generator"].generate(request.query, results)
elapsed = (time.time() - start) * 1000
sources = []
for r in results[:5]:
meta = r.get("metadata", {})
sources.append({
"file": meta.get("file_path", ""),
"name": meta.get("name", ""),
"type": meta.get("chunk_type", "")
})
return QueryResponse(answer=answer, sources=sources, time_ms=elapsed)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/explain")
def explain(name: str):
if not state["indexed"]:
raise HTTPException(status_code=400, detail="No repository indexed")
return state["intelligence"].explain_function(name)
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)