class_docstr: Represents a LLaMA chatbot.
class_docstr_tok: ["Represents","a","LLaMA","chatbot","."]
code: def _encode_dialog(
      self, dialog: ChatDialog, param_name: str
  ) -> Tensor:
      if len(dialog) == 0:
          raise ValueError(
              f"`{param_name}` must have at least one message with the role 'user'."
          )
  
      if dialog[-1].role != "user":
          raise ValueError(
              f"The last message of `{param_name}` must have the role 'user'."
          )
  
      # Merge the system message, if any, with the first user message.
      if dialog[0].role == "system":
          content = f"<<SYS>>\n{dialog[0].content}\n<</SYS>>\n\n{dialog[1].content}"
  
          first_message = ChatMessage(dialog[1].role, content)
  
          dialog = [first_message] + list(dialog[2:])
  
      dialog_contents: List[Tensor] = []
  
      for user, bot in zip(dialog[::2], dialog[1::2]):
          if user.role != "user" or bot.role != "bot":
              raise ValueError(
                  f"The messages of `{param_name}` might optionally start with the role 'system', and then must alternate between the roles 'user' and 'bot'."
              )
  
          user_bot_seq = self.text_encoder(
              f"[INST] {user.content.strip()} [/INST] {bot.content.strip()}"
          )
  
          dialog_contents += [self.bos_idx, user_bot_seq, self.eos_idx]
  
      user_seq = self.text_encoder(
          f"[INST] {dialog[-1].content.strip()} [/INST]"
      )
  
      dialog_contents += [self.bos_idx, user_seq]
  
      return torch.cat(dialog_contents, dim=0)
  
file_path: fairseq2/src/fairseq2/models/llama/chat.py
imports: from typing import List, final
  import torch
  from torch import Tensor
  from fairseq2.data.text import TextTokenEncoder
  from fairseq2.generation import Chatbot, ChatDialog, ChatMessage, SequenceGenerator
  from fairseq2.models.llama.tokenizer import LLaMATokenizer
  from fairseq2.nn.utils.module import infer_device
  from fairseq2.typing import finaloverride
label_desc: Use image node_id 2 for calling the LLaMAChatbot obj's underlying member method code with example usage: obj._encode_dialog(dialog, param_name) and returns: torch