File size: 4,186 Bytes
929e339
 
 
1c7a9a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
929e339
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d63189c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from langchain_core.tools import tool


@tool(parse_docstring=True)
def get_text_file(file_name: str) -> str:
    """
    Loads a text file by its name and returns its content as a string.

    Args:
        file_name (str): The name of the text file to load. The file name should include
                         a task ID followed by a file extension (e.g., "12345.txt").

    Returns:
        str: The content of the text file as a string.
    """
    print(f"Loading text file: {file_name}")
    try:
        import questions

        task_id, ext = file_name.rsplit(".", 1)
        if not task_id or not ext:
            return "Invalid file name format. Please provide a valid file name with task ID and extension."

        file_content = questions.get_file(task_id)
        if not file_content:
            return f"File with task ID {task_id} not found."

        return file_content.decode("utf-8")
    except Exception as e:
        return f"Error loading text file: {e}"


@tool(parse_docstring=True)
def load_excel_file(file_name: str) -> str:
    """
    Loads an Excel file by its name and returns its content as a string.

    Args:
        file_name (str): The name of the Excel file to load. The file name should include
                         a task ID followed by a file extension (e.g., "12345.xlsx").

    Returns:
        str: The content of the Excel file as a string.
    """
    print(f"Loading Excel file: {file_name}")
    try:
        from io import BytesIO

        import pandas as pd

        import questions

        task_id, ext = file_name.rsplit(".", 1)
        if not task_id or not ext:
            return "Invalid file name format. Please provide a valid file name with task ID and extension."

        if ext.lower() not in ["xlsx", "xls"]:
            return "Invalid file format. Please provide an Excel file."

        file_bytes = questions.get_file(task_id)
        if not file_bytes:
            return f"File with task ID {task_id} not found."

        data = pd.read_excel(BytesIO(file_bytes))
        print(f"Data shape: {data.shape}")
        print(f"Data columns: {data.columns.tolist()}")
        print(f"Data preview:\n{data.head()}")
        return data.to_markdown(index=False)
    except Exception as e:
        return f"Error loading Excel file: {e}"


@tool(parse_docstring=True)
def sum_rows_cols(file_name: str, rows: list[str], cols: list[str]) -> float | str:
    """
    Sums all data from the specified rows and columns in an Excel file.

    Args:
        file_name (str): The name of the Excel file to load. The file name should include
                         a task ID followed by a file extension (e.g., "12345.xlsx").
        rows (list[str]): List of row labels to sum. These should match the index labels in the Excel file.
        cols (list[str]): List of column labels to sum. These should match the column headers in the Excel file.

    Returns:
        float | str: The total sum of the specified rows and columns, or an error message if the operation fails.
    """
    try:
        from io import BytesIO

        import pandas as pd

        import questions

        task_id, ext = file_name.rsplit(".", 1)
        if not task_id or not ext or ext.lower() not in ["xlsx", "xls"]:
            return "Invalid file name format. Please provide a valid Excel file."

        file_bytes = questions.get_file(task_id)
        if not file_bytes:
            return f"File with task ID {task_id} not found."

        data = pd.read_excel(BytesIO(file_bytes), index_col=0)
        print(f"Data shape: {data.shape}")
        print(f"Data columns: {data.columns.tolist()}")

        # Ensure rows and cols are in the DataFrame
        if not all(row in data.index for row in rows):
            return f"Some specified rows {rows} are not found in the data."
        if not all(col in data.columns for col in cols):
            return f"Some specified columns {cols} are not found in the data."

        # Sum all data from the specified rows and columns
        total_sum = data.loc[rows, cols].sum().sum()

        return total_sum
    except Exception as e:
        return f"Error processing Excel file: {e}"