gaia-agent / tools.py
Daniel Amendoeira
Update tools.py
66051de verified
raw
history blame
3.06 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
def current_date(_):
return datetime.datetime.now().strftime("%Y-%m-%d")
def day_of_week(_):
return datetime.datetime.now().strftime("%A")
def days_until(date_str):
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 = [
Tool(
name="current_date",
func=current_date,
description="Returns the current date in YYYY-MM-DD format."
),
Tool(
name="current_day_of_week",
func=day_of_week,
description="Returns the current day of the week (e.g., Monday, Tuesday)."
),
Tool(
name="days_until",
func=days_until,
description="Returns the number of days from today until a given date (input format: YYYY-MM-DD)."
)
]
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}"
transcribe_audio_tool = Tool(
name="transcribe_audio",
func=transcribe_audio,
description="Transcribes an audio file from a local file or a URL"
)