Skip to content

Commit

Permalink
- add a pub coordination simulation
Browse files Browse the repository at this point in the history
- add ability to pass thought chains and maximum conversation length to game master factory

PiperOrigin-RevId: 654054735
Change-Id: Ia282c593ee557ff7450966ca203e53ef85e46e13
  • Loading branch information
vezhnick authored and copybara-github committed Jul 19, 2024
1 parent 10ab158 commit 3741a12
Show file tree
Hide file tree
Showing 5 changed files with 782 additions and 17 deletions.
1 change: 1 addition & 0 deletions concordia/components/game_master/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""Library of components specifically for generative game masters."""

from concordia.components.game_master import conversation
from concordia.components.game_master import coordination_payoffs
from concordia.components.game_master import current_scene
from concordia.components.game_master import direct_effect
from concordia.components.game_master import inventory
Expand Down
53 changes: 36 additions & 17 deletions concordia/factory/environment/basic_game_master.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from concordia.associative_memory import importance_function
from concordia.clocks import game_clock
from concordia.components import game_master as gm_components
from concordia.document import interactive_document
from concordia.environment import game_master
from concordia.environment.scenes import runner
from concordia.language_model import language_model
Expand All @@ -47,7 +48,14 @@ def build_game_master(
memory: associative_memory.AssociativeMemory | None = None,
supporting_players_at_fixed_locations: Sequence[str] | None = None,
additional_components: Sequence[component.Component] | None = tuple([]),
thought_chain: (
Sequence[
Callable[[interactive_document.InteractiveDocument, str, str], str]
]
| None
) = None,
npc_context: str = '',
max_conversation_length: int = 10,
verbose: bool = False,
) -> tuple[game_master.GameMaster, associative_memory.AssociativeMemory]:
"""Build a game master (i.e., an environment).
Expand All @@ -70,7 +78,9 @@ def build_game_master(
characters who never move are located.
additional_components: Add more components specific to the current
environment.
thought_chain: The thought chain to use for the game master.
npc_context: extra context provided only to non-player characters
max_conversation_length: The maximum number of turns in a conversation.
verbose: whether or not to print verbose debug information
Returns:
Expand All @@ -82,24 +92,26 @@ def build_game_master(
game_master_memory = associative_memory.AssociativeMemory(
sentence_embedder=embedder,
importance=importance_model.importance,
clock=clock.now)
clock=clock.now,
)

player_names = [player.name for player in players]

scenario_knowledge = generic_components.constant.ConstantComponent(
state='\n'.join(shared_memories),
name='Background:\n')
state='\n'.join(shared_memories), name='Background:\n'
)

if supporting_players_at_fixed_locations is not None:
supporting_character_locations_if_any = (
generic_components.constant.ConstantComponent(
state='\n'.join(supporting_players_at_fixed_locations),
name='Notes:\n'))
name='Notes:\n',
)
)
else:
supporting_character_locations_if_any = (
generic_components.constant.ConstantComponent(
state='',
name='Notes:\n'))
generic_components.constant.ConstantComponent(state='', name='Notes:\n')
)

player_status = gm_components.player_status.PlayerStatus(
clock_now=clock.now,
Expand All @@ -117,6 +129,7 @@ def build_game_master(
components=[player_status],
cap_nonplayer_characters=cap_nonplayer_characters_in_conversation,
shared_context=f'{shared_context}\n{npc_context}',
max_conversation_length=max_conversation_length,
)

direct_effect_externality = gm_components.direct_effect.DirectEffect(
Expand All @@ -128,13 +141,15 @@ def build_game_master(
)

relevant_events = gm_components.relevant_events.RelevantEvents(
clock.now, model, game_master_memory)
clock.now, model, game_master_memory
)
time_display = gm_components.time_display.TimeDisplay(clock)

# Create the game master's thought chain
account_for_agency_of_others = thought_chains_lib.AccountForAgencyOfOthers(
model=model, players=players, verbose=False)
thought_chain = [
model=model, players=players, verbose=False
)
thought_chain = thought_chain or [
thought_chains_lib.extract_direct_quote,
thought_chains_lib.attempt_to_most_likely_outcome,
thought_chains_lib.result_to_effect_caused_by_active_player,
Expand Down Expand Up @@ -205,29 +220,33 @@ def create_html_log(
model: The language model to use.
primary_environment: The main game master.
secondary_environments: Sequence of secondary game masters.
Returns:
An HTML string log of the simulation.
"""
primary_gm_memories = (
primary_environment.get_memory().retrieve_recent(k=10000, add_time=True))
primary_gm_memories = primary_environment.get_memory().retrieve_recent(
k=10000, add_time=True
)

detailed_story = '\n'.join(primary_gm_memories)
episode_summary = model.sample_text(
f'Sequence of events:\n{detailed_story}'+
'\nNarratively summarize the above temporally ordered ' +
'sequence of events. Write it as a news report. Summary:\n',
f'Sequence of events:\n{detailed_story}'
+ '\nNarratively summarize the above temporally ordered '
+ 'sequence of events. Write it as a news report. Summary:\n',
max_tokens=3500,
terminators=(),
)

history_sources = [primary_environment] + list(secondary_environments)
histories_html = [
html_lib.PythonObjectToHTMLConverter(history.get_history()).convert()
for history in history_sources]
for history in history_sources
]
histories_names = [history.name for history in history_sources]

gm_mem_html = html_lib.PythonObjectToHTMLConverter(
primary_gm_memories).convert()
primary_gm_memories
).convert()
tabbed_html = html_lib.combine_html_pages(
histories_html + [gm_mem_html],
histories_names + ['GM'],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright 2024 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 set of social context descriptions."""

SOCIAL_CONTEXT = [
(
"The sun peeks through the morning"
" mist as a {players} gather near the entrance of Hackney"
" Marshes. They stretch and laugh, their colorful running attire"
" contrasting with the green expanse. A few dog walkers pass by, their"
" furry companions excitedly sniffing the air. "
"{player_name} just arrived."
),
(
"The aroma of freshly brewed coffee and"
" artisan pastries fills the air. {players} sit at a long wooden table"
" under a striped awning, their laughter mingling with the chatter of"
" the bustling market. A street musician strums a guitar nearby, adding"
" a bohemian touch to the scene. "
"{player_name} just arrived."
),
(
"Sunlight dances on the water as"
" {players} cycle along the towpath of Regent's Canal. They"
" pause to admire the colorful houseboats and wave at fellow cyclists."
" The gentle sound of water lapping against the canal banks creates a"
" peaceful atmosphere. "
"{player_name} just arrived."
),
(
"Vibrant murals and graffiti adorn the brick walls of Shoreditch."
" {players} wander through the streets, their eyes wide with wonder as"
" they discover hidden gems of urban art. The smell of street food"
" wafts from nearby vendors, tempting them to take a break."
" {player_name} just arrived."
),
(
"A checkered blanket is spread out on the"
" lush green lawn of Victoria Park. {players} lounge in the sunshine,"
" sharing snacks and stories. The laughter of children playing nearby"
" adds a joyful backdrop to the scene. "
"{player_name} just arrived."
),
]
70 changes: 70 additions & 0 deletions examples/modular/environment/modules/player_names.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Copyright 2024 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 set of names to be used for creating characters."""

FIRST_NAMES = [
"Aaliyah",
"Adrian",
"Aurora",
"Benjamin",
"Camila",
"Daniel",
"Eleanor",
"Ethan",
"Fiona",
"Gabriel",
"Harper",
"Isaac",
"Jasmine",
"Julian",
"Kaitlyn",
"Leo",
"Maya",
"Mateo",
"Naomi",
"Noah",
"Olivia",
"Owen",
"Penelope",
"Quinn",
"Riley",
"Samuel",
"Sophia",
"Theodore",
"Valentina",
"William",
"Xander",
"Yasmine",
"Zachary",
"Amelia",
"Alexander",
"Ava",
"Liam",
"Mia",
"Elijah",
"Isabella",
"James",
"Emily",
"Lucas",
"Abigail",
"Mason",
"Harper",
"Logan",
"Evelyn",
"Oliver",
"Chloe",
"Jackson",
"Ella",
]
Loading

0 comments on commit 3741a12

Please sign in to comment.