-
Notifications
You must be signed in to change notification settings - Fork 3k
/
memory_record.py
143 lines (123 loc) · 4.18 KB
/
memory_record.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
# Copyright (c) Microsoft. All rights reserved.
from datetime import datetime
from numpy import ndarray
from semantic_kernel.utils.experimental_decorator import experimental_class
@experimental_class
class MemoryRecord:
_key: str
_timestamp: datetime | None
_is_reference: bool
_external_source_name: str | None
_id: str
_description: str | None
_text: str | None
_additional_metadata: str | None
_embedding: ndarray
def __init__(
self,
is_reference: bool,
external_source_name: str | None,
id: str,
description: str | None,
text: str | None,
additional_metadata: str | None,
embedding: ndarray | None,
key: str | None = None,
timestamp: datetime | None = None,
) -> None:
"""Initialize a new instance of MemoryRecord.
Arguments:
is_reference {bool} -- Whether the record is a reference record.
external_source_name {Optional[str]} -- The name of the external source.
id {str} -- A unique for the record.
description {Optional[str]} -- The description of the record.
text {Optional[str]} -- The text of the record.
additional_metadata {Optional[str]} -- Custom metadata for the record.
embedding {ndarray} -- The embedding of the record.
Returns:
None -- None.
"""
self._key = key
self._timestamp = timestamp
self._is_reference = is_reference
self._external_source_name = external_source_name
self._id = id
self._description = description
self._text = text
self._additional_metadata = additional_metadata
self._embedding = embedding
@staticmethod
def reference_record(
external_id: str,
source_name: str,
description: str | None,
additional_metadata: str | None,
embedding: ndarray,
) -> "MemoryRecord":
"""Create a reference record.
Arguments:
external_id {str} -- The external id of the record.
source_name {str} -- The name of the external source.
description {Optional[str]} -- The description of the record.
additional_metadata {Optional[str]} -- Custom metadata for the record.
embedding {ndarray} -- The embedding of the record.
Returns:
MemoryRecord -- The reference record.
"""
return MemoryRecord(
is_reference=True,
external_source_name=source_name,
id=external_id,
description=description,
text=None,
additional_metadata=additional_metadata,
embedding=embedding,
)
@staticmethod
def local_record(
id: str,
text: str,
description: str | None,
additional_metadata: str | None,
embedding: ndarray,
timestamp: datetime | None = None,
) -> "MemoryRecord":
"""Create a local record.
Arguments:
id {str} -- A unique for the record.
text {str} -- The text of the record.
description {Optional[str]} -- The description of the record.
additional_metadata {Optional[str]} -- Custom metadata for the record.
embedding {ndarray} -- The embedding of the record.
timestamp {Optional[datetime]} -- The timestamp of the record.
Returns:
MemoryRecord -- The local record.
"""
return MemoryRecord(
is_reference=False,
external_source_name=None,
id=id,
description=description,
text=text,
additional_metadata=additional_metadata,
timestamp=timestamp,
embedding=embedding,
)
@property
def id(self):
return self._id
@property
def embedding(self) -> ndarray:
return self._embedding
@property
def text(self):
return self._text
@property
def additional_metadata(self):
return self._additional_metadata
@property
def description(self):
return self._description
@property
def timestamp(self):
return self._timestamp