Spaces:
Sleeping
Sleeping
Update server.py
Browse files
server.py
CHANGED
|
@@ -8,7 +8,7 @@ from llama_index.core import (
|
|
| 8 |
from llama_index.core import Settings
|
| 9 |
from llama_index.llms.azure_openai import AzureOpenAI
|
| 10 |
from llama_index.embeddings.azure_openai import AzureOpenAIEmbedding
|
| 11 |
-
from typing import Dict, Optional
|
| 12 |
import json
|
| 13 |
import os
|
| 14 |
import aiohttp # Necesario para las peticiones HTTP asíncronas
|
|
@@ -86,36 +86,121 @@ mcp = FastMCP("OnBase", port=port)
|
|
| 86 |
|
| 87 |
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
@mcp.tool()
|
| 90 |
-
def retrieve_docs(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
"""
|
| 92 |
Retrieve documents from different regulations using semantic search.
|
| 93 |
|
| 94 |
Parameters:
|
| 95 |
query: Search query (required).
|
| 96 |
-
|
| 97 |
-
top_k: Number of results to return (default: 3).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
"""
|
| 99 |
if not query:
|
| 100 |
return {"error": "Query parameter is required"}
|
| 101 |
|
| 102 |
-
if
|
| 103 |
-
|
| 104 |
-
return {"error": f"Fuente '{fuente}' no disponible. Opciones: {available}"}
|
| 105 |
-
|
| 106 |
-
retriever = indices[fuente].as_retriever(similarity_top_k=top_k)
|
| 107 |
-
nodes = retriever.retrieve(query)
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
"
|
|
|
|
| 114 |
}
|
| 115 |
-
for node in nodes
|
| 116 |
-
]
|
| 117 |
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
|
| 121 |
|
|
|
|
| 8 |
from llama_index.core import Settings
|
| 9 |
from llama_index.llms.azure_openai import AzureOpenAI
|
| 10 |
from llama_index.embeddings.azure_openai import AzureOpenAIEmbedding
|
| 11 |
+
from typing import Dict, Optional, List
|
| 12 |
import json
|
| 13 |
import os
|
| 14 |
import aiohttp # Necesario para las peticiones HTTP asíncronas
|
|
|
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
+
|
| 90 |
+
@mcp.resource(
|
| 91 |
+
uri="info://available_retrievers",
|
| 92 |
+
name="AvailableRetrievers",
|
| 93 |
+
description="Provides information about available document retrievers including their names and descriptions.",
|
| 94 |
+
mime_type="application/json"
|
| 95 |
+
)
|
| 96 |
+
def get_available_retrievers() -> dict:
|
| 97 |
+
"""
|
| 98 |
+
Returns a mapping of available retrievers with their metadata.
|
| 99 |
+
|
| 100 |
+
The structure includes:
|
| 101 |
+
- retriever_name: The full name used to reference the retriever
|
| 102 |
+
- source: The source system (e.g., 'oms')
|
| 103 |
+
- index_name: The specific index name
|
| 104 |
+
- description: Human-readable description
|
| 105 |
+
"""
|
| 106 |
+
available_retrievers = []
|
| 107 |
+
|
| 108 |
+
for full_index_name in indices.keys():
|
| 109 |
+
# Parse the full index name (e.g., "oms_vec_1")
|
| 110 |
+
parts = full_index_name.split('_')
|
| 111 |
+
source = parts[0]
|
| 112 |
+
index_name = '_'.join(parts[1:]) if len(parts) > 1 else "default"
|
| 113 |
+
|
| 114 |
+
# Create a description based on the index name
|
| 115 |
+
description = f"Documentos de {source.upper()}"
|
| 116 |
+
if index_name.startswith("vec"):
|
| 117 |
+
description += f" - Índice vectorial {index_name.split('_')[-1]}"
|
| 118 |
+
elif index_name.startswith("tree"):
|
| 119 |
+
description += f" - Índice jerárquico {index_name.split('_')[-1]}"
|
| 120 |
+
else:
|
| 121 |
+
description += f" - Índice {index_name}"
|
| 122 |
+
|
| 123 |
+
available_retrievers.append({
|
| 124 |
+
"retriever_name": full_index_name,
|
| 125 |
+
"source": source,
|
| 126 |
+
"index_name": index_name,
|
| 127 |
+
"description": description
|
| 128 |
+
})
|
| 129 |
+
|
| 130 |
+
return {
|
| 131 |
+
"retrievers": available_retrievers,
|
| 132 |
+
"count": len(available_retrievers),
|
| 133 |
+
"default_source": "oms",
|
| 134 |
+
"default_top_k": 3
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
|
| 141 |
@mcp.tool()
|
| 142 |
+
def retrieve_docs(
|
| 143 |
+
query: str,
|
| 144 |
+
retrievers: List[str],
|
| 145 |
+
top_k: int = 3
|
| 146 |
+
) -> dict:
|
| 147 |
"""
|
| 148 |
Retrieve documents from different regulations using semantic search.
|
| 149 |
|
| 150 |
Parameters:
|
| 151 |
query: Search query (required).
|
| 152 |
+
retrievers: List of specific retriever names to use (required).
|
| 153 |
+
top_k: Number of results to return per retriever (default: 3).
|
| 154 |
+
|
| 155 |
+
Example:
|
| 156 |
+
retrieve_docs(
|
| 157 |
+
query="salud pública",
|
| 158 |
+
retrievers=["oms_vec_1", "oms_tree_2"],
|
| 159 |
+
top_k=2
|
| 160 |
+
)
|
| 161 |
"""
|
| 162 |
if not query:
|
| 163 |
return {"error": "Query parameter is required"}
|
| 164 |
|
| 165 |
+
if not retrievers:
|
| 166 |
+
return {"error": "At least one retriever must be specified", "available_retrievers": list(indices.keys())}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
+
# Verificar que todos los retrievers solicitados existan
|
| 169 |
+
invalid_retrievers = [r for r in retrievers if r not in indices]
|
| 170 |
+
if invalid_retrievers:
|
| 171 |
+
return {
|
| 172 |
+
"error": f"Invalid retrievers specified: {invalid_retrievers}",
|
| 173 |
+
"available_retrievers": list(indices.keys())
|
| 174 |
}
|
|
|
|
|
|
|
| 175 |
|
| 176 |
+
results = {}
|
| 177 |
+
|
| 178 |
+
for retriever_name in retrievers:
|
| 179 |
+
try:
|
| 180 |
+
retriever = indices[retriever_name].as_retriever(similarity_top_k=top_k)
|
| 181 |
+
nodes = retriever.retrieve(query)
|
| 182 |
+
|
| 183 |
+
results[retriever_name] = [
|
| 184 |
+
{
|
| 185 |
+
"content": node.get_content(),
|
| 186 |
+
"metadata": node.metadata,
|
| 187 |
+
"score": node.score
|
| 188 |
+
}
|
| 189 |
+
for node in nodes
|
| 190 |
+
]
|
| 191 |
+
except Exception as e:
|
| 192 |
+
results[retriever_name] = {
|
| 193 |
+
"error": f"Error retrieving documents: {str(e)}"
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
return {
|
| 197 |
+
"results": results,
|
| 198 |
+
"query": query,
|
| 199 |
+
"retrievers_used": retrievers,
|
| 200 |
+
"top_k": top_k,
|
| 201 |
+
"successful_retrievers": [r for r in retrievers if isinstance(results[r], list)],
|
| 202 |
+
"failed_retrievers": [r for r in retrievers if not isinstance(results[r], list)]
|
| 203 |
+
}
|
| 204 |
|
| 205 |
|
| 206 |
|