Skip to content

Memories

Memory

Bases: BaseModel, ABC

Base class for memories.

Source code in adala/memories/base.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Memory(BaseModel, ABC):
    """
    Base class for memories.
    """

    @abstractmethod
    def remember(self, observation: str, data: Dict):
        """
        Base method for remembering experiences in long term memory.
        """

    def remember_many(self, observations: List[str], data: List[Dict]):
        """
        Base method for remembering experiences in long term memory.
        """
        for observation, d in zip(observations, data):
            self.remember(observation, d)

    @abstractmethod
    def retrieve(self, observation: str, num_results: int = 1) -> Any:
        """
        Base method for retrieving past experiences from long term memory, based on current observations

        Args:
            observation: the current observation
            num_results: the number of results to return
        """

    def retrieve_many(self, observations: List[str], num_results: int = 1) -> List[Any]:
        """
        Base method for retrieving past experiences from long term memory, based on current observations

        Args:
            observation: the current observation
            num_results: the number of results to return
        """
        return [self.retrieve(observation) for observation in observations]

    @abstractmethod
    def clear(self):
        """
        Base method for clearing memory.
        """

clear() abstractmethod

Base method for clearing memory.

Source code in adala/memories/base.py
46
47
48
49
50
@abstractmethod
def clear(self):
    """
    Base method for clearing memory.
    """

remember(observation, data) abstractmethod

Base method for remembering experiences in long term memory.

Source code in adala/memories/base.py
13
14
15
16
17
@abstractmethod
def remember(self, observation: str, data: Dict):
    """
    Base method for remembering experiences in long term memory.
    """

remember_many(observations, data)

Base method for remembering experiences in long term memory.

Source code in adala/memories/base.py
19
20
21
22
23
24
def remember_many(self, observations: List[str], data: List[Dict]):
    """
    Base method for remembering experiences in long term memory.
    """
    for observation, d in zip(observations, data):
        self.remember(observation, d)

retrieve(observation, num_results=1) abstractmethod

Base method for retrieving past experiences from long term memory, based on current observations

Parameters:

Name Type Description Default
observation str

the current observation

required
num_results int

the number of results to return

1
Source code in adala/memories/base.py
26
27
28
29
30
31
32
33
34
@abstractmethod
def retrieve(self, observation: str, num_results: int = 1) -> Any:
    """
    Base method for retrieving past experiences from long term memory, based on current observations

    Args:
        observation: the current observation
        num_results: the number of results to return
    """

retrieve_many(observations, num_results=1)

Base method for retrieving past experiences from long term memory, based on current observations

Parameters:

Name Type Description Default
observation

the current observation

required
num_results int

the number of results to return

1
Source code in adala/memories/base.py
36
37
38
39
40
41
42
43
44
def retrieve_many(self, observations: List[str], num_results: int = 1) -> List[Any]:
    """
    Base method for retrieving past experiences from long term memory, based on current observations

    Args:
        observation: the current observation
        num_results: the number of results to return
    """
    return [self.retrieve(observation) for observation in observations]

FileMemory

Bases: Memory

Source code in adala/memories/file_memory.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class FileMemory(Memory):
    filepath: str

    def remember(self, observation: str, experience: Any):
        """
        Serialize experience in JSON and append to file
        """
        with open(self.filepath) as f:
            memory = json.load(f)
        memory[observation] = experience
        with open(self.filepath, "w") as f:
            json.dump(memory, f, indent=2)

    def retrieve(self, observation: str) -> Any:
        """
        Retrieve experience from file
        """
        with open(self.filepath) as f:
            memory = json.load(f)
        return memory[observation]

remember(observation, experience)

Serialize experience in JSON and append to file

Source code in adala/memories/file_memory.py
 9
10
11
12
13
14
15
16
17
def remember(self, observation: str, experience: Any):
    """
    Serialize experience in JSON and append to file
    """
    with open(self.filepath) as f:
        memory = json.load(f)
    memory[observation] = experience
    with open(self.filepath, "w") as f:
        json.dump(memory, f, indent=2)

retrieve(observation)

Retrieve experience from file

Source code in adala/memories/file_memory.py
19
20
21
22
23
24
25
def retrieve(self, observation: str) -> Any:
    """
    Retrieve experience from file
    """
    with open(self.filepath) as f:
        memory = json.load(f)
    return memory[observation]