Spaces:
Sleeping
Sleeping
Update server.py
Browse files
server.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
from mcp.server.fastmcp import FastMCP
|
| 2 |
from datetime import datetime
|
|
|
|
|
|
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
import aiohttp # Necesario para las peticiones HTTP as铆ncronas
|
|
@@ -12,35 +14,57 @@ mcp = FastMCP("OnBase", port=port)
|
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
"""
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
Si no coincide, devuelve un mensaje de error.
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
clave_secreta: La clave secreta para comparar (default: "321")
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
"hora_actual": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
| 35 |
-
"codigo": "codigo 123",
|
| 36 |
-
"status": "success"
|
| 37 |
}
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
| 44 |
|
| 45 |
|
| 46 |
@mcp.tool()
|
|
|
|
| 1 |
from mcp.server.fastmcp import FastMCP
|
| 2 |
from datetime import datetime
|
| 3 |
+
from llama_index import VectorStoreIndex, SimpleDirectoryReader
|
| 4 |
+
from typing import Dict, Optional
|
| 5 |
import json
|
| 6 |
import os
|
| 7 |
import aiohttp # Necesario para las peticiones HTTP as铆ncronas
|
|
|
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
+
|
| 18 |
+
# Rutas a los documentos por fuente
|
| 19 |
+
DOCUMENTS_BASE_PATH = "./"
|
| 20 |
+
SOURCES = {
|
| 21 |
+
"oms": "oms/",
|
| 22 |
+
#"other": "other/",
|
| 23 |
+
#"other2": "other2/"
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
# Cargar 铆ndices (se ejecuta una vez al iniciar el servidor)
|
| 27 |
+
indices: Dict[str, VectorStoreIndex] = {}
|
| 28 |
+
for source, rel_path in SOURCES.items():
|
| 29 |
+
full_path = os.path.join(DOCUMENTS_BASE_PATH, rel_path)
|
| 30 |
+
if os.path.exists(full_path):
|
| 31 |
+
documents = SimpleDirectoryReader(full_path).load_data()
|
| 32 |
+
indices[source] = VectorStoreIndex.from_documents(documents)
|
| 33 |
+
|
| 34 |
+
# Recurso din谩mico para b煤squeda
|
| 35 |
+
@mcp.resource(
|
| 36 |
+
uri="retriever://documentos/{fuente}",
|
| 37 |
+
name="DocumentRetriever",
|
| 38 |
+
description="Retrieve documents from differente regulations using semantic search.",
|
| 39 |
+
mime_type="application/json",
|
| 40 |
+
tags={"llm", "retrieval"}
|
| 41 |
+
)
|
| 42 |
+
def retrieve_docs(fuente: str, query: str, top_k: int = 3) -> dict:
|
| 43 |
+
"""
|
| 44 |
+
Parameters:
|
| 45 |
+
fuente: Source name (for example: oms).
|
| 46 |
+
query: Search query.
|
| 47 |
+
top_k: Number of results to return.
|
| 48 |
"""
|
| 49 |
+
if fuente not in indices:
|
| 50 |
+
return {"error": f"Fuente '{fuente}' no disponible. Opciones: {list(indices.keys())}"}
|
|
|
|
| 51 |
|
| 52 |
+
retriever = indices[fuente].as_retriever(similarity_top_k=top_k)
|
| 53 |
+
nodes = retriever.retrieve(query)
|
|
|
|
| 54 |
|
| 55 |
+
results = [
|
| 56 |
+
{
|
| 57 |
+
"content": node.get_content(),
|
| 58 |
+
"metadata": node.metadata,
|
| 59 |
+
"score": node.score
|
|
|
|
|
|
|
|
|
|
| 60 |
}
|
| 61 |
+
for node in nodes
|
| 62 |
+
]
|
| 63 |
+
|
| 64 |
+
return {"results": results}
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
|
| 69 |
|
| 70 |
@mcp.tool()
|