Spike of football fixture lookup.

This commit is contained in:
Rob Holland
2025-02-03 17:34:42 +00:00
parent b125548844
commit 879abade05
6 changed files with 167 additions and 19 deletions

View File

@@ -1,4 +1,5 @@
from .find_events import find_events
from .find_fixtures import find_fixtures
from .search_flights import search_flights
from .create_invoice import create_invoice
@@ -6,6 +7,8 @@ from .create_invoice import create_invoice
def get_handler(tool_name: str):
if tool_name == "FindEvents":
return find_events
if tool_name == "FindFixtures":
return find_fixtures
if tool_name == "SearchFlights":
return search_flights
if tool_name == "CreateInvoice":

59
tools/find_fixtures.py Normal file
View File

@@ -0,0 +1,59 @@
import os
import requests
from datetime import datetime
from dotenv import load_dotenv
BASE_URL = 'https://api.football-data.org/v4'
def find_fixtures(args: dict) -> dict:
load_dotenv(override=True)
api_key = os.getenv("FOOTBALL_DATA_API_KEY", "YOUR_DEFAULT_KEY")
team_name = args.get("team")
month = args.get("month")
headers = {'X-Auth-Token': api_key}
team_name = team_name.lower()
month = month.capitalize()
try:
month_number = datetime.strptime(month, "%B").month
except ValueError:
return {"error": "Invalid month provided."}
# Fetch team ID
teams_response = requests.get(f'{BASE_URL}/competitions/PL/teams', headers=headers)
if teams_response.status_code != 200:
return {"error": "Failed to fetch teams data."}
teams_data = teams_response.json()
team_id = None
for team in teams_data['teams']:
if team_name in team['name'].lower():
team_id = team['id']
break
if not team_id:
return {"error": "Team not found."}
# Fetch fixtures
match_date_from = f'2025-{month_number:02}-01'
match_date_to = f'2025-{month_number:02}-31'
print(f'{BASE_URL}/teams/{team_id}/matches?dateFrom={match_date_from}&dateTo={match_date_to}')
fixtures_response = requests.get(f'{BASE_URL}/teams/{team_id}/matches?dateFrom={match_date_from}&dateTo={match_date_to}', headers=headers)
if fixtures_response.status_code != 200:
return {"error": "Failed to fetch fixtures data."}
fixtures_data = fixtures_response.json()
matching_fixtures = []
for match in fixtures_data['matches']:
print(match)
match_date = datetime.strptime(match['utcDate'], "%Y-%m-%dT%H:%M:%SZ")
if match['competition']['code'] == 'PL':
matching_fixtures.append({
"date": match_date.strftime("%Y-%m-%d"),
"homeTeam": match['homeTeam']['name'],
"awayTeam": match['awayTeam']['name'],
})
return {"fixtures": matching_fixtures}

View File

@@ -1,29 +1,29 @@
from models.tool_definitions import AgentGoal
from tools.tool_registry import (
find_events_tool,
find_fixtures_tool,
search_flights_tool,
create_invoice_tool,
)
goal_event_flight_invoice = AgentGoal(
tools=[find_events_tool, search_flights_tool, create_invoice_tool],
tools=[find_fixtures_tool, search_flights_tool, create_invoice_tool],
description="Help the user gather args for these tools in order: "
"1. FindEvents: Find an event to travel to "
"1. FindFixtures: Find fixtures for a team in a given month "
"2. SearchFlights: search for a flight around the event dates "
"3. CreateInvoice: Create a simple invoice for the cost of that flight ",
example_conversation_history="\n ".join(
[
"user: I'd like to travel to an event",
"agent: Sure! Let's start by finding an event you'd like to attend. I know about events in Australia and New Zealand cities. Could you tell me which city and month you're interested in?",
"user: sydney in may please",
"agent: Great! Let's find an events in Sydney in May.",
"user_confirmed_tool_run: <user clicks confirm on FindEvents tool>",
"tool_result: { 'event_name': 'Vivid Sydney', 'event_date': '2023-05-01' }",
"agent: Found an event! There's Vivid Sydney on May 1 2025, ending on May 14 2025. Would you like to search for flights around these dates?",
"user: I'd like to travel to a football match",
"agent: Sure! Let's start by finding an match you'd like to attend. I know about Premier League fixtures in the UK. Could you tell me which team and month you're interested in?",
"user: wolverhamptom in may please",
"agent: Great! Let's find a match for Wolverhampton Wanderers in May.",
"user_confirmed_tool_run: <user clicks confirm on FindFixtures tool>",
'tool_result: results including {"homeTeam": "Wolverhampton Wanderers FC", "awayTeam": "Manchester United", "date": "2025-05-04"}',
"agent: Found a match! There's an away game against Manchester United on May 4 2025. Would you like to search for flights around this date?",
"user: Yes, please",
"agent: Let's search for flights around these dates. Could you provide your departure city?",
"agent: Let's search for flights around this date. Could you provide your departure city?",
"user: San Francisco",
"agent: Thanks, searching for flights from San Francisco to Sydney around 2023-02-25 to 2023-02-28.",
"agent: Thanks, searching for flights from San Francisco to the Manchester around 2025-05-03 to 2025-05-05.",
"user_confirmed_tool_run: <user clicks confirm on SearchFlights tool>"
'tool_result: results including {"flight_number": "CX101", "return_flight_number": "CX102", "price": 850.0}',
"agent: Found some flights! The cheapest is CX101 for $850. Would you like to generate an invoice for this flight?",

View File

@@ -65,3 +65,20 @@ create_invoice_tool = ToolDefinition(
),
],
)
find_fixtures_tool = ToolDefinition(
name="FindFixtures",
description="Find upcoming fixtures for a given team and month",
arguments=[
ToolArgument(
name="team",
type="string",
description="The name of the team to search for",
),
ToolArgument(
name="month",
type="string",
description="The month to search for fixtures",
),
],
)