From b81c21909da3a132e2fc12a1ec1d2fbada21e48b Mon Sep 17 00:00:00 2001 From: Sasha Vezhnevets Date: Tue, 5 Dec 2023 09:31:16 -0800 Subject: [PATCH] moving components usable by both GM and agents to generic_components. Also changing the name of the component that reports the state returned by a callback PiperOrigin-RevId: 588098003 Change-Id: I15b80a4d23034377240a6fbbe070ad30c57e9a1e --- concordia/agents/components/__init__.py | 29 ------------------- concordia/components/__init__.py | 19 ++++++++++++ concordia/components/agent/__init__.py | 29 +++++++++++++++++++ .../agent}/characteristic.py | 0 .../agent}/identity.py | 2 +- .../agent}/observation.py | 0 .../agent}/person_by_situation.py | 0 .../components => components/agent}/plan.py | 0 .../agent}/reflection.py | 0 .../agent}/self_perception.py | 0 .../agent}/situation_perception.py | 0 .../agent}/somatic_state.py | 2 +- concordia/{agents => }/components/constant.py | 2 +- .../game_master}/__init__.py | 16 +++++----- .../game_master}/conversation.py | 7 +++-- .../game_master}/direct_effect.py | 0 .../game_master}/inventory.py | 0 .../game_master}/player_status.py | 0 .../game_master}/relevant_events.py | 0 .../game_master}/schedule.py | 0 .../game_master}/time_display.py | 0 .../report_function.py} | 19 ++++++------ .../{agents => }/components/sequential.py | 0 concordia/tests/concordia_integration_test.py | 15 +++++----- examples/three_key_questions.ipynb | 9 +++--- examples/village/day_in_riverbend.ipynb | 11 +++---- examples/village/riverbend_elections.ipynb | 18 +++++------- 27 files changed, 99 insertions(+), 79 deletions(-) delete mode 100644 concordia/agents/components/__init__.py create mode 100644 concordia/components/__init__.py create mode 100644 concordia/components/agent/__init__.py rename concordia/{agents/components => components/agent}/characteristic.py (100%) rename concordia/{agents/components => components/agent}/identity.py (97%) rename concordia/{agents/components => components/agent}/observation.py (100%) rename concordia/{agents/components => components/agent}/person_by_situation.py (100%) rename concordia/{agents/components => components/agent}/plan.py (100%) rename concordia/{agents/components => components/agent}/reflection.py (100%) rename concordia/{agents/components => components/agent}/self_perception.py (100%) rename concordia/{agents/components => components/agent}/situation_perception.py (100%) rename concordia/{agents/components => components/agent}/somatic_state.py (98%) rename concordia/{agents => }/components/constant.py (96%) rename concordia/{environment/components => components/game_master}/__init__.py (55%) rename concordia/{environment/components => components/game_master}/conversation.py (97%) rename concordia/{environment/components => components/game_master}/direct_effect.py (100%) rename concordia/{environment/components => components/game_master}/inventory.py (100%) rename concordia/{environment/components => components/game_master}/player_status.py (100%) rename concordia/{environment/components => components/game_master}/relevant_events.py (100%) rename concordia/{environment/components => components/game_master}/schedule.py (100%) rename concordia/{environment/components => components/game_master}/time_display.py (100%) rename concordia/{agents/components/report_state.py => components/report_function.py} (69%) rename concordia/{agents => }/components/sequential.py (100%) diff --git a/concordia/agents/components/__init__.py b/concordia/agents/components/__init__.py deleted file mode 100644 index 976554bf..00000000 --- a/concordia/agents/components/__init__.py +++ /dev/null @@ -1,29 +0,0 @@ -# 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. - - -"""Library of components for generative game master and agents.""" - -from concordia.agents.components import characteristic -from concordia.agents.components import constant -from concordia.agents.components import identity -from concordia.agents.components import observation -from concordia.agents.components import person_by_situation -from concordia.agents.components import plan -from concordia.agents.components import reflection -from concordia.agents.components import report_state -from concordia.agents.components import self_perception -from concordia.agents.components import sequential -from concordia.agents.components import situation_perception -from concordia.agents.components import somatic_state diff --git a/concordia/components/__init__.py b/concordia/components/__init__.py new file mode 100644 index 00000000..cb152df6 --- /dev/null +++ b/concordia/components/__init__.py @@ -0,0 +1,19 @@ +# 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. + +"""Library of components for both generative game master and agents.""" + +from concordia.components import constant +from concordia.components import report_function +from concordia.components import sequential diff --git a/concordia/components/agent/__init__.py b/concordia/components/agent/__init__.py new file mode 100644 index 00000000..9e09a3ae --- /dev/null +++ b/concordia/components/agent/__init__.py @@ -0,0 +1,29 @@ +# 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. + + +"""Library of components specifically for generative agents.""" + +from concordia.components import constant +from concordia.components import report_function +from concordia.components import sequential +from concordia.components.agent import characteristic +from concordia.components.agent import identity +from concordia.components.agent import observation +from concordia.components.agent import person_by_situation +from concordia.components.agent import plan +from concordia.components.agent import reflection +from concordia.components.agent import self_perception +from concordia.components.agent import situation_perception +from concordia.components.agent import somatic_state diff --git a/concordia/agents/components/characteristic.py b/concordia/components/agent/characteristic.py similarity index 100% rename from concordia/agents/components/characteristic.py rename to concordia/components/agent/characteristic.py diff --git a/concordia/agents/components/identity.py b/concordia/components/agent/identity.py similarity index 97% rename from concordia/agents/components/identity.py rename to concordia/components/agent/identity.py index 3fd2767a..6f694c78 100644 --- a/concordia/agents/components/identity.py +++ b/concordia/components/agent/identity.py @@ -16,8 +16,8 @@ """Agent identity component.""" import concurrent -from concordia.agents.components import characteristic from concordia.associative_memory import associative_memory +from concordia.components.agent import characteristic from concordia.language_model import language_model from concordia.typing import component diff --git a/concordia/agents/components/observation.py b/concordia/components/agent/observation.py similarity index 100% rename from concordia/agents/components/observation.py rename to concordia/components/agent/observation.py diff --git a/concordia/agents/components/person_by_situation.py b/concordia/components/agent/person_by_situation.py similarity index 100% rename from concordia/agents/components/person_by_situation.py rename to concordia/components/agent/person_by_situation.py diff --git a/concordia/agents/components/plan.py b/concordia/components/agent/plan.py similarity index 100% rename from concordia/agents/components/plan.py rename to concordia/components/agent/plan.py diff --git a/concordia/agents/components/reflection.py b/concordia/components/agent/reflection.py similarity index 100% rename from concordia/agents/components/reflection.py rename to concordia/components/agent/reflection.py diff --git a/concordia/agents/components/self_perception.py b/concordia/components/agent/self_perception.py similarity index 100% rename from concordia/agents/components/self_perception.py rename to concordia/components/agent/self_perception.py diff --git a/concordia/agents/components/situation_perception.py b/concordia/components/agent/situation_perception.py similarity index 100% rename from concordia/agents/components/situation_perception.py rename to concordia/components/agent/situation_perception.py diff --git a/concordia/agents/components/somatic_state.py b/concordia/components/agent/somatic_state.py similarity index 98% rename from concordia/agents/components/somatic_state.py rename to concordia/components/agent/somatic_state.py index cb8ec2df..d72069a7 100644 --- a/concordia/agents/components/somatic_state.py +++ b/concordia/components/agent/somatic_state.py @@ -17,8 +17,8 @@ import concurrent import datetime from typing import Callable -from concordia.agents.components import characteristic from concordia.associative_memory import associative_memory +from concordia.components.agent import characteristic from concordia.language_model import language_model from concordia.typing import component diff --git a/concordia/agents/components/constant.py b/concordia/components/constant.py similarity index 96% rename from concordia/agents/components/constant.py rename to concordia/components/constant.py index ed0bb032..d84689cf 100644 --- a/concordia/agents/components/constant.py +++ b/concordia/components/constant.py @@ -18,7 +18,7 @@ from concordia.typing import component -class ConstantConstruct(component.Component): +class ConstantComponent(component.Component): """A constant memory component.""" def __init__(self, state: str, name: str = 'constant'): diff --git a/concordia/environment/components/__init__.py b/concordia/components/game_master/__init__.py similarity index 55% rename from concordia/environment/components/__init__.py rename to concordia/components/game_master/__init__.py index 130c16db..5d666e6b 100644 --- a/concordia/environment/components/__init__.py +++ b/concordia/components/game_master/__init__.py @@ -13,12 +13,12 @@ # limitations under the License. -"""Library of components for generative game master and agents.""" +"""Library of components specifically for generative game master.""" -from concordia.environment.components import conversation -from concordia.environment.components import direct_effect -from concordia.environment.components import inventory -from concordia.environment.components import player_status -from concordia.environment.components import relevant_events -from concordia.environment.components import schedule -from concordia.environment.components import time_display +from concordia.components.game_master import conversation +from concordia.components.game_master import direct_effect +from concordia.components.game_master import inventory +from concordia.components.game_master import player_status +from concordia.components.game_master import relevant_events +from concordia.components.game_master import schedule +from concordia.components.game_master import time_display diff --git a/concordia/environment/components/conversation.py b/concordia/components/game_master/conversation.py similarity index 97% rename from concordia/environment/components/conversation.py rename to concordia/components/game_master/conversation.py index c490b163..11ff23f9 100644 --- a/concordia/environment/components/conversation.py +++ b/concordia/components/game_master/conversation.py @@ -17,11 +17,12 @@ from collections.abc import Sequence +from concordia import components as generic_components from concordia.agents import basic_agent -from concordia.agents import components as sim_components from concordia.associative_memory import associative_memory from concordia.associative_memory import blank_memories from concordia.clocks import game_clock +from concordia.components import agent as sim_components from concordia.document import interactive_document from concordia.environment.scenes import conversation as conversation_scene from concordia.language_model import language_model @@ -120,10 +121,10 @@ def _make_npc( agent_name=name, clock=scene_clock, components=[ - sim_components.constant.ConstantConstruct( + generic_components.constant.ConstantComponent( name='Instructions:', state=self._game_master_instructions ), - sim_components.constant.ConstantConstruct( + generic_components.constant.ConstantComponent( name='General knowledge:', state=context ), sim_components.observation.Observation(agent_name=name, memory=mem), diff --git a/concordia/environment/components/direct_effect.py b/concordia/components/game_master/direct_effect.py similarity index 100% rename from concordia/environment/components/direct_effect.py rename to concordia/components/game_master/direct_effect.py diff --git a/concordia/environment/components/inventory.py b/concordia/components/game_master/inventory.py similarity index 100% rename from concordia/environment/components/inventory.py rename to concordia/components/game_master/inventory.py diff --git a/concordia/environment/components/player_status.py b/concordia/components/game_master/player_status.py similarity index 100% rename from concordia/environment/components/player_status.py rename to concordia/components/game_master/player_status.py diff --git a/concordia/environment/components/relevant_events.py b/concordia/components/game_master/relevant_events.py similarity index 100% rename from concordia/environment/components/relevant_events.py rename to concordia/components/game_master/relevant_events.py diff --git a/concordia/environment/components/schedule.py b/concordia/components/game_master/schedule.py similarity index 100% rename from concordia/environment/components/schedule.py rename to concordia/components/game_master/schedule.py diff --git a/concordia/environment/components/time_display.py b/concordia/components/game_master/time_display.py similarity index 100% rename from concordia/environment/components/time_display.py rename to concordia/components/game_master/time_display.py diff --git a/concordia/agents/components/report_state.py b/concordia/components/report_function.py similarity index 69% rename from concordia/agents/components/report_state.py rename to concordia/components/report_function.py index ce6e3a97..4e50271a 100644 --- a/concordia/agents/components/report_state.py +++ b/concordia/components/report_function.py @@ -13,29 +13,30 @@ # limitations under the License. -"""This components report what the get_state returns at the moment. +"""This components report what the function returns at the moment. For example, can be used for reporting current time -current_time_component = ReportState( +current_time_component = ReportFunction( 'Current time', - get_state=clock.current_time_interval_str) + function=clock.current_time_interval_str) """ from typing import Callable from concordia.typing import component -class ReportState(component.Component): - """A component that reports what the get_state returns at the moment.""" +class ReportFunction(component.Component): + """A component that reports what the function returns at the moment.""" - def __init__(self, get_state: Callable[[], str], name: str = 'State'): + def __init__(self, function: Callable[[], str], name: str = 'State'): """Initializes the component. Args: - get_state: the game clock. + function: the function that returns a string to report as state of the + component. name: The name of the component. """ - self._get_state = get_state + self._function = function self._name = name def name(self) -> str: @@ -44,7 +45,7 @@ def name(self) -> str: def state(self) -> str: """Returns the state of the component.""" - return self._get_state() + return self._function() def update(self) -> None: """This component always returns the same string, update does nothing.""" diff --git a/concordia/agents/components/sequential.py b/concordia/components/sequential.py similarity index 100% rename from concordia/agents/components/sequential.py rename to concordia/components/sequential.py diff --git a/concordia/tests/concordia_integration_test.py b/concordia/tests/concordia_integration_test.py index b1397e25..0670fe53 100644 --- a/concordia/tests/concordia_integration_test.py +++ b/concordia/tests/concordia_integration_test.py @@ -16,13 +16,14 @@ from typing import List from absl.testing import absltest from absl.testing import parameterized +from concordia import components from concordia.agents import basic_agent -from concordia.agents import components from concordia.associative_memory import associative_memory from concordia.associative_memory import blank_memories from concordia.associative_memory import importance_function from concordia.clocks import game_clock -from concordia.environment import components as gm_components +from concordia.components import agent as agent_components +from concordia.components import game_master as gm_components from concordia.environment import game_master from concordia.environment.metrics import common_sense_morality from concordia.environment.metrics import goal_achievement @@ -51,13 +52,13 @@ def _make_agent( name, clock, [ - components.constant.ConstantConstruct( + components.constant.ConstantComponent( 'Instructions:', game_master_instructions ), - components.constant.ConstantConstruct( + components.constant.ConstantComponent( 'General knowledge:', 'this is a test' ), - components.observation.Observation('Alice', mem), + agent_components.observation.Observation('Alice', mem), ], verbose=True, ) @@ -84,10 +85,10 @@ def _make_environment( shared_context = 'There is a hamlet named Riverbend.' - instructions_construct = components.constant.ConstantConstruct( + instructions_construct = components.constant.ConstantComponent( game_master_instructions, 'Instructions' ) - facts_on_village = components.constant.ConstantConstruct( + facts_on_village = components.constant.ConstantComponent( ' '.join(shared_memories), 'General knowledge of Riverbend' ) player_status = gm_components.player_status.PlayerStatus( diff --git a/examples/three_key_questions.ipynb b/examples/three_key_questions.ipynb index ce9359e6..af3f8f43 100644 --- a/examples/three_key_questions.ipynb +++ b/examples/three_key_questions.ipynb @@ -72,6 +72,7 @@ "from google.colab import widgets\n", "from IPython import display\n", "\n", + "from concordia import components\n", "from concordia.agents import basic_agent\n", "from concordia.agents import components\n", "from concordia.associative_memory import associative_memory\n", @@ -320,8 +321,8 @@ " person_by_situation,\n", " ],\n", " )\n", - " current_time_component = components.report_state.ReportState(name='Current time',\n", - " get_state=clock.current_time_interval_str)\n", + " current_time_component = components.report_callback.ReportFunction(name='Current time',\n", + " function=clock.current_time_interval_str)\n", "\n", " current_obs = components.observation.Observation(agent_config.name, mem)\n", " summary_obs = components.observation.ObservationSummary(\n", @@ -418,10 +419,10 @@ "# @title Create components of the Game Master\n", "player_names = [player.name for player in players]\n", "\n", - "instructions_construct = components.constant.ConstantConstruct(\n", + "instructions_construct = components.constant.ConstantComponent(\n", " state=game_master_instructions,\n", " name='Instructions')\n", - "scenario_knowledge = components.constant.ConstantConstruct(\n", + "scenario_knowledge = components.constant.ConstantComponent(\n", " state=' '.join(shared_memories),\n", " name='Background')\n", "\n", diff --git a/examples/village/day_in_riverbend.ipynb b/examples/village/day_in_riverbend.ipynb index 21eff86b..f30984ab 100644 --- a/examples/village/day_in_riverbend.ipynb +++ b/examples/village/day_in_riverbend.ipynb @@ -62,6 +62,7 @@ "from google.colab import widgets\n", "from IPython import display\n", "\n", + "from concordia import components\n", "from concordia.agents import basic_agent\n", "from concordia.agents import components\n", "from concordia.associative_memory import associative_memory\n", @@ -234,12 +235,12 @@ "\n", " # Build the player.\n", "\n", - " time = components.report_state.ReportState(\n", + " time = components.report_function.ReportFunction(\n", " name='current_time',\n", - " get_state=clock.current_time_interval_str)\n", + " function=clock.current_time_interval_str)\n", "\n", " identity = components.identity.SimIdentity(model, mem, agent_config.name)\n", - " goal_component = components.constant.ConstantConstruct(state=agent_config.goal)\n", + " goal_component = components.constant.ConstantComponent(state=agent_config.goal)\n", " reflection = components.reflection.Reflection(\n", " model=model,\n", " memory=mem,\n", @@ -436,10 +437,10 @@ "citizen_names = [player.name for player in players]\n", "player_names = [player.name for player in players]\n", "\n", - "instructions_construct = components.constant.ConstantConstruct(\n", + "instructions_construct = components.constant.ConstantComponent(\n", " game_master_instructions, 'Instructions'\n", ")\n", - "facts_on_village = components.constant.ConstantConstruct(\n", + "facts_on_village = components.constant.ConstantComponent(\n", " ' '.join(shared_memories), 'General knowledge of Riverbend'\n", ")\n", "player_status = gm_components.player_status.PlayerStatus(\n", diff --git a/examples/village/riverbend_elections.ipynb b/examples/village/riverbend_elections.ipynb index 010561ca..44a307bc 100644 --- a/examples/village/riverbend_elections.ipynb +++ b/examples/village/riverbend_elections.ipynb @@ -61,6 +61,7 @@ "from google.colab import widgets\n", "from IPython import display\n", "\n", + "from concordia import components\n", "from concordia.agents import basic_agent\n", "from concordia.agents import components\n", "from concordia.associative_memory import associative_memory\n", @@ -240,9 +241,9 @@ "\n", " # Build the player.\n", "\n", - " time = components.report_state.ReportState(\n", + " time = generic_components.report_function.ReportFunction(\n", " name='Current time',\n", - " get_state=clock.current_time_interval_str,\n", + " function=clock.current_time_interval_str,\n", " )\n", " somatic_state = components.somatic_state.SomaticState(\n", " model=model,\n", @@ -255,9 +256,7 @@ " memory=mem,\n", " agent_name=agent_config.name,\n", " )\n", - " goal_component = components.constant.ConstantConstruct(\n", - " state=agent_config.goal,\n", - " )\n", + " goal_component = generic_components.constant.ConstantComponent(state=agent_config.goal)\n", " plan = components.plan.SimPlan(\n", " model=model,\n", " memory=mem,\n", @@ -551,12 +550,9 @@ "# @title Create components and externalities\n", "player_names = [player.name for player in players]\n", "\n", - "instructions_construct = components.constant.ConstantConstruct(\n", - " game_master_instructions, 'Instructions')\n", - "facts_on_village = components.constant.ConstantConstruct(\n", - " ' '.join(shared_memories), 'General knowledge of Riverbend')\n", - "player_status = gm_components.player_status.PlayerStatus(\n", - " clock.now, model, game_master_memory, player_names)\n", + "instructions_construct = components.constant.ConstantComponent(game_master_instructions, 'Instructions')\n", + "facts_on_village = components.constant.ConstantComponent(' '.join(shared_memories), 'General knowledge of Riverbend')\n", + "player_status = gm_components.player_status.PlayerStatus(clock.now, model, game_master_memory, player_names)\n", "\n", "relevant_events = gm_components.relevant_events.RelevantEvents(\n", " clock.now, model, game_master_memory)\n",