class_docstr: Tensorforce agent interface.
class_docstr_tok: ["Tensorforce","agent","interface","."]
docstr: Create an agent from a specification.
  
  Args:
      agent (specification | Agent class/object | callable[states -> actions]): JSON file,
          specification key, configuration dictionary, library module, or `Agent`
          class/object. Alternatively, an act-function mapping states to actions which is
          supposed to be recorded.
          (<span style="color:#00C000"><b>default</b></span>: Tensorforce base agent).
      environment (Environment object): Environment which the agent is supposed to be trained
          on, environment-related arguments like state/action space specifications and
          maximum episode length will be extract if given
          (<span style="color:#C00000"><b>recommended</b></span>).
      kwargs: Additional agent arguments.
docstr_tok: ["Create","an","agent","from","a","specification",".","Args",":","agent","(","specification","|","Agent","class\/object","|","callable","[","states","-",">","actions","]",")",":","JSON","file",",","specification","key",",","configuration","dictionary",",","library","module",",","or","`","Agent","`","class\/object",".","Alternatively",",","an","act-function","mapping","states","to","actions","which","is","supposed","to","be","recorded",".","(","<","span","style=","''","color",":","#","00C000","''",">","<","b",">","default","<","\/b",">","<","\/span",">",":","Tensorforce","base","agent",")",".","environment","(","Environment","object",")",":","Environment","which","the","agent","is","supposed","to","be","trained","on",",","environment-related","arguments","like","state\/action","space","specifications","and","maximum","episode","length","will","be","extract","if","given","(","<","span","style=","''","color",":","#","C00000","''",">","<","b",">","recommended","<","\/b",">","<","\/span",">",")",".","kwargs",":","Additional","agent","arguments","."]
code: def create(agent="tensorforce", environment=None, **kwargs):
      """
      Create an agent from a specification.
  
      Args:
          agent (specification | Agent class/object | callable[states -> actions]): JSON file,
              specification key, configuration dictionary, library module, or `Agent`
              class/object. Alternatively, an act-function mapping states to actions which is
              supposed to be recorded.
              (<span style="color:#00C000"><b>default</b></span>: Tensorforce base agent).
          environment (Environment object): Environment which the agent is supposed to be trained
              on, environment-related arguments like state/action space specifications and
              maximum episode length will be extract if given
              (<span style="color:#C00000"><b>recommended</b></span>).
          kwargs: Additional agent arguments.
      """
      if isinstance(agent, Recorder):
          if environment is not None:
              # TODO:
              # assert agent.spec['states'] == environment.states()
              # assert agent.spec['actions'] == environment.actions()
              # assert environment.max_episode_timesteps() is None or \
              #     agent.spec['max_episode_timesteps'] >= environment.max_episode_timesteps()
              pass
  
          for key, value in kwargs.items():
              if key == "parallel_interactions":
                  assert agent.spec[key] >= value
              else:
                  assert agent.spec[key] == value
  
          if agent.is_initialized:
              agent.reset()
          else:
              agent.initialize()
  
          return agent
  
      elif (
          isinstance(agent, type) and issubclass(agent, Agent)
      ) or callable(agent):
          # Type specification, or Recorder
          if environment is not None:
              if "states" in kwargs:
                  # TODO:
                  # assert kwargs['states'] == environment.states()
                  pass
              else:
                  kwargs["states"] = environment.states()
              if "actions" in kwargs:
                  # assert kwargs['actions'] == environment.actions()
                  pass
              else:
                  kwargs["actions"] = environment.actions()
              if environment.max_episode_timesteps() is None:
                  pass
              elif "max_episode_timesteps" in kwargs:
                  # assert kwargs['max_episode_timesteps'] >= environment.max_episode_timesteps()
                  pass
              else:
                  kwargs[
                      "max_episode_timesteps"
                  ] = environment.max_episode_timesteps()
  
          if isinstance(agent, type) and issubclass(agent, Agent):
              agent = agent(**kwargs)
              assert isinstance(agent, Agent)
          else:
              if "recorder" not in kwargs:
                  raise TensorforceError.required(
                      name="Recorder", argument="recorder"
                  )
              agent = Recorder(fn_act=agent, **kwargs)
          return Agent.create(agent=agent, environment=environment)
  
      elif isinstance(agent, dict):
          # Dictionary specification
          agent.update(kwargs)
          kwargs = dict(agent)
          agent = kwargs.pop("agent", kwargs.pop("type", "default"))
  
          return Agent.create(
              agent=agent, environment=environment, **kwargs
          )
  
      elif isinstance(agent, str):
          if os.path.isfile(agent):
              # JSON file specification
              with open(agent, "r") as fp:
                  agent = json.load(fp=fp)
              return Agent.create(
                  agent=agent, environment=environment, **kwargs
              )
  
          elif agent in tensorforce.agents.agents:
              # Keyword specification
              agent = tensorforce.agents.agents[agent]
              return Agent.create(
                  agent=agent, environment=environment, **kwargs
              )
  
          else:
              # Library specification
              _agent = util.try_import_module(
                  module=agent, parent_class=Agent
              )
              if _agent is not None:
                  return Agent.create(
                      agent=_agent, environment=environment, **kwargs
                  )
  
              raise TensorforceError.value(
                  name="Agent.create", argument="agent", value=agent
              )
  
      else:
          raise TensorforceError.type(
              name="Agent.create", argument="agent", dtype=type(agent)
          )
  
imports: from collections import OrderedDict
  import json
  import logging
  import os
  import random
  import numpy
  import tensorflow
  from tensorforce import util, TensorforceError
  from tensorforce.agents import Recorder
  import tensorforce.agents
  from tensorforce.core import ArrayDict, TensorSpec, TensorforceConfig
label_desc: Use image node_id 1 for calling the Agent obj's underlying member method code with example usage: obj.create(agent, environment) and returns: agent, Agent, Agent, Agent, Agent, Agent