Skip to content

Commit

Permalink
Add a component class that allows access to its pre_act context dur…
Browse files Browse the repository at this point in the history
…ing the `PRE_ACT` phase.

This is achieved by making the context ignore the action spec, and computing its context only once during the first time it is requested. Components that want their `pre_act` context available to other components should derive from this class.

PiperOrigin-RevId: 650012324
Change-Id: Ic63aaa84a997fa6ddd42ce01567e1bb8d2a697ab
  • Loading branch information
duenez authored and Copybara-Service committed Jul 7, 2024
1 parent 40b0093 commit 9154bc6
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 9 deletions.
1 change: 1 addition & 0 deletions concordia/components/agent/v2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

"""Library of components specifically for generative agents."""

from concordia.components.agent.v2 import action_spec_ignored
from concordia.components.agent.v2 import constant
from concordia.components.agent.v2 import legacy_act_component
from concordia.components.agent.v2 import no_op_context_processor
Expand Down
63 changes: 63 additions & 0 deletions concordia/components/agent/v2/action_spec_ignored.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# 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 component that ignores the action spec in the `pre_act` method."""

import abc

from concordia.typing import component_v2
from concordia.typing import entity as entity_lib


class ActionSpecIgnored(component_v2.EntityComponent, metaclass=abc.ABCMeta):
"""A component that ignores the action spec in the `pre_act` method.
As a consequence, its `pre_act` state can be accessed safely by other
components. This is useful for components that need to condition their
`pre_act` state on the state of other components. Derived classes should
implement `make_pre_act_context` instead of `pre_act`. The pre_act context
will be cached and returned by `get_pre_act_context` and `pre_act`, and
cleaned up by `update`.
"""

def __init__(self):
"""Initializes the component."""
self._pre_act_context: str | None = None

@abc.abstractmethod
def make_pre_act_context(self) -> str:
"""Creates the pre-act context."""
raise NotImplementedError()

def set_pre_act_context(self, pre_act_context: str) -> None:
"""Creates the pre-act context."""
if self._pre_act_context is not None:
raise ValueError('pre_act_context is already set.')
self._pre_act_context = pre_act_context

def get_pre_act_context(self) -> str:
"""Creates the pre-act context."""
if self._pre_act_context is None:
self._pre_act_context = self.make_pre_act_context()
return self._pre_act_context

def pre_act(
self,
action_spec: entity_lib.ActionSpec,
) -> str:
del action_spec
return self.get_pre_act_context()

def update(self) -> None:
self._pre_act_context = None
16 changes: 7 additions & 9 deletions concordia/components/agent/v2/observation.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
from collections.abc import Callable
import datetime
from concordia.associative_memory import associative_memory
from concordia.typing import component_v2
from concordia.typing import entity as entity_lib
from concordia.components.agent.v2 import action_spec_ignored

import overrides

class Observation(component_v2.EntityComponent):
"""A simple component to receive observations.
"""

class Observation(action_spec_ignored.ActionSpecIgnored):
"""A simple component to receive observations."""

def __init__(
self,
Expand All @@ -45,10 +45,8 @@ def pre_observe(
)
return ''

def pre_act(
self,
unused_action_spec: entity_lib.ActionSpec,
) -> str:
@overrides.overrides
def make_pre_act_context(self) -> str:
mems = self._memory.retrieve_time_interval(
self._clock_now() - self._timeframe, self._clock_now(), add_time=True
)
Expand Down

0 comments on commit 9154bc6

Please sign in to comment.