ESkuratov's picture
update agent
64b6717
Raw History Blame Contribute Delete
6.23 kB
import re
from typing import List
from typing_extensions import TypedDict, Annotated
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.messages import AnyMessage, HumanMessage
from langchain_core.tools import tool
from langgraph.graph.message import add_messages
from langgraph.graph import StateGraph, START, END
from langgraph.prebuilt import ToolNode, tools_condition
import sys
# Increase Python's recursion limit
sys.setrecursionlimit(10000) # Default is typically 1000
from prompts import prompt, solve_prompt
from llm import model
from tools import \
web_search, \
wiki_search, \
arxiv_search, \
multiply, \
add, \
subtract, \
divide, \
square_root, \
save_file, \
read_file, \
extract_text_from_image, \
get_youtube_transcript, \
execute_code_multilang, \
download_file_from_url, \
analyze_csv_file, \
analyze_excel_file, \
analyze_image
# Define regex pattern at the module level
regex_pattern = r"Plan:\s*(.+)\s*(#E\d+)\s*=\s*(\w+)\s*\[([^\]]+)\]"
class ReWOO(TypedDict):
messages: Annotated[list[AnyMessage], add_messages]
task: str
plan_string: str
steps: List
results: dict
result: str
tool_calls: List
tool_results: List
current_step: int
tools = [
web_search,
wiki_search,
arxiv_search,
multiply,
add,
subtract,
divide,
square_root,
save_file,
read_file,
extract_text_from_image,
get_youtube_transcript,
execute_code_multilang,
download_file_from_url,
analyze_csv_file,
analyze_excel_file,
analyze_image
]
try:
llm_with_tools = model.bind_tools(tools)
except ValueError as e:
print(f"Warning: Failed to bind tools: {e}")
print("Continuing without binding tools...")
llm_with_tools = model
# Define planner at module level
prompt_template = ChatPromptTemplate.from_messages([("user", prompt)])
planner = prompt_template | model
def get_plan(state: ReWOO):
task = state["task"]
result = planner.invoke({"task": task})
matches = re.findall(regex_pattern, result.content)
# Safety check - if no steps found, add a simple LLM step
if not matches:
print("No steps found in planner output, creating fallback step")
return {
"steps": [("Answer the question directly", "#E1", "LLM", task)],
"plan_string": f"Plan: Answer the question directly\n#E1 = LLM[{task}]"
}
return {"steps": matches, "plan_string": result.content}
def _get_current_task(state: ReWOO):
if "results" not in state or state["results"] is None:
return 1
# Safety check - ensure steps exist
if "steps" not in state or not state["steps"]:
return None
if len(state["results"]) >= len(state["steps"]):
return None
else:
return len(state["results"]) + 1
def tool_execution(state: ReWOO):
_step = _get_current_task(state)
# Safety checks
if _step is None:
return {"results": state.get("results", {})}
if "steps" not in state or not state["steps"] or _step > len(state["steps"]):
return {"results": state.get("results", {})}
try:
_, step_name, tool, tool_input = state["steps"][_step - 1]
_results = (state["results"] or {}) if "results" in state else {}
for k, v in _results.items():
if isinstance(v, str) and isinstance(tool_input, str):
tool_input = tool_input.replace(k, v)
if tool == "Google":
result = web_search.invoke(tool_input)
elif tool == "LLM":
result = model.invoke(tool_input)
else:
# Handle unsupported tools gracefully
result = f"Tool '{tool}' not supported. Using LLM instead."
result = model.invoke(tool_input)
_results[step_name] = str(result)
return {"results": _results}
except Exception as e:
print(f"Error in tool execution: {e}")
# Return current results without modification in case of error
return {"results": state.get("results", {})}
def solve(state: ReWOO):
plan = ""
# Safety check
if "steps" not in state or not state["steps"]:
return {"result": "I couldn't generate a plan to solve this task. Here's my best attempt: " + state["task"]}
try:
for _plan, step_name, tool, tool_input in state["steps"]:
_results = (state["results"] or {}) if "results" in state else {}
for k, v in _results.items():
if isinstance(v, str) and isinstance(tool_input, str):
tool_input = tool_input.replace(k, v)
if isinstance(v, str) and isinstance(step_name, str):
step_name = step_name.replace(k, v)
plan += f"Plan: {_plan}\n{step_name} = {tool}[{tool_input}]\n\n"
# Fallback if plan is empty
if not plan.strip():
plan = f"Direct answer: {state['task']}"
prompt_text = solve_prompt.format(plan=plan, task=state["task"])
result = model.invoke(prompt_text)
return {"result": result.content}
except Exception as e:
print(f"Error in solve: {e}")
# Provide a fallback answer in case of errors
return {"result": f"I tried to solve this but encountered a problem. Here's what I know: {state.get('results', {})}"}
def _route(state):
_step = _get_current_task(state)
if _step is None:
return "solve"
else:
return "tool"
def rewoo_graph():
prompt_template = ChatPromptTemplate.from_messages([("user", prompt)])
global planner # Define planner at the global scope so get_plan can use it
planner = prompt_template | model
graph = StateGraph(ReWOO)
graph.add_node("plan", get_plan)
graph.add_node("tool", tool_execution)
graph.add_node("solve", solve)
graph.add_edge("plan", "tool")
graph.add_edge("solve", END)
graph.add_conditional_edges("tool", _route)
graph.add_edge(START, "plan")
compiled_graph = graph.compile()
return compiled_graph