Memory Cognition

img

The MemoryCognition class is a component in the llamarch library that provides an interface to work with memory cognition patterns, which involve the use of memory to improve the performance of LLMs.

MemoryCognition

A class to handle memory processing, including storing, retrieving, summarizing, and evaluating information from short-term and long-term memory.

Attributes:
  • llm (object) –

    The large language model used for processing.

  • embedding (object) –

    The embedding used for representing queries.

  • short_term_memory (object) –

    The short-term memory system for storing and retrieving data.

  • long_term_memory (object) –

    The long-term memory system for storing and retrieving data.

  • summarizer (object) –

    The summarizer used to summarize similar items from memory.

  • memory_decay (object) –

    The system used to evaluate memory decay.

  • logger (Logger) –

    Logger for the MemoryCognition class.

Source code in llamarch/patterns/memory_cognition/__init__.py
  4
  5
  6
  7
  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
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
class MemoryCognition:
    """
    A class to handle memory processing, including storing, retrieving, summarizing, 
    and evaluating information from short-term and long-term memory.

    Attributes
    ----------
    llm : object
        The large language model used for processing.
    embedding : object
        The embedding used for representing queries.
    short_term_memory : object
        The short-term memory system for storing and retrieving data.
    long_term_memory : object
        The long-term memory system for storing and retrieving data.
    summarizer : object
        The summarizer used to summarize similar items from memory.
    memory_decay : object
        The system used to evaluate memory decay.
    logger : logging.Logger
        Logger for the MemoryCognition class.
    """

    def __init__(self, llm, embedding, short_term_memory, long_term_memory, summarizer, memory_decay):
        """
        Initializes the MemoryCognition instance with the provided components.

        Parameters
        ----------
        llm : object
            The large language model used for processing.
        embedding : object
            The embedding used for representing queries.
        short_term_memory : object
            The short-term memory system for storing and retrieving data.
        long_term_memory : object
            The long-term memory system for storing and retrieving data.
        summarizer : object
            The summarizer used to summarize similar items from memory.
        memory_decay : object
            The system used to evaluate memory decay.
        """
        self.llm = llm
        self.embedding = embedding
        self.short_term_memory = short_term_memory
        self.long_term_memory = long_term_memory
        self.summarizer = summarizer
        self.memory_decay = memory_decay
        self.logger = logging.getLogger(__name__)
        self.logger.info("MemoryCognition initialized.")

    def store_information(self, query, query_vector):
        """
        Store information in short-term memory.

        Parameters
        ----------
        query : str
            The query text to be stored in memory.
        query_vector : numpy.ndarray
            The vector representation of the query to be stored in memory.
        """
        self.short_term_memory.store_information(query_vector, query)
        self.logger.info(f"Information stored in Short-Term Memory: {query}")

    def fetch_similar(self, query_vector):
        """
        Fetch similar items from short-term memory based on the query vector.

        Parameters
        ----------
        query_vector : numpy.ndarray
            The vector representation of the query to search for similar items.

        Returns
        -------
        list
            A list of similar items found in short-term memory.
        """
        similar_items_stm = self.short_term_memory.fetch_similar(query_vector)
        self.logger.info(
            f"Similar items in Short-Term Memory: {[getattr(x, 'metadata', {}).get('query') for x in similar_items_stm]}")
        return similar_items_stm

    def summarize(self, similar_items_stm):
        """
        Summarize the similar items fetched from short-term memory.

        Parameters
        ----------
        similar_items_stm : list
            A list of similar items from short-term memory to be summarized.

        Returns
        -------
        str
            A summary of the similar items.
        """
        summary = self.summarizer.summarize(similar_items_stm)
        self.logger.info(f"Summary of similar items: {summary}")
        return summary

    def evaluate(self, summary):
        """
        Evaluate the summary based on memory decay.

        Parameters
        ----------
        summary : str
            The summary of similar items to be evaluated.

        Returns
        -------
        float
            The evaluation result based on memory decay.
        """
        evaluation = self.memory_decay.evaluate(summary)
        self.logger.info(f"Evaluation: {evaluation}")
        return evaluation

    def flush_to_long_term(self, long_term_memory):
        """
        Flush the short-term memory content to long-term memory.

        Parameters
        ----------
        long_term_memory : object
            The long-term memory system to store the flushed data.
        """
        self.short_term_memory.flush_to_long_term(long_term_memory)
        self.logger.info("Summary flushed to Long-Term Memory.")

    def fetch_similar_from_long_term(self, query_vector):
        """
        Fetch similar items from long-term memory based on the query vector.

        Parameters
        ----------
        query_vector : numpy.ndarray
            The vector representation of the query to search for similar items.

        Returns
        -------
        list
            A list of similar items found in long-term memory.
        """
        long_term_results = self.long_term_memory.fetch_similar(query_vector)
        self.logger.info(
            f"Similar items in Long-Term Memory: {long_term_results}")

__init__(llm, embedding, short_term_memory, long_term_memory, summarizer, memory_decay)

Initializes the MemoryCognition instance with the provided components.

Parameters:
  • llm (object) –

    The large language model used for processing.

  • embedding (object) –

    The embedding used for representing queries.

  • short_term_memory (object) –

    The short-term memory system for storing and retrieving data.

  • long_term_memory (object) –

    The long-term memory system for storing and retrieving data.

  • summarizer (object) –

    The summarizer used to summarize similar items from memory.

  • memory_decay (object) –

    The system used to evaluate memory decay.

Source code in llamarch/patterns/memory_cognition/__init__.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def __init__(self, llm, embedding, short_term_memory, long_term_memory, summarizer, memory_decay):
    """
    Initializes the MemoryCognition instance with the provided components.

    Parameters
    ----------
    llm : object
        The large language model used for processing.
    embedding : object
        The embedding used for representing queries.
    short_term_memory : object
        The short-term memory system for storing and retrieving data.
    long_term_memory : object
        The long-term memory system for storing and retrieving data.
    summarizer : object
        The summarizer used to summarize similar items from memory.
    memory_decay : object
        The system used to evaluate memory decay.
    """
    self.llm = llm
    self.embedding = embedding
    self.short_term_memory = short_term_memory
    self.long_term_memory = long_term_memory
    self.summarizer = summarizer
    self.memory_decay = memory_decay
    self.logger = logging.getLogger(__name__)
    self.logger.info("MemoryCognition initialized.")

store_information(query, query_vector)

Store information in short-term memory.

Parameters:
  • query (str) –

    The query text to be stored in memory.

  • query_vector (ndarray) –

    The vector representation of the query to be stored in memory.

Source code in llamarch/patterns/memory_cognition/__init__.py
55
56
57
58
59
60
61
62
63
64
65
66
67
def store_information(self, query, query_vector):
    """
    Store information in short-term memory.

    Parameters
    ----------
    query : str
        The query text to be stored in memory.
    query_vector : numpy.ndarray
        The vector representation of the query to be stored in memory.
    """
    self.short_term_memory.store_information(query_vector, query)
    self.logger.info(f"Information stored in Short-Term Memory: {query}")

fetch_similar(query_vector)

Fetch similar items from short-term memory based on the query vector.

Parameters:
  • query_vector (ndarray) –

    The vector representation of the query to search for similar items.

Returns:
  • list

    A list of similar items found in short-term memory.

Source code in llamarch/patterns/memory_cognition/__init__.py
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
def fetch_similar(self, query_vector):
    """
    Fetch similar items from short-term memory based on the query vector.

    Parameters
    ----------
    query_vector : numpy.ndarray
        The vector representation of the query to search for similar items.

    Returns
    -------
    list
        A list of similar items found in short-term memory.
    """
    similar_items_stm = self.short_term_memory.fetch_similar(query_vector)
    self.logger.info(
        f"Similar items in Short-Term Memory: {[getattr(x, 'metadata', {}).get('query') for x in similar_items_stm]}")
    return similar_items_stm

summarize(similar_items_stm)

Summarize the similar items fetched from short-term memory.

Parameters:
  • similar_items_stm (list) –

    A list of similar items from short-term memory to be summarized.

Returns:
  • str

    A summary of the similar items.

Source code in llamarch/patterns/memory_cognition/__init__.py
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
def summarize(self, similar_items_stm):
    """
    Summarize the similar items fetched from short-term memory.

    Parameters
    ----------
    similar_items_stm : list
        A list of similar items from short-term memory to be summarized.

    Returns
    -------
    str
        A summary of the similar items.
    """
    summary = self.summarizer.summarize(similar_items_stm)
    self.logger.info(f"Summary of similar items: {summary}")
    return summary

evaluate(summary)

Evaluate the summary based on memory decay.

Parameters:
  • summary (str) –

    The summary of similar items to be evaluated.

Returns:
  • float

    The evaluation result based on memory decay.

Source code in llamarch/patterns/memory_cognition/__init__.py
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
def evaluate(self, summary):
    """
    Evaluate the summary based on memory decay.

    Parameters
    ----------
    summary : str
        The summary of similar items to be evaluated.

    Returns
    -------
    float
        The evaluation result based on memory decay.
    """
    evaluation = self.memory_decay.evaluate(summary)
    self.logger.info(f"Evaluation: {evaluation}")
    return evaluation

flush_to_long_term(long_term_memory)

Flush the short-term memory content to long-term memory.

Parameters:
  • long_term_memory (object) –

    The long-term memory system to store the flushed data.

Source code in llamarch/patterns/memory_cognition/__init__.py
124
125
126
127
128
129
130
131
132
133
134
def flush_to_long_term(self, long_term_memory):
    """
    Flush the short-term memory content to long-term memory.

    Parameters
    ----------
    long_term_memory : object
        The long-term memory system to store the flushed data.
    """
    self.short_term_memory.flush_to_long_term(long_term_memory)
    self.logger.info("Summary flushed to Long-Term Memory.")

fetch_similar_from_long_term(query_vector)

Fetch similar items from long-term memory based on the query vector.

Parameters:
  • query_vector (ndarray) –

    The vector representation of the query to search for similar items.

Returns:
  • list

    A list of similar items found in long-term memory.

Source code in llamarch/patterns/memory_cognition/__init__.py
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
def fetch_similar_from_long_term(self, query_vector):
    """
    Fetch similar items from long-term memory based on the query vector.

    Parameters
    ----------
    query_vector : numpy.ndarray
        The vector representation of the query to search for similar items.

    Returns
    -------
    list
        A list of similar items found in long-term memory.
    """
    long_term_results = self.long_term_memory.fetch_similar(query_vector)
    self.logger.info(
        f"Similar items in Long-Term Memory: {long_term_results}")