Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuel-ferdman authored Dec 9, 2024
2 parents 25d07e3 + 18451dc commit a8869c0
Show file tree
Hide file tree
Showing 33 changed files with 4,430 additions and 278 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/pypi-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ jobs:
python --version
pip list
- name: Build distribution
run: python setup.py sdist bdist_wheel
run: |
python setup.py sdist bdist_wheel
# Workaround old setuptools not normalizing name in sdist.
for OLD in ./dist/gdm-concordia-*; do
NEW="$(echo "$OLD" | sed s/gdm-concordia/gdm_concordia/)"
mv "$OLD" "$NEW"
done
ls dist/*
- name: Save artifact
uses: actions/upload-artifact@50769540e7f4bd5e21e526ee35c689e35e0d6874
with:
Expand Down
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,26 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/).

## [1.8.9] - 2024-11-25

### Changed

- Update launch and eval scripts for the eval phase of the contest
- Further improve alternative baseline agents
- Improve a few baseline agents

### Added

- Add another time and place module for reality show
- Add support for scene-wise computation of metrics
- Add alternative versions of basic and rational agent factories

### Fixed

- Catch another type of together API exception
- Fix time serialization in associative_memory


## [1.8.8] - 2024-11-13

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion concordia/components/sequential.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Sequential(component.Component):
def __init__(self, name: str, components: Sequence[component.Component]):
self._components = components
self._name = name
logging.warn(
logging.warning(
'The Sequential component is deprecated. Please use Entity Components '
'and specifically `action_spec_ignored` to achieve the same effect '
'as the old Sequential component.')
Expand Down
2 changes: 2 additions & 0 deletions concordia/contrib/components/agent/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@

from concordia.contrib.components.agent import affect_reflection
from concordia.contrib.components.agent import dialectical_reflection
from concordia.contrib.components.agent import observations_since_last_update
from concordia.contrib.components.agent import situation_representation_via_narrative
140 changes: 140 additions & 0 deletions concordia/contrib/components/agent/observations_since_last_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
# 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 component for tracking observations since the last update.
"""

from collections.abc import Callable
import datetime

from absl import logging as absl_logging
from concordia.components import agent as agent_components
from concordia.components.agent import action_spec_ignored
from concordia.components.agent import memory_component
from concordia.language_model import language_model
from concordia.memory_bank import legacy_associative_memory
from concordia.typing import entity_component
from concordia.typing import logging


def _get_earliest_timepoint(
memory_component_: agent_components.memory_component.MemoryComponent,
) -> datetime.datetime:
"""Returns all memories in the memory bank.
Args:
memory_component_: The memory component to retrieve memories from.
"""
memories_data_frame = memory_component_.get_raw_memory()
if not memories_data_frame.empty:
sorted_memories_data_frame = memories_data_frame.sort_values(
'time', ascending=True)
return sorted_memories_data_frame['time'][0]
else:
absl_logging.warning('No memories found in memory bank.')
return datetime.datetime.now()


class ObservationsSinceLastUpdate(action_spec_ignored.ActionSpecIgnored):
"""Report all observations since the last update."""

def __init__(
self,
model: language_model.LanguageModel,
clock_now: Callable[[], datetime.datetime],
memory_component_name: str = (
memory_component.DEFAULT_MEMORY_COMPONENT_NAME
),
pre_act_key: str = '\nObservations',
logging_channel: logging.LoggingChannel = logging.NoOpLoggingChannel,
):
"""Initialize a component to consider the latest observations.
Args:
model: The language model to use.
clock_now: Function that returns the current time.
memory_component_name: The name of the memory component from which to
retrieve related memories.
pre_act_key: Prefix to add to the output of the component when called
in `pre_act`.
logging_channel: The channel to log debug information to.
"""
super().__init__(pre_act_key)
self._model = model
self._clock_now = clock_now
self._memory_component_name = memory_component_name
self._logging_channel = logging_channel

self._previous_time = None

def pre_observe(
self,
observation: str,
) -> str:
memory = self.get_entity().get_component(
self._memory_component_name,
type_=memory_component.MemoryComponent)
memory.add(
f'[observation] {observation}',
metadata={'tags': ['observation']},
)
return ''

def _make_pre_act_value(self) -> str:
"""Returns a representation of the current situation to pre act."""
current_time = self._clock_now()
memory = self.get_entity().get_component(
self._memory_component_name,
type_=memory_component.MemoryComponent)

if self._previous_time is None:
self._previous_time = _get_earliest_timepoint(memory)

interval_scorer = legacy_associative_memory.RetrieveTimeInterval(
time_from=self._previous_time,
time_until=current_time,
add_time=True,
)
mems = [mem.text for mem in memory.retrieve(scoring_fn=interval_scorer)]
result = '\n'.join(mems) + '\n'

self._logging_channel({
'Key': self.get_pre_act_key(),
'Value': result,
})

self._previous_time = current_time

return result

def get_state(self) -> entity_component.ComponentState:
"""Converts the component to JSON data."""
with self._lock:
if self._previous_time is None:
previous_time = ''
else:
previous_time = self._previous_time.strftime('%Y-%m-%d %H:%M:%S')
return {
'previous_time': previous_time,
}

def set_state(self, state: entity_component.ComponentState) -> None:
"""Sets the component state from JSON data."""
with self._lock:
if state['previous_time']:
previous_time = datetime.datetime.strptime(
state['previous_time'], '%Y-%m-%d %H:%M:%S')
else:
previous_time = None
self._previous_time = previous_time
Loading

0 comments on commit a8869c0

Please sign in to comment.