def __init__(self, text: str, tokens: Optional[torch.Tensor]):
        """
        **Overview:**
            Initialize TextHistory.
        **Arguments:**
            - text: The text of the first segment.
            - tokens: The tokens of the first segment.
        """
        if len(text) == 0:
            self.text, self.tokens = None, None
            return
        # Record the total text generated by both user and language model.
        self.text = text
        # Record the ranges of text for each reply.
        self.text_spans = []
        # Record the tokenized total text generated by both user and language model.
        self.tokens = tokens
        # This flag shows whether this record is finished.
        self.completed = False

        self.append_segment(text, tokens)

        **Overview:**
            Initialize TextHistory.
        **Arguments:**
            - text: The text of the first segment.
            - tokens: The tokens of the first segment.
        
chapter8_large/lm_env.py