Common Abstract classes for many agents.
This module provides a set of basic agents:
Agent(object)base class for all other agents, implements the
observe()method which receives an observation/action dict and the
act()method which returns a dict in response.
Teacher(Agent)also implements the
report()method for returning metrics. All ParlAI tasks implement the
MultiTaskTeacher(Teacher)creates a set of teachers based on a task string passed to the
Teacher, creating multiple teachers within it and alternating between them.
All agents are initialized with the following parameters:
opt– contains any options needed to set up the agent. This generally contains all command-line arguments recognized from
core.params, as well as other options that might be set through the framework to enable certain modes.
shared(optional) – if not
None, contains any shared data used to construct this particular instantiation of the agent. This data might have been initialized by another agent, so that different agents can share the same data (possibly in different Processes).
This module also provides a utility method:
create_task_agents(str): instantiate task-specific agents (e.g. a teacher) from a given task string (e.g. ‘babi:task1k:1’ or ‘squad’). Used by
Agent(opt: parlai.core.opt.Opt, shared=None)¶
Base class for all other agents.
__init__(opt: parlai.core.opt.Opt, shared=None)¶
Initialize self. See help(type(self)) for accurate signature.
Receive an observation/action dict.
Return an observation/action dict based upon given observation.
Return the agent ID.
Return whether the epoch is done or not.
- Return type
Reset the agent, clearing its observation.
Many subclasses implement additional reset logic.
Reset any metrics reported by this agent.
This is called to indicate metrics should start fresh, and is typically called between loggings or after a report().
Save any parameters needed to recreate this agent from loaded parameters.
Default implementation is no-op, but many subagents implement this logic.
Make a shared copy of this agent.
Should be the same as using create_agent_from_shared(.), but slightly easier.
Share any parameters needed to create a shared version of this agent.
Default implementation shares the class and the opt, but most agents will want to also add model weights, teacher data, etc. This especially useful for avoiding providing pointers to large objects to all agents in a batch.
Perform any final cleanup if needed.
respond(text_or_message: Union[str, parlai.core.message.Message], **other_message_fields) → str¶
An agent convenience function which calls the act() and provides a string response to a text or message field.
Message] text_or_message (Union[str,) – A string for the ‘text’ field or a message which MUST comprise of the ‘text’ field apart from other fields.
other_message_fields (kwargs) – Provide fields for the message in the form of keyword arguments.
Agent’s response to the message.
- Return type
batch_respond(messages: List[parlai.core.message.Message]) → List[str]¶
An agent convenience function which calls the batch_act() and provides a batch response to a list of messages.
messages (List[Message]) – A list of messages each of which MUST comprise of the ‘text’ field apart from other fields.
Agent’s batch response to the messages.
- Return type
Upgrade legacy options when loading an opt file from disk.
This is primarily made available to provide a safe space to handle backwards-compatible behavior. For example, perhaps we introduce a new option today, which wasn’t previously available. We can have the argument have a new default, but fall back to the “legacy” compatibility behavior if the option doesn’t exist.
upgrade_optprovides an opportunity for such checks for backwards compatibility. It is called shortly after loading the opt file from disk, and is called before the Agent is initialized.
Other possible examples include:
Renaming an option,
Deprecating an old option,
Splitting coupled behavior, etc.
upgrade_optshould conform to high standards, due to the risk of these methods becoming complicated and difficult to reason about. We recommend the following behaviors:
upgrade_optshould only be used to provide backwards compatibility. Other behavior should find a different location. 2. Children should always call the parent’s
upgrade_optshould always warn when an option was overwritten. 4. Include comments annotating the date and purpose of each upgrade. 5. Add an integration test which ensures your old work behaves appropriately.
compare_init_model_opts(opt: parlai.core.opt.Opt, curr_opt: parlai.core.opt.Opt)¶
Print loud warning when init_model opts differ from previous configuration.
Load agent from model file if it exists.
opt_overrides – An optional dict of option overrides can also be provided.
Load agent options and module from file if opt file exists.
Checks to see if file exists opt[‘model_file’] + “.opt”; if so, load up the options from the file and use that to create an agent, loading the model type from that file and overriding any options specified in that file when instantiating the agent.
If that file does not exist, return None.
create_agent(opt: parlai.core.opt.Opt, requireModelExists=False)¶
Create an agent from the options
The input is either of the form
parlai.agents.ir_baseline.agents:IrBaselineAgent(i.e. the path followed by the class name) or else just
ir_baselinewhich assumes the path above, and a class name suffixed with ‘Agent’.
model-fileis available in the options this function can also attempt to load the model from that location instead. This avoids having to specify all the other options necessary to set up the model including its name as they are all loaded from the options file if it exists (the file opt[‘model_file’] + ‘.opt’ must exist and contain a pickled or json dict containing the model’s options).
Instantiate an agent from the default shared params.
shared_agent – should include an opt dictionary and agent class, along with whatever other parameters the agent needs to instantiate.
Create agents based on shared data.
shared – list of dict objects created by calling e.g. [a.share() for a in agents].
Returns a list of instantiated agents.