File size: 1,811 Bytes
6accb61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
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