Spaces:
Sleeping
Sleeping
Update rag_routerv2.py
Browse files- rag_routerv2.py +61 -56
rag_routerv2.py
CHANGED
|
@@ -76,63 +76,68 @@ class QueryTableResponse(BaseModel):
|
|
| 76 |
|
| 77 |
@router.post("/create_table", response_model=CreateTableResponse)
|
| 78 |
async def create_embedding_table(
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
) -> CreateTableResponse:
|
| 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 |
@router.post("/query_table/{table_id}", response_model=QueryTableResponse)
|
|
|
|
| 76 |
|
| 77 |
@router.post("/create_table", response_model=CreateTableResponse)
|
| 78 |
async def create_embedding_table(
|
| 79 |
+
user_id: str,
|
| 80 |
+
files: List[UploadFile] = File(...),
|
| 81 |
+
table_id: Optional[str] = None,
|
| 82 |
+
table_name: Optional[str] = None
|
| 83 |
) -> CreateTableResponse:
|
| 84 |
+
try:
|
| 85 |
+
db = get_db()
|
| 86 |
+
table_id = table_id or str(uuid.uuid4())
|
| 87 |
+
table_name = table_name or f"knowledge-base-{str(uuid.uuid4())[:4]}"
|
| 88 |
+
|
| 89 |
+
# Check if table exists
|
| 90 |
+
existing = db.execute(
|
| 91 |
+
'SELECT id FROM tables WHERE user_id = ? AND table_id = ?',
|
| 92 |
+
(user_id, table_id)
|
| 93 |
+
).fetchone()
|
| 94 |
+
|
| 95 |
+
directory_path = f"./data/{table_id}"
|
| 96 |
+
os.makedirs(directory_path, exist_ok=True)
|
| 97 |
+
|
| 98 |
+
for file in files:
|
| 99 |
+
if not file.filename:
|
| 100 |
+
raise HTTPException(status_code=400, detail="Invalid filename")
|
| 101 |
+
if os.path.splitext(file.filename)[1].lower() not in {".pdf", ".docx", ".csv", ".txt", ".md"}:
|
| 102 |
+
raise HTTPException(status_code=400, detail="Unsupported file type")
|
| 103 |
+
|
| 104 |
+
file_path = os.path.join(directory_path, file.filename)
|
| 105 |
+
with open(file_path, "wb") as buffer:
|
| 106 |
+
shutil.copyfileobj(file.file, buffer)
|
| 107 |
+
|
| 108 |
+
vector_store = LanceDBVectorStore(
|
| 109 |
+
uri="./lancedb/dev",
|
| 110 |
+
table_name=table_id,
|
| 111 |
+
mode="overwrite",
|
| 112 |
+
query_type="hybrid"
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
documents = SimpleDirectoryReader(directory_path).load_data()
|
| 116 |
+
index = VectorStoreIndex.from_documents(documents, vector_store=vector_store)
|
| 117 |
+
index.storage_context.persist(persist_dir=f"./lancedb/index/{table_id}")
|
| 118 |
+
|
| 119 |
+
if not existing:
|
| 120 |
+
db.execute(
|
| 121 |
+
'INSERT INTO tables (user_id, table_id, table_name) VALUES (?, ?, ?)',
|
| 122 |
+
(user_id, table_id, table_name)
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
for file in files:
|
| 126 |
+
db.execute(
|
| 127 |
+
'INSERT OR REPLACE INTO table_files (table_id, filename, file_path) VALUES (?, ?, ?)',
|
| 128 |
+
(table_id, file.filename, f"./data/{table_id}/{file.filename}")
|
| 129 |
+
)
|
| 130 |
+
db.commit()
|
| 131 |
+
|
| 132 |
+
return CreateTableResponse(
|
| 133 |
+
table_id=table_id,
|
| 134 |
+
message="Success",
|
| 135 |
+
status="success",
|
| 136 |
+
table_name=table_name
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
except Exception as e:
|
| 140 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 141 |
|
| 142 |
|
| 143 |
@router.post("/query_table/{table_id}", response_model=QueryTableResponse)
|