[docs]classToTDFSMemory:""" Memory for the Tree of Thought (ToT) chain. It is implemented as a stack of thoughts. This allows for a depth first search (DFS) of the ToT. """
[docs]deftop(self)->Optional[Thought]:"Get the top of the stack without popping it."returnself.stack[-1]iflen(self.stack)>0elseNone
[docs]defpop(self,n:int=1)->Optional[Thought]:"Pop the top n elements of the stack and return the last one."iflen(self.stack)<n:returnNonefor_inrange(n):node=self.stack.pop()returnnode
[docs]deftop_parent(self)->Optional[Thought]:"Get the parent of the top of the stack without popping it."returnself.stack[-2]iflen(self.stack)>1elseNone
[docs]defstore(self,node:Thought)->None:"Add a node on the top of the stack."iflen(self.stack)>0:self.stack[-1].children.add(node)self.stack.append(node)
@propertydeflevel(self)->int:"Return the current level of the stack."returnlen(self.stack)
[docs]defcurrent_path(self)->List[Thought]:"Return the thoughts path."returnself.stack[:]