|
|
|
|
|
from fastapi import FastAPI, HTTPException |
|
|
from pydantic import BaseModel |
|
|
import logging, json |
|
|
from pathlib import Path |
|
|
|
|
|
logging.basicConfig(level=logging.INFO) |
|
|
|
|
|
STATUS_FILE = Path('/tmp/setup_status.json') |
|
|
READY_FLAG = Path('/tmp/faiss_ready') |
|
|
|
|
|
def get_status(): |
|
|
if not STATUS_FILE.exists(): |
|
|
return {'status': 'init', 'progress': 0} |
|
|
with open(STATUS_FILE) as f: |
|
|
return json.load(f) |
|
|
|
|
|
query_engine = None |
|
|
|
|
|
def get_engine(): |
|
|
global query_engine |
|
|
if query_engine is None: |
|
|
if not READY_FLAG.exists(): |
|
|
raise HTTPException(503, "Em construção") |
|
|
from query_engine import QueryEngine |
|
|
query_engine = QueryEngine() |
|
|
return query_engine |
|
|
|
|
|
app = FastAPI(title="Para.AI RAG", version="3.5.0") |
|
|
|
|
|
class SearchRequest(BaseModel): |
|
|
query: str |
|
|
top_k: int = 10 |
|
|
|
|
|
@app.get("/") |
|
|
async def root(): |
|
|
return {"status": "online", "rag_ready": READY_FLAG.exists(), "setup": get_status()} |
|
|
|
|
|
@app.get("/setup/status") |
|
|
async def status(): |
|
|
return get_status() |
|
|
|
|
|
@app.get("/setup/logs") |
|
|
async def logs(): |
|
|
try: |
|
|
with open('/tmp/setup_debug.log') as f: |
|
|
return {"logs": f.read()} |
|
|
except: |
|
|
return {"logs": "N/A"} |
|
|
|
|
|
@app.post("/search/embedding") |
|
|
async def search(req: SearchRequest): |
|
|
return get_engine().search_by_embedding(req.query, req.top_k) |
|
|
|