class_docstr: Transformers handler class for sequence, token classification and question answering.
class_docstr_tok: ["Transformers","handler","class","for","sequence",",","token","classification","and","question","answering","."]
code: def _prepare_dialog(self, input_data):
      dialog = input_data["dialog"]
      if dialog[0]["role"] == "system":
          dialog = [
              {
                  "role": dialog[1]["role"],
                  "content": B_SYS
                  + dialog[0]["content"]
                  + E_SYS
                  + dialog[1]["content"],
              }
          ] + dialog[2:]
      assert all(
          [msg["role"] == "user" for msg in dialog[::2]]
      ) and all([msg["role"] == "assistant" for msg in dialog[1::2]]), (
          "model only supports 'system', 'user' and 'assistant' roles, "
          "starting with 'system', then 'user' and alternating (u/a/u/a/u...)"
      )
      dialog_tokens: List[int] = sum(
          [
              self.tokenizer.encode(
                  f"{B_INST} {(prompt['content']).strip()} {E_INST} {(answer['content']).strip()} ",
                  bos=True,
                  eos=True,
              )
              for prompt, answer in zip(
                  dialog[::2],
                  dialog[1::2],
              )
          ],
          [],
      )
      assert (
          dialog[-1]["role"] == "user"
      ), f"Last message must be from user, got {dialog[-1]['role']}"
      dialog_tokens += self.tokenizer.encode(
          f"{B_INST} {(dialog[-1]['content']).strip()} {E_INST}",
          bos=True,
          eos=False,
      )
      del input_data["dialog"]
      input_data["prompt"] = self.tokenizer.decode(dialog_tokens)
      input_data["encoded"] = torch.tensor(
          dialog_tokens, dtype=torch.long, device=self.device
      )
      return input_data
  
imports: import json
  import logging
  import os
  import sys
  import time
  from abc import ABC
  from typing import List
  import packaging.version
  import requests
  import torch
  import transformers
  from ts.torch_handler.base_handler import BaseHandler
  from generate import sample_top_p
  from llama2 import Llama
label_desc: Use image node_id 7 for calling the LlamaHandler obj's underlying member method code with example usage: obj._prepare_dialog(input_data) and returns: input_data