File size: 6,226 Bytes
bee32cd
 
 
 
 
 
 
 
 
9bd34a5
 
 
 
bee32cd
 
 
 
 
 
64b6717
bee32cd
 
 
 
d888f9f
bee32cd
 
d888f9f
 
 
 
 
 
 
bee32cd
9bd34a5
46d5b0d
 
bee32cd
 
 
 
 
 
 
 
 
 
 
 
 
 
64b6717
bee32cd
 
 
 
d888f9f
bee32cd
 
d888f9f
 
 
 
 
 
 
bee32cd
 
 
 
 
 
 
 
 
9bd34a5
 
 
 
bee32cd
 
 
 
9bd34a5
 
 
 
 
 
 
 
 
bee32cd
 
 
 
 
9bd34a5
 
 
 
 
 
bee32cd
 
 
 
 
 
9bd34a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bee32cd
 
 
9bd34a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bee32cd
 
 
 
 
 
 
 
e7c10d7
2139c78
 
 
 
fef2a27
 
 
 
 
 
 
 
bee32cd
eb15d69
5abc589
eb15d69
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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