File size: 1,474 Bytes
c584a7a
 
 
 
 
aee7d6f
c584a7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from langchain.tools import BaseTool

class GroupTool(BaseTool):
    name: str = "group_tool"
    description: str = (
        "Perform algebraic/group-theory computations and set-based reasoning. Use for tasks like finding counter-examples to algebraic properties (commutativity, associativity, etc.) from a Cayley table."
    )

    def _run(self, table: str) -> str:
        # Оставляем только строки с '|'
        lines = [l for l in table.splitlines() if "|" in l]
        if len(lines) < 2:
            return "ERROR: Table format not recognized."
        header_cells = lines[0].split("|")[1:-1]
        elems = [c.strip() for c in header_cells if c.strip()]
        if not elems:
            return "ERROR: No elements in header."
        # матрица
        mat = []
        for row in lines[1:]:
            cells = row.split("|")[1:-1]
            if len(cells) != len(elems):
                return "ERROR: Row length mismatch."
            mat.append([c.strip() for c in cells])
        # проверка коммутативности
        bad = set()
        n = len(elems)
        for i in range(n):
            for j in range(n):
                if mat[i][j] != mat[j][i]:
                    bad.add(elems[i])
                    bad.add(elems[j])
        return ",".join(sorted(bad)) if bad else "Commutes everywhere"

    async def _arun(self, table: str) -> str:
        raise NotImplementedError("Async not supported.")