
    R"h>                         d dl Z d dlZd dlZd dlZd dlmZmZmZm	Z	m
Z
mZ ddlmZ ddlmZ  G d d      Z G d d	      Z G d
 d      Zy)    N)SetDictListOptionalUnionTuple   )Encoder)Informationc                       e Zd ZdZ	 	 	 	 	 ddedededee   dee   deee      d	eee      d
eee      fdZd Z	d Z
edefd       Zy)ConversationTurna<  
    A class to represent a turn in a conversation.

    Attributes:
        role (str): A short phrase of the role of the speaker for the current conversation turn.
        raw_utterance (str): The response generated by the LM model without polished style and tone.
        utterance_type (str): The type of utterance (e.g., statement, question).
        claim_to_make (Optional[str]): The point that this utterance tries to make. Should be empty if the utterance type is questioning.
        utterance (Optional[str]): The response generated by the model with polished style and tone. Defaults to raw_utterance if not provided.
        queries (List[str]): The queries used to gather information to have a grounded answer.
        raw_retrieved_info (List['Information']): A list of Information type that is retrieved.
        cited_info (Dict[int, 'Information']): A dictionary where the key is the citation index and the value is Information type.
        role_description (Optional[str]): A few sentences description of the role. Defaults to an empty string if not provided.
    Nroleraw_utteranceutterance_typeclaim_to_make	utterancequeriesraw_retrieved_info
cited_infoc	                    ||n|| _         || _        d|vr|n|j                  d      d   | _        d|vrdn|j                  d      d   | _        ||ng | _        ||ng | _        ||ni | _        || _        ||| _	        y d| _	        y )N:r    r	   )
r   r   splitr   role_descriptionr   r   r   r   r   )	selfr   r   r   r   r   r   r   r   s	            :/var/www/html/sandstorm/storm/knowledge_storm/dataclass.py__init__zConversationTurn.__init__   s     '0&;*tODC1C	&)o4::c?1;M")"5w2"4"@b 	 )3(>*B,.;.G]R    c                     t        j                  d      }t        t        t        |j                  | j                                    S )Nz	\[(\d+)\])recompilelistmapintfindallr   )r   citation_patterns     r   get_all_citation_indexz'ConversationTurn.get_all_citation_index2   s3    ::l3C-55dnnEFGGr   c           
          | j                   D cg c]  }|j                          }}| j                  | j                  | j                  | j
                  | j                  | j                  | j                  |d d	S c c}w )N)	r   r   r   r   r   r   r   r   r   )	r   to_dictr   r   r   r   r   r   r   )r   infor   s      r   r)   zConversationTurn.to_dict6   sq    9=9P9PQdllnQQ!//II $ 5 5||"11!//"4

 
	
 Rs   A:conv_turn_dictc                     |d   D cg c]  }t        j                  |       }} | |d   |d   |d    d|d    |d   |d |d   |d	   
      S c c}w )Nr   r   r   r   z: r   r   r   r   )r   r   r   r   r   r   r   r   )r   	from_dict)clsr+   r*   r   s       r   r-   zConversationTurn.from_dictD   s     5CCW4X
,0K!!$'
 
 $[1(9"6*+2n=O.P-QR"9-1)*:;(9	
 		
	
s   A)NNNNN)__name__
__module____qualname____doc__strr   r   r   r   r'   r)   classmethodr   r-    r   r   r   r      s    ( (,#''+:>26PP P 	P
  }P C=P $s)$P %T+%67P T+./P.H
 
t 
 
r   r   c                       e Zd ZdZ	 	 	 	 	 ddedee   ded    deed       dee   defd	Zd
 Z	defdZ
ddedefdZd Zd Zd Zd Zdded    fdZdefdZded    fdZded    fdZd Zed        Zy)KnowledgeNodea5  
    Class representing a node in the knowledge base.

    Attributes:
        name (str): The name of the node.
        content (list): A list of Information instances.
        children (list): A list of child KnowledgeNode instances.
        parent (KnowledgeNode): The parent node of the current node.
    Nnamecontentparentchildrensynthesize_output!need_regenerate_synthesize_outputc                     || _         |t        |      n	t               | _        |g n|| _        || _        || _        || _        y)a-  
        Initializes a KnowledgeNode instance.

        Args:
            name (str): The name of the node.
            content (list, optional): A list of information uuid. Defaults to None.
            parent (KnowledgeNode, optional): The parent node of the current node. Defaults to None.
        N)r8   setr9   r;   r:   r<   r=   )r   r8   r9   r:   r;   r<   r=   s          r   r   zKnowledgeNode.__init__a   sD    " 	181DW#%&.H!21R.r   c                     t        | j                        }| j                  D ]!  }|j                  |j	                                # |S )z
        Collects all content from the current node and its descendants.

        Returns:
            Set[int]: A set containing all content from the current node and its descendants.
        )r?   r9   r;   updatecollect_all_content)r   all_contentchilds      r   rB   z!KnowledgeNode.collect_all_contenty   sB     $,,']] 	<Eu88:;	<r   child_node_namec                 X    || j                   D cg c]  }|j                   c}v S c c}w )z@
        Check if the node has the child of given name.
        r;   r8   )r   rE   rD   s      r   	has_childzKnowledgeNode.has_child   s%     4=="I%5::"III"Is   'duplicate_handlingc                    | j                  |      rK|dk(  r%| j                  D ]  }|j                  |k(  s|c S  n!|dk(  rt        d| d| j                   d      t	        ||       }| j                  j                  |       |S )z
        Adds a child node to the current node.
        duplicate_handling (str): How to handle duplicate nodes. Options are "skip", "none", and "raise error".
        skipraise errorzInsert node error. Node z& already exists under its parent node .)r8   r:   )rH   r;   r8   	Exceptionr7   append)r   rE   rI   rD   
child_nodes        r   	add_childzKnowledgeNode.add_child   s    
 >>/*!V+!]] %Ezz_4$% $}4..??efjfofoeppqr  #E
Z(r   c                     | j                   S )z
        Returns the parent node of the current node.

        Returns:
            KnowledgeNode: The parent node of the current node.
        )r:   r   s    r   
get_parentzKnowledgeNode.get_parent   s     {{r   c                     | j                   S )z
        Returns the children of the current node.

        Returns:
            list: A list of child KnowledgeNode instances.
        r;   rS   s    r   get_childrenzKnowledgeNode.get_children   s     }}r   c                 T    | j                   D cg c]  }|j                   c}S c c}w )z3
        Returns a list of children names.
        rG   r   rD   s     r   get_children_namesz KnowledgeNode.get_children_names   s      )-6u

666s   %c                 h    d| j                    d| j                   dt        | j                         dS )z
        Returns a string representation of the KnowledgeNode instance.

        Returns:
            str: String representation of the KnowledgeNode instance.
        zKnowledgeNode(name=z
, content=z, children=))r8   r9   lenr;   rS   s    r   __repr__zKnowledgeNode.__repr__   s6     %TYYKz$,,{SVW[WdWdSeRffghhr   rootc                     g }| }|rF|j                  |j                         ||j                  |j                  k(  rn|j                  }|rF|ddd   S )z
        Get a list of names from the root to this node.

        Returns:
            List[str]: A list of node names from the root to this node.
        N)rO   r8   r:   )r   r_   pathcurrent_nodes       r   get_path_from_rootz KnowledgeNode.get_path_from_root   s]     KK))*L$5$5$B'..L	 
 DbDzr   information_indexc                 f    || j                   vr#d| _        | j                   j                  |       y y NT)r9   r=   add)r   re   s     r   insert_informationz KnowledgeNode.insert_information   s.    DLL059D2LL./ 1r   returnc                 *    g fd |        S )z
        Get a list of all descendant nodes.

        Returns:
            List[KnowledgeNode]: A list of all descendant nodes.
        c                 Z    | j                   D ]  }j                  |        |        y Nr;   rO   )noderD   collect_descendentsdescendentss     r   rp   z>KnowledgeNode.get_all_descendents.<locals>.collect_descendents   s,     +""5)#E*+r   r5   )r   rp   rq   s    @@r   get_all_descendentsz!KnowledgeNode.get_all_descendents   s     	+
 	D!r   c                 f    g }| j                   }| |j                  |       |j                   }| |S )z
        Get a list of all predecessor nodes (from current node to root).

        Returns:
            List[KnowledgeNode]: A list of all predecessor nodes.
        )r:   rO   )r   predecessorsrc   s      r   get_all_predecessorsz"KnowledgeNode.get_all_predecessors   sA     {{&-'..L & r   c                    | j                   t        | j                        | j                  D cg c]  }|j	                          c}| j
                  r| j
                  j                   nd| j                  | j                  dS c c}w )z
        Converts the KnowledgeNode instance to a dictionary representation.

        Returns:
            dict: The dictionary representation of the KnowledgeNode.
        N)r8   r9   r;   r:   r<   r=   )r8   r"   r9   r;   r)   r:   r<   r=   rY   s     r   r)   zKnowledgeNode.to_dict   sf     IIDLL)6:mmDUD*.++dkk&&4!%!7!7151W1W
 	
 Es   Bc                 "    dfd	 | |      S )a  
        Constructs a KnowledgeNode instance from a dictionary representation.

        Args:
            data (dict): The dictionary representation of the KnowledgeNode.

        Returns:
            KnowledgeNode: The constructed KnowledgeNode instance.
        c                    ||d   |d   |j                   k(  sJ  | |d   |d   |d |j                  dd       |j                  dd            }|d   D ](  } | ||	      }|j                  j                  |       * |S )
Nr:   r8   r9   r<   r=   T)r8   r9   r:   r;   r<   r=   r;   )parent_node)r8   getr;   rO   )r.   datary   ro   
child_datarP   helpers         r   r}   z'KnowledgeNode.from_dict.<locals>.helper  s    &H~1d8nHXHX6XXX&\Y""&((+>"E26((73	D #:. 1
#CF
$$Z01 Kr   rm   r5   )r.   r{   r}   s     @r   r-   zKnowledgeNode.from_dict  s    	$ c4  r   )NNNNTrK   rm   )r/   r0   r1   r2   r3   r   r   boolr   rB   rH   rQ   rT   rW   rZ   r^   rd   r$   ri   rr   ru   r)   r4   r-   r5   r   r   r7   r7   V   s    "&,048+/26SS #S )	S
 401S $C=S ,0S0
J J # $7ix'@  0C 0
T/%: "d?&; 
  ! !r   r7   c            	       .   e Zd ZdZdedeej                  j                  ej                  j                  f   de
defdZd Zededeej                  j                  ej                  j                  f   de
defd	       Z	 d&dee   deej*                  ee   f   fdZd Zd Zd Z	 	 d'dee   fdZd Zd(dZ	 	 	 	 	 	 d)deee
      dee   defdZ	 	 d*dedee   fdZ	 	 d*dede dee   fdZ!d Z"d Z#d Z$d Z%	 	 d+de&d e'd!e'fd"Z(d# Z)d$ Z*d% Z+y
),KnowledgeBasea  
    Represents the dynamic, hierarchical mind map used in Co-STORM to track and organize discourse.

    The knowledge base serves as a shared conceptual space between the user and the system, allowing for effective collaboration by reducing the user's cognitive load and ensuring that the discourse is easy to follow.

    The knowledge base is structured as a tree (or mind map) that dynamically organizes collected information and concepts as the conversation progresses.

    The mind map consists of concepts (nodes) and edges that represent parent-child relationships among topics. Each concept is linked to retrieved information,
    which is placed under the most appropriate concept based on its associated question and semantic similarity.

    For more details, please refer to Section 3.2 of Co-STORM paper: https://www.arxiv.org/pdf/2408.15232
    Attributes:
        root (KnowledgeNode): The root node of the hierarchical knowledge base, representing the top-level concept.

    topicknowledge_base_lmnode_expansion_trigger_countencoderc                    ddl m} ddlm}m} ddlm} || _        || _         ||| j                        | _	         ||| j                  |      | _
         ||      | _         ||      | _        t        d	      | _        t        d
      t!        j"                  g g      d
d| _        i | _        i | _        t+        j,                         | _        y)a  
        Initializes a KnowledgeBase instance.

        Args:
            topic (str): The topic of the knowledge base
            expand_node_module (dspy.Module): The module that organize knowledge base in place.
                The module should accept knowledge base as param. E.g. expand_node_module(self)
            article_generation_module (dspy.Module): The module that generate report from knowledge base.
                The module should return string. E.g. report = article_generation_module(self)
        r	   )ArticleGenerationModule)InsertInformationModuleExpandNodeModule)KnowledgeBaseSummaryModule)enginer   )r   information_insert_moduler   )r   r_   r8   r   hashencoded_structurestructure_stringN).collaborative_storm.modules.article_generationr   8collaborative_storm.modules.information_insertion_moduler   r   2collaborative_storm.modules.knowledge_base_summaryr   r   r   r   expand_node_modulearticle_generation_modulegen_summary_moduler7   r_   r   nparraykb_embeddinginfo_uuid_to_info_dictinfo_hash_to_uuid_dict	threadingLock_lock)	r   r   r   r   r   r   r   r   r   s	            r   r   zKnowledgeBase.__init__4  s    "	
	
	
  
 ')@$dll*
& #3$&*&D&D)E#

 *A$*
& #=DU"V#0f#=	H!#2$ "

 ?A#68#^^%
r   c                     | j                   j                         D ci c]  \  }}||j                          }}}| j                  | j                  j                         || j
                  dS c c}}w )N)r   treer   r   )r   itemsr)   r   r_   r   )r   keyvaluer   s       r   r)   zKnowledgeBase.to_dictj  so    373N3N3T3T3V"
%/S%C "
 "
 ZZII%%'&<&*&A&A	
 	
"
s   A/r{   c                     | |d   |||      }t         j                  |d         |_        |d   j                         D ci c]  \  }}t	        |      t	        |       c}}|_        |d   j                         D ci c]$  \  }}t	        |      t        j                  |      & }}}||_        |S c c}}w c c}}w )Nr   )r   r   r   r   r   r   r   )r7   r-   r_   r   r$   r   r   r   )	r.   r{   r   r   r   knowledge_baser   r   r   s	            r   r-   zKnowledgeBase.from_dictu  s     w-/)E	
 ,55d6lC ##;<BBD1
U Hc%j 1
- ##;<BBD"
U Hk++E22"
 "
 1G-1
"
s   B6)B<Nr_   rj   c                 X   | j                  ddd|      }t        |      }|| j                  d   k7  rV|j                  d      }|D cg c]  }|j	                  dd       }}| j
                  j                  |      }|||d| _        | j                  d	   | j                  d
   fS c c}w )NFT)include_indentinclude_full_pathinclude_hash_tagr_   r   
 -> z, r   r   r   )get_node_hierarchy_stringr   r   r   replacer   encode)r   r_   outline_stringoutline_string_hashoutline_stringsoutlinecleaned_outline_stringsencoded_outlines           r   &get_knowledge_base_structure_embeddingz4KnowledgeBase.get_knowledge_base_structure_embedding  s     77 ""	 8 
 #>2$"3"3F";;)7)=)=d)CO=L'29-'# ' #ll112IJO+%4$3!D 1201
 	
's   B'c                 *    g fd |       S )z
        Traverses the tree downward from the given node.

        Args:
            node (KnowledgeNode): The node to start the traversal from.

        Returns:
            list: A list of KnowledgeNode instances in the order they were visited.
        c                 b    j                  |        | j                         D ]
  } |        y rm   )rO   rW   )rc   rD   	_traversenodess     r   r   z.KnowledgeBase.traverse_down.<locals>._traverse  s/    LL&%224 !% !r   r5   )r   ro   r   r   s     @@r   traverse_downzKnowledgeBase.traverse_down  s     	!
 	$r   c                 V    g }|$|j                  |       |j                         }|$|S )z
        Traverses the tree upward from the given node.

        Args:
            node (KnowledgeNode): The node to start the traversal from.

        Returns:
            list: A list of KnowledgeNode instances in the order they were visited.
        )rO   rT   )r   ro   r   s      r   traverse_upzKnowledgeBase.traverse_up  s6     LL??$D  r   c                 >    g fd | j                          S )Nc                 Z    j                  |        | j                  D ]
  } |        y rm   )rO   r;   )ro   rD   _collectr   s     r   r   z1KnowledgeBase.collect_all_nodes.<locals>._collect  s)    LL   r   r_   )r   r   r   s    @@r   collect_all_nodeszKnowledgeBase.collect_all_nodes  s     	 
 	r   ry   c                 f    || j                   j                  ||      S |j                  ||      S )a  
        Inserts a new node into the knowledge base under the specified parent node.

        Args:
            new_node_name (str): The name of the new node.
            parent_node_name (str): The name of the parent node. If None, the new node is inserted under the root.
            duplicate_handling (str): How to handle duplicate nodes. Options are "skip", "none", and "raise error".
        )rI   )r_   rQ   )r   new_node_namery   rI   s       r   insert_nodezKnowledgeBase.insert_node  sL     99&&2D '   ((2D )  r   c                     |j                   |k(  r|S |j                         D ]  }| j                  ||      }||c S  y)a9  
        Finds a node by name in the knowledge base.

        Args:
            current_node (KnowledgeNode): The node to start the search from.
            node_name (str): The name of the node to find.

        Returns:
            KnowledgeNode: The node with the specified name, or None if not found.
        N)r8   rW   	find_node)r   rc   	node_namerD   results        r   r   zKnowledgeBase.find_node  sP     	)!..0 	E^^E95F!	 r   c                 |   i }|j                  d      D ]  }|j                  d      }|dkD  s|j                  d      j                         }|j                         dv rL|dk(  rdn|j	                  |dz
        }| j                  |||      }|||<   t        |j                               D ]  }	|	|kD  s	||	=   y)	aF  
        Creates and inserts nodes into the knowledge base from a string outline.

        Args:
            outline_string (str): The outline string where each line starts with '#' denoting the level.
            duplicate_handling (str): How to handle duplicate nodes. Options are "skip", "none", and "raise error".
        r   #r   z# )overviewsummaryintroductionr	   N)r   ry   rI   )r   countstriplowerrz   r   r"   keys)
r   r   rI   last_node_at_levellineleveltitlery   new_nodedeeper_levels
             r   insert_from_outline_stringz(KnowledgeBase.insert_from_outline_string  s      "((. 	=DJJsOEqy

4(..0;;=$KK&+qjd6H6L6LUUVY6W++"' +'9 , 
 -5"5)$();)@)@)B$C =L#e+.|<=	=r   cited_indicesc           
         d }t               t               D ]  } || j                  |      D ]  }	j                  dj                  |	j	                                      j                  |	       j                  |	j                                |	j                         }
|
D ]  }j                  |j                          j                  |
         fdfdf
dg }A| j                  5| j                  j                  D ]  }|j                   |d              n|j                   d             dj                  |      S )	Nc                 .    g fd |        S )a  
            Traverses the tree downward from the given node.

            Args:
                node (KnowledgeNode): The node to start the traversal from.

            Returns:
                list: A list of KnowledgeNode instances in the order they were visited.
            c                     | | j                   v rj                  |        | j                         D ]
  } |        y rm   )r9   rO   rW   )rc   rD   r   indexr   s     r   r   z[KnowledgeBase.get_node_hierarchy_string.<locals>.find_node_contain_index.<locals>._traverse1  sB    +9M9M0MLL.)668 %Ee$%r   r5   )ro   r   r   r   s    `@@r   find_node_contain_indexzHKnowledgeBase.get_node_hierarchy_string.<locals>.find_node_contain_index%  s     E% dOLr   r   c                     y| v S rg   r5   )ro   r   nodes_to_includes    r   should_include_nodezDKnowledgeBase.get_node_hierarchy_string.<locals>.should_include_nodeG  s    $+++r   c                 B    y| j                   D ]  } |      s y y)NFTrV   )ro   rD   r   r   s     r   should_omit_child_nodeszHKnowledgeBase.get_node_hierarchy_string.<locals>.should_omit_child_nodesL  s/    $ !&u- ! r   c                   
 g }|  |       }sdnd|dz
  z  }dj                  | j                              }s| j                  n|}rd|z  dz   nd}rdt        | j                         d	nd}|vrdnd
}	|r|j                  | | | | |	         |       r9t        | j                        dkD  rsdnd|z  x}
}|j                  |
 d       |S | j                  D ]  }|j                   ||dz                 |S )Nr   	r	   r   r   r    z (r\   u    ⭐r   z...)joinrd   r8   r]   r9   rO   r;   extend)cur_rootr   	to_returnshould_include_current_nodeindent	full_path	node_infohash_tagcontent_countspecial_notechild_indentrD   r   r}   r   r   r   include_node_content_countpaths_to_highlightr_   r   r   s               r   r}   z7KnowledgeBase.get_node_hierarchy_string.<locals>.helperT  sX   I#.A(.K+#1tuqy7I"KK(C(C(C(NO	1BHMM		0@3;,b5ObX--./q1UW 
 %,	AS0S   /$$!(8*YK|nU /x8x001A5*8denL6 &,,~S-AB  &.%6%6 GE%,,VE519-EFGr   r	   )r   r   )
r?   r_   rh   r   rd   rA   rr   ru   r;   r   )r   r   r   r   r   r   r_   r   r   cur_nodert   predecessorr   rD   r}   r   r   r   r   s    ``````       @@@@@r   r   z'KnowledgeBase.get_node_hierarchy_string  sN   	* !U5$& : 7		5 I :H&**6;;x7R7R7T+UV$((2$++H,H,H,JK#+#@#@#BL'3 F(//0D0DEF$++L9::	,
		 	B 	<DII1++ 9  Q!789 VD23yy##r   rb   c           	      V  	 |j                  d      }|| j                  n|}|dd D ]~  	t        	fd|j                  D        d      }|Y|dk(  r y|dk(  r|j	                  	      }|}E|dk(  sK| j                  d	d
d	      }t        d	 d|j                   d|       |} |S )aV  
        Returns the target node given a path string.

        Args:
            path (str): The path to the node, with node names connected by " -> ".
            missing_node_handling (str): How to handle missing nodes. Options are "abort", "create", and "raise error".

        Returns:
            KnowledgeNode: The target node.
        r   Nr	   c              3   B   K   | ]  }|j                   k(  s|  y wrm   r   ).0rD   r8   s     r   	<genexpr>z2KnowledgeBase.find_node_by_path.<locals>.<genexpr>  s     P5UZZ4=OPs   abortcreate)rE   rL   TF)r   r   r   z/Insert information error. Unable to find node {z	} under {z}
)r   r_   nextr;   rQ   r   rN   r8   )
r   rb   missing_node_handlingr_   
node_namesrc   
found_noder   	structurer8   s
            @r   find_node_by_pathzKnowledgeBase.find_node_by_path~  s      ZZ'
$(LtyydqrN 	*DPL$9$9PRVJ !(G3*h6+55d5KH#+L*m; $ > >'+*/)- !? !I
 $J4&P[\h\m\m[nnrs|r}~   *)	** r   informationc                    | j                   5  | j                  |||      }t        |      }|j                  dk(  rW| j                  j                  |t        | j                        dz         }||_        || j                  |<   || j                  |<   |^dj                  |j                               | j                  |j                     j                  d<   |j                  |j                         ddd       y# 1 sw Y   yxY w)a  
        Inserts information into the knowledge base at the specified path.

        Args:
            path (str): The placement path string, connected by " -> " linking the name of nodes.
            information (Information): The information to insert.
            missing_node_handling (str): How to handle missing nodes. Options are "abort", "create", and "raise error".
        Return:
            uuid of insertion information
        )rb   r   r_   ra   r	   Nr   	placement)r   r  r   citation_uuidr   rz   r]   r   r   rd   metari   )r   rb   r  r   r_   target_nodeinformation_hashinfo_citation_uuids           r   ri   z KnowledgeBase.insert_information  s   " ZZ 	J)-)?)?1FT *@ *K  $K0((B.%)%@%@%D%D$c$*E*E&F&J&" -?)@R++,<=BM++,>?& KK > > @A ++K,E,EFKK ..{/H/HI!	J 	J 	Js   C&C<<Dc                     fd	 t        | j                               } | j                         t        | j                               }||k(  ryK)z
        Trims all leaf nodes that do not have any content. Iteratively does it until all leaf nodes have at least one content.
        c                     | j                   s| j                  sy| j                   D cg c]  } |      r| c}| _         | j                    xr | j                   S c c}w rg   )r;   r9   )ro   rD   	trim_nodes     r   r  z6KnowledgeBase.trim_empty_leaf_nodes.<locals>.trim_node  sO    ==04VuYuEUUVDM}}$9T\\)99 Ws
   AAN)r]   get_all_leaf_nodesr_   )r   before_trim
after_trimr  s      @r   trim_empty_leaf_nodesz#KnowledgeBase.trim_empty_leaf_nodes  sO    
	: d5578Kdii T4467Jj( r   c                 >    g fd | j                          S )z
        Helper function to get all leaf nodes.

        Returns:
            List[KnowledgeNode]: A list of all leaf nodes in the knowledge base.
        c                 r    | j                   sj                  |        | j                   D ]
  } |        y rm   rn   )ro   rD   find_leaf_nodes
leaf_nodess     r   r  z9KnowledgeBase.get_all_leaf_nodes.<locals>.find_leaf_nodes  s3    ==!!$' '&'r   r   )r   r  r  s    @@r   r  z KnowledgeBase.get_all_leaf_nodes  s#     
	' 			"r   c                 4    fd | j                          y)z
        Merges content of a node with its single child and removes the child node.
        Iteratively does this from leaf nodes back to the root.
        c                 $   | j                   D ]
  } |        t        | j                         dk(  r^| j                   d   }| j                  j                  |j                         |j                   | _         | j                   D ]	  }| |_         y y )Nr	   r   )r;   r]   r9   rA   r:   )ro   rD   single_child
grandchild
merge_nodes       r   r  z:KnowledgeBase.merge_single_child_nodes.<locals>.merge_node  s     "5!" 4==!Q&#}}Q/##L$8$89 , 5 5"&-- -J(,J%-	 'r   Nr   )r   r  s    @r   merge_single_child_nodesz&KnowledgeBase.merge_single_child_nodes  s    	- 	499r   c                 8      fd  j                          y )Nc                     | j                   D ];  }dj                  | j                               j                  |   j                  d<   = | j
                  D ]
  } |        y )Nr   r  )r9   r   rd   r   r  r;   )ro   citation_idxrD   _helperr   s      r   r   z3KnowledgeBase.update_all_info_path.<locals>._helper  sb     $ KK 7 7 9: ++L9>>{K  r   r   )r   r   s   `@r   update_all_info_pathz"KnowledgeBase.update_all_info_path  s    	 			r   	conv_turnallow_create_new_nodeinsert_under_rootc                 z   |y t        |j                  j                               }|r/|D ])  }| j                  | j                  j
                  |       + n| j                  | ||       |j                  j                         D ci c]  \  }}||j                   }}}|j                         D ]W  \  }}|j                  j                  d| dd| d      |_	        |j                  j                  d| dd| d      |_        Y |j                         D ]  \  }	}|j                  j                  d| dd| d      |_	        |j                  j                  dd       |j                  j                  d| dd| d      |_        |j                  j                  dd        d |_        y c c}}w )	N)rb   r  )r   r  r#  []z[_z_]z[-1]r   )r"   r   valuesri   r_   r8   r   r   r  r   r   r   )
r   r"  r#  r$  info_to_insertr*   old_idxold_to_new_citation_idx_mappingnew_idx_s
             r   update_from_conv_turnz#KnowledgeBase.update_from_conv_turn  s    i2299;<& O''TYY^^'NO **#*&; +  "+!5!5!;!;!=+
 T'''+
' +

 !@ E E G 	GW"+"5"5"="=G9A"WIR 0#I '0&=&=&E&EG9A"WIR 0'I#		 :??A 	8JAw"+"5"5"="=WIR AgYa.#I ''3&/&=&=&E&EWIR AgYa.'I# ##++FB7	8  $	++
s   	F7c                 $    | j                  |       S rm   )r   rS   s    r   get_knowledge_base_summaryz(KnowledgeBase.get_knowledge_base_summary9  s    &&t,,r   c                     | j                          | j                          | j                  |        | j                          | j                          | j                          y)a  
        Reorganizes the knowledge base through two main processes: top-down expansion and bottom-up cleaning.

        The reorganization process ensures that the knowledge base remains well-structured and relevant as new information is added. It consists of the following steps:
        1.Top-Down Expansion: Expands nodes that have accumulated significant amounts of information by creating subtopics,
          ensuring that each concept remains specific and manageable.
        2.Bottom-Up Cleaning: Cleans the knowledge base by removing empty leaf nodes (nodes with no supporting information)
          and merging nodes that have only a single child, simplifying the structure and maintaining clarity.
        r   N)r  r  r   r!  rS   s    r   
reogranizezKnowledgeBase.reogranize<  sQ     	""$%%'t4""$%%'!!#r   c                 &    | j                  |       S )Nr2  )r   rS   s    r   	to_reportzKnowledgeBase.to_reportP  s    --T-BBr   rm   )NrK   r~   )FFTFNN)r   N)FF),r/   r0   r1   r2   r3   r   dspydspLMHFModelr$   r
   r   r)   r4   r   r-   r   r7   r   r   ndarrayr   r   r   r   r   r   r   r   r   r  r   ri   r  r  r  r!  r   r   r.  r0  r3  r5  r5   r   r   r   r   #  s     4&4& !dhh.>.>!>?4& '*	4&
 4&l	
  !dhh.>.>!>? '*	
  4 /3
]+
	rzz49$	%
4( 	 04!	 m,.&=8 #(-1(,`$  S	*`$ }%`$ 
`$J &(,	(( }%	(\ &(,!J!J !!J
 }%!JF&$*	 ',"'	'$#'$  $'$  	'$R-$(Cr   r   )r6  numpyr   r    r   typingr   r   r   r   r   r   r   r
   	interfacer   r   r7   r   r5   r   r   <module>r>     sE      	  : :  "H
 H
VJ! J!ZnC nCr   