Spaces:
Runtime error
Runtime error
Update tools.py
Browse files
tools.py
CHANGED
|
@@ -25,6 +25,43 @@ import wikipedia
|
|
| 25 |
from bs4 import BeautifulSoup
|
| 26 |
from langchain_core.tools import tool
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
@tool
|
| 29 |
def extract_wikipedia_section_html(page_title: str, section_title: str, mode: str = "html") -> str:
|
| 30 |
"""
|
|
@@ -120,6 +157,7 @@ def filter_vegetables(items: list[str]) -> list[str]:
|
|
| 120 |
|
| 121 |
# List of all tools
|
| 122 |
all_tools = [
|
|
|
|
| 123 |
reverse_sentence,
|
| 124 |
filter_vegetables,
|
| 125 |
extract_wikipedia_section_html
|
|
|
|
| 25 |
from bs4 import BeautifulSoup
|
| 26 |
from langchain_core.tools import tool
|
| 27 |
|
| 28 |
+
@tool
|
| 29 |
+
def detect_non_commutative_subset(table_text: str) -> str:
|
| 30 |
+
"""
|
| 31 |
+
Given a multiplication table in markdown format, return the subset of elements
|
| 32 |
+
involved in at least one non-commutative pair.
|
| 33 |
+
|
| 34 |
+
Args:
|
| 35 |
+
table_text (str): Markdown-style table defining the operation.
|
| 36 |
+
|
| 37 |
+
Returns:
|
| 38 |
+
str: Comma-separated list of elements in alphabetical order that are involved in non-commutative cases.
|
| 39 |
+
"""
|
| 40 |
+
try:
|
| 41 |
+
import re
|
| 42 |
+
from collections import defaultdict
|
| 43 |
+
|
| 44 |
+
lines = [line.strip() for line in table_text.strip().splitlines() if '|' in line]
|
| 45 |
+
headers = [h.strip() for h in lines[0].split('|')[1:-1]] # skip empty edges
|
| 46 |
+
operation = {}
|
| 47 |
+
|
| 48 |
+
for row in lines[2:]: # skip header and separator
|
| 49 |
+
cells = [c.strip() for c in row.split('|')[1:-1]]
|
| 50 |
+
row_label = cells[0]
|
| 51 |
+
for col_index, result in enumerate(cells[1:]):
|
| 52 |
+
col_label = headers[col_index]
|
| 53 |
+
operation[(row_label, col_label)] = result
|
| 54 |
+
|
| 55 |
+
non_commutative_elements = set()
|
| 56 |
+
for a in headers:
|
| 57 |
+
for b in headers:
|
| 58 |
+
if operation.get((a, b)) != operation.get((b, a)):
|
| 59 |
+
non_commutative_elements.update([a, b])
|
| 60 |
+
|
| 61 |
+
return ", ".join(sorted(non_commutative_elements)) if non_commutative_elements else "The operation is commutative."
|
| 62 |
+
except Exception as e:
|
| 63 |
+
return f"Error analyzing table: {str(e)}"
|
| 64 |
+
|
| 65 |
@tool
|
| 66 |
def extract_wikipedia_section_html(page_title: str, section_title: str, mode: str = "html") -> str:
|
| 67 |
"""
|
|
|
|
| 157 |
|
| 158 |
# List of all tools
|
| 159 |
all_tools = [
|
| 160 |
+
detect_non_commutative_subset,
|
| 161 |
reverse_sentence,
|
| 162 |
filter_vegetables,
|
| 163 |
extract_wikipedia_section_html
|