| 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.") |
|
|
|
|