Spaces:
Sleeping
Sleeping
Update server.py
Browse files
server.py
CHANGED
|
@@ -5,6 +5,7 @@ from llama_index.core import (
|
|
| 5 |
StorageContext,
|
| 6 |
load_index_from_storage,
|
| 7 |
)
|
|
|
|
| 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
|
|
@@ -94,6 +95,75 @@ port = int(os.getenv("PORT", 7860))
|
|
| 94 |
mcp = FastMCP("OnBase", port=port)
|
| 95 |
|
| 96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
@mcp.tool()
|
| 99 |
async def list_retrievers(source: str = None) -> dict:
|
|
|
|
| 5 |
StorageContext,
|
| 6 |
load_index_from_storage,
|
| 7 |
)
|
| 8 |
+
from llama_index.tools.arxiv import ArxivToolSpec
|
| 9 |
from llama_index.core import Settings
|
| 10 |
from llama_index.llms.azure_openai import AzureOpenAI
|
| 11 |
from llama_index.embeddings.azure_openai import AzureOpenAIEmbedding
|
|
|
|
| 95 |
mcp = FastMCP("OnBase", port=port)
|
| 96 |
|
| 97 |
|
| 98 |
+
# Instancia global de ArXiv
|
| 99 |
+
paper_tool = ArxivToolSpec()
|
| 100 |
+
|
| 101 |
+
@mcp.tool()
|
| 102 |
+
async def search_arxiv_papers(
|
| 103 |
+
ctx: Context,
|
| 104 |
+
query: str,
|
| 105 |
+
max_results: Optional[int] = 5,
|
| 106 |
+
sort_by: Optional[str] = "relevance"
|
| 107 |
+
) -> dict:
|
| 108 |
+
"""
|
| 109 |
+
Search for academic papers on ArXiv using natural language queries.
|
| 110 |
+
|
| 111 |
+
Args:
|
| 112 |
+
query: Natural language search query (e.g. "machine learning in healthcare")
|
| 113 |
+
max_results: Maximum number of results to return (default 5, max 10)
|
| 114 |
+
sort_by: Sorting method ("relevance" or "last_updated_date")
|
| 115 |
+
|
| 116 |
+
Returns:
|
| 117 |
+
dict: {
|
| 118 |
+
"papers": List of paper summaries,
|
| 119 |
+
"count": Number of results,
|
| 120 |
+
"query": Original query,
|
| 121 |
+
"status": "success" or "error"
|
| 122 |
+
}
|
| 123 |
+
"""
|
| 124 |
+
try:
|
| 125 |
+
# Validar parámetros
|
| 126 |
+
max_results = min(max(1, max_results), 10) # Limitar entre 1 y 10
|
| 127 |
+
if sort_by not in ["relevance", "last_updated_date"]:
|
| 128 |
+
sort_by = "relevance"
|
| 129 |
+
|
| 130 |
+
# Usar el tool de ArXiv
|
| 131 |
+
results = paper_tool.arxiv_search(
|
| 132 |
+
query=query,
|
| 133 |
+
max_results=max_results,
|
| 134 |
+
sort_by=sort_by
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
# Procesar resultados
|
| 138 |
+
papers = []
|
| 139 |
+
for paper in results:
|
| 140 |
+
papers.append({
|
| 141 |
+
"title": paper.metadata.get("Title", ""),
|
| 142 |
+
"authors": paper.metadata.get("Authors", ""),
|
| 143 |
+
"abstract": paper.metadata.get("Summary", ""),
|
| 144 |
+
"published": paper.metadata.get("Published", ""),
|
| 145 |
+
"pdf_url": paper.metadata.get("PDF url", ""),
|
| 146 |
+
"arxiv_id": paper.metadata.get("Entry ID", "").split('/')[-1]
|
| 147 |
+
})
|
| 148 |
+
|
| 149 |
+
return {
|
| 150 |
+
"papers": papers,
|
| 151 |
+
"count": len(papers),
|
| 152 |
+
"query": query,
|
| 153 |
+
"status": "success"
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
except Exception as e:
|
| 157 |
+
await ctx.error(f"Error in ArXiv search: {str(e)}")
|
| 158 |
+
return {
|
| 159 |
+
"papers": [],
|
| 160 |
+
"count": 0,
|
| 161 |
+
"query": query,
|
| 162 |
+
"status": "error",
|
| 163 |
+
"error": str(e)
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
|
| 167 |
|
| 168 |
@mcp.tool()
|
| 169 |
async def list_retrievers(source: str = None) -> dict:
|