oshul's picture
SYNC FIX
96b1f03
Raw History Blame Contribute Delete
2.95 kB
"""Tool functions for LLM tool calling — holidays and daylight info."""
import logging
from datetime import datetime
import httpx
from langchain_core.tools import tool
logger = logging.getLogger(__name__)
_HTTP_TIMEOUT = 10 # seconds
@tool
def get_public_holidays(country_code: str = "KZ", year: int = 0) -> str:
"""Get public holidays for a given country and year.
Use this to check which days are official holidays so you can avoid
scheduling tasks on those dates. Returns a list of holidays with dates
and names.
Args:
country_code: ISO 3166-1 alpha-2 country code, e.g. "KZ" for Kazakhstan.
year: Year to query. Defaults to the current year if 0 or omitted.
"""
if year == 0:
year = datetime.now().year
url = f"https://date.nager.at/api/v3/PublicHolidays/{year}/{country_code}"
try:
resp = httpx.get(url, timeout=_HTTP_TIMEOUT)
resp.raise_for_status()
holidays = resp.json()
# Return compact representation
lines = [f"Праздники {country_code} {year}:"]
for h in holidays:
lines.append(f" {h['date']} — {h['localName']} ({h['name']})")
return "\n".join(lines)
except Exception as e:
logger.warning("get_public_holidays failed: %s", e)
return f"Ошибка получения праздников: {e}"
@tool
def get_daylight_info(lat: float = 43.24, lng: float = 76.95, date: str = "") -> str:
"""Get sunrise, sunset and day length for a given location and date.
Use this to understand how long the daylight lasts so you can adapt
work schedules — e.g. don't plan outdoor tasks after sunset.
Args:
lat: Latitude. Defaults to 43.24 (Almaty).
lng: Longitude. Defaults to 76.95 (Almaty).
date: Date in YYYY-MM-DD format. Defaults to today if empty.
"""
if not date:
date = datetime.now().strftime("%Y-%m-%d")
url = "https://api.sunrise-sunset.org/json"
params = {"lat": lat, "lng": lng, "date": date, "formatted": 0}
try:
resp = httpx.get(url, params=params, timeout=_HTTP_TIMEOUT)
resp.raise_for_status()
data = resp.json()["results"]
sunrise = data["sunrise"] # ISO 8601
sunset = data["sunset"]
day_length_sec = data["day_length"]
hours = day_length_sec // 3600
minutes = (day_length_sec % 3600) // 60
return (
f"Световой день ({date}, lat={lat}, lng={lng}):\n"
f" Рассвет: {sunrise}\n"
f" Закат: {sunset}\n"
f" Длительность: {hours}ч {minutes}мин"
)
except Exception as e:
logger.warning("get_daylight_info failed: %s", e)
return f"Ошибка получения данных светового дня: {e}"
# List of all tools for binding to LLM
ALL_TOOLS = [get_public_holidays, get_daylight_info]