Spaces:
Sleeping
Sleeping
File size: 1,858 Bytes
050d655 e55e243 050d655 e55e243 050d655 | 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 | from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from ingest import ingest_repository
from query import (
VECTORSTORE_CACHE,
MEMORY_CACHE,
initialize_repo_caches,
ask_question,
)
app = FastAPI(title="RAG Backend", version="1.0.0")
app.mount("/static", StaticFiles(directory="static"), name="static")
@app.get("/")
def index():
return FileResponse("static/index.html")
class LoadRepoRequest(BaseModel):
repo_url: str
class AskRequest(BaseModel):
repo_name: str
question: str
@app.post("/load_repo")
def load_repo(payload: LoadRepoRequest):
repo_url = payload.repo_url.strip()
if not repo_url:
raise HTTPException(status_code=400, detail="repo_url is required")
repo_name = ingest_repository(repo_url)
initialize_repo_caches(repo_name)
print("AFTER LOAD:", VECTORSTORE_CACHE.keys(), MEMORY_CACHE.keys())
return {
"status": "success",
"repo": repo_name,
}
@app.post("/ask")
def ask(payload: AskRequest):
repo_name = payload.repo_name.strip()
question = payload.question.strip()
if not repo_name:
raise HTTPException(status_code=400, detail="repo_name is required")
if not question:
raise HTTPException(status_code=400, detail="question is required")
if repo_name not in VECTORSTORE_CACHE or repo_name not in MEMORY_CACHE:
raise HTTPException(status_code=400, detail="repo not loaded")
answer, docs = ask_question(question, repo_name)
sources = []
seen = set()
for doc in docs:
path = doc.metadata.get("path")
if path and path not in seen:
seen.add(path)
sources.append(path)
return {
"answer": answer,
"sources": sources,
}
|