code: def make_eval_fn_with_agent(
      agent_type,
      eval_mode,
      planner_hparams,
      model_dir,
      log_every_steps=None,
      video_writers=(),
      random_starts_step_limit=None,
  ):
      """Returns an out-of-graph eval_fn using the Agent API."""
  
      def eval_fn(
          env, loop_hparams, policy_hparams, policy_dir, sampling_temp
      ):
          """Eval function."""
          base_env = env
          env = rl_utils.BatchStackWrapper(
              env, loop_hparams.frame_stack_size
          )
          agent = make_agent_from_hparams(
              agent_type,
              base_env,
              env,
              loop_hparams,
              policy_hparams,
              planner_hparams,
              model_dir,
              policy_dir,
              sampling_temp,
              video_writers,
          )
  
          if eval_mode == "agent_simulated":
              real_env = base_env.new_like(batch_size=1)
              stacked_env = rl_utils.BatchStackWrapper(
                  real_env, loop_hparams.frame_stack_size
              )
              collect_frames_for_random_starts(
                  real_env,
                  stacked_env,
                  agent,
                  loop_hparams.frame_stack_size,
                  random_starts_step_limit,
                  log_every_steps,
              )
              initial_frame_chooser = (
                  rl_utils.make_initial_frame_chooser(
                      real_env,
                      loop_hparams.frame_stack_size,
                      simulation_random_starts=True,
                      simulation_flip_first_random_for_beginning=False,
                      split=None,
                  )
              )
              env_fn = rl.make_simulated_env_fn_from_hparams(
                  real_env,
                  loop_hparams,
                  batch_size=loop_hparams.eval_batch_size,
                  initial_frame_chooser=initial_frame_chooser,
                  model_dir=model_dir,
              )
              sim_env = env_fn(in_graph=False)
              env = rl_utils.BatchStackWrapper(
                  sim_env, loop_hparams.frame_stack_size
              )
  
          kwargs = {}
          if not agent.records_own_videos:
              kwargs["video_writers"] = video_writers
          step_limit = base_env.rl_env_max_episode_steps
          if step_limit == -1:
              step_limit = None
          rl_utils.run_rollouts(
              env,
              agent,
              env.reset(),
              log_every_steps=log_every_steps,
              step_limit=step_limit,
              **kwargs,
          )
          if eval_mode == "agent_real":
              assert (
                  len(base_env.current_epoch_rollouts())
                  == env.batch_size
              )
  
      return eval_fn
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import datetime
  import os
  from tensor2tensor.data_generators import gym_env
  from tensor2tensor.layers import common_video
  from tensor2tensor.models.research import rl
  from tensor2tensor.rl import rl_utils
  from tensor2tensor.rl import trainer_model_based_params
  from tensor2tensor.utils import flags
  from tensor2tensor.utils import hparam
  from tensor2tensor.utils import registry
  from tensor2tensor.utils import trainer_lib
  import tensorflow.compat.v1
label_desc: Use image node_id 18 for calling a global function with example usage: make_eval_fn_with_agent(agent_type, eval_mode, planner_hparams, model_dir, log_every_steps, video_writers, random_starts_step_limit) and returns: eval_fn