-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathshort_term_memory.py
More file actions
193 lines (142 loc) · 6.09 KB
/
short_term_memory.py
File metadata and controls
193 lines (142 loc) · 6.09 KB
1
2
3
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import chromadb
from utils import np
from chromadb.utils import embedding_functions
from long_term_memory import add_content_to_ltm
embedding_function = embedding_functions.DefaultEmbeddingFunction()
client = chromadb.PersistentClient(path="_memories/short_term_memory")
short_term_memory = client.get_or_create_collection(name="short_term_memory", embedding_function=embedding_function, metadata={"hnsw:space": "cosine"})
def get_stm(): # Only get documents and ids from short term memory
res = short_term_memory.get(
include=["documents", "metadatas"]
)
return res
def add_content_to_stm(agent, content, virality_score, sentiment_score, iteration):
ids: list = []
metadatas: list = []
documents: list = []
ids.append(str(agent.name.lower()) + "_" + str(iteration + 1))
metadatas.append({"Author": str(agent.name.lower()), "Virality Score": virality_score, "Sentiment Score": sentiment_score, "Iteration": iteration + 1})
documents.append(content)
try:
short_term_memory.add(
ids=ids,
metadatas=metadatas,
documents=documents
)
except Exception as e:
print("Add data to db failed: ", e)
def modify_stm_virality_score(content_id, new_virality_score):
res = short_term_memory.get(
ids=content_id,
include=["metadatas"],
)
virality_score = res["metadatas"][0]["Virality Score"]
short_term_memory.update(
ids=content_id,
metadatas=[{"Virality Score": virality_score + new_virality_score}]
)
def modify_stm_sentiment_score(content_id, new_sentiment_score):
res = short_term_memory.get(
ids=content_id,
include=["metadatas"],
)
current_sentiment_score = res["metadatas"][0]["Sentiment Score"]
short_term_memory.update(
ids=content_id,
metadatas=[{"Sentiment Score": current_sentiment_score + new_sentiment_score}]
)
def evaluate_stm_content_for_ltm_transfer(content_id):
res = short_term_memory.get(
ids=content_id,
include=["metadatas", "documents"],
)
content_author = res["metadatas"][0]["Author"]
content_iteration = res["metadatas"][0]["Iteration"]
virality_score = res["metadatas"][0]["Virality Score"]
sentiment_score = res["metadatas"][0]["Sentiment Score"]
if virality_score >= 2 or abs(sentiment_score) >= 1:
add_content_to_ltm(content_id, res["documents"][0], content_author, content_iteration, virality_score, sentiment_score)
delete_content_from_stm(content_id)
def delete_content_from_stm(content_id):
short_term_memory.delete(
ids=content_id
)
def is_content_in_stm(content_id):
res = short_term_memory.get(
ids=content_id,
include=["documents"],
)
return bool(res["documents"])
def get_feedbacks_from_stm(agent):
res = short_term_memory.get(
where={"Author": str(agent.name.lower())},
include=["documents", "metadatas"],
)
num_documents = len(res['documents'])
output_strings = []
for i in range(num_documents):
document = res['documents'][i]
virality_score = res['metadatas'][i]['Virality Score']
content_score = res['metadatas'][i]['Sentiment Score']
output_string = f"{document} - Virality Score: {virality_score} - Content Score: {content_score}"
output_strings.append(output_string)
final_output = "\n".join(output_strings)
return final_output
def get_source_agent_from_stm(content_id):
res = short_term_memory.get(
ids=content_id,
include=["documents", "metadatas"],
)
return res["metadatas"][0]["Author"]
def calculate_recency(current_iteration, content_iteration):
"""
Calculate the normalized recency score.
"""
iterations_passed = current_iteration - content_iteration
recency_score = 1.0 - iterations_passed / (iterations_passed + 1)
return recency_score
def calculate_importance(virality_score, sentiment_score, virality_weight=0.6, sentiment_weight=0.4):
"""
Calculate the combined importance score using a weighted average.
"""
importance_score = (virality_score * virality_weight + abs(sentiment_score) * sentiment_weight) / (virality_weight + sentiment_weight)
return importance_score
def decay_probability(s_i, r_i, delta, beta):
"""
Calculate the forgetting probability for a memory M_i.
Parameters:
s_i (float): Normalized recency score in the range [0.0, 1.0].
r_i (float): Normalized importance score in the range [0.0, 1.0].
delta (float): Strength parameter in the range [0.0, 1.0].
beta (float): Hyper-parameter controlling the power function shape.
Returns:
float: Forgetting probability in the range [0.0, 1.0].
"""
power_function = max(r_i**beta, delta)
average_score = (s_i + r_i) / 2
forgetting_probability = 1 - average_score * power_function
forgetting_probability = np.clip(forgetting_probability, 0.0, 1.0)
return forgetting_probability
def content_score_decadency_law_stm(current_iteration):
current_iteration = current_iteration + 1
res = short_term_memory.get(
include=["documents", "metadatas"]
)
delta = 1 # Decay rate
beta = 4.0 # Importance of recency
for i in range(len(res["documents"])): # For each content in STM
content_iteration = res["metadatas"][i]["Iteration"]
virality_score = res["metadatas"][i]["Virality Score"] # Assuming virality score is stored in metadata
sentiment_score = res["metadatas"][i]["Sentiment Score"] # Assuming sentiment score is stored in metadata
# Calculate recency score
recency_score = calculate_recency(current_iteration, content_iteration)
# Calculate importance score
importance_score = calculate_importance(virality_score, sentiment_score)
# Calculate forgetting probability
forgetting_probability = decay_probability(recency_score, importance_score, delta, beta)
if forgetting_probability >= 0.9:
delete_content_from_stm(res["ids"][i])
def clear_stm():
short_term_memory.delete(
where={"Iteration": {"$gte": 0}}
)