Skip to content

Commit

Permalink
moving components usable by both GM and agents to generic_components.…
Browse files Browse the repository at this point in the history
… Also changing the name of the component that reports the state returned by a callback

PiperOrigin-RevId: 588098003
Change-Id: I15b80a4d23034377240a6fbbe070ad30c57e9a1e
  • Loading branch information
vezhnick authored and copybara-github committed Dec 5, 2023
1 parent 5a92f9e commit b81c219
Show file tree
Hide file tree
Showing 27 changed files with 99 additions and 79 deletions.
29 changes: 0 additions & 29 deletions concordia/agents/components/__init__.py

This file was deleted.

19 changes: 19 additions & 0 deletions concordia/components/__init__.py
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions concordia/components/agent/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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."""
Expand Down
File renamed without changes.
15 changes: 8 additions & 7 deletions concordia/tests/concordia_integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
)
Expand All @@ -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(
Expand Down
9 changes: 5 additions & 4 deletions examples/three_key_questions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
11 changes: 6 additions & 5 deletions examples/village/day_in_riverbend.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
18 changes: 7 additions & 11 deletions examples/village/riverbend_elections.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit b81c219

Please sign in to comment.