Skip to content

Commit

Permalink
making sure clock_now is passed when clock is not needed to avoid sid…
Browse files Browse the repository at this point in the history
…e-effects

PiperOrigin-RevId: 587700999
Change-Id: I76b684860677605828d0e19db93a4a4ad5f113d1
  • Loading branch information
vezhnick authored and copybara-github committed Dec 4, 2023
1 parent 9b75546 commit 46be541
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 72 deletions.
23 changes: 12 additions & 11 deletions concordia/agents/components/characteristic.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@


"""Agent characteristic component."""
import datetime
from typing import Callable

from concordia.associative_memory import associative_memory
from concordia.document import interactive_document
from concordia.language_model import language_model
from concordia.typing import clock as game_clock
from concordia.typing import component
import termcolor

Expand Down Expand Up @@ -50,7 +51,7 @@ def __init__(
memory: associative_memory.AssociativeMemory,
agent_name: str,
characteristic_name: str,
state_clock: game_clock.GameClock | None = None,
state_clock_now: Callable[[], datetime.datetime] | None = None,
extra_instructions: str = '',
num_memories_to_retrieve: int = 25,
verbose: bool = False,
Expand All @@ -62,7 +63,7 @@ def __init__(
memory: an associative memory
agent_name: the name of the agent
characteristic_name: the string to use in similarity search of memory
state_clock: if None then consider this component as representing a
state_clock_now: if None then consider this component as representing a
`trait`. If a clock is used then consider this component to represent a
`state`. A state is temporary whereas a trait is meant to endure.
extra_instructions: append additional instructions when asking the model
Expand All @@ -77,7 +78,7 @@ def __init__(
self._characteristic_name = characteristic_name
self._agent_name = agent_name
self._extra_instructions = extra_instructions
self._clock = state_clock
self._clock_now = state_clock_now
self._num_memories_to_retrieve = num_memories_to_retrieve

def name(self) -> str:
Expand All @@ -88,13 +89,13 @@ def state(self) -> str:

def update(self) -> None:
query = f"{self._agent_name}'s {self._characteristic_name}"
if self._clock is not None:
query = f'[{self._clock.now()}] {query}'
if self._clock_now is not None:
query = f'[{self._clock_now()}] {query}'

mems = '\n'.join(
self._memory.retrieve_associative(query,
self._num_memories_to_retrieve,
add_time=True)
self._memory.retrieve_associative(
query, self._num_memories_to_retrieve, add_time=True
)
)

prompt = interactive_document.InteractiveDocument(self._model)
Expand All @@ -105,8 +106,8 @@ def update(self) -> None:
f'{self._extra_instructions}'
f'Start the answer with "{self._agent_name} is"'
)
if self._clock is not None:
question = f'Current time: {self._clock.now()}.\n{question}'
if self._clock_now is not None:
question = f'Current time: {self._clock_now()}.\n{question}'

self._cache = prompt.open_question(
'\n'.join([question, f'Statements:\n{mems}']),
Expand Down
27 changes: 13 additions & 14 deletions concordia/agents/components/person_by_situation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
# limitations under the License.

"""Agent component for self perception."""

import datetime
from typing import Callable
from typing import Sequence

from concordia.associative_memory import associative_memory
from concordia.document import interactive_document
from concordia.language_model import language_model
from concordia.typing import clock
from concordia.typing import component
import termcolor

Expand All @@ -33,7 +34,7 @@ def __init__(
memory: associative_memory.AssociativeMemory,
agent_name: str,
components=Sequence[component.Component] | None,
state_clock: clock.GameClock | None = None,
clock_now: Callable[[], datetime.datetime] | None = None,
num_memories_to_retrieve: int = 25,
verbose: bool = False,
):
Expand All @@ -45,7 +46,7 @@ def __init__(
memory: The memory to use.
agent_name: The name of the agent.
components: The components to condition the answer on.
state_clock: The clock to use.
clock_now: time callback to use for the state.
num_memories_to_retrieve: The number of memories to retrieve.
verbose: Whether to print the state of the component.
"""
Expand All @@ -56,7 +57,7 @@ def __init__(
self._state = ''
self._components = components or []
self._agent_name = agent_name
self._clock = state_clock
self._clock_now = clock_now
self._num_memories_to_retrieve = num_memories_to_retrieve
self._name = name

Expand All @@ -77,21 +78,19 @@ def update(self) -> None:

prompt.statement(f'Memories of {self._agent_name}:\n{mems}')

component_states = '\n'.join(
[
f"{self._agent_name}'s "
+ (construct.name() + ':\n' + construct.state())
for construct in self._components
]
)
component_states = '\n'.join([
f"{self._agent_name}'s "
+ (construct.name() + ':\n' + construct.state())
for construct in self._components
])

prompt.statement(component_states)
question = (
f'What would a person like {self._agent_name} do in a situation like'
' this?'
)
if self._clock is not None:
question = f'Current time: {self._clock.now()}.\n{question}'
if self._clock_now is not None:
question = f'Current time: {self._clock_now()}.\n{question}'

self._state = prompt.open_question(
question,
Expand Down
7 changes: 3 additions & 4 deletions concordia/agents/components/report_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,18 @@

"""This components report what the get_state returns at the moment.
For example, can be used for reporting current time
For example, can be used for reporting current time
current_time_component = ReportState(
'Current time',
'Current time',
get_state=clock.current_time_interval_str)
"""

from typing import Callable
from concordia.typing import component


class ReportState(component.Component):
"""A component that shows the current time interval."""
"""A component that reports what the get_state returns at the moment."""

def __init__(self, get_state: Callable[[], str], name: str = 'State'):
"""Initializes the component.
Expand Down
13 changes: 7 additions & 6 deletions concordia/agents/components/self_perception.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
# limitations under the License.

"""Agent component for self perception."""
import datetime
from typing import Callable

from concordia.associative_memory import associative_memory
from concordia.document import interactive_document
from concordia.language_model import language_model
from concordia.typing import clock
from concordia.typing import component
import termcolor

Expand All @@ -31,7 +32,7 @@ def __init__(
model: language_model.LanguageModel,
memory: associative_memory.AssociativeMemory,
agent_name: str,
state_clock: clock.GameClock | None = None,
clock_now: Callable[[], datetime.datetime] | None = None,
num_memories_to_retrieve: int = 100,
verbose: bool = False,
):
Expand All @@ -42,7 +43,7 @@ def __init__(
model: Language model.
memory: Associative memory.
agent_name: Name of the agent.
state_clock: Clock to use for the state.
clock_now: time callback to use for the state.
num_memories_to_retrieve: Number of memories to retrieve.
verbose: Whether to print the state.
"""
Expand All @@ -52,7 +53,7 @@ def __init__(
self._memory = memory
self._state = ''
self._agent_name = agent_name
self._clock = state_clock
self._clock_now = clock_now
self._num_memories_to_retrieve = num_memories_to_retrieve
self._name = name

Expand All @@ -72,8 +73,8 @@ def update(self) -> None:
prompt = interactive_document.InteractiveDocument(self._model)
prompt.statement(f'Memories of {self._agent_name}:\n{mems}')

if self._clock is not None:
prompt.statement(f'Current time: {self._clock.now()}.\n')
if self._clock_now is not None:
prompt.statement(f'Current time: {self._clock_now()}.\n')

question = (
f'Given the memories above, what kind of person is {self._agent_name}?'
Expand Down
13 changes: 7 additions & 6 deletions concordia/agents/components/situation_perception.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
# limitations under the License.

"""Agent component for situation perception."""
import datetime
from typing import Callable

from concordia.associative_memory import associative_memory
from concordia.document import interactive_document
from concordia.language_model import language_model
from concordia.typing import clock
from concordia.typing import component
import termcolor

Expand All @@ -31,7 +32,7 @@ def __init__(
model: language_model.LanguageModel,
memory: associative_memory.AssociativeMemory,
agent_name: str,
state_clock: clock.GameClock | None = None,
clock_now: Callable[[], datetime.datetime] | None = None,
num_memories_to_retrieve: int = 25,
verbose: bool = False,
):
Expand All @@ -42,7 +43,7 @@ def __init__(
model: The language model to use.
memory: The memory to use.
agent_name: The name of the agent.
state_clock: The clock to use.
clock_now: time callback to use for the state.
num_memories_to_retrieve: The number of memories to retrieve.
verbose: Whether to print the last chain.
"""
Expand All @@ -51,7 +52,7 @@ def __init__(
self._memory = memory
self._state = ''
self._agent_name = agent_name
self._clock = state_clock
self._clock_now = clock_now
self._num_memories_to_retrieve = num_memories_to_retrieve
self._name = name

Expand All @@ -71,8 +72,8 @@ def update(self) -> None:
prompt = interactive_document.InteractiveDocument(self._model)
prompt.statement(f'Memories of {self._agent_name}:\n{mems}')

if self._clock is not None:
prompt.statement(f'Current time: {self._clock.now()}.\n')
if self._clock_now is not None:
prompt.statement(f'Current time: {self._clock_now()}.\n')

question = (
'Given the memories above, what kind of situation is'
Expand Down
22 changes: 10 additions & 12 deletions concordia/agents/components/somatic_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@


"""Agent component for tracking the somatic state."""

import concurrent
import datetime
from typing import Callable
from concordia.agents.components import characteristic
from concordia.associative_memory import associative_memory
from concordia.language_model import language_model
from concordia.typing import clock as game_clock
from concordia.typing import component


Expand All @@ -35,7 +35,7 @@ def __init__(
model: language_model.LanguageModel,
memory: associative_memory.AssociativeMemory,
agent_name: str,
clock: game_clock.GameClock,
clock_now: Callable[[], datetime.datetime] | None = None,
summarize: bool = True,
):
"""Initialize somatic state component.
Expand All @@ -44,7 +44,7 @@ def __init__(
model: a language model
memory: an associative memory
agent_name: the name of the agent
clock: the game clock is needed to know when is the current time
clock_now: time callback to use for the state.
summarize: if True, the resulting state will be a one sentence summary,
otherwise state it would be a concatentation of five separate
characteristics
Expand All @@ -53,7 +53,7 @@ def __init__(
self._memory = memory
self._state = ''
self._agent_name = agent_name
self._clock = clock
self._clock_now = clock_now
self._summarize = summarize

self._characteristic_names = [
Expand Down Expand Up @@ -81,7 +81,7 @@ def __init__(
memory=self._memory,
agent_name=self._agent_name,
characteristic_name=characteristic_name,
state_clock=self._clock,
state_clock_now=self._clock_now,
extra_instructions=extra_instructions,
)
)
Expand All @@ -97,12 +97,10 @@ def update(self):
for c in self._characteristics:
executor.submit(c.update)

self._state = '\n'.join(
[
f"{self._agent_name}'s {c.name()}: " + c.state()
for c in self._characteristics
]
)
self._state = '\n'.join([
f"{self._agent_name}'s {c.name()}: " + c.state()
for c in self._characteristics
])
if self._summarize:
prompt = (
f'Summarize the somatic state of {self._agent_name} in one'
Expand Down
13 changes: 6 additions & 7 deletions concordia/environment/components/conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ def __init__(
players: A list of players to generate conversations for.
model: A language model to use for generating utterances.
memory: GM memory, used to add the summary of the conversation
clock: multi intercal game clock.
clock: multi interval game clock. If conversation happens, the clock will
advance in higher gear during the conversation scene.
burner_memory_factory: a memory factory to create temporary memory for
npcs and conversation gm
cap_nonplayer_characters: The maximum number of non-player characters
Expand Down Expand Up @@ -188,12 +189,10 @@ def _who_talked(
who_talked = (
who_talked
+ 'Also present: '
+ ', '.join(
[
npc_conversant.name
for npc_conversant in nonplayers_in_conversation
]
)
+ ', '.join([
npc_conversant.name
for npc_conversant in nonplayers_in_conversation
])
+ '.'
)
return who_talked
Expand Down
10 changes: 5 additions & 5 deletions concordia/environment/components/schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

"""This construct implements scheduled events."""

from collections.abc import Callable
import dataclasses
import datetime
from typing import Callable
from typing import Optional

from concordia.typing import component
Expand All @@ -44,10 +44,10 @@ class Schedule(component.Component):

def __init__(
self,
clock,
schedule,
clock_now: Callable[[], datetime.datetime],
schedule: dict[str, EventData],
):
self._clock = clock
self._clock_now = clock_now
self._schedule = schedule
self._state = None

Expand All @@ -58,7 +58,7 @@ def state(self) -> str | None:
return self._state

def update(self) -> None:
now = self._clock.now()
now = self._clock_now()
events = []
for _, event_data in self._schedule.items():
if now == event_data.time:
Expand Down
Loading

0 comments on commit 46be541

Please sign in to comment.