def get_relevant_keys(agent_key, all_keys_for_role, ontology, direction):
    """Return relevant agent keys for an agent key for refinement finding.

    Parameters
    ----------
    agent_key : tuple or None
        An agent key of interest.
    all_keys_for_role : set
        The set of all agent keys in a given statement corpus with a
        role matching that of the given agent_key.
    ontology : indra.ontology.IndraOntology
        An IndraOntology instance with respect to which relevant other
        agent keys are found for the purposes of refinement.
    direction: str
        The direction in which to find relevant agents. The two options
        are 'less_specific' and 'more_specific' for agents that are less and
        more specific, per the ontology, respectively.

    Returns
    -------
    set
        The set of relevant agent keys which this given agent key can
        possibly refine.
    """
    rel_fun = ontology.get_parents if direction == 'less_specific' else \
        ontology.get_children
    relevant_keys = {None, agent_key}
    if agent_key is not None:
        relevant_keys |= set(rel_fun(*agent_key))
    relevant_keys &= all_keys_for_role
    return relevant_keys