-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmemory.py
1194 lines (1094 loc) · 46.2 KB
/
memory.py
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import asyncio
from datetime import datetime
import pytz
from tzlocal import get_localzone
import json
import os
import re
import secrets
import time
import aiohttp
from fastapi import HTTPException
import openai
from tenacity import retry, stop_after_attempt, wait_fixed
from agentmemory import (
create_memory,
create_unique_memory,
get_memories,
search_memory,
get_memory,
update_memory,
delete_memory,
delete_similar_memories,
count_memories,
wipe_category,
wipe_all_memories,
import_file_to_memory,
export_memory_to_file,
stop_database,
search_memory_by_date,
create_alternative_memory,
get_last_message,
update_memory,
delete_memory,
)
import utils
from dateutil.parser import parse
import textwrap
from nltk.tokenize import sent_tokenize
import config
import logs
import llmcalls
import simple_utils
logger = logs.Log("memory", "memory.log").get_logger()
class MemoryManager:
"""A class to manage the memory of the agent."""
def __init__(self):
self.model_used = "gpt-4o"
pass
async def create_memory(
self,
category,
document,
metadata={},
username=None,
mUsername=None,
regenerate=False,
):
if "created_at" not in metadata:
metadata["created_at"] = time.time() # Use current time if not provided
"""Create a new memory and return the ID."""
return create_memory(
category,
document,
metadata,
username=username,
mUsername=mUsername,
regenerate=regenerate,
)
async def create_unique_memory(
self, category, content, metadata={}, similarity=0.15, username=None
):
"""Create a new memory if it doesn't exist yet and return the ID."""
return create_unique_memory(
category, content, metadata, similarity, username=username
)
async def create_alternative_memory(
self, category, content, metadata={}, username=None
):
"""Create a new memory if it doesn't exist yet and return the ID."""
return create_alternative_memory(category, content, metadata, username=username)
async def get_memories(self, category, username=None):
"""Return all memories in the category."""
return get_memories(category, username=username)
async def search_memory(
self,
category,
search_term,
username=None,
min_distance=0.0,
max_distance=1.0,
contains_text=None,
n_results=5,
filter_metadata=None,
):
"""Search the memory and return the results."""
return search_memory(
category,
search_term,
username=username,
min_distance=min_distance,
max_distance=max_distance,
contains_text=contains_text,
n_results=n_results,
filter_metadata=filter_metadata,
)
async def search_memory_by_date(
self, category, search_term, username=None, n_results=100, filter_date=None
):
"""Search the memory by date and return the results."""
return search_memory_by_date(
category,
search_term,
username=username,
n_results=n_results,
filter_date=filter_date,
)
async def get_memory(self, category, id, username=None):
"""Return the memory with the given ID."""
# id format is 0000000000000019, add the leading zeros back so its 16 characters long
id = id.zfill(16)
return get_memory(category, id, username=username)
async def update_memory(
self, category, id, document=None, metadata={}, username=None
):
"""Update the memory with the given ID and return the ID."""
return update_memory(category, id, document, metadata, username=username)
async def delete_memory(self, category, id, username=None):
"""Delete the memory with the given ID and return the ID."""
return delete_memory(category, id, username=username)
async def delete_similar_memories(
self, category, content, similarity_threshold=0.95, username=None
):
"""Delete all memories with a similarity above the threshold and return the number of deleted memories."""
return delete_similar_memories(
category, content, similarity_threshold, username=username
)
async def count_memories(self, category, username=None):
"""Return the number of memories in the category."""
return count_memories(category, username=username)
async def wipe_category(self, category, username=None):
"""Delete all memories in the category and return the number of deleted memories."""
return wipe_category(category, username=username)
async def wipe_all_memories(self, username=None):
"""Delete all memories and return the number of deleted memories."""
return wipe_all_memories(username=username)
async def import_memories(self, path, username=None):
"""Import memories from a file and return the number of imported memories."""
return import_file_to_memory(path, username=username)
async def export_memories(self, path, username=None):
"""Export memories to a file and return the number of exported memories."""
return export_memory_to_file(path, username=username)
async def stop_database(self, username=None):
"""Stop the database."""
return stop_database(username=username)
async def split_text_into_chunks(self, text, max_chunk_len=200):
"""Split the text into chunks of up to `max_chunk_len`."""
# Check if text is a string; if not, directly return it in a list assuming it's already chunked properly
if not isinstance(text, str):
return [text]
chunks = []
chunk = []
token_count = 0
def add_to_chunk(line):
nonlocal chunk, token_count, chunks
current_line_token_count = utils.MessageParser.num_tokens_from_string(line)
if token_count + current_line_token_count > max_chunk_len:
# If the current chunk is full, add it to chunks and start a new chunk
chunks.append("\n".join(chunk))
chunk = [line]
token_count = current_line_token_count
else:
# Add the current line to the chunk and update the token count
chunk.append(line)
token_count += current_line_token_count
# Split text into lines if it contains newline characters; otherwise, process it as a single line
lines = text.split("\n") if "\n" in text else [text]
for line in lines:
if "\n" in text or len(lines) == 1:
add_to_chunk(line)
else:
while line:
part = line[:max_chunk_len]
add_to_chunk(part)
line = line[max_chunk_len:]
if chunk:
chunks.append("\n".join(chunk))
return chunks
async def get_most_recent_messages(
self, category, username=None, n_results=100, chat_id=None
):
"""Return the most recent messages in the category."""
category = category.lower().replace(" ", "_")
if chat_id is None:
memories = get_memories(category, username=username, n_results=n_results)
else:
memories = get_memories(
category,
username=username,
n_results=n_results,
filter_metadata={"chat_id": chat_id},
)
memories.sort(key=lambda x: x["metadata"]["created_at"], reverse=False)
for memory in memories:
if memory["metadata"].get("username") == "user":
memory["document"].replace("User :", username + ":")
return memories[:n_results]
async def get_episodic_memory(
self,
new_messages,
username=None,
all_messages=None,
remaining_tokens=1000,
verbose=False,
settings={},
):
category = "active_brain"
process_dict = {
"input": new_messages,
"results": [],
"subject": "none",
"error": None,
}
default_params = config.default_params
default_params["max_tokens"] = settings.get("memory", {}).get("output", 1000)
default_params["model"] = settings.get("active_model").get("active_model")
responder = llmcalls.get_responder(
(
config.api_keys["openai"]
if settings.get("active_model").get("active_model").startswith("gpt")
else config.api_keys["anthropic"]
),
settings.get("active_model").get("active_model"),
default_params,
)
response = None
async for resp in responder.get_response(
username,
all_messages,
stream=False,
function_metadata=config.fakedata,
function_call="auto",
role="date-extractor",
):
response = resp
if response:
process_dict["subject"] = response
else:
process_dict[
"error"
] = "timeline does not contain the required elements"
if process_dict["subject"].lower() in ["none", "'none'", '"none"', '""']:
# return process_dict
return []
logger.debug(f"Timeline: {process_dict['subject']}")
parsed_date = None
episodic_messages = []
if isinstance(process_dict["subject"], str):
date_formats = [
"%Y-%m-%d",
"%d/%m/%Y %H:%M:%S",
"%d-%m-%Y",
"%d/%m/%Y",
"%d-%m-%Y",
"%d-%m-%Y %H:%M:%S",
]
for date_format in date_formats:
try:
parsed_date = datetime.strptime(
process_dict["subject"].strip(), date_format
)
logger.debug(f"parsed_date: {parsed_date}")
break
except ValueError:
continue
if parsed_date is not None:
logger.debug(
f"searching for episodic messages on a specific date: {parsed_date} in category: {category} for user: {username} and message: {new_messages}"
)
episodic_messages = search_memory_by_date(
category, new_messages, username=username, filter_date=parsed_date
)
logger.debug(f"episodic_messages: {len(episodic_messages)}")
return episodic_messages
async def process_episodic_memory(
self,
new_messages,
username=None,
all_messages=None,
remaining_tokens=1000,
verbose=False,
settings={},
):
category = "active_brain"
process_dict = {"input": new_messages}
subject = "none"
default_params = config.default_params
default_params["max_tokens"] = settings.get("memory", {}).get("output", 1000)
default_params["model"] = settings.get("active_model").get("active_model")
responder = llmcalls.get_responder(
(
config.api_keys["openai"]
if settings.get("active_model").get("active_model").startswith("gpt")
else config.api_keys["anthropic"]
),
settings.get("active_model").get("active_model"),
default_params,
)
async for resp in responder.get_response(
username,
all_messages,
function_metadata=config.fakedata,
role="date-extractor",
):
response = resp
if response:
subject = response
else:
process_dict[
"error"
] = "timeline does not contain the required elements"
if (
subject.lower() == "none"
or subject.lower() == "'none'"
or subject.lower() == '"none"'
or subject.lower() == '""'
):
return "", ""
logger.debug(f"Timeline: {subject}")
if isinstance(subject, str):
# List of date formats
date_formats = [
"%Y-%m-%d",
"%d/%m/%Y %H:%M:%S",
"%d-%m-%Y",
"%d/%m/%Y",
"%d-%m-%Y",
"%d-%m-%Y %H:%M:%S",
]
# Try parsing the date with each format
for date_format in date_formats:
try:
parsed_date = datetime.strptime(subject.strip(), date_format)
logger.debug(f"parsed_date: {parsed_date}")
break
except ValueError:
parsed_date = None
continue
else:
return "", ""
if parsed_date is not None:
results_string = ""
logger.debug(
f"searching for episodic messages on a specific date: {parsed_date} in category: {category} for user: {username} and message: {new_messages}"
)
episodic_messages = search_memory_by_date(
category, new_messages, username=username, filter_date=parsed_date
)
logger.debug(f"episodic_messages: {len(episodic_messages)}")
for memory in episodic_messages:
date = memory["metadata"]["created_at"]
formatted_date = datetime.fromtimestamp(date).strftime(
"%Y-%m-%d %H:%M:%S"
)
results_string += f"({formatted_date}) [{memory['metadata']['username']}]: {memory['document']} (score: {memory['distance']:.4f})\n"
logger.debug(f"results_string:\n{results_string}")
# Check tokens
token_count = utils.MessageParser.num_tokens_from_string(results_string)
while token_count > min(1000, remaining_tokens):
# Split the result_string by newline and remove the last line if possible
result_lines = results_string.split("\n")
if len(result_lines) > 1:
result_lines.pop(-1)
results_string = "\n".join(result_lines)
else:
# If there are no newline characters, remove a certain number of characters from the endsecretso
results_string = results_string[:-100]
token_count = utils.MessageParser.num_tokens_from_string(results_string)
return results_string, subject
async def process_active_brain(
self,
new_messages,
username=None,
all_messages=None,
remaining_tokens=1000,
verbose=False,
chat_id=None,
regenerate=False,
uid=None,
settings={},
custom_metadata={},
):
"""Process the active brain and return the updated active brain data."""
category = "active_brain"
process_dict = {"input": new_messages}
seen_ids = set()
chunks = await self.split_text_into_chunks(new_messages, 200)
if uid is None:
uid = secrets.token_hex(10)
for chunk in chunks:
# Create a memory for each chunk
metadata = {"uid": uid, "chat_id": chat_id}
if custom_metadata:
metadata.update(custom_metadata) # Merge custom metadata if provided
await self.create_memory(
category,
chunk,
username=username,
metadata=metadata,
mUsername="user",
regenerate=regenerate,
)
logger.debug(
f"adding memory: {chunk} to category: {category} with uid: {uid} for user: {username} and chat_id: {chat_id}"
)
process_dict["created_new_memory"] = "yes"
if remaining_tokens > 100:
subject_query = None
response = ""
default_params = config.default_params
default_params["max_tokens"] = settings.get("memory", {}).get(
"output", 1000
)
default_params["model"] = settings.get("active_model").get("active_model")
responder = llmcalls.get_responder(
(
config.api_keys["openai"]
if settings.get("active_model")
.get("active_model")
.startswith("gpt")
else config.api_keys["anthropic"]
),
settings.get("active_model").get("active_model"),
default_params,
)
async for resp in responder.get_response(
username,
all_messages,
function_metadata=config.fakedata,
role="date-extractor",
):
if resp:
subject_query = resp
if subject_query:
if subject_query.lower() == "none":
response = new_messages
process_dict[category] = {}
parsed_data = self.process_observation(response)
results_list = []
process_dict[category]["query_results"] = {}
for data in parsed_data:
data_results = await self.search_memory(
category,
data,
username,
min_distance=0.0,
max_distance=2.0,
n_results=10,
)
# Initialize the list for this data item in the dictionary
process_dict[category]["query_results"][data] = []
for result in data_results:
if result.get("id") not in seen_ids:
seen_ids.add(result.get("id"))
id = result.get("id")
id = id.lstrip("0") or "0"
document = result.get("document")
distance = round(result.get("distance"), 3)
date = result["metadata"]["created_at"]
formatted_date = datetime.fromtimestamp(date).strftime(
"%Y-%m-%d %H:%M:%S"
)
results_list.append((id, document, distance, formatted_date))
# Add the result to the list for this data item
process_dict[category]["query_results"][data].append(
(id, document, distance, formatted_date)
)
process_dict["results_list_before_token_check"] = results_list.copy()
result_string = ""
result_string = "\n".join(
f"({id}) {formatted_date} - {document} (score: {distance})"
for id, document, distance, formatted_date in results_list
)
token_count = utils.MessageParser.num_tokens_from_string(result_string)
while token_count > min(1000, remaining_tokens):
if len(results_list) > 1:
results_list.sort(key=lambda x: int(x[2]), reverse=True)
results_list.pop(0)
result_string = "\n".join(
f"({id}) {formatted_date} - {document} (score: {distance})"
for id, document, distance, formatted_date in results_list
)
else:
# If there's only one entry, shorten the document content
id, document, distance, formatted_date = results_list[0]
document = document[:-100]
result_string = (
f"({id}) {formatted_date} - {document} (score: {distance})"
)
token_count = utils.MessageParser.num_tokens_from_string(result_string)
unique_results = set() # Create a set to store unique results
for id, document, distance, formatted_date in results_list:
unique_results.add((id, document, distance, formatted_date))
results_list.sort(key=lambda x: int(x[0]))
result_string = "\n".join(
f"({id}) {formatted_date} - {document} (score: {distance})"
for id, document, distance, formatted_date in results_list
)
process_dict["results_list_after_token_check"] = results_list
process_dict["result_string"] = result_string
process_dict["token_count"] = token_count
if verbose:
await utils.MessageSender.send_message(
{"type": "relations", "content": process_dict}, "blue", username
)
return result_string, token_count, unique_results
else:
return "", 0, set()
async def process_incoming_memory(
self,
category,
content,
username=None,
remaining_tokens=1000,
verbose=False,
settings={},
):
"""Process the incoming memory and return the updated active brain data."""
process_dict = {"input": content}
unique_results = set()
logger.debug(f"Processing incoming memory: {content}")
subject_query = "none"
default_params = config.default_params
default_params["max_tokens"] = settings.get("memory", {}).get("output", 1000)
default_params["model"] = settings.get("active_model").get("active_model")
responder = llmcalls.get_responder(
(
config.api_keys["openai"]
if settings.get("active_model").get("active_model").startswith("gpt")
else config.api_keys["anthropic"]
),
settings.get("active_model").get("active_model"),
default_params,
)
async for resp in responder.get_response(
username,
content,
function_metadata=config.fakedata,
role="categorise_query",
):
if resp:
subject_query = resp
else:
logger.error("Error: choice does not contain 'message' or 'content'")
subject = subject_query
if (
subject.lower() == "none"
or subject.lower() == "'none'"
or subject.lower() == '"none"'
or subject.lower() == '""'
):
subject = "personal_information: " + content[:40]
result_string = ""
parts = self.process_category_query(subject)
if len(parts) < 1:
logger.error(
"Error: parts does not contain the required elements: "
+ str(parts)
+ "query: "
+ str(subject)
)
process_dict["error"] = "parts does not contain the required elements"
else:
for part in parts:
category, query = part
process_dict[category] = {}
process_dict[category]["query_results"] = {}
process_dict[category]["query_results"][query] = []
search_result, new_process_dict = await self.search_queries(
category,
[query],
username,
process_dict[category]["query_results"][query],
)
process_dict[category]["query_results"][query] = new_process_dict
for id, document, distance, formatted_date in process_dict[category][
"query_results"
][query]:
try:
unique_results.add((id, document, distance, formatted_date))
except Exception as e:
logger.error(
f"Error while adding result to unique_results: {e}"
)
result_string += search_result
result_string = "\n".join(
f"({id}) {formatted_date} - {document} (score: {distance})"
for id, document, distance, formatted_date in unique_results
)
# Check tokens
token_count = utils.MessageParser.num_tokens_from_string(result_string)
while token_count > min(1000, remaining_tokens):
# Split the result_string by newline and remove the last line if possible
result_lines = result_string.split("\n")
if len(result_lines) > 1:
result_lines.pop(-1)
result_string = "\n".join(result_lines)
else:
# If there are no newline characters, remove a certain number of characters from the end
result_string = result_string[:-100]
token_count = utils.MessageParser.num_tokens_from_string(result_string)
similar_messages = None
if len(parts) > 0:
for part in parts:
category, query = part
similar_messages = await self.search_memory(
category, content, username, max_distance=0.15, n_results=10
)
if similar_messages:
logger.debug(
"Not adding to memory, message is similar to a previous message(s):"
)
process_dict["similar_messages"] = [
(m["document"], m["id"], m["distance"], m["metadata"]["created_at"])
for m in similar_messages
]
process_dict["created_new_memory"] = "no"
for similar_message in similar_messages:
logger.debug(
f"({similar_message['id']}){similar_message['metadata']['created_at']} - {similar_message['document']} - score: {similar_message['distance']}"
)
else:
subject_category = "none"
default_params = config.default_params
default_params["max_tokens"] = settings.get("memory", {}).get(
"output", 1000
)
default_params["model"] = settings.get("active_model").get("active_model")
responder = llmcalls.get_responder(
(
config.api_keys["openai"]
if settings.get("active_model")
.get("active_model")
.startswith("gpt")
else config.api_keys["anthropic"]
),
settings.get("active_model").get("active_model"),
default_params,
)
async for resp in responder.get_response(
username,
content,
function_metadata=config.fakedata,
role="categorise",
):
if resp:
subject_category = resp
else:
logger.error(
"Error: subject_query does not contain the required elements"
)
else:
logger.error(
"Error: subject_query does not contain the required elements"
)
category = subject_category
if category.lower() == "none":
category = content
categories = self.process_category(category)
uid = secrets.token_hex(10)
for category in categories:
chunks = await self.split_text_into_chunks(content, 200)
for chunk in chunks:
# Create a memory for each chunk
await self.create_memory(
category,
chunk,
username=username,
metadata={"uid": uid},
mUsername="user",
)
logger.debug(f"adding memory: {chunk} to category: {category}")
process_dict["created_new_memory"] = "yes, categories: " + ", ".join(
categories
)
process_dict["result_string"] = result_string
process_dict["token_count"] = token_count
if verbose:
await utils.MessageSender.send_message(
{"type": "relations", "content": process_dict}, "blue", username
)
return result_string, token_count, unique_results
async def search_queries(self, category, queries, username, process_dict):
"""Search the queries in the memory and return the results."""
seen_ids = set()
full_search_result = ""
for query in queries:
search_result = await self.search_memory(
category, query, username, n_results=10
)
for result in search_result:
if result.get("id") not in seen_ids:
seen_ids.add(result.get("id"))
id = result.get("id")
id = id.lstrip("0") or "0"
document = result.get("document")
distance = round(result.get("distance"), 3)
date = result["metadata"]["created_at"]
formatted_date = datetime.fromtimestamp(date).strftime(
"%Y-%m-%d %H:%M:%S"
)
full_search_result += (
f"({id}) {formatted_date} - {document} - score: {distance}\n"
)
process_dict.append((id, document, distance, formatted_date))
return full_search_result, process_dict
async def process_incoming_memory_assistant(
self, category, content, username=None, chat_id=None, regenerate=False, uid=None
):
"""Process the incoming memory and return the updated active brain data."""
logger.debug(f"Processing incoming memory: {content}")
# todo: get version from the memory
version = 0
if regenerate:
version = 1
# create a new memory
if uid is None:
uid = secrets.token_hex(10)
chunks = await self.split_text_into_chunks(content, 200)
settings = await utils.SettingsManager.load_settings("users", username)
model_used = settings["active_model"]["active_model"]
for chunk in chunks:
await self.create_memory(
category,
chunk,
username=username,
metadata={
"uid": uid,
"chat_id": chat_id,
"version": version,
"model": model_used,
},
mUsername="assistant",
)
logger.debug(f"adding memory: {chunk} to category: {category}")
return
def process_observation(self, string):
"""Process the observation and returns each part"""
parts = string.split("\n")
# remove the : and the space after it
parts = [
part.split(":", 1)[1].lstrip() if ":" in part else part for part in parts
]
return parts
def process_category_query(self, string):
"""Process the input data and return the results"""
lines = string.split("\n")
result = []
valid_categories = [
"factual_information",
"personal_information",
"procedural_knowledge",
"conceptual_knowledge",
"meta_knowledge",
"temporal_information",
]
for line in lines:
line = line.strip()
if not line:
continue
# Check if the line contains a colon separator
if ":" in line:
category, query = line.split(":", 1)
category = category.strip().lower()
query = query.strip()
else:
# Split the line into category and query
parts = line.split(" ", 1)
if len(parts) == 1:
category = "active_brain"
query = parts[0]
else:
category, query = parts
category = category.lower()
# Check if the category is valid
if category not in valid_categories:
category = "active_brain"
# Clean the category name
category = re.sub(r"[^a-zA-Z0-9_-]", "", category)
category = category[:63]
if not category:
category = "active_brain"
# Make sure the category starts and ends with an alphanumeric character
category = re.sub(r"^[^a-zA-Z0-9]+|[^a-zA-Z0-9]+$", "", category)
# Remove consecutive periods (..)
category = re.sub(r"\.{2,}", ".", category)
# Check if the category is a valid IPv4 address
if re.match(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", category):
category = "active_brain"
# Truncate the query to a maximum of 100 characters
query = query[:100]
result.append((category, query))
return result
def process_category(self, string):
"""Process the input data and return the results"""
lines = string.split("\n")
result = []
for line in lines:
if not line.strip():
continue
line = line.lower().replace(" ", "_")
line = re.sub(r"[^a-z0-9-_]", "", line) # Remove invalid characters
line = re.sub(
r"\.{2,}", ".", line
) # Replace consecutive periods with a single one
line = re.sub(
r"^[^a-z0-9]*|[^a-z0-9]*$", "", line
) # Remove leading/trailing invalid characters
# If line is still too long or too short, replace it with a default category
if len(line) < 3 or len(line) > 63:
line = "active_brain"
result.append(line)
return result
async def note_taking(
self,
content,
message,
user_dir,
username,
show=True,
verbose=False,
tokens_notes=1000,
settings={},
):
max_tokens = tokens_notes
process_dict = {
"actions": [],
"content": content,
"message": message,
"timestamp": None,
"final_message": None,
"note_taking_query": None,
"files_content_string": None,
"error": None,
}
filedir = "notes"
filedir = os.path.join(user_dir, username, filedir)
if not os.path.exists(filedir):
os.makedirs(filedir)
dir_list = os.listdir(filedir)
files_content_string = ""
for file in dir_list:
current_file = os.path.join(filedir, file)
if os.path.isfile(current_file): # Check if it's a file
with open(current_file, "r") as f:
files_content_string += f"{file}:\n{f.read()}\n\n"
process_dict["files_content_string"] = files_content_string
token_count = utils.MessageParser.num_tokens_from_string(files_content_string)
if token_count > max_tokens:
new_files_content_string = ""
for file in dir_list:
current_file = os.path.join(filedir, file)
if os.path.isfile(current_file): # Check if it's a file
max_tokens_per_file = max_tokens / len(dir_list)
with open(current_file, "r") as f:
file_content = f.read()
default_params = config.default_params
default_params["max_tokens"] = settings.get("memory", {}).get(
"output", 1000
)
default_params["model"] = settings.get("active_model").get(
"active_model"
)
responder = llmcalls.get_responder(
(
config.api_keys["openai"]
if settings.get("active_model")
.get("active_model")
.startswith("gpt")
else config.api_keys["anthropic"]
),
settings.get("active_model").get("active_model"),
default_params,
)
async for response in responder.get_response(
username,
file_content,
function_metadata=config.fakedata,
role="summary_memory",
):
if response:
summary = response
else:
logger.error(
"Error: summary does not contain the required elements"
)
with open(current_file, "w") as f:
new_content = summary
f.write(new_content)
new_files_content_string += f"{file}:\n{new_content}\n\n"
files_content_string = new_files_content_string
if show:
return f"{files_content_string}"
else:
timestamp = await utils.SettingsManager.get_current_date_time(username)
process_dict["timestamp"] = timestamp
token_count = utils.MessageParser.num_tokens_from_string(message)
if token_count > 500:
default_params = config.default_params
default_params["max_tokens"] = settings.get("memory", {}).get(
"output", 1000
)
default_params["model"] = settings.get("active_model").get(
"active_model"
)
responder = llmcalls.get_responder(
(
config.api_keys["openai"]
if settings.get("active_model")