Spaces:
Sleeping
Sleeping
Update server.py
Browse files
server.py
CHANGED
|
@@ -21,38 +21,54 @@ mcp = FastMCP("OnBase", port=port)
|
|
| 21 |
|
| 22 |
DOCUMENTS_BASE_PATH = "./"
|
| 23 |
SOURCES = {
|
| 24 |
-
"oms": "oms/",
|
| 25 |
-
# "other": "other/",
|
| 26 |
-
# "other2": "other2/"
|
| 27 |
}
|
| 28 |
|
| 29 |
-
# Cargar índices
|
| 30 |
indices: Dict[str, VectorStoreIndex] = {}
|
|
|
|
| 31 |
for source, rel_path in SOURCES.items():
|
| 32 |
full_path = os.path.join(DOCUMENTS_BASE_PATH, rel_path)
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
# Recurso dinámico para búsqueda
|
| 40 |
@mcp.resource(
|
| 41 |
uri="retriever://documentos/{fuente}",
|
| 42 |
name="DocumentRetriever",
|
| 43 |
-
description="Retrieve documents from
|
| 44 |
mime_type="application/json",
|
| 45 |
tags={"llm", "retrieval"}
|
| 46 |
)
|
| 47 |
-
def retrieve_docs(fuente: str, query: str, top_k: int = 3) -> dict:
|
| 48 |
"""
|
| 49 |
Parameters:
|
| 50 |
-
fuente: Source name (
|
| 51 |
-
query: Search query.
|
| 52 |
-
top_k: Number of results to return.
|
| 53 |
"""
|
| 54 |
if fuente not in indices:
|
| 55 |
-
|
|
|
|
| 56 |
|
| 57 |
retriever = indices[fuente].as_retriever(similarity_top_k=top_k)
|
| 58 |
nodes = retriever.retrieve(query)
|
|
|
|
| 21 |
|
| 22 |
DOCUMENTS_BASE_PATH = "./"
|
| 23 |
SOURCES = {
|
| 24 |
+
"oms": "oms/", # Esta será la carpeta base que contiene todos los subíndices
|
|
|
|
|
|
|
| 25 |
}
|
| 26 |
|
| 27 |
+
# Cargar índices recursivamente
|
| 28 |
indices: Dict[str, VectorStoreIndex] = {}
|
| 29 |
+
|
| 30 |
for source, rel_path in SOURCES.items():
|
| 31 |
full_path = os.path.join(DOCUMENTS_BASE_PATH, rel_path)
|
| 32 |
+
|
| 33 |
+
if not os.path.exists(full_path):
|
| 34 |
+
continue
|
| 35 |
+
|
| 36 |
+
# Buscar todas las subcarpetas que contengan índices
|
| 37 |
+
for root, dirs, files in os.walk(full_path):
|
| 38 |
+
if "storage_nodes" in dirs:
|
| 39 |
+
# Esta es una carpeta que contiene un índice
|
| 40 |
+
try:
|
| 41 |
+
storage_path = os.path.join(root, "storage_nodes")
|
| 42 |
+
storage_context = StorageContext.from_defaults(persist_dir=storage_path)
|
| 43 |
+
|
| 44 |
+
# Usamos el nombre de la carpeta padre como clave (ej: "vec_1")
|
| 45 |
+
index_name = os.path.basename(root)
|
| 46 |
+
full_index_name = f"{source}_{index_name}" # ej: "oms_vec_1"
|
| 47 |
+
|
| 48 |
+
index = load_index_from_storage(storage_context, index_id="vector_index")
|
| 49 |
+
indices[full_index_name] = index
|
| 50 |
+
except Exception as e:
|
| 51 |
+
print(f"Error cargando índice en {root}: {str(e)}")
|
| 52 |
+
continue
|
| 53 |
|
| 54 |
# Recurso dinámico para búsqueda
|
| 55 |
@mcp.resource(
|
| 56 |
uri="retriever://documentos/{fuente}",
|
| 57 |
name="DocumentRetriever",
|
| 58 |
+
description="Retrieve documents from different regulations using semantic search.",
|
| 59 |
mime_type="application/json",
|
| 60 |
tags={"llm", "retrieval"}
|
| 61 |
)
|
| 62 |
+
def retrieve_docs(fuente: str = 'oms', query: str, top_k: int = 3) -> dict:
|
| 63 |
"""
|
| 64 |
Parameters:
|
| 65 |
+
fuente: Source name (default: oms).
|
| 66 |
+
query: Search query (required).
|
| 67 |
+
top_k: Number of results to return (default: 3).
|
| 68 |
"""
|
| 69 |
if fuente not in indices:
|
| 70 |
+
available = [k for k in indices.keys() if k.startswith("oms_")] if fuente.startswith("oms") else list(indices.keys())
|
| 71 |
+
return {"error": f"Fuente '{fuente}' no disponible. Opciones: {available}"}
|
| 72 |
|
| 73 |
retriever = indices[fuente].as_retriever(similarity_top_k=top_k)
|
| 74 |
nodes = retriever.retrieve(query)
|