class_docstr: Stacked model that generates first the knowledge, and then the dialogue response.
class_docstr_tok: ["Stacked","model","that","generates","first","the","knowledge",",","and","then","the","dialogue","response","."]
code: def batch_act(self, observations):
      knowledge_agent_observations = [
          o["knowledge_agent"] for o in observations
      ]
      raw_observations = [o["raw"] for o in observations]
  
      # Get the knowledge replies.
      if self.knowledge_agent is None:
          self._init_knowledge_model()
      batch_reply_knowledge = self.knowledge_agent.batch_act(
          knowledge_agent_observations
      )
  
      if (
          "top_docs" in batch_reply_knowledge[0]
          and self.opts["init"]["use_supporting_sentence_as_knowledge"]
      ):
          # The knowledge agent is a rag-style model. Instead of the actual knowledge
          # response, we will use the best matching sentence from the retrieved docs
          # as the knowledge conditioning.
          for i, reply_knowledge in enumerate(batch_reply_knowledge):
              reply_knowledge[
                  "support_sentence"
              ] = find_supporting_sentence(
                  question=raw_observations[i]["text"],
                  answer=reply_knowledge["text"],
                  docs=reply_knowledge["top_docs"],
              )
  
      if self.dialogue_agent is None:
          self._init_dialogue_models()
  
      if self.dialogue_agent_no_knowledge:
          batch_reply_dialogue_no_knowledge = self.batch_dialogue_reply(
              self.dialogue_agent_no_knowledge, raw_observations
          )
      if self.dialogue_agent_rag_wiki:
          batch_reply_dialogue_rag_wiki = self.batch_dialogue_reply(
              self.dialogue_agent_rag_wiki, raw_observations
          )
  
      knowledge_infused_observations = (
          self.generate_knowledge_observation(
              knowledges=[
                  reply_knowledge.get("text", "")
                  for reply_knowledge in batch_reply_knowledge
              ],
              observations=raw_observations,
          )
      )
      batch_reply_dialogue = self.batch_dialogue_reply(
          self.dialogue_agent, knowledge_infused_observations
      )
  
      batch_reply_dialogue_knowledge_sentence = None
      if (
          self.opts["init"]["use_supporting_sentence_as_knowledge"]
          and "support_sentence" in batch_reply_knowledge[0]
      ):
          knowledge_sentence_infused_observations = (
              self.generate_knowledge_observation(
                  knowledges=[
                      reply_knowledge.get("support_sentence", "")
                      for reply_knowledge in batch_reply_knowledge
                  ],
                  observations=raw_observations,
              )
          )
          batch_reply_dialogue_knowledge_sentence = (
              self.batch_dialogue_reply(
                  self.dialogue_agent,
                  knowledge_sentence_infused_observations,
              )
          )
  
      for i in range(len(batch_reply_dialogue)):
          if batch_reply_knowledge and len(batch_reply_knowledge) > i:
              batch_reply_dialogue[i][
                  "knowledge_response"
              ] = batch_reply_knowledge[i].get("text", "")
              if "support_sentence" in batch_reply_knowledge[i]:
                  batch_reply_dialogue[i][
                      "support_sentence"
                  ] = batch_reply_knowledge[i].get(
                      "support_sentence", ""
                  )
          if (
              self.dialogue_agent_no_knowledge
              and batch_reply_dialogue_no_knowledge
              and len(batch_reply_dialogue_no_knowledge) > i
          ):
              batch_reply_dialogue[i][
                  "text_no_knowledge"
              ] = batch_reply_dialogue_no_knowledge[i].get("text", "")
  
          if (
              self.dialogue_agent_rag_wiki
              and batch_reply_dialogue_rag_wiki
              and len(batch_reply_dialogue_rag_wiki) > i
          ):
              batch_reply_dialogue[i][
                  "text_rag_wiki"
              ] = batch_reply_dialogue_rag_wiki[i].get("text", "")
  
          if (
              batch_reply_dialogue_knowledge_sentence
              and len(batch_reply_dialogue_knowledge_sentence) > i
          ):
              batch_reply_dialogue[i][
                  "text_knowledge_sentence"
              ] = batch_reply_dialogue_knowledge_sentence[i].get(
                  "text", ""
              )
  
      [
          self._filter_beams(
              reply=reply,
              filter_for_knowledge=self.opts["init"][
                  "beam_filter_for_knowledge_response"
              ],
              filter_questions=self.opts["init"][
                  "beam_filter_questions"
              ],
              filter_self_references=self.opts["init"][
                  "beam_filter_self_references"
              ],
          )
          for reply in batch_reply_dialogue
      ]
  
      return batch_reply_dialogue
  
imports: from copy import deepcopy
  from typing import Optional, List
  from types import MethodType
  from collections import defaultdict
  from nltk.tokenize import sent_tokenize
  from nltk.corpus import stopwords
  import random
  import spacy
  import torch
  from parlai.core.agents import create_agent, create_agent_from_shared
  from parlai.core.params import ParlaiParser
  from parlai.core.opt import Opt
  from parlai.core.message import Message
  from parlai.core.agents import Agent
  from parlai.core.metrics import F1Metric
  from parlai.agents.rag.args import setup_rag_args
  from parlai.agents.bart.bart import BartAgent
  from parlai.core.metrics import normalize_answer
  from parlai.tasks.wizard_of_wikipedia.agents import TOKEN_NOCHOSEN
  from parlai.tasks.wizard_of_wikipedia.agents import TOKEN_KNOWLEDGE, TOKEN_END_KNOWLEDGE
label_desc: Use image node_id 13 for calling the StackedKnowledgeDialogueAgent obj's underlying member method code with example usage: obj.batch_act(observations) and returns: batch_reply_dialogue