<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def leave_chat( self, chat_id: Union[int, str], delete: bool = False ):
 """Use this method to leave a group chat or channel. Args: chat_id (``int`` | ``str``):
 Unique identifier for the target chat or username of the target channel/supergroup (in the format @username). delete (``bool``, *optional*):
 Deletes the group chat dialog after leaving (for simple group chats, not supergroups). Raises: :class:`RPCError <pyrogram.RPCError>` in case of a Telegram RPC error. """

        peer = self.resolve_peer(chat_id)

        if isinstance(peer, types.InputPeerChannel):
            return self.send(
                functions.channels.LeaveChannel(
                    channel=self.resolve_peer(chat_id)
                )
            )
        elif isinstance(peer, types.InputPeerChat):
            r = self.send(
                functions.messages.DeleteChatUser(
                    chat_id=peer.chat_id,
                    user_id=types.InputPeerSelf()
                )
            )

            if delete:
                self.send(
                    functions.messages.DeleteHistory(
                        peer=peer,
                        max_id=0
                    )
                )

            return r