[docs]classToTController:""" Tree of Thought (ToT) controller. This is a version of a ToT controller, dubbed in the paper as a "Simple Controller". It has one parameter `c` which is the number of children to explore for each thought. """
[docs]def__init__(self,c:int=3):""" Initialize the controller. Args: c: The number of children to explore at each node. """self.c=c
def__call__(self,memory:ToTDFSMemory)->Tuple[str,...]:next_thought=memory.top()parent_thought=memory.top_parent()validity=(ThoughtValidity.VALID_INTERMEDIATEifnext_thoughtisNoneelsenext_thought.validity)# 1 if the current partial solution is invalid, backtrack to the parent# thought.ifvalidity==ThoughtValidity.INVALID:memory.pop()next_thought=memory.top()ifnext_thoughtandlen(next_thought.children)>=self.c:memory.pop()# 2 if the current partial solution is valid but C children were# explored and yet failed to find a final solution, backtrack to the# parent thought.elif(validity==ThoughtValidity.VALID_INTERMEDIATEandparent_thoughtandlen(parent_thought.children)>=self.c):memory.pop(2)returntuple(thought.textforthoughtinmemory.current_path())