agents-course-v2 / tools /file_tools.py
D3MI4N's picture
first working version
6accb61
"""
File processing and data extraction tools for the Retriever Agent.
Handles Excel, CSV, audio, video, and document processing.
"""
import pandas as pd
import os
from typing import Any, Dict, List
from langchain.tools import tool
@tool
def read_excel_file(file_path: str, sheet_name: str = None) -> str:
"""
Read and analyze Excel files.
Args:
file_path: Path to the Excel file
sheet_name: Specific sheet to read (optional)
Returns:
String representation of the data
"""
try:
if sheet_name:
df = pd.read_excel(file_path, sheet_name=sheet_name)
else:
df = pd.read_excel(file_path)
return df.to_string()
except Exception as e:
return f"Error reading Excel file: {str(e)}"
@tool
def read_csv_file(file_path: str) -> str:
"""
Read and analyze CSV files.
Args:
file_path: Path to the CSV file
Returns:
String representation of the data
"""
try:
df = pd.read_csv(file_path)
return df.to_string()
except Exception as e:
return f"Error reading CSV file: {str(e)}"
@tool
def calculate_column_sum(file_path: str, column_name: str) -> float:
"""
Calculate sum of a specific column in Excel/CSV file.
Args:
file_path: Path to the file
column_name: Name of the column to sum
Returns:
Sum of the column values
"""
try:
if file_path.endswith('.xlsx') or file_path.endswith('.xls'):
df = pd.read_excel(file_path)
else:
df = pd.read_csv(file_path)
return float(df[column_name].sum())
except Exception as e:
return f"Error calculating sum: {str(e)}"
# Add more file processing tools as needed