def add_memories(
      self,
      memory: associative_memory.AssociativeMemoryBank,
      agent_config: AgentConfig,
  ) -> None:
    """Creates formative memories of the agent at specific ages based on traits.

    First, a series of descriptive statements will be generated and based on
    them the formative episodes. There is an option to add description to memory
    as well.
    Args:
      memory: the memory structure to fill
      agent_config: structured description of an agent
    """
    description = self.make_backstory(agent_config)
    prompt = interactive_document.InteractiveDocument(self._model)
    prompt.statement('Creative Writing Master Class\n')
    prompt.statement('Character background story:\n\n' + description)

    question = (
        'Given the life story above, invent formative episodes from '
        f'the life of {agent_config.name} which could have taken '
        f'place at the following ages: {agent_config.formative_ages}. '
        'The episodes should be age appropriate and believeable. '
        f'They should be memorable events for {agent_config.name} and '
        'important for establishing who they are as a person. They should '
        f'be consistent with {agent_config.name}\'s personality and '
        f'circumstances. Describe each episode from {agent_config.name}\'s '
        'perspective and use third-person limited point of view. Each episode '
        'must mention their age at the time the event occurred using language '
        f'such as "When {agent_config.name} was 5 years old, they '
        'experienced..." . Use past tense. Write no more than three sentences '
        'per episode. Separate episodes from one another by the delimiter '
        f'"{self._delimiter_symbol}". Do not apply any other '
        'special formatting besides these delimiters.'
    )
    if agent_config.traits:
      question += (
          '\nTaken as a whole, these formative episodes from the life of '
          f'{agent_config.name} should explain their personality, which has '
          f'been described as: "{agent_config.traits}".')
    if agent_config.context:
      question += (
          'Make a few of the episodes relate to the '
          f'following context: "{agent_config.context}".'
      )

    aggregated_result = prompt.open_question(
        question=question,
        max_tokens=6000,
        terminators=[],
    )

    episodes = list(aggregated_result.split(self._delimiter_symbol))

    # If some episodes are still missing then try to regenerate them.
    formative_ages_list = list(agent_config.formative_ages)
    if len(episodes) != len(formative_ages_list):
      num_missing = len(formative_ages_list) - len(episodes)
      if num_missing > 0:
        for age in list(formative_ages_list[len(episodes):]):
          episode = prompt.open_question(
              question=(
                  f"What is {agent_config.name}'s formative memory from "
                  f'age {age}?'
              ),
              max_tokens=1000,
              terminators=(self._delimiter_symbol, '.\n', '\nQuestion:'),
          )
          episodes.append(episode)

    if len(episodes) != len(formative_ages_list):
      logger.warning(
          'Warning: Number of generated formative episodes ' +
          f'({len(episodes)}) does not match number of formative ages ' +
          f'({len(formative_ages_list)}). This is just a warning and '
          'probably not problematic.')

    for episode_age, episode in zip(agent_config.formative_ages, episodes):
      if agent_config.date_of_birth is not None:
        timestamp = (
            agent_config.date_of_birth + relativedelta(years=episode_age))
        memory_to_add = f'[{timestamp}] {episode}'
      else:
        memory_to_add = episode
      memory.add(memory_to_add)

    if self._current_date and agent_config.date_of_birth is not None:
      age = relativedelta(self._current_date, agent_config.date_of_birth).years
      timestamp = self._current_date
      memory.add(
          f'[{timestamp}] {agent_config.name} is {age} years old.',
      )
Creates formative memories of the agent at specific ages based on traits.

    First, a series of descriptive statements will be generated and based on
    them the formative episodes. There is an option to add description to memory
    as well.
    Args:
      memory: the memory structure to fill
      agent_config: structured description of an agent
    
concordia/associative_memory/formative_memories.py