core.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 Teacher class.

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 MultiTaskTeacher.
class parlai.core.agents.Agent(opt, shared=None)

Base class for all other agents.

observe(observation)

Receive an observation/action dict.

act()

Return an observation/action dict based upon given observation.

save()

If applicable, save any parameters needed to recreate this agent from loaded parameters.

share()

If applicable, share any parameters needed to create a shared version of this agent.

shutdown()

Perform any final cleanup if needed.

class parlai.core.agents.Teacher(opt, shared=None)

Basic Teacher agent which keeps track of how many times it’s received messages. Teachers provide the report() method to get back metrics.

__iter__()

Teacher can be iterated over. Subclasses can specify a certain length of iteration, such as e.g. one epoch.

__next__()

Raise StopIteration if epoch is done (never for default teacher).

share()

In addition to default Agent shared parameters, share metrics.

class parlai.core.agents.MultiTaskTeacher(opt, shared=None)

Creates a teacher that is actually a set of teachers each based on a task string–each of these teachers will get called in turn, either randomly or in order. They are all in the same world (they are the same agent switching tasks).

The task string format is described for the create_task_agents() function above.

shutdown()

Shutdown each agent.

parlai.core.agents.create_agent(opt)

Create an agent from the options model, model_params and model_file. 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_baseline which assumes the path above, and a class name suffixed with ‘Agent’.

parlai.core.agents.create_task_agent_from_taskname(opt)

Creates task agent(s) assuming the input task_dir:teacher_class.

e.g. def_string is a shorthand path like babi:Task1k:1 or #babi or a complete path like parlai.tasks.babi.agents:Task1kTeacher:1, which essentially performs from parlai.tasks.babi import Task1kTeacher with the parameter 1 in opt['task'] to be used by the class Task1kTeacher.