Spaces:
Sleeping
Sleeping
Add deterministic operation table solver
Browse files
agent.py
CHANGED
|
@@ -759,6 +759,12 @@ class LangGraphBenchmarkAgent:
|
|
| 759 |
return text[:max_chars]
|
| 760 |
|
| 761 |
def _draft_answer(self, state: AgentState) -> AgentState:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 762 |
tool_output = state.get("tool_output", "")
|
| 763 |
if state.get("task_type") == "code_question" and "python_stdout:" in tool_output:
|
| 764 |
stdout = tool_output.split("python_stdout:", 1)[1].split("python_stderr:", 1)[0].strip()
|
|
@@ -815,6 +821,55 @@ class LangGraphBenchmarkAgent:
|
|
| 815 |
sections.append(section)
|
| 816 |
return "\n\n".join(sections)[:6000]
|
| 817 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 818 |
def _answer_table_question(self, question: str, attachment_path: str) -> str:
|
| 819 |
if not attachment_path:
|
| 820 |
return ""
|
|
|
|
| 759 |
return text[:max_chars]
|
| 760 |
|
| 761 |
def _draft_answer(self, state: AgentState) -> AgentState:
|
| 762 |
+
operation_table_answer = self._answer_operation_table_question(
|
| 763 |
+
state.get("question", "")
|
| 764 |
+
)
|
| 765 |
+
if operation_table_answer:
|
| 766 |
+
return {"draft_answer": operation_table_answer}
|
| 767 |
+
|
| 768 |
tool_output = state.get("tool_output", "")
|
| 769 |
if state.get("task_type") == "code_question" and "python_stdout:" in tool_output:
|
| 770 |
stdout = tool_output.split("python_stdout:", 1)[1].split("python_stderr:", 1)[0].strip()
|
|
|
|
| 821 |
sections.append(section)
|
| 822 |
return "\n\n".join(sections)[:6000]
|
| 823 |
|
| 824 |
+
@staticmethod
|
| 825 |
+
def _answer_operation_table_question(question: str) -> str:
|
| 826 |
+
question_lower = question.lower()
|
| 827 |
+
if "not commutative" not in question_lower and "commutative" not in question_lower:
|
| 828 |
+
return ""
|
| 829 |
+
|
| 830 |
+
table_lines = [
|
| 831 |
+
line.strip()
|
| 832 |
+
for line in question.splitlines()
|
| 833 |
+
if line.strip().startswith("|") and line.strip().endswith("|")
|
| 834 |
+
]
|
| 835 |
+
if len(table_lines) < 3:
|
| 836 |
+
return ""
|
| 837 |
+
|
| 838 |
+
rows = [
|
| 839 |
+
[cell.strip() for cell in line.strip().strip("|").split("|")]
|
| 840 |
+
for line in table_lines
|
| 841 |
+
]
|
| 842 |
+
rows = [
|
| 843 |
+
row
|
| 844 |
+
for row in rows
|
| 845 |
+
if row and not all(set(cell) <= {"-"} for cell in row if cell)
|
| 846 |
+
]
|
| 847 |
+
if len(rows) < 2 or len(rows[0]) < 2:
|
| 848 |
+
return ""
|
| 849 |
+
|
| 850 |
+
headers = rows[0][1:]
|
| 851 |
+
operation_table = {}
|
| 852 |
+
for row in rows[1:]:
|
| 853 |
+
if len(row) != len(headers) + 1:
|
| 854 |
+
return ""
|
| 855 |
+
row_label = row[0]
|
| 856 |
+
operation_table[row_label] = dict(zip(headers, row[1:]))
|
| 857 |
+
|
| 858 |
+
if set(headers) - set(operation_table):
|
| 859 |
+
return ""
|
| 860 |
+
|
| 861 |
+
counterexample_elements = set()
|
| 862 |
+
for left in headers:
|
| 863 |
+
for right in headers:
|
| 864 |
+
left_result = operation_table[left].get(right)
|
| 865 |
+
right_result = operation_table[right].get(left)
|
| 866 |
+
if left_result != right_result:
|
| 867 |
+
counterexample_elements.update([left, right])
|
| 868 |
+
|
| 869 |
+
if not counterexample_elements:
|
| 870 |
+
return ""
|
| 871 |
+
return ", ".join(sorted(counterexample_elements))
|
| 872 |
+
|
| 873 |
def _answer_table_question(self, question: str, attachment_path: str) -> str:
|
| 874 |
if not attachment_path:
|
| 875 |
return ""
|