code: def interactive_loop(model, babi):
      """
      Loop used to interact with trained conversational agent with access to knowledge base API
      """
      context = []
      response = None
      time_feat = 1
      interactive_output = None
      db_results = []
      allow_oov = False
  
      print("Building database...")
      print_help()
      db, names_to_idxs, kb_text = build_kb_db(babi)
  
      while True:
          line_in = text_input(">>> ").strip().lower()
          if not line_in:
              line_in = "<SILENCE>"
          if line_in in ("exit", "quit"):
              break
          if line_in == "help":
              print_help()
              continue
          if line_in in ("restart", "clear"):
              context = []
              response = None
              time_feat = 1
              interactive_output = None
              db_results = []
              print("Memory cleared")
              continue
          if line_in == "vocab":
              print_human_vocab(babi)
              continue
          if line_in == "show_memory":
              print_memory(context)
              continue
          if line_in == "allow_oov":
              allow_oov = not allow_oov
              print("Allow OOV = {}".format(allow_oov))
              continue
          if "api_call" in line_in:
              db_results = issue_api_call(
                  line_in, db, names_to_idxs, kb_text, babi
              )
  
          old_context = copy(context)
          (
              user_utt,
              context,
              memory,
              cands_mat,
              time_feat,
          ) = babi.process_interactive(
              line_in, context, response, db_results, time_feat
          )
  
          if (
              babi.word_to_index["<OOV>"] in user_utt
              and allow_oov is False
          ):
              oov_word = line_in.split(" ")[
                  list(user_utt).index(babi.word_to_index["<OOV>"])
              ]
              print(
                  'Sorry, "{}" is outside my vocabulary. '.format(
                      oov_word
                  )
                  + "Please say 'allow_oov' to toggle OOV words"
                  + ", or 'help' for more commands."
              )
              context = old_context
              continue
  
          interactive_output = model.predict(
              np.expand_dims(memory, 0),
              np.expand_dims(user_utt, 0),
              np.expand_dims(cands_mat, 0),
          )
          pred_cand_idx = interactive_output[0]
          response = babi.candidate_answers[pred_cand_idx]
  
          print(response)
  
          if "api_call" in response:
              db_results = issue_api_call(
                  response, db, names_to_idxs, kb_text, babi
              )
          else:
              db_results = []
  
file_path: nlp-architect/examples/memn2n_dialogue/interactive_utils.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  from __future__ import unicode_literals
  import itertools
  from builtins import input
  from copy import copy
  from functools import reduce
  import numpy
label_desc: Use image node_id 1 for calling a global function with example usage: interactive_loop(model, babi) without return types