Spaces:
Runtime error
Runtime error
Update LG_agent.py
Browse files- LG_agent.py +32 -32
LG_agent.py
CHANGED
|
@@ -76,37 +76,37 @@ def build_graph(max_steps: int = 5):
|
|
| 76 |
graph = builder.compile()
|
| 77 |
|
| 78 |
def limited_invoke(state, max_steps: int = 5, max_reasoning_steps_after_tool: int = 2):
|
| 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 |
# def limited_invoke(state):
|
| 112 |
# steps = 0
|
|
@@ -132,7 +132,7 @@ def build_graph(max_steps: int = 5):
|
|
| 132 |
# steps += 1
|
| 133 |
# return state
|
| 134 |
|
| 135 |
-
|
| 136 |
|
| 137 |
# 5. BasicAgent class
|
| 138 |
|
|
|
|
| 76 |
graph = builder.compile()
|
| 77 |
|
| 78 |
def limited_invoke(state, max_steps: int = 5, max_reasoning_steps_after_tool: int = 2):
|
| 79 |
+
steps = 0
|
| 80 |
+
reasoning_steps_since_last_tool = 0
|
| 81 |
+
|
| 82 |
+
while steps < max_steps:
|
| 83 |
+
print(f"\U0001f501 Step {steps + 1}")
|
| 84 |
+
state = graph.invoke(state)
|
| 85 |
+
|
| 86 |
+
for msg in state["messages"]:
|
| 87 |
+
if isinstance(msg, AIMessage):
|
| 88 |
+
print("\nπ€ Assistant says:")
|
| 89 |
+
print("-" * 40)
|
| 90 |
+
print(msg.content.strip())
|
| 91 |
+
print("-" * 40)
|
| 92 |
+
|
| 93 |
+
latest_message = state["messages"][-1] if state["messages"] else None
|
| 94 |
+
|
| 95 |
+
if isinstance(latest_message, AIMessage):
|
| 96 |
+
if latest_message.tool_calls:
|
| 97 |
+
print("π Tool call detected β continuing loop.")
|
| 98 |
+
reasoning_steps_since_last_tool = 0 # reset counter
|
| 99 |
+
else:
|
| 100 |
+
reasoning_steps_since_last_tool += 1
|
| 101 |
+
print(f"π§ No tool call β reasoning step #{reasoning_steps_since_last_tool}")
|
| 102 |
+
|
| 103 |
+
if reasoning_steps_since_last_tool >= max_reasoning_steps_after_tool:
|
| 104 |
+
print("β
Final answer assumed after sufficient reasoning.")
|
| 105 |
+
break
|
| 106 |
+
|
| 107 |
+
steps += 1
|
| 108 |
+
|
| 109 |
+
return state
|
| 110 |
|
| 111 |
# def limited_invoke(state):
|
| 112 |
# steps = 0
|
|
|
|
| 132 |
# steps += 1
|
| 133 |
# return state
|
| 134 |
|
| 135 |
+
return limited_invoke
|
| 136 |
|
| 137 |
# 5. BasicAgent class
|
| 138 |
|