gaia-agent / tools.py
Daniel Amendoeira
Update tools.py
7e057ea verified
raw
history blame
2.73 kB
from langchain.tools import tool
import datetime
import requests
import openai
import os
import tempfile
from urllib.parse import urlparse
from openai import OpenAI
@tool
def current_date(_) -> str :
""" Returns the current date in YYYY-MM-DD format """
return datetime.datetime.now().strftime("%Y-%m-%d")
@tool
def day_of_week(_) -> str :
""" Returns the current day of the week (e.g., Monday, Tuesday) """
return datetime.datetime.now().strftime("%A")
@tool
def days_until(date_str: str) -> str :
""" Returns the number of days from today until a given date (input format: YYYY-MM-DD) """
try:
future_date = datetime.datetime.strptime(date_str, "%Y-%m-%d").date()
today = datetime.date.today()
delta_days = (future_date - today).days
return f"{delta_days} days until {date_str}"
except Exception as e:
return f"Error parsing date: {str(e)}"
datetime_tools = [current_date, day_of_week, days_until]
@tool
def transcribe_audio(audio_input: str) -> str:
"""
Transcribes an audio file from a local file or a URL
Args:
audio_input (str): A local file path or a direct URL to the audio file (.mp3, .m4a, etc.)
Returns:
str: The transcribed text from the audio.
"""
try:
# Detects if audio_input is a URL
is_url = audio_input.startswith("http://") or audio_input.startswith("https://")
if is_url:
parsed = urlparse(audio_input) # breaks down the URL into components (scheme, netloc, path, params, etc.)
extension = os.path.splitext(parsed.path)[1] or ".mp3" # get the actual file extension from the URL path or define it to .mp3 if no extension is found
# Download to temporary file
with tempfile.NamedTemporaryFile(suffix=extension, delete=False) as tmp_file: # creates a temporary file
response = requests.get(audio_input) # downloads the content
response.raise_for_status() # checks if the http request was successful
tmp_file.write(response.content) # saves the file to disk
file_path = tmp_file.name
else:
file_path = audio_input
# Transcribing audio using OpenAI Whisper
client = OpenAI()
with open(file_path, "rb") as audio_file: # opens the audio file from disk in binary mode (rb); the "with" block ensures the file is automatically closed afterward
transcription = client.audio.transcriptions.create(
model="whisper-1",
file=audio_file
)
return transcription.text
except Exception as e:
return f"Transcription failed: {e}"