-
Notifications
You must be signed in to change notification settings - Fork 0
/
tot_strategy.py
348 lines (294 loc) Β· 14.2 KB
/
tot_strategy.py
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
from __future__ import annotations
from collections import deque
from typing import (
AsyncIterator,
Deque,
Dict,
Iterator,
List,
Optional,
Tuple,
)
from langchain_core.agents import AgentAction, AgentFinish
from langchain_core.callbacks import (
AsyncCallbackManagerForChainRun,
CallbackManagerForChainRun,
)
from ...action_executors import BaseActionExecutor, LangchainActionExecutor, MetaTools
from ..base_strategy import BaseCustomStrategy
from .components import (
ThoughtEvaluator,
ThoughtEvaluatorConfig,
ThoughtEvaluatorInput,
ThoughtGenerator,
ThoughtGeneratorConfig,
ThoughtGeneratorInput,
ThoughtSorter,
ThoughtSorterConfig,
ThoughtSorterInput,
)
from .utils import ToTNode
class TreeOfThoughtsDFSStrategy(BaseCustomStrategy):
"""Tree of Thoughts powered by Depth-First Search.
Based on "Tree of Thoughts: Deliberate Problem Solving with Large Language Models" by Yao et al.
Also supports DFSDT from "ToolLLM: Facilitating Large Language Models to Master 16000+ Real-world APIs" by Qin et al.
"""
action_executor: BaseActionExecutor
thought_generator: ThoughtGenerator
thought_evaluator: ThoughtEvaluator
thought_sorter: Optional[ThoughtSorter] = None
do_sorting: bool = False # True for DFS (Tree of Thoughts), False for DFSDT (ToolLLM)
root: Optional[ToTNode] = None
terminals: List[ToTNode] = []
@property
def agent(self):
return self.thought_generator.agent.agent
@property
def input_keys(self) -> List[str]:
return self.agent.input_keys
@property
def output_keys(self) -> List[str]:
if self.return_intermediate_steps:
return self.agent.return_values + ["intermediate_steps"]
else:
return self.agent.return_values
@classmethod
def create(
cls,
meta_tools: Optional[MetaTools] = None,
return_intermediate_steps: bool = False,
return_finish_log: bool = False,
max_iterations: int = 15,
verbose: bool = True,
action_executor: Optional[BaseActionExecutor] = None,
generator_config: Optional[ThoughtGeneratorConfig] = None,
evaluator_config: Optional[ThoughtEvaluatorConfig] = None,
sorter_config: Optional[ThoughtSorterConfig] = None,
do_sorting: bool = False,
**kwargs,
) -> "TreeOfThoughtsDFSStrategy":
"""Creates an instance of Tree of Thoughts + DFS strategy.
Tree of Thoughts + DFS requires an evaluator component. The default setting is as follows:
* evaluator is a runnable that accepts EvaluatorInput and returns a float in a 0-1 range;
* evaluator judges whether a new thought should be explored or discarded based on the threshold;
thought is explored further only when value is greater than the given threshold.
Args:
tools: The valid tools the agent can call.
action_executor: The action executor for the current strategy. If None, the default will be used.
max_thoughts: Maximum number of new thoughts at each DFS step.
max_iterations: Maximum number of iterations.
"""
if generator_config is None:
raise ValueError("Default thought generator config is currently not supported.")
if evaluator_config is None:
raise ValueError("Default thought evaluator config is currently not supported.")
if do_sorting and sorter_config is None:
raise ValueError("Default thought sorter config is currently not supported.")
generator = ThoughtGenerator.create_from_config(generator_config)
evaluator = ThoughtEvaluator.create_from_config(evaluator_config)
sorter = ThoughtSorter.create_from_config(sorter_config) if do_sorting else None # type: ignore[arg-type]
if action_executor is None:
action_executor = LangchainActionExecutor(tools=generator_config.tools, meta_tools=meta_tools)
return cls(
thought_generator=generator,
thought_evaluator=evaluator,
thought_sorter=sorter,
do_sorting=do_sorting,
action_executor=action_executor,
return_intermediate_steps=return_intermediate_steps,
return_finish_log=return_finish_log,
max_iterations=max_iterations,
verbose=verbose,
)
def _dfs_step(
self,
inputs: Dict[str, str],
trajectory: List[Tuple[AgentAction, str]],
run_manager: Optional[CallbackManagerForChainRun] = None,
) -> Iterator[List[AgentAction] | AgentAction | AgentFinish]:
"""Performs a single step of DFS algorithm.
Args:
inputs: Agent inputs.
trajectory: Current trajectory β path from the root node to the current node. Essentially, intermediate steps before the current DFS step.
run_manager: Callback for the current run.
Returns:
Iterator over promising thoughts for the current step with three options possible:
* List[AgentAction] - for multi-action thoughts
* AgentAction - for single-action thoughts
* AgentFinish - for finishing thoughts / thoughts without tool calls
"""
# 1: generate k possible next steps
thoughts = self.thought_generator.invoke(
ThoughtGeneratorInput(inputs=inputs, intermediate_steps=trajectory),
run_manager=run_manager.get_child(tag="generate_thoughts") if run_manager else None,
)
# 2: (optional) sort them
if self.do_sorting:
assert self.thought_sorter is not None, "Sorting enabled, but thought sorter was not passed."
thoughts = self.thought_sorter.invoke(
ThoughtSorterInput(thoughts=thoughts, inputs=inputs, intermediate_steps=trajectory),
run_manager=run_manager.get_child(tag="sort_thoughts") if run_manager else None,
)
for cur_thought in thoughts:
# 3: evaluate each thought
cur_thought_should_continue = self.thought_evaluator.invoke(
ThoughtEvaluatorInput(
inputs=inputs,
intermediate_steps=trajectory,
next_thought=cur_thought,
),
run_manager=run_manager.get_child(tag="evaluate_thought") if run_manager else None,
)
# 4: proceed only with thoughts with value above a certain threshold
if cur_thought_should_continue:
yield cur_thought
def _run_strategy(
self,
inputs: Dict[str, str],
run_manager: Optional[CallbackManagerForChainRun] = None,
) -> Iterator[Tuple[AgentFinish, List[Tuple[AgentAction, str]]]]:
"""Runs Tree of Thoughts + DFS strategy.
Args:
inputs: Agent inputs.
name_to_tool_map: Mapping from tool names to actual tools, used for calling tools based on agent's output.
color_mapping: Mapping from tool names to colors, used for logging purposes when calling tools.
run_manager: Callback for the current run.
Returns:
Iterator over tuples (AgentFinish, List[Tuple[AgentAction, str]]):
essentially, each tuple consists of the final result and of intermediate steps.
The current implementation iterates over ALL terminal nodes in a tree.
"""
if not self.root:
self.root = ToTNode()
frontier: Deque[ToTNode] = deque([self.root])
cur_step = 0
while frontier and cur_step < self.max_iterations:
cur_node = frontier.pop()
# TODO: traverses from the tree root to the cur_node on each call. how to optimize?
trajectory = cur_node.trajectory
for new_thought in self._dfs_step(
inputs=inputs,
trajectory=trajectory,
run_manager=run_manager,
):
# actually do action(s)
if isinstance(new_thought, AgentFinish):
observation = None
else:
# reset to parent node state
self.action_executor.reset(
actions=[t[0] for t in trajectory],
run_manager=run_manager.get_child() if run_manager else None,
)
# execute action
observation = self.action_executor.execute(
actions=new_thought,
run_manager=run_manager.get_child() if run_manager else None,
)
new_node = ToTNode(parent=cur_node, thought=new_thought, observation=observation)
cur_node.children.append(new_node)
if isinstance(new_thought, AgentFinish):
self.terminals.append(new_node)
else:
frontier.appendleft(new_node)
cur_step += 1
for node in self.terminals:
assert isinstance(node.thought, AgentFinish), "Terminal nodes are expected to contain AgentFinish."
yield node.thought, node.trajectory
async def _adfs_step(
self,
inputs: Dict[str, str],
trajectory: List[Tuple[AgentAction, str]],
run_manager: Optional[AsyncCallbackManagerForChainRun] = None,
) -> AsyncIterator[List[AgentAction] | AgentAction | AgentFinish]:
"""Performs a single step of DFS algorithm asynchronously.
Args:
inputs: Agent inputs.
trajectory: Current trajectory β path from the root node to the current node. Essentially, intermediate steps before the current DFS step.
name_to_tool_map: Mapping from tool names to actual tools, used for calling tools based on agent's output.
color_mapping: Mapping from tool names to colors, used for logging purposes when calling tools.
run_manager: Callback for the current run.
Returns:
Iterator over promising thoughts for the current step with three options possible:
* List[AgentAction] - for multi-action thoughts
* AgentAction - for single-action thoughts
* AgentFinish - for finishing thoughts / thoughts without tool calls
"""
# 1: generate k possible next steps
thoughts = await self.thought_generator.ainvoke(
ThoughtGeneratorInput(inputs=inputs, intermediate_steps=trajectory),
run_manager=run_manager.get_child(tag="generate_thoughts") if run_manager else None,
)
# 2: (optional) sort them
if self.do_sorting:
assert self.thought_sorter is not None, "Sorting enabled, but thought sorter was not passed."
thoughts = await self.thought_sorter.ainvoke(
ThoughtSorterInput(thoughts=thoughts, inputs=inputs, intermediate_steps=trajectory),
run_manager=run_manager.get_child(tag="sort_thoughts") if run_manager else None,
)
for cur_thought in thoughts:
# 3: evaluate each thought
cur_thought_should_continue = await self.thought_evaluator.ainvoke(
ThoughtEvaluatorInput(
inputs=inputs,
intermediate_steps=trajectory,
next_thought=cur_thought,
),
run_manager=run_manager.get_child(tag="evaluate_thought") if run_manager else None,
)
# 4: proceed only with thoughts with value above a certain threshold
if cur_thought_should_continue:
yield cur_thought
async def _arun_strategy(
self,
inputs: Dict[str, str],
run_manager: Optional[AsyncCallbackManagerForChainRun] = None,
) -> AsyncIterator[Tuple[AgentFinish, List[Tuple[AgentAction, str]]]]:
"""Runs Tree of Thoughts + DFS strategy asynchronously.
Args:
inputs: Agent inputs.
name_to_tool_map: Mapping from tool names to actual tools, used for calling tools based on agent's output.
color_mapping: Mapping from tool names to colors, used for logging purposes when calling tools.
run_manager: Callback for the current run.
Returns:
Iterator over tuples (AgentFinish, List[Tuple[AgentAction, str]]):
essentially, each tuple consists of the final result and of intermediate steps.
The current implementation iterates over ALL terminal nodes in a tree.
"""
if not self.root:
self.root = ToTNode()
frontier: Deque[ToTNode] = deque([self.root])
cur_step = 0
while frontier and cur_step < self.max_iterations:
cur_node = frontier.pop()
# TODO: traverses from the tree root to the cur_node on each call. how to optimize?
trajectory = cur_node.trajectory
async for new_thought in self._adfs_step(
inputs=inputs,
trajectory=trajectory,
run_manager=run_manager,
):
# actually do action(s)
if isinstance(new_thought, AgentFinish):
observation = None
else:
# reset to parent node state
await self.action_executor.areset(
actions=[t[0] for t in trajectory],
run_manager=run_manager.get_child() if run_manager else None,
)
# execute action
observation = await self.action_executor.aexecute(
actions=new_thought,
run_manager=run_manager.get_child() if run_manager else None,
)
new_node = ToTNode(parent=cur_node, thought=new_thought, observation=observation)
cur_node.children.append(new_node)
if isinstance(new_thought, AgentFinish):
self.terminals.append(new_node)
else:
frontier.appendleft(new_node)
cur_step += 1
for node in self.terminals:
assert isinstance(node.thought, AgentFinish), "Terminal nodes are expected to contain AgentFinish."
yield node.thought, node.trajectory