Spaces:
Sleeping
Sleeping
Initial implementation of the course_insights_tool. It uses the GolfCourseAPI (api.golfcourseapi.com).
Browse files- backend/.env.template +9 -1
- backend/tools/course_insights_tool.py +50 -0
- backend/tools/registry.py +6 -1
backend/.env.template
CHANGED
|
@@ -4,7 +4,15 @@
|
|
| 4 |
ALLOWED_ORIGINS=*
|
| 5 |
|
| 6 |
# Required API Keys
|
| 7 |
-
OPENAI_API_KEY=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Environment Setting
|
| 10 |
# Development: Set to "development" to run API-only mode
|
|
|
|
| 4 |
ALLOWED_ORIGINS=*
|
| 5 |
|
| 6 |
# Required API Keys
|
| 7 |
+
OPENAI_API_KEY=your_openai_api_key_here
|
| 8 |
+
TAVILY_API_KEY=your_tavily_api_key_here
|
| 9 |
+
LANGCHAIN_API_KEY=your_langchain_api_key_here
|
| 10 |
+
GOLFCOURSE_API_KEY=your_golfcourse_api_key_here
|
| 11 |
+
|
| 12 |
+
#LangSmith Variables
|
| 13 |
+
LANGCHAIN_TRACING_V2=true
|
| 14 |
+
LANGCHAIN_ENDPOINT=https://api.smith.langchain.com
|
| 15 |
+
LANGCHAIN_PROJECT="your_project_name_here"
|
| 16 |
|
| 17 |
# Environment Setting
|
| 18 |
# Development: Set to "development" to run API-only mode
|
backend/tools/course_insights_tool.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
from langchain.tools import tool
|
| 4 |
+
|
| 5 |
+
GOLFCOURSE_API_KEY = os.environ["GOLFCOURSE_API_KEY"]
|
| 6 |
+
BASE_URL = "https://api.golfcourseapi.com/v1"
|
| 7 |
+
|
| 8 |
+
HEADERS = {
|
| 9 |
+
"Authorization": f"Key {GOLFCOURSE_API_KEY}"
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
@tool
|
| 13 |
+
def course_insights(search_query: str) -> str:
|
| 14 |
+
"""Fetches detailed course info for a given golf course name using GolfCourseAPI."""
|
| 15 |
+
try:
|
| 16 |
+
# Step 1: Search
|
| 17 |
+
search_resp = requests.get(f"{BASE_URL}/search", headers=HEADERS, params={"search_query": search_query})
|
| 18 |
+
search_resp.raise_for_status()
|
| 19 |
+
courses = search_resp.json().get("courses", [])
|
| 20 |
+
|
| 21 |
+
if not courses:
|
| 22 |
+
return f"No courses found for query '{search_query}'."
|
| 23 |
+
|
| 24 |
+
course_id = courses[0]["id"]
|
| 25 |
+
course_name = courses[0]["course_name"]
|
| 26 |
+
club_name = courses[0]["club_name"]
|
| 27 |
+
|
| 28 |
+
# Step 2: Fetch course by ID
|
| 29 |
+
detail_resp = requests.get(f"{BASE_URL}/courses/{course_id}", headers=HEADERS)
|
| 30 |
+
detail_resp.raise_for_status()
|
| 31 |
+
course = detail_resp.json()
|
| 32 |
+
|
| 33 |
+
male_tees = course.get("tees", {}).get("male", [])
|
| 34 |
+
if not male_tees:
|
| 35 |
+
return f"No male tee data available for {club_name} - {course_name}."
|
| 36 |
+
|
| 37 |
+
tee = male_tees[0] # Assume the first tee (often the back tees)
|
| 38 |
+
|
| 39 |
+
return (
|
| 40 |
+
f"{club_name} - {course_name} (ID: {course_id})\n"
|
| 41 |
+
f"Location: {course['location']['address']}\n"
|
| 42 |
+
f"Rating: {tee['course_rating']} | Slope: {tee['slope_rating']}\n"
|
| 43 |
+
f"Yards: {tee['total_yards']} | Par: {tee['par_total']}\n"
|
| 44 |
+
f"Hardest Hole: TBD\n"
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
except requests.HTTPError as e:
|
| 48 |
+
return f"API request failed: {e}"
|
| 49 |
+
except Exception as e:
|
| 50 |
+
return f"Unexpected error: {e}"
|
backend/tools/registry.py
CHANGED
|
@@ -4,12 +4,17 @@ Tool registry for the agent. Add tools here to make them available to your agent
|
|
| 4 |
|
| 5 |
from langchain.tools import Tool
|
| 6 |
from backend.tools.search_golfpedia_tool import search_golfpedia
|
| 7 |
-
|
| 8 |
tools = [
|
| 9 |
Tool(
|
| 10 |
name="search_golfpedia",
|
| 11 |
func=search_golfpedia,
|
| 12 |
description="Searches the web for general golf-related knowledge using Tavily.",
|
| 13 |
),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
# Add other tools here later
|
| 15 |
]
|
|
|
|
| 4 |
|
| 5 |
from langchain.tools import Tool
|
| 6 |
from backend.tools.search_golfpedia_tool import search_golfpedia
|
| 7 |
+
from backend.tools.course_insights_tool import course_insights
|
| 8 |
tools = [
|
| 9 |
Tool(
|
| 10 |
name="search_golfpedia",
|
| 11 |
func=search_golfpedia,
|
| 12 |
description="Searches the web for general golf-related knowledge using Tavily.",
|
| 13 |
),
|
| 14 |
+
Tool(
|
| 15 |
+
name="course_insights",
|
| 16 |
+
func=course_insights,
|
| 17 |
+
description="Fetches detailed course info for a given golf course name using GolfCourseAPI.",
|
| 18 |
+
),
|
| 19 |
# Add other tools here later
|
| 20 |
]
|