|
""" |
|
This file is the main file for the hackathon. |
|
It contains the Gradio interface for the hackathon as a MCP server. |
|
It exposes the following tools: |
|
- search_knowledge_base_for_context |
|
- research_write_review_topic |
|
|
|
""" |
|
from dotenv import load_dotenv |
|
load_dotenv() |
|
|
|
import logging |
|
|
|
|
|
|
|
logging.basicConfig( |
|
filename='hackathon-mcp.log', |
|
level=logging.INFO, |
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' |
|
) |
|
logger = logging.getLogger(__name__) |
|
|
|
import gradio as gr |
|
from tools.rag_tools import search_groundx_for_rag_context |
|
from tools.multi_agent_workflow_for_research import run_research_workflow |
|
|
|
def search_knowledge_base_for_context(query: str) -> str: |
|
""" |
|
Searches and retrieves relevant context from a knowledge base (GroundX), |
|
based on the user's query. |
|
|
|
Example queries: |
|
- "What are the main features of fuel system of SU-35." |
|
- "What are the combat potential of SU-35." |
|
|
|
Args: |
|
query: The search query supplied by the user. |
|
|
|
Returns: |
|
str: Relevant text content that can be used by the LLM to answer the query. |
|
""" |
|
logger.info(f"Searching document for RAG context {query}") |
|
response = search_groundx_for_rag_context(query) |
|
logger.info(f"RAG Response: {response}") |
|
return response |
|
|
|
def research_write_review_topic(query: str) -> str: |
|
""" |
|
Helps with writing a report with research, writing, and review on any topic. |
|
Returns a reviewed topic. |
|
|
|
The query is a string that contains the topic to be researched and reviewed. |
|
|
|
Example queries: |
|
- "Write me a report on the history of the internet." |
|
- "Write me a report on origin of the universe." |
|
- "Write me a report on the impact of climate change on polar bears." |
|
- "Write me a report on the benefits of meditation." |
|
- "Write me a report on the future of artificial intelligence." |
|
- "Write me a report on the effects of social media on mental health." |
|
|
|
Args: |
|
query (str): The query to research, write and review . |
|
|
|
Returns: |
|
str: A nicely formatted string. |
|
""" |
|
try: |
|
logger.info(f"Researching the topic: {query}") |
|
result = run_research_workflow(query) |
|
return result or "Research completed, but no content was generated." |
|
except Exception as e: |
|
return f"Error: {e}" |
|
|
|
with gr.Blocks() as server_info: |
|
gr.Markdown(""" |
|
# MCP powered RAG and Research |
|
|
|
I present to you a MCP powered RAG and Research. |
|
|
|
RAG Tool uses GroundX service to fetch the knowledge base. The knowledge base is a document that contains information about the SU-35 aircraft, including its features, capabilities, and specifications. |
|
Please check [this PDF](https://airgroup2000.com/gallery/albums/userpics/32438/SU-35_TM_eng.pdf) to formulate queries on Sukhoi. |
|
|
|
The Research Tool is implemented using multi-agent workflow using LlamaIndex (ResearchAgent, WriteAgent, and ReviewAgent). |
|
|
|
## Available Tools |
|
|
|
### search_knowledge_base_for_context |
|
- **Description**: Searches and retrieves relevant context from a knowledge base based on the user's query. |
|
- **Example Queries**: |
|
- "What are the main features of fuel system of SU-35." |
|
- "What are the combat potential of SU-35." |
|
|
|
### research_write_review_topic |
|
- **Description**: Helps with writing a report with research, writing, and review on any topic. |
|
- **Example Queries**: |
|
- "Write me a report on the history of the internet." |
|
- "Write me a report on origin of the universe." |
|
- "Write me a report on the impact of climate change on polar bears." |
|
|
|
## How to Use |
|
- Use the MCP RAG Tool tab above to query the knowledge base. |
|
- Use the Research Tool tab above to write report on any topic. |
|
|
|
## Demo Link |
|
[Link to Demo on Youtube](https://www.youtube.com/mcp-rag-research) |
|
""") |
|
|
|
mcp_rag_tool = gr.Interface( |
|
fn=search_knowledge_base_for_context, |
|
inputs=["text"], |
|
outputs=[gr.Textbox(label="Knowledge Base", max_lines=10)], |
|
title="MCP RAG Tool", |
|
description="Searches and retrieves relevant context from a knowledge base" |
|
) |
|
|
|
research_tool = gr.Interface( |
|
fn=research_write_review_topic, |
|
inputs=["text"], |
|
outputs=[gr.Textbox(label="Reviewed Topic", max_lines=10)], |
|
title="Research Tool", |
|
description="Helps with report writing with research, writing, and review agents on any topic. ", |
|
concurrency_limit=10 |
|
) |
|
|
|
named_interfaces = { |
|
"Project Information": server_info, |
|
"RAG - Tool": mcp_rag_tool, |
|
"Research a Topic - Tool": research_tool |
|
} |
|
|
|
|
|
tab_names = list(named_interfaces.keys()) |
|
interface_list = list(named_interfaces.values()) |
|
|
|
mcp_server = gr.TabbedInterface( |
|
interface_list, |
|
tab_names=tab_names, |
|
title="π MCP powered RAG and Research π" |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
mcp_server.queue(default_concurrency_limit=10) |
|
mcp_server.launch( |
|
server_name="0.0.0.0", |
|
server_port=7860, |
|
share=False, |
|
debug=False, |
|
mcp_server=True |
|
) |