1
+ import asyncio
1
2
from abc import abstractmethod
2
3
from typing import Any , List , Optional
3
4
11
12
12
13
13
14
class BaseMemory (BaseComponent ):
14
- """Base class for all memory types.
15
-
16
- NOTE: The interface for memory is not yet finalized and is subject to change.
17
- """
15
+ """Base class for all memory types."""
18
16
19
17
@classmethod
20
18
def class_name (cls ) -> str :
@@ -33,17 +31,27 @@ def from_defaults(
33
31
def get (self , input : Optional [str ] = None , ** kwargs : Any ) -> List [ChatMessage ]:
34
32
"""Get chat history."""
35
33
34
+ async def aget (
35
+ self , input : Optional [str ] = None , ** kwargs : Any
36
+ ) -> List [ChatMessage ]:
37
+ """Get chat history."""
38
+ return await asyncio .to_thread (self .get , input = input , ** kwargs )
39
+
36
40
@abstractmethod
37
41
def get_all (self ) -> List [ChatMessage ]:
38
42
"""Get all chat history."""
39
43
44
+ async def aget_all (self ) -> List [ChatMessage ]:
45
+ """Get all chat history."""
46
+ return await asyncio .to_thread (self .get_all )
47
+
40
48
@abstractmethod
41
49
def put (self , message : ChatMessage ) -> None :
42
50
"""Put chat history."""
43
51
44
52
async def aput (self , message : ChatMessage ) -> None :
45
53
"""Put chat history."""
46
- self .put ( message )
54
+ await asyncio . to_thread ( self .put , message )
47
55
48
56
def put_messages (self , messages : List [ChatMessage ]) -> None :
49
57
"""Put chat history."""
@@ -52,23 +60,27 @@ def put_messages(self, messages: List[ChatMessage]) -> None:
52
60
53
61
async def aput_messages (self , messages : List [ChatMessage ]) -> None :
54
62
"""Put chat history."""
55
- for message in messages :
56
- await self .aput (message )
63
+ await asyncio .to_thread (self .put_messages , messages )
57
64
58
65
@abstractmethod
59
66
def set (self , messages : List [ChatMessage ]) -> None :
60
67
"""Set chat history."""
61
68
69
+ async def aset (self , messages : List [ChatMessage ]) -> None :
70
+ """Set chat history."""
71
+ await asyncio .to_thread (self .set , messages )
72
+
62
73
@abstractmethod
63
74
def reset (self ) -> None :
64
75
"""Reset chat history."""
65
76
77
+ async def areset (self ) -> None :
78
+ """Reset chat history."""
79
+ await asyncio .to_thread (self .reset )
80
+
66
81
67
82
class BaseChatStoreMemory (BaseMemory ):
68
- """Base class for any .
69
-
70
- NOTE: The interface for memory is not yet finalized and is subject to change.
71
- """
83
+ """Base class for storing multi-tenant chat history."""
72
84
73
85
chat_store : SerializeAsAny [BaseChatStore ] = Field (default_factory = SimpleChatStore )
74
86
chat_store_key : str = Field (default = DEFAULT_CHAT_STORE_KEY )
@@ -98,6 +110,20 @@ def get_all(self) -> List[ChatMessage]:
98
110
"""Get all chat history."""
99
111
return self .chat_store .get_messages (self .chat_store_key )
100
112
113
+ async def aget_all (self ) -> List [ChatMessage ]:
114
+ """Get all chat history."""
115
+ return await self .chat_store .aget_messages (self .chat_store_key )
116
+
117
+ def get (self , input : Optional [str ] = None , ** kwargs : Any ) -> List [ChatMessage ]:
118
+ """Get chat history."""
119
+ return self .chat_store .get_messages (self .chat_store_key , ** kwargs )
120
+
121
+ async def aget (
122
+ self , input : Optional [str ] = None , ** kwargs : Any
123
+ ) -> List [ChatMessage ]:
124
+ """Get chat history."""
125
+ return await self .chat_store .aget_messages (self .chat_store_key , ** kwargs )
126
+
101
127
def put (self , message : ChatMessage ) -> None :
102
128
"""Put chat history."""
103
129
# ensure everything is serialized
@@ -112,6 +138,15 @@ def set(self, messages: List[ChatMessage]) -> None:
112
138
"""Set chat history."""
113
139
self .chat_store .set_messages (self .chat_store_key , messages )
114
140
141
+ async def aset (self , messages : List [ChatMessage ]) -> None :
142
+ """Set chat history."""
143
+ # ensure everything is serialized
144
+ await self .chat_store .aset_messages (self .chat_store_key , messages )
145
+
115
146
def reset (self ) -> None :
116
147
"""Reset chat history."""
117
148
self .chat_store .delete_messages (self .chat_store_key )
149
+
150
+ async def areset (self ) -> None :
151
+ """Reset chat history."""
152
+ await self .chat_store .adelete_messages (self .chat_store_key )
0 commit comments