restricted the client AI agent to only use the OpenF1 to avoid confusion and overlapping functionality.
Browse files- mcp_client.py +11 -4
- openf1_tools.py +5 -0
mcp_client.py
CHANGED
|
@@ -1,10 +1,11 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
-
|
| 4 |
from smolagents import InferenceClientModel, LiteLLMModel, ToolCallingAgent, MCPClient
|
| 5 |
-
import datetime
|
| 6 |
|
| 7 |
-
time
|
|
|
|
| 8 |
|
| 9 |
SYSTEM_PROMPT = """You are a helpful Formula 1 assistant and strategist. You have access to various F1 data and tools to help answer questions about races, drivers, teams, and more.
|
| 10 |
Be concise and accurate in your responses. If you don't know something, use the available tools to find the information.
|
|
@@ -22,8 +23,9 @@ def agent_chat(message: str, history: list):
|
|
| 22 |
|
| 23 |
if __name__ == "__main__":
|
| 24 |
|
| 25 |
-
list_tools =
|
| 26 |
local_model = True # If you have Ollama installed, set this to True
|
|
|
|
| 27 |
|
| 28 |
try:
|
| 29 |
|
|
@@ -31,6 +33,11 @@ if __name__ == "__main__":
|
|
| 31 |
{"url": "https://agents-mcp-hackathon-f1-mcp-server.hf.space/gradio_api/mcp/sse", "transport": "sse"})
|
| 32 |
tools = mcp_client.get_tools()
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
if list_tools:
|
| 35 |
print("### MCP tools ### ")
|
| 36 |
print("\n".join(f"Tool {1+i}: {t.name}: {t.description}" for i,t in enumerate(tools)))
|
|
|
|
| 1 |
import os
|
| 2 |
+
import datetime
|
| 3 |
import gradio as gr
|
| 4 |
+
import openf1_tools
|
| 5 |
from smolagents import InferenceClientModel, LiteLLMModel, ToolCallingAgent, MCPClient
|
|
|
|
| 6 |
|
| 7 |
+
# Can manully set this to a specific time to make the agent think it is in the past
|
| 8 |
+
time = datetime.datetime.now().astimezone().isoformat()
|
| 9 |
|
| 10 |
SYSTEM_PROMPT = """You are a helpful Formula 1 assistant and strategist. You have access to various F1 data and tools to help answer questions about races, drivers, teams, and more.
|
| 11 |
Be concise and accurate in your responses. If you don't know something, use the available tools to find the information.
|
|
|
|
| 23 |
|
| 24 |
if __name__ == "__main__":
|
| 25 |
|
| 26 |
+
list_tools = True # Set to True to only list tools (used for debugging)
|
| 27 |
local_model = True # If you have Ollama installed, set this to True
|
| 28 |
+
openf1_tool_only = True
|
| 29 |
|
| 30 |
try:
|
| 31 |
|
|
|
|
| 33 |
{"url": "https://agents-mcp-hackathon-f1-mcp-server.hf.space/gradio_api/mcp/sse", "transport": "sse"})
|
| 34 |
tools = mcp_client.get_tools()
|
| 35 |
|
| 36 |
+
if openf1_tool_only:
|
| 37 |
+
openf1_fn_names = [f"f1_mcp_server_{fn}" for fn in dir(openf1_tools) if callable(getattr(openf1_tools, fn))]
|
| 38 |
+
openf1_fn_names.remove("f1_mcp_server_urlopen")
|
| 39 |
+
tools = [t for t in tools if (t.name in openf1_fn_names)]
|
| 40 |
+
|
| 41 |
if list_tools:
|
| 42 |
print("### MCP tools ### ")
|
| 43 |
print("\n".join(f"Tool {1+i}: {t.name}: {t.description}" for i,t in enumerate(tools)))
|
openf1_tools.py
CHANGED
|
@@ -31,6 +31,7 @@ def get_api_endpoint(endpoint: str) -> dict:
|
|
| 31 |
"filter_metadata": dict()
|
| 32 |
}
|
| 33 |
|
|
|
|
| 34 |
def get_filter_string(filter_name: str, filter_value: str, operator: str = "=") -> str:
|
| 35 |
"""
|
| 36 |
Create a filter string for OpenF1 API requests.
|
|
@@ -45,6 +46,7 @@ def get_filter_string(filter_name: str, filter_value: str, operator: str = "=")
|
|
| 45 |
"""
|
| 46 |
return f"{filter_name}{operator}{filter_value}&"
|
| 47 |
|
|
|
|
| 48 |
def apply_filters(api_string: str, *filters: str) -> str:
|
| 49 |
"""
|
| 50 |
Apply one or more filter strings to an API endpoint URL.
|
|
@@ -61,6 +63,7 @@ def apply_filters(api_string: str, *filters: str) -> str:
|
|
| 61 |
api_string += filter
|
| 62 |
return api_string.rstrip("&") # Remove trailing & for last filter
|
| 63 |
|
|
|
|
| 64 |
def send_request(api_string: str) -> dict:
|
| 65 |
"""
|
| 66 |
Send an HTTP GET request to the specified API endpoint and return the JSON response.
|
|
@@ -96,6 +99,7 @@ def get_api_endpoints() -> dict:
|
|
| 96 |
"endpoints": f1_api.list_all_endpoints(),
|
| 97 |
}
|
| 98 |
|
|
|
|
| 99 |
def get_endpoint_info(endpoint: str) -> dict:
|
| 100 |
"""
|
| 101 |
Retrieve detailed information about a specific OpenF1 API endpoint.
|
|
@@ -115,6 +119,7 @@ def get_endpoint_info(endpoint: str) -> dict:
|
|
| 115 |
"endpoint_help": f1_api.get_endpoint_help(endpoint)
|
| 116 |
}
|
| 117 |
|
|
|
|
| 118 |
def get_filter_info(filter_name: str) -> dict:
|
| 119 |
"""
|
| 120 |
Retrieve detailed information about a specific OpenF1 API filter.
|
|
|
|
| 31 |
"filter_metadata": dict()
|
| 32 |
}
|
| 33 |
|
| 34 |
+
|
| 35 |
def get_filter_string(filter_name: str, filter_value: str, operator: str = "=") -> str:
|
| 36 |
"""
|
| 37 |
Create a filter string for OpenF1 API requests.
|
|
|
|
| 46 |
"""
|
| 47 |
return f"{filter_name}{operator}{filter_value}&"
|
| 48 |
|
| 49 |
+
|
| 50 |
def apply_filters(api_string: str, *filters: str) -> str:
|
| 51 |
"""
|
| 52 |
Apply one or more filter strings to an API endpoint URL.
|
|
|
|
| 63 |
api_string += filter
|
| 64 |
return api_string.rstrip("&") # Remove trailing & for last filter
|
| 65 |
|
| 66 |
+
|
| 67 |
def send_request(api_string: str) -> dict:
|
| 68 |
"""
|
| 69 |
Send an HTTP GET request to the specified API endpoint and return the JSON response.
|
|
|
|
| 99 |
"endpoints": f1_api.list_all_endpoints(),
|
| 100 |
}
|
| 101 |
|
| 102 |
+
|
| 103 |
def get_endpoint_info(endpoint: str) -> dict:
|
| 104 |
"""
|
| 105 |
Retrieve detailed information about a specific OpenF1 API endpoint.
|
|
|
|
| 119 |
"endpoint_help": f1_api.get_endpoint_help(endpoint)
|
| 120 |
}
|
| 121 |
|
| 122 |
+
|
| 123 |
def get_filter_info(filter_name: str) -> dict:
|
| 124 |
"""
|
| 125 |
Retrieve detailed information about a specific OpenF1 API filter.
|