File size: 2,611 Bytes
5675ecd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd
from smolagents import Tool
from typing import Any, Dict, Optional

class ReverseTextTool(Tool):
    name = "reverse_text"
    description = "Reverses the input text."
    # tell the validator: I’m expecting a dict with key "text"
    inputs = {"input": {"type": "any", "description": "The text to be reversed"}}
    output_type = "string"

    def forward(self, input: Any) -> Any:
        return input[::-1]


class TableCommutativityTool(Tool):
    name = "find_non_commutative_elements"
    description = (
        "Given a multiplication table (2D list) and its header elements, "
        "returns the elements involved in any a*b != b*a."
    )
    inputs = {
        "input": {
            "type": "any",
            "description": "Dict with keys 'table' (list of lists) and 'elements' (list of strings)."
        }
    }
    output_type = "string"

    def forward(self, input: dict) -> list[str]:
        table   = input["table"]
        elements = input["elements"]
        non_comm = set()
        for i, a in enumerate(elements):
            for j, b in enumerate(elements):
                if table[i][j] != table[j][i]:
                    non_comm.update({a, b})
        return str(sorted(non_comm))



class VegetableListTool(Tool):
    name = "list_vegetables"
    description = (
        "From a list of grocery items, returns those that are true vegetables "
        "(botanical definition), sorted alphabetically."
    )
    inputs = {
        "input": {
            "type": "any",
            "description": "Dict with key 'items' containing a list of item strings."
        }
    }
    output_type = "string"

    _VEG_SET = {
        "broccoli", "bell pepper", "celery", "corn",
        "green beans", "lettuce", "sweet potatoes", "zucchini"
    }

    def forward(self, input: Any) -> Any:
        items = input["items"]
        return str(sorted(item for item in items if item in self._VEG_SET))


class ExcelSumFoodTool(Tool):
    name = "sum_food_sales"
    description = (
        "Reads an Excel file with columns 'Category' and 'Sales', "
        "and returns total sales where Category != 'Drink', rounded to two decimals."
    )
    inputs = {
        "input": {
            "type": "any",
            "description": "Dict with key 'excel_path' pointing to the .xlsx file to read."
        }
    }
    output_type = "string"

    def forward(self, input: Any) -> Any:
        excel_path = input["excel_path"]
        df = pd.read_excel(excel_path)
        total = df.loc[df["Category"] != "Drink", "Sales"].sum()
        return str(round(float(total), 2))