mirror of
https://github.com/temporal-community/temporal-ai-agent.git
synced 2026-03-15 05:58:08 +01:00
fix train_api IDs, mock search_fixtures function and arg rename
This commit is contained in:
@@ -47,6 +47,7 @@ See the next section for tool configuration for each goal.
|
||||
#### Agent Goal: goal_match_train_invoice
|
||||
|
||||
* Finding a match requires a key from [Football Data](https://www.football-data.org). Sign up for a free account, then see the 'My Account' page to get your API token. Set `FOOTBALL_DATA_API_KEY` to this value.
|
||||
* If you're lazy go to `tools/search_fixtures.py` and replace the `search_fixtures` function with the mock `search_fixtures_example` that exists in the same file.
|
||||
* We use a mock function to search for trains. Start the train API server to use the real API: `python thirdparty/train_api.py`
|
||||
* * The train activity is 'enterprise' so it's written in C# and requires a .NET runtime. See the [.NET backend](#net-(enterprise)-backend) section for details on running it.
|
||||
* Requires a Stripe key for the `create_invoice` tool. Set this in the `STRIPE_API_KEY` environment variable in .env
|
||||
|
||||
4
thirdparty/train_api.py
vendored
4
thirdparty/train_api.py
vendored
@@ -68,7 +68,7 @@ class TrainServer(BaseHTTPRequestHandler):
|
||||
arr_hour = arr_hour % 24
|
||||
|
||||
journey = {
|
||||
"id": "T%s".format(random.randint(1000, 9999)),
|
||||
"id": "T{}".format(random.randint(1000, 9999)),
|
||||
"type": "outbound",
|
||||
"departure": origin,
|
||||
"arrival": destination,
|
||||
@@ -101,7 +101,7 @@ class TrainServer(BaseHTTPRequestHandler):
|
||||
arr_hour = arr_hour % 24
|
||||
|
||||
journey = {
|
||||
"id": "T%s".format(random.randint(1000, 9999)),
|
||||
"id": "T{}".format(random.randint(1000, 9999)),
|
||||
"type": "return",
|
||||
"departure": destination,
|
||||
"arrival": origin,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import os
|
||||
import requests
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timedelta
|
||||
from dotenv import load_dotenv
|
||||
|
||||
BASE_URL = "https://api.football-data.org/v4"
|
||||
@@ -11,17 +11,17 @@ def search_fixtures(args: dict) -> dict:
|
||||
api_key = os.getenv("FOOTBALL_DATA_API_KEY", "YOUR_DEFAULT_KEY")
|
||||
|
||||
team_name = args.get("team")
|
||||
start_date_str = args.get("start_date")
|
||||
end_date_str = args.get("end_date")
|
||||
date_from_str = args.get("date_from")
|
||||
date_to_str = args.get("date_to")
|
||||
headers = {"X-Auth-Token": api_key}
|
||||
team_name = team_name.lower()
|
||||
|
||||
try:
|
||||
start_date = datetime.strptime(start_date_str, "%Y-%m-%d")
|
||||
end_date = datetime.strptime(end_date_str, "%Y-%m-%d")
|
||||
date_from = datetime.strptime(date_from_str, "%Y-%m-%d")
|
||||
date_to = datetime.strptime(date_to_str, "%Y-%m-%d")
|
||||
except ValueError:
|
||||
return {
|
||||
"error": "Invalid date provided. Expected format YYYY-MM-DD for both start_date and end_date."
|
||||
"error": "Invalid date provided. Expected format YYYY-MM-DD for both date_from and date_to."
|
||||
}
|
||||
|
||||
# Fetch team ID
|
||||
@@ -39,9 +39,9 @@ def search_fixtures(args: dict) -> dict:
|
||||
if not team_id:
|
||||
return {"error": "Team not found."}
|
||||
|
||||
start_date_formatted = start_date.strftime("%Y-%m-%d")
|
||||
end_date_formatted = end_date.strftime("%Y-%m-%d")
|
||||
fixtures_url = f"{BASE_URL}/teams/{team_id}/matches?dateFrom={start_date_formatted}&dateTo={end_date_formatted}"
|
||||
date_from_formatted = date_from.strftime("%Y-%m-%d")
|
||||
date_to_formatted = date_to.strftime("%Y-%m-%d")
|
||||
fixtures_url = f"{BASE_URL}/teams/{team_id}/matches?dateFrom={date_from_formatted}&dateTo={date_to_formatted}"
|
||||
print(fixtures_url)
|
||||
|
||||
fixtures_response = requests.get(fixtures_url, headers=headers)
|
||||
@@ -63,3 +63,92 @@ def search_fixtures(args: dict) -> dict:
|
||||
)
|
||||
|
||||
return {"fixtures": matching_fixtures}
|
||||
|
||||
|
||||
def search_fixtures_example(args: dict) -> dict:
|
||||
"""
|
||||
Example version of search_fixtures that returns hardcoded data without making API calls.
|
||||
The function respects the team name provided and generates fixture dates within the specified range.
|
||||
|
||||
Args:
|
||||
args: Dictionary containing 'team', 'date_from', and 'date_to'
|
||||
|
||||
Returns:
|
||||
Dictionary with 'fixtures' key containing a list of fixture objects
|
||||
"""
|
||||
team_name = args.get("team", "Default Team FC")
|
||||
date_from_str = args.get("date_from")
|
||||
date_to_str = args.get("date_to")
|
||||
|
||||
# Validate dates
|
||||
try:
|
||||
date_from = datetime.strptime(date_from_str, "%Y-%m-%d")
|
||||
date_to = datetime.strptime(date_to_str, "%Y-%m-%d")
|
||||
except ValueError:
|
||||
return {
|
||||
"error": "Invalid date provided. Expected format YYYY-MM-DD for both date_from and date_to."
|
||||
}
|
||||
|
||||
# Calculate 3 reasonable fixture dates within the given range
|
||||
date_range = (date_to - date_from).days
|
||||
if date_range < 21:
|
||||
# If range is less than 3 weeks, use evenly spaced fixtures
|
||||
fixture_dates = [
|
||||
date_from + timedelta(days=max(1, date_range // 3)),
|
||||
date_from + timedelta(days=max(2, date_range * 2 // 3)),
|
||||
date_to - timedelta(days=min(2, date_range // 4)),
|
||||
]
|
||||
else:
|
||||
# Otherwise space them out by weeks
|
||||
fixture_dates = [
|
||||
date_from + timedelta(days=7),
|
||||
date_from + timedelta(days=14),
|
||||
date_to - timedelta(days=7),
|
||||
]
|
||||
|
||||
# Ensure we only have 3 dates
|
||||
fixture_dates = fixture_dates[:3]
|
||||
|
||||
# Expanded pool of opponent teams to avoid team playing against itself
|
||||
all_opponents = [
|
||||
"Manchester United FC",
|
||||
"Leicester City FC",
|
||||
"Manchester City FC",
|
||||
"Liverpool FC",
|
||||
"Chelsea FC",
|
||||
"Arsenal FC",
|
||||
"Tottenham Hotspur FC",
|
||||
"West Ham United FC",
|
||||
"Everton FC",
|
||||
]
|
||||
|
||||
# Select opponents that aren't the same as the requested team
|
||||
available_opponents = [
|
||||
team for team in all_opponents if team.lower() != team_name.lower()
|
||||
]
|
||||
|
||||
# Ensure we have at least 3 opponents
|
||||
if len(available_opponents) < 3:
|
||||
# Add generic opponents if needed
|
||||
additional_teams = [f"Opponent {i} FC" for i in range(1, 4)]
|
||||
available_opponents.extend(additional_teams)
|
||||
|
||||
# Take only the first 3 opponents
|
||||
opponents = available_opponents[:3]
|
||||
|
||||
# Generate fixtures - always exactly 3
|
||||
fixtures = []
|
||||
for i, fixture_date in enumerate(fixture_dates):
|
||||
date_str = fixture_date.strftime("%Y-%m-%d")
|
||||
|
||||
# Alternate between home and away games
|
||||
if i % 2 == 0:
|
||||
fixtures.append(
|
||||
{"date": date_str, "homeTeam": opponents[i], "awayTeam": team_name}
|
||||
)
|
||||
else:
|
||||
fixtures.append(
|
||||
{"date": date_str, "homeTeam": team_name, "awayTeam": opponents[i]}
|
||||
)
|
||||
|
||||
return {"fixtures": fixtures}
|
||||
|
||||
@@ -93,12 +93,12 @@ search_fixtures_tool = ToolDefinition(
|
||||
description="The full name of the team to search for.",
|
||||
),
|
||||
ToolArgument(
|
||||
name="start_date",
|
||||
name="date_from",
|
||||
type="string",
|
||||
description="The start date in format (YYYY-MM-DD) for the fixture search inferred from the user's request (e.g. mid-March).",
|
||||
),
|
||||
ToolArgument(
|
||||
name="end_date",
|
||||
name="date_to",
|
||||
type="string",
|
||||
description="The end date in format (YYYY-MM-DD) for the fixture search (e.g. 'the last week of May').",
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user