Skip to content

Commit

Permalink
Add ContextProcessorComponent, a privileged component that processes …
Browse files Browse the repository at this point in the history
…context from regular components for any of its phases.

PiperOrigin-RevId: 648677475
Change-Id: I18fa30af87a47c38d7c7fcf6e5a42cdcfabc834b
  • Loading branch information
duenez authored and copybara-github committed Jul 2, 2024
1 parent f85d4e6 commit 4793bf5
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
14 changes: 14 additions & 0 deletions concordia/components/agent/v2/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2023 DeepMind Technologies Limited.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

31 changes: 31 additions & 0 deletions concordia/components/agent/v2/no_op_context_processor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2023 DeepMind Technologies Limited.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""A context processor component that does nothing."""

from concordia.typing import component_v2

from typing_extensions import override


class NoOpContextProcessor(component_v2.ContextProcessorComponent):
"""A context processor component that does nothing."""

@override
def process(
self,
phase: component_v2.Phase,
contexts: component_v2.ComponentContextMapping,
) -> None:
del phase, contexts
52 changes: 51 additions & 1 deletion concordia/typing/component_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,43 @@

import abc
from collections.abc import Mapping
from concordia.typing import entity as entity_lib
import enum

from concordia.typing import entity as entity_lib

ComponentName = str
ComponentContext = str
ComponentContextMapping = Mapping[ComponentName, ComponentContext]


class Phase(enum.Enum):
"""Phases of the agent lifecycle.
Attributes:
INIT: The agent has just been created. No action has been requested nor
observation has been received. This can be followed by a call to `pre_act`
or `pre_observe`.
PRE_ACT: The agent has received a request to act. Components are being
requested for their action context. This will be followed by `POST_ACT`.
POST_ACT: The agent has just submitted an action attempt. Components are
being informed of the action attempt. This will be followed by
`UPDATE`.
PRE_OBSERVE: The agent has received an observation. Components are being
informed of the observation. This will be followed by `POST_OBSERVE`.
POST_OBSERVE: The agent has just observed. Components are given a chance to
provide context after processing the observation. This will be followed by
`UPDATE`.
UPDATE: The agent is about to update its internal state. This will be
followed by `PRE_ACT` or `PRE_OBSERVE`.
"""
INIT = enum.auto()
PRE_ACT = enum.auto()
POST_ACT = enum.auto()
PRE_OBSERVE = enum.auto()
POST_OBSERVE = enum.auto()
UPDATE = enum.auto()


class BaseComponent:
"""A base class for components."""

Expand Down Expand Up @@ -162,3 +191,24 @@ def get_action_attempt(
The action that the entity is attempting.
"""
raise NotImplementedError()


class ContextProcessorComponent(BaseComponent, metaclass=abc.ABCMeta):
"""A component that processes context from components."""

@abc.abstractmethod
def process(
self,
phase: Phase,
contexts: ComponentContextMapping,
) -> None:
"""Processes the context from components.
This function will be called by the entity with the context from other
components. The component should process the context and possibly update its
internal state or access other components.
Args:
phase: The phase in which the context is being processed.
contexts: The context from other components.
"""

0 comments on commit 4793bf5

Please sign in to comment.