class_docstr: Basic tests on the util functions in TorchAgent.
class_docstr_tok: ["Basic","tests","on","the","util","functions","in","TorchAgent","."]
docstr: Test different dialog history settings.
docstr_tok: ["Test","different","dialog","history","settings","."]
code: def test_history(self):
      """
      Test different dialog history settings.
      """
      # try with unlimited history
      agent = get_agent(history_size=-1)
      obs = {
          "text": "I am Groot.",
          "labels": ["I am Groot?"],
          "episode_done": False,
      }
  
      # first exchange
      agent.history.update_history(obs)
      text = agent.history.get_history_str()
      self.assertEqual(text, "I am Groot.")
  
      # second exchange, no reply
      agent.history.update_history(obs)
      text = agent.history.get_history_str()
      self.assertEqual(text, "I am Groot.\nI am Groot.")
  
      # include reply
      agent.history.add_reply("I am Groot?")
      agent.history.update_history(obs)
      text = agent.history.get_history_str()
      self.assertEqual(
          text, "I am Groot.\nI am Groot.\nI am Groot?\nI am Groot."
      )
  
      # on reset should be same as first exchange
      agent.history.reset()
      agent.history.update_history(obs)
      text = agent.history.get_history_str()
      self.assertEqual(text, "I am Groot.")
  
      # now try with history size = 1
      agent = get_agent(history_size=1)
      # first exchange
      agent.history.update_history(obs)
      text = agent.history.get_history_str()
      self.assertEqual(text, "I am Groot.")
      # second exchange should change nothing
      agent.history.update_history(obs)
      text = agent.history.get_history_str()
      self.assertEqual(text, "I am Groot.")
      # third exchange with reply should change nothing
      agent.history.update_history(obs)
      text = agent.history.get_history_str()
      self.assertEqual(text, "I am Groot.")
      # now if we add the reply, we should only have the reply left
      agent.history.add_reply(obs["labels"][0])
      text = agent.history.get_history_str()
      self.assertEqual(text, "I am Groot?")
  
      # now try with history size = 2
      agent = get_agent(history_size=2)
  
      # first exchange
      agent.history.update_history(obs)
      text = agent.history.get_history_str()
      self.assertEqual(text, "I am Groot.")
  
      # second exchange with reply should contain reply
      agent.history.add_reply("I am Groot?")
      agent.history.update_history(obs)
      text = agent.history.get_history_str()
      self.assertEqual(text, "I am Groot?\nI am Groot.")
  
      # now try with history size = 3
      agent = get_agent(history_size=3)
      # first exchange
      agent.history.update_history(obs)
      text = agent.history.get_history_str()
      self.assertEqual(text, "I am Groot.")
      # second exchange with reply should contain reply and input
      agent.history.add_reply("I am Groot?")
      agent.history.update_history(obs)
      text = agent.history.get_history_str()
      self.assertEqual(text, "I am Groot.\nI am Groot?\nI am Groot.")
  
      # now test add_person_tokens
      agent = get_agent(history_size=3, person_tokens=True)
      agent.history.update_history(obs)
      text = agent.history.get_history_str()
      self.assertEqual(text, "{} I am Groot.".format(agent.P1_TOKEN))
      # second exchange, history should still contain the tokens
      agent.history.add_reply("I am Groot?")
      agent.history.update_history(obs)
      text = agent.history.get_history_str()
      self.assertEqual(
          text,
          "{} I am Groot.\n{} I am Groot?\n{} I am Groot.".format(
              agent.P1_TOKEN, agent.P2_TOKEN, agent.P1_TOKEN
          ),
      )
  
      # now add add_p1_after_newln
      agent = get_agent(
          history_size=3, person_tokens=True, add_p1_after_newln=True
      )
      ctx_obs = obs.copy()  # context then utterance in this text field
      ctx_obs["text"] = "Groot is Groot.\nI am Groot."
      agent.history.update_history(ctx_obs)
      text = agent.history.get_history_str()
      self.assertEqual(
          text, "Groot is Groot.\n{} I am Groot.".format(agent.P1_TOKEN)
      )
      # second exchange, history should still contain context text
      agent.history.add_reply("I am Groot?")
      agent.history.update_history(obs)
      text = agent.history.get_history_str()
      self.assertEqual(
          text,
          "Groot is Groot.\n{} I am Groot.\n{} I am Groot?\n{} I am Groot.".format(
              agent.P1_TOKEN, agent.P2_TOKEN, agent.P1_TOKEN
          ),
      )
  
      # test history vecs
      agent.history.reset()
      agent.history.update_history(obs)
      vec = agent.history.get_history_vec()
      self.assertEqual(vec, [2001, 1, 2, 3])
  
      # test history vec list
      agent.history.update_history(obs)
      vecs = agent.history.get_history_vec_list()
      self.assertEqual(vecs, [[2001, 1, 2, 3], [2001, 1, 2, 3]])
  
      # test clearing history
      agent.history.reset()
      text = agent.history.get_history_str()
      self.assertIsNone(text)
      vecs = agent.history.get_history_vec_list()
      self.assertEqual(vecs, [])
  
      # test delimiter
      agent = get_agent(history_size=-1, delimiter=" Groot! ")
      agent.history.update_history(obs)
      agent.history.update_history(obs)
      text = agent.history.get_history_str()
      self.assertEqual(text, "I am Groot. Groot! I am Groot.")
  
      # test global_end_token, this will append a selected token to the end
      # of history block
      agent = get_agent(history_add_global_end_token="end")
      agent.history.reset()
      agent.history.update_history(obs)
      vec = agent.history.get_history_vec()
      self.assertEqual(vec, [1, 2, 3, MockDict.END_IDX])
  
      # test temp history
      agent = get_agent(
          history_size=-1,
          include_temp_history=True,
          delimiter="__delim__",
      )
      agent.history.reset()
      agent.history.update_history(obs, temp_history=" temp history")
      text = agent.history.get_history_str()
      self.assertEqual(text, "I am Groot. temp history")
      vec = agent.history.get_history_vec()
      self.assertEqual(vec, [1, 2, 3, 1, 2])
  
      agent.history.update_history(obs, temp_history=" temp history")
      text = agent.history.get_history_str()
      self.assertEqual(
          text, "I am Groot.__delim__I am Groot. temp history"
      )
      vecs = agent.history.get_history_vec_list()
      self.assertEqual(vecs, [[1, 2, 3], [1, 2, 3]])
      vec = agent.history.get_history_vec()
      self.assertEqual(vec, [1, 2, 3, 1, 1, 2, 3, 1, 2])
  
imports: import os
  import unittest
  from parlai.core.agents import create_agent_from_shared
  from parlai.utils.testing import tempdir
  from parlai.utils.misc import Message
label_desc: Use image node_id 9 for calling the TestTorchAgent obj's underlying member method code with example usage: obj.test_history() without return types