Spaces:
Sleeping
Sleeping
KoRiF
commited on
Commit
·
5fe139f
1
Parent(s):
e2522a1
[refactor] reorder LangGraph method definitions
Browse files- workflow.py +68 -66
workflow.py
CHANGED
|
@@ -64,72 +64,6 @@ class GAIAAnsweringWorkflow:
|
|
| 64 |
answer = str(response.content)
|
| 65 |
return answer
|
| 66 |
|
| 67 |
-
@staticmethod
|
| 68 |
-
def default_qa_function(question: str) -> str:
|
| 69 |
-
"""Placeholder QA function (override with your CodeAgent)"""
|
| 70 |
-
return "42"
|
| 71 |
-
|
| 72 |
-
@staticmethod
|
| 73 |
-
def default_formatter(answer: str) -> str:
|
| 74 |
-
"""Default GAIA formatting"""
|
| 75 |
-
return answer #f"\\boxed{{{answer}}}"
|
| 76 |
-
|
| 77 |
-
def check_context_independent(self, state: AgentState)->bool:
|
| 78 |
-
if ctx := state.get("context"):
|
| 79 |
-
if ctx.get("filename"):
|
| 80 |
-
return False
|
| 81 |
-
prompt = f"""
|
| 82 |
-
|
| 83 |
-
I have a CodeAgent based on the text-to-text model that can use Internet search and parse the information found.
|
| 84 |
-
If this approach is enough to successfully cope with the task, then we will call such a task an "easy question"
|
| 85 |
-
|
| 86 |
-
AS AN ERUDITE PERSON YOU must analyze how difficult it will be to solve the next question
|
| 87 |
-
<<{state["question"]}>>
|
| 88 |
-
|
| 89 |
-
If you think that the question is easy, then return an empty string. Important! You should NOT add any symbols to the output in this case!
|
| 90 |
-
If the question concerns the use of additional resources such as complex analysis of downloaded files or resources on the Internet, then return an action plan
|
| 91 |
-
|
| 92 |
-
"""
|
| 93 |
-
reply = self.ask_llm(prompt, True)
|
| 94 |
-
prompt = f""" The reasonings from other LLM is provided: <<{reply}>>
|
| 95 |
-
You have to Summarize:
|
| 96 |
-
output either empty string ('') for easy question
|
| 97 |
-
or extract action plan for non-easy question
|
| 98 |
-
"""
|
| 99 |
-
reply = self.ask_llm(prompt, False)
|
| 100 |
-
if reply:
|
| 101 |
-
state["reasoning"].append(reply)
|
| 102 |
-
return False
|
| 103 |
-
return True
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
def build_workflow(self) -> Any:
|
| 107 |
-
"""Construct and compile the LangGraph workflow"""
|
| 108 |
-
# Create graph
|
| 109 |
-
workflow = StateGraph(AgentState)
|
| 110 |
-
|
| 111 |
-
# Add nodes
|
| 112 |
-
workflow.add_node("preparations", self.preparations_node)
|
| 113 |
-
workflow.add_node("triage", self.triage_node)
|
| 114 |
-
workflow.add_node("deep_processing", self.deep_processing_node)
|
| 115 |
-
workflow.add_node("generate_answer", self.generate_answer_node)
|
| 116 |
-
workflow.add_node("format_output", self.format_output_node)
|
| 117 |
-
|
| 118 |
-
# Define edges
|
| 119 |
-
workflow.set_entry_point("preparations")
|
| 120 |
-
workflow.add_edge("preparations", "triage")
|
| 121 |
-
workflow.add_conditional_edges("triage"
|
| 122 |
-
, self.check_context_independent
|
| 123 |
-
, {
|
| 124 |
-
True: "generate_answer",
|
| 125 |
-
False: "deep_processing"
|
| 126 |
-
})
|
| 127 |
-
workflow.add_edge("deep_processing", "format_output")
|
| 128 |
-
|
| 129 |
-
workflow.add_edge("generate_answer", "format_output")
|
| 130 |
-
workflow.add_edge("format_output", END)
|
| 131 |
-
|
| 132 |
-
return workflow.compile()
|
| 133 |
|
| 134 |
def extract_noted_urls_with_llm(self, question: str) -> List[str]:
|
| 135 |
"""Use LLM to extract URLs specifically noted in the question"""
|
|
@@ -177,6 +111,35 @@ class GAIAAnsweringWorkflow:
|
|
| 177 |
print(f"File download failed: {str(e)}")
|
| 178 |
return ""
|
| 179 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
def preparations_node(self, state: AgentState) -> dict:
|
| 181 |
if not state["context"]:
|
| 182 |
return {}
|
|
@@ -260,6 +223,35 @@ Do NOT include << >> in your answer! Don't use full answer formulations! If you
|
|
| 260 |
except Exception as e:
|
| 261 |
return {"formatted_answer": f"\\boxed{{\\text{{Formatting error: {str(e)}}}}}"}
|
| 262 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
|
| 264 |
def __call__(self, question: str, context: Dict|None=None) -> str:
|
| 265 |
"""
|
|
@@ -286,6 +278,16 @@ Do NOT include << >> in your answer! Don't use full answer formulations! If you
|
|
| 286 |
result = self.workflow.invoke(initial_state)
|
| 287 |
return result["formatted_answer"]
|
| 288 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 289 |
# Example usage with custom QA function
|
| 290 |
if __name__ == "__main__":
|
| 291 |
# Custom QA function (replace with your CodeAgent integration)
|
|
|
|
| 64 |
answer = str(response.content)
|
| 65 |
return answer
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
def extract_noted_urls_with_llm(self, question: str) -> List[str]:
|
| 69 |
"""Use LLM to extract URLs specifically noted in the question"""
|
|
|
|
| 111 |
print(f"File download failed: {str(e)}")
|
| 112 |
return ""
|
| 113 |
|
| 114 |
+
|
| 115 |
+
def check_context_independent(self, state: AgentState)->bool:
|
| 116 |
+
if ctx := state.get("context"):
|
| 117 |
+
if ctx.get("filename"):
|
| 118 |
+
return False
|
| 119 |
+
prompt = f"""
|
| 120 |
+
|
| 121 |
+
I have a CodeAgent based on the text-to-text model that can use Internet search and parse the information found.
|
| 122 |
+
If this approach is enough to successfully cope with the task, then we will call such a task an "easy question"
|
| 123 |
+
|
| 124 |
+
AS AN ERUDITE PERSON YOU must analyze how difficult it will be to solve the next question
|
| 125 |
+
<<{state["question"]}>>
|
| 126 |
+
|
| 127 |
+
If you think that the question is easy, then return an empty string. Important! You should NOT add any symbols to the output in this case!
|
| 128 |
+
If the question concerns the use of additional resources such as complex analysis of downloaded files or resources on the Internet, then return an action plan
|
| 129 |
+
|
| 130 |
+
"""
|
| 131 |
+
reply = self.ask_llm(prompt, True)
|
| 132 |
+
prompt = f""" The reasonings from other LLM is provided: <<{reply}>>
|
| 133 |
+
You have to Summarize:
|
| 134 |
+
output either empty string ('') for easy question
|
| 135 |
+
or extract action plan for non-easy question
|
| 136 |
+
"""
|
| 137 |
+
reply = self.ask_llm(prompt, False)
|
| 138 |
+
if reply:
|
| 139 |
+
state["reasoning"].append(reply)
|
| 140 |
+
return False
|
| 141 |
+
return True
|
| 142 |
+
|
| 143 |
def preparations_node(self, state: AgentState) -> dict:
|
| 144 |
if not state["context"]:
|
| 145 |
return {}
|
|
|
|
| 223 |
except Exception as e:
|
| 224 |
return {"formatted_answer": f"\\boxed{{\\text{{Formatting error: {str(e)}}}}}"}
|
| 225 |
|
| 226 |
+
def build_workflow(self) -> Any:
|
| 227 |
+
"""Construct and compile the LangGraph workflow"""
|
| 228 |
+
# Create graph
|
| 229 |
+
workflow = StateGraph(AgentState)
|
| 230 |
+
|
| 231 |
+
# Add nodes
|
| 232 |
+
workflow.add_node("preparations", self.preparations_node)
|
| 233 |
+
workflow.add_node("triage", self.triage_node)
|
| 234 |
+
workflow.add_node("deep_processing", self.deep_processing_node)
|
| 235 |
+
workflow.add_node("generate_answer", self.generate_answer_node)
|
| 236 |
+
workflow.add_node("format_output", self.format_output_node)
|
| 237 |
+
|
| 238 |
+
# Define edges
|
| 239 |
+
workflow.set_entry_point("preparations")
|
| 240 |
+
workflow.add_edge("preparations", "triage")
|
| 241 |
+
workflow.add_conditional_edges("triage"
|
| 242 |
+
, self.check_context_independent
|
| 243 |
+
, {
|
| 244 |
+
True: "generate_answer",
|
| 245 |
+
False: "deep_processing"
|
| 246 |
+
})
|
| 247 |
+
workflow.add_edge("deep_processing", "format_output")
|
| 248 |
+
|
| 249 |
+
workflow.add_edge("generate_answer", "format_output")
|
| 250 |
+
workflow.add_edge("format_output", END)
|
| 251 |
+
|
| 252 |
+
return workflow.compile()
|
| 253 |
+
|
| 254 |
+
|
| 255 |
|
| 256 |
def __call__(self, question: str, context: Dict|None=None) -> str:
|
| 257 |
"""
|
|
|
|
| 278 |
result = self.workflow.invoke(initial_state)
|
| 279 |
return result["formatted_answer"]
|
| 280 |
|
| 281 |
+
@staticmethod
|
| 282 |
+
def default_qa_function(question: str) -> str:
|
| 283 |
+
"""Placeholder QA function (override with your CodeAgent)"""
|
| 284 |
+
return "42"
|
| 285 |
+
|
| 286 |
+
@staticmethod
|
| 287 |
+
def default_formatter(answer: str) -> str:
|
| 288 |
+
"""Default GAIA formatting"""
|
| 289 |
+
return answer #f"\\boxed{{{answer}}}"
|
| 290 |
+
|
| 291 |
# Example usage with custom QA function
|
| 292 |
if __name__ == "__main__":
|
| 293 |
# Custom QA function (replace with your CodeAgent integration)
|