Spaces:
Running
Running
File size: 18,589 Bytes
27ea68e ad40800 12c2f26 ad40800 f6ecd68 8439dff 1c586d4 ad40800 f6ecd68 ad40800 97520b6 ad40800 97520b6 ad40800 97520b6 ad40800 97520b6 ad40800 97520b6 ad40800 97520b6 ad40800 319eac2 fa9810e 319eac2 fa9810e ad40800 97520b6 ad40800 97520b6 ad40800 97520b6 ad40800 97520b6 ad40800 97520b6 ad40800 97520b6 ad40800 97520b6 ad40800 46a6817 97520b6 27ea68e 97520b6 46a6817 27ea68e 46a6817 97520b6 46a6817 97520b6 46a6817 27ea68e 46a6817 97520b6 46a6817 bc3a4c2 3082da0 97520b6 3082da0 97520b6 3082da0 97520b6 3082da0 97520b6 3082da0 97520b6 3082da0 97520b6 3082da0 97520b6 3082da0 97520b6 3082da0 97520b6 3082da0 97520b6 3082da0 97520b6 3082da0 97520b6 3082da0 97520b6 3082da0 97520b6 3082da0 bc3a4c2 97520b6 bc3a4c2 982f629 bc3a4c2 8df00df 97520b6 8df00df 97520b6 8df00df 97520b6 3082da0 8df00df 97520b6 8df00df 97520b6 3082da0 8df00df 97520b6 8df00df 97520b6 8df00df 97520b6 3082da0 97520b6 3082da0 97520b6 3082da0 97520b6 3082da0 97520b6 3082da0 97520b6 3082da0 8df00df 982f629 97520b6 e313929 982f629 17027bd 27ea68e 39cb644 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 | import gradio as gr
from llama_index.core import VectorStoreIndex
from llama_index.core import (
StorageContext,
load_index_from_storage,
)
from llama_index.tools.arxiv import ArxivToolSpec
from llama_index.core import Settings
from llama_index.llms.azure_openai import AzureOpenAI
from llama_index.embeddings.azure_openai import AzureOpenAIEmbedding
from llama_index.llms.openai import OpenAI
from llama_index.embeddings.openai import OpenAIEmbedding
from typing import Optional, List, Dict, Any
from pathlib import Path
import aiohttp
import json
import os
import asyncio
from gradio_client import Client, handle_file
HF_TOKEN = os.environ.get('HF_TOKEN')
##### LLM #####
openai_api_key = os.environ.get('OPENAI_API_KEY')
llm = OpenAI(
model="gpt-4.1",
api_key=openai_api_key,
)
embed_model = OpenAIEmbedding(
model="text-embedding-ada-002",
api_key=openai_api_key,
)
Settings.llm = llm
Settings.embed_model = embed_model
##### END LLM #####
##### LOAD RETRIEVERS #####
DOCUMENTS_BASE_PATH = "./"
RETRIEVERS_JSON_PATH = Path("./retrievers.json")
# Load metadata
def load_retrievers_metadata():
try:
with open(RETRIEVERS_JSON_PATH, 'r', encoding='utf-8') as f:
return json.load(f)
except Exception as e:
print(f"Error loading retrievers.json: {str(e)}")
print(f"Error details: {traceback.format_exc()}") # You would need to import traceback
return {}
retrievers_metadata = load_retrievers_metadata()
SOURCES = {source: f"{source.lower()}/" for source in retrievers_metadata.keys()}
# Load indexes
indices: Dict[str, VectorStoreIndex] = {}
for source, rel_path in SOURCES.items():
full_path = os.path.join(DOCUMENTS_BASE_PATH, rel_path)
if not os.path.exists(full_path):
print(f"Warning: Path not found for {source}")
continue
for root, dirs, files in os.walk(full_path):
if "storage_nodes" in dirs:
try:
storage_path = os.path.join(root, "storage_nodes")
storage_context = StorageContext.from_defaults(persist_dir=storage_path)
index_name = os.path.basename(root)
indices[index_name] = load_index_from_storage(storage_context) #, index_id="vector_index"
print(f"Index loaded successfully: {index_name}")
except Exception as e:
print(f"Error loading index {index_name}: {str(e)}")
print(f"Error details: {traceback.format_exc()}")
##### ARXIV INSTANCE #####
arxiv_tool = ArxivToolSpec(max_results=5).to_tool_list()[0]
arxiv_tool.return_direct = True
##### MCP TOOLS #####
async def search_arxiv(
query: str,
max_results: int = 5
) -> Dict[str, Any]:
"""
Searches for academic papers on ArXiv.
Args:
query: Search terms (e.g. "deep learning")
max_results: Maximum number of results (1-10, default 5)
Returns:
Dict: Search results with paper metadata
"""
try:
# Configure maximum results
max_results = min(max(1, max_results), 10)
arxiv_tool.metadata.max_results = max_results
# Execute search and get results
tool_output = arxiv_tool(query=query)
# Process documents
papers = []
for doc in tool_output.raw_output: # Correctly access documents
content = doc.text_resource.text.split('\n')
papers.append({
'title': content[0].split(': ')[1] if ': ' in content[0] else content[0],
'abstract': '\n'.join(content[1:]).strip(),
'pdf_url': content[0].split(': ')[0].replace('http://', 'https://'),
'arxiv_id': content[0].split(': ')[0].split('/')[-1].replace('v1', '')
})
return {
'papers': papers,
'count': len(papers),
'query': query,
'status': 'success'
}
except Exception as e:
return {
'papers': [],
'count': 0,
'query': query,
'status': 'error',
'error': str(e)
}
async def list_retrievers(source: str = None) -> dict:
"""
Returns the list of available retrievers.
If a source is specified and exists, filters by it; if it doesn't exist, returns all.
Args:
source (str, optional): Source to filter by. If it doesn't exist, it will be ignored. Defaults to None.
Returns:
dict: {
"retrievers": List of retrievers (filtered or complete),
"count": Total count,
"status": "success"|"error",
"source_requested": source, # Shows what was requested
"source_used": "all"|source # Shows what was actually used
}
"""
try:
available = []
source_exists = source in retrievers_metadata if source else False
for current_source, indexes in retrievers_metadata.items():
# Only filter if source exists, otherwise show all
if source_exists and current_source != source:
continue
for index_name, metadata in indexes.items():
available.append({
"name": index_name,
"source": current_source,
"title": metadata.get("title", ""),
"description": metadata.get("description", "")
})
return {
"retrievers": available,
"count": len(available),
"status": "success",
"source_requested": source,
"source_used": source if source_exists else "all"
}
except Exception as e:
return {
"retrievers": [],
"count": 0,
"status": "error",
"error": str(e),
"source_requested": source,
"source_used": "none"
}
def retrieve_docs(
query: str,
retrievers: List[str],
top_k: int = 3
) -> dict:
"""
Performs semantic search on indexed documents.
Parameters:
query (str): Search text (required)
retrievers (List[str]): Names of retrievers to query (required)
top_k (int): Number of results per retriever (optional, default=3)
"""
print(f"Starting search for query: '{query}'")
print(f"Parameters - retrievers: {retrievers}, top_k: {top_k}")
results = {}
invalid = []
for name in retrievers:
if name not in indices:
print(f"Retriever not found: {name}")
invalid.append(name)
continue
try:
print(f"Processing retriever: {name}")
retriever = indices[name].as_retriever(similarity_top_k=top_k)
nodes = retriever.retrieve(query)
print(f"Retrieved {len(nodes)} documents from {name}")
# 2. Search for COMPLETE metadata
metadata = {}
source = "unknown"
for src, indexes in retrievers_metadata.items():
if name in indexes:
metadata = indexes[name]
source = src
break
print(f"Metadata found for {name}: {metadata.keys()}")
# 3. Build response
results[name] = {
"title": metadata.get("title", name),
"documents": [
{
"content": node.get_content(),
"metadata": node.metadata,
"score": node.score
}
for node in nodes
],
"description": metadata.get("description", ""),
"source": source,
"last_updated": metadata.get("last_updated", "")
}
print(f"Retriever {name} processed successfully")
except Exception as e:
print(f"Error processing retriever {name}: {str(e)}", exc_info=True)
results[name] = {
"error": str(e),
"retriever": name
}
# Build final response
response = {
"query": query,
"results": results,
"top_k": top_k,
}
if invalid:
print(f"Invalid retrievers: {invalid}. Valid options: {list(indices.keys())}")
response["warnings"] = {
"invalid_retrievers": invalid,
"valid_options": list(indices.keys())
}
print(f"Search completed. Total results: {len(results)}")
return response
async def search_tavily(
query: str,
days: int = 7,
max_results: int = 1,
include_answer: bool = False
) -> dict:
"""Perform a web search using the Tavily API.
Args:
query: Search query string (required)
days: Restrict search to last N days (default: 7)
max_results: Maximum results to return (default: 1)
include_answer: Include a direct answer only when requested by the user (default: False)
Returns:
dict: Search results from Tavily
"""
# Get API key from environment variables
tavily_api_key = os.environ.get('TAVILY_API_KEY')
if not tavily_api_key:
raise ValueError("TAVILY_API_KEY environment variable not set")
headers = {
"Authorization": f"Bearer {tavily_api_key}",
"Content-Type": "application/json"
}
payload = {
"query": query,
"search_depth": "basic",
"max_results": max_results,
"days": days if days else None,
"include_answer": include_answer
}
try:
async with aiohttp.ClientSession() as session:
async with session.post(
"https://api.tavily.com/search",
headers=headers,
json=payload
) as response:
response.raise_for_status()
result = await response.json()
return result
except Exception as e:
return {
"error": str(e),
"status": "failed",
"query": query
}
##### EVALS #####
async def evaluate_answer_relevancy(
query: str,
response: str,
) -> float:
"""Evaluate how relevant the answer is to the query using AnswerRelevancyEvaluator.
Args:
query: Original user query (required)
response: Generated response to evaluate (required)
Returns:
float: Relevancy score between 0 and 1 (higher is better)
"""
try:
from llama_index.core.evaluation import AnswerRelevancyEvaluator
# Initialize the evaluator
evaluator = AnswerRelevancyEvaluator(llm=llm)
# Perform the evaluation
eval_result = evaluator.evaluate(query=query, response=response)
# Return the score as a float
return float(eval_result.score)
except Exception as e:
# In case of error, return 0.0 (minimum score) and log the error
print(f"Error in relevancy evaluation: {str(e)}")
return 0.0
async def evaluate_context_relevancy(
context: str,
query: str,
response: str
) -> float:
"""Evaluates the relevance of the response considering both the query and the context.
Args:
context: Contextual information / knowledge base (required)
query: Original user query (required)
response: Generated response to evaluate (required)
Returns:
float: Relevance score between 0 and 1 (higher is better)
"""
try:
from llama_index.core.evaluation import ContextRelevancyEvaluator
# Initialize the relevancy evaluator with context
evaluator = ContextRelevancyEvaluator(llm=llm)
# Perform the evaluation (adapted to handle context)
eval_result = evaluator.evaluate(
query=query,
response=response,
contexts=[context]
)
return float(eval_result.score)
except Exception as e:
print(f"Error during context relevancy evaluation: {str(e)}")
return 0.0
async def evaluate_faithfulness(
query: str,
response: str,
context: str
) -> float:
"""Evaluate how faithful (factually consistent) the response is to the provided context.
Args:
query: Original user query (required)
response: Generated response to evaluate (required)
context: Source context/knowledge base used for the response (required)
Returns:
float: Faithfulness score between 0 and 1 (higher is better)
"""
try:
from llama_index.core.evaluation import FaithfulnessEvaluator
# Initialize evaluator
evaluator = FaithfulnessEvaluator(llm=llm)
# Perform evaluation
eval_result = evaluator.evaluate(
query=query,
response=response,
contexts=[context]
)
# Return score as float
return float(eval_result.score)
except Exception as e:
# On error, return 0.0 (minimum score) and log the error
print(f"Error in faithfulness evaluation: {str(e)}")
return 0.0
# Gradio interface
with gr.Blocks(title="MCP Tools", theme=gr.themes.Base()) as arxiv_tab:
arxiv_interface = gr.Interface(
fn=search_arxiv,
inputs=[
gr.Textbox(label="Search terms", placeholder="E.g.: deep learning"),
gr.Slider(1, 10, value=5, step=1, label="Maximum number of results")
],
outputs=gr.JSON(label="Search results"),
title="ArXiv Search",
description="Search for academic papers on ArXiv using keywords.",
api_name="_search_arxiv"
)
with gr.Blocks(title="MCP Tools", theme=gr.themes.Base()) as list_retrievers_tab:
retrievers_interface = gr.Interface(
fn=list_retrievers,
inputs=gr.Textbox(label="Source (optional)", placeholder="Leave empty to list all"),
outputs=gr.JSON(label="List of retrievers"),
title="List of Retrievers",
description="Shows available retrievers, optionally filtered by source.",
api_name="_list_retrievers"
)
with gr.Blocks(title="MCP Tools", theme=gr.themes.Base()) as tavily_tab:
tavily_interface = gr.Interface(
fn=search_tavily,
inputs=[
gr.Textbox(label="Search query", placeholder="E.g.: latest news about AI"),
gr.Slider(1, 30, value=7, step=1, label="Last N days (0 for no limit)"),
gr.Slider(1, 10, value=1, step=1, label="Maximum results"),
gr.Checkbox(label="Include direct answer", value=False)
],
outputs=gr.JSON(label="Tavily results"),
title="Web Search (Tavily)",
description="Perform web searches using the Tavily API.",
api_name="_search_tavily"
)
with gr.Blocks(title="MCP Tools", theme=gr.themes.Base()) as retrieve_tab:
# Interface for retrieve_docs
retrieve_interface = gr.Interface(
fn=retrieve_docs,
inputs=[
gr.Textbox(label="Query", placeholder="Enter your question or search terms..."),
gr.Dropdown(
choices=list(indices.keys()),
label="Retrievers",
multiselect=True,
info="Select one or more retrievers"
),
gr.Slider(1, 10, value=3, step=1, label="Number of results per retriever (top_k)")
],
outputs=gr.JSON(label="Semantic search results"),
title="Semantic Document Search",
description="""Perform semantic search on indexed documents using retrievers.
Select available retrievers and adjust the number of results.""",
api_name="_retrieve"
)
with gr.Blocks(title="MCP Tools", theme=gr.themes.Base()) as asw_relevance_tab:
relevancy_interface = gr.Interface(
fn=evaluate_answer_relevancy,
inputs=[
gr.Textbox(label="Original Query", placeholder="E.g.: How does photosynthesis work?"),
gr.Textbox(label="Answer to Evaluate", placeholder="Paste the generated answer here", lines=5),
],
outputs=gr.Number(label="Relevancy Score (0-1)", precision=3),
title="Relevancy Evaluator (Query-Answer)",
description="Evaluates how relevant an answer is to the original query (1 = perfectly relevant).",
api_name="_evaluate_relevancy"
)
with gr.Blocks(title="MCP Tools", theme=gr.themes.Base()) as ctx_relevance_tab:
context_relevancy_interface = gr.Interface(
fn=evaluate_context_relevancy,
inputs=[
gr.Textbox(label="Context", placeholder="Relevant text / knowledge base", lines=3),
gr.Textbox(label="Original Query", placeholder="What question is being answered?"),
gr.Textbox(label="Generated Answer", placeholder="The answer to evaluate", lines=5),
],
outputs=gr.Number(label="Relevancy Score (0-1)", precision=3),
title="Relevancy Evaluator (Context-Query-Answer)",
description="Evaluates how relevant the answer is considering both the query and the reference context.",
api_name="_evaluate_context_relevancy"
)
with gr.Blocks(title="MCP Tools", theme=gr.themes.Base()) as faithfulness_tab:
faithfulness_interface = gr.Interface(
fn=evaluate_faithfulness,
inputs=[
gr.Textbox(label="Original Query", placeholder="E.g.: What are the causes of climate change?"),
gr.Textbox(label="Answer to Evaluate", placeholder="Paste the generated answer here", lines=5),
gr.Textbox(label="Context", placeholder="Reference text / knowledge base", lines=3),
],
outputs=gr.Number(label="Faithfulness Score (0-1)", precision=3),
title="Faithfulness Evaluator",
description="Evaluates how faithful/factually consistent the answer is with respect to the provided context (1 = perfectly faithful).",
api_name="_evaluate_faithfulness"
)
# Create the interface with separate tabs
demo = gr.TabbedInterface(
[arxiv_tab, tavily_tab, list_retrievers_tab, retrieve_tab, asw_relevance_tab, ctx_relevance_tab, faithfulness_tab],
["ArXiv", "Tavily", "List Retrievers", "Retrieve", "Answer Relevance", "Context Relevance", "Faithfulness"],
theme=gr.themes.Base(),
)
demo.launch(mcp_server=True) |