|
24 | 24 |
|
25 | 25 | from typing_extensions import override |
26 | 26 |
|
27 | | -from . import _utils |
28 | | -from ..events.event import Event |
29 | | -from .base_memory_service import BaseMemoryService |
30 | | -from .base_memory_service import SearchMemoryResponse |
31 | | -from .memory_entry import MemoryEntry |
| 27 | +from ...memory import _utils |
| 28 | +from ...events.event import Event |
| 29 | +from ...memory.base_memory_service import BaseMemoryService |
| 30 | +from ...memory.base_memory_service import SearchMemoryResponse |
| 31 | +from ...memory.memory_entry import MemoryEntry |
32 | 32 |
|
33 | 33 | if TYPE_CHECKING: |
34 | 34 | from google.cloud import firestore |
35 | 35 |
|
36 | | - from ..sessions.session import Session |
| 36 | + from ...sessions.session import Session |
37 | 37 |
|
38 | 38 | logger = logging.getLogger("google_adk." + __name__) |
39 | 39 |
|
40 | 40 | DEFAULT_EVENTS_COLLECTION = "events" |
41 | 41 |
|
42 | | -# Standard English stop words |
43 | 42 | DEFAULT_STOP_WORDS = { |
44 | | - "a", |
45 | | - "an", |
46 | | - "the", |
47 | | - "and", |
48 | | - "or", |
49 | | - "but", |
50 | | - "if", |
51 | | - "then", |
52 | | - "else", |
53 | | - "to", |
54 | | - "of", |
55 | | - "in", |
56 | | - "on", |
57 | | - "for", |
58 | | - "with", |
59 | | - "is", |
60 | | - "are", |
61 | | - "was", |
62 | | - "were", |
63 | | - "be", |
64 | | - "been", |
65 | | - "being", |
66 | | - "have", |
67 | | - "has", |
68 | | - "had", |
69 | | - "do", |
70 | | - "does", |
71 | | - "did", |
72 | | - "can", |
73 | | - "could", |
74 | | - "will", |
75 | | - "would", |
76 | | - "should", |
77 | | - "shall", |
78 | | - "may", |
79 | | - "might", |
80 | | - "must", |
81 | | - "up", |
82 | | - "down", |
83 | | - "out", |
84 | | - "in", |
85 | | - "over", |
86 | | - "under", |
87 | | - "again", |
88 | | - "further", |
89 | | - "then", |
90 | | - "once", |
91 | | - "here", |
92 | | - "there", |
93 | | - "when", |
94 | | - "where", |
95 | | - "why", |
96 | | - "how", |
97 | | - "all", |
98 | | - "any", |
99 | | - "both", |
100 | | - "each", |
101 | | - "few", |
102 | | - "more", |
103 | | - "most", |
104 | | - "other", |
105 | | - "some", |
106 | | - "such", |
107 | | - "no", |
108 | | - "nor", |
109 | | - "not", |
110 | | - "only", |
111 | | - "own", |
112 | | - "same", |
113 | | - "so", |
114 | | - "than", |
115 | | - "too", |
116 | | - "very", |
117 | | - "i", |
118 | | - "me", |
119 | | - "my", |
120 | | - "myself", |
121 | | - "we", |
122 | | - "our", |
123 | | - "ours", |
124 | | - "ourselves", |
125 | | - "you", |
126 | | - "your", |
127 | | - "yours", |
128 | | - "yourself", |
129 | | - "yourselves", |
130 | | - "he", |
131 | | - "him", |
132 | | - "his", |
133 | | - "himself", |
134 | | - "she", |
135 | | - "her", |
136 | | - "hers", |
137 | | - "herself", |
138 | | - "it", |
139 | | - "its", |
140 | | - "itself", |
141 | | - "they", |
142 | | - "them", |
143 | | - "their", |
144 | | - "theirs", |
145 | | - "themselves", |
146 | | - "what", |
147 | | - "which", |
148 | | - "who", |
149 | | - "whom", |
150 | | - "this", |
151 | | - "that", |
152 | | - "these", |
153 | | - "those", |
154 | | - "am", |
155 | | - "is", |
156 | | - "are", |
157 | | - "was", |
158 | | - "were", |
159 | | - "be", |
160 | | - "been", |
161 | | - "being", |
162 | | - "have", |
163 | | - "has", |
164 | | - "had", |
165 | | - "having", |
166 | | - "do", |
167 | | - "does", |
168 | | - "did", |
169 | | - "doing", |
170 | | - "a", |
171 | | - "an", |
172 | | - "the", |
173 | | - "and", |
174 | | - "but", |
175 | | - "if", |
176 | | - "or", |
177 | | - "because", |
178 | | - "as", |
179 | | - "until", |
180 | | - "while", |
181 | | - "of", |
182 | | - "at", |
183 | | - "by", |
184 | | - "for", |
185 | | - "with", |
186 | | - "about", |
187 | | - "against", |
188 | | - "between", |
189 | | - "into", |
190 | | - "through", |
191 | | - "during", |
192 | | - "before", |
193 | | - "after", |
194 | | - "above", |
195 | | - "below", |
196 | | - "to", |
197 | | - "from", |
198 | | - "up", |
199 | | - "down", |
200 | | - "in", |
201 | | - "out", |
202 | | - "on", |
203 | | - "off", |
204 | | - "over", |
205 | | - "under", |
206 | | - "again", |
207 | | - "further", |
208 | | - "then", |
209 | | - "once", |
210 | | - "here", |
211 | | - "there", |
212 | | - "when", |
213 | | - "where", |
214 | | - "why", |
215 | | - "how", |
216 | | - "all", |
217 | | - "any", |
218 | | - "both", |
219 | | - "each", |
220 | | - "few", |
221 | | - "more", |
222 | | - "most", |
223 | | - "other", |
224 | | - "some", |
225 | | - "such", |
226 | | - "no", |
227 | | - "nor", |
228 | | - "not", |
229 | | - "only", |
230 | | - "own", |
231 | | - "same", |
232 | | - "so", |
233 | | - "than", |
234 | | - "too", |
235 | | - "very", |
236 | | - "s", |
237 | | - "t", |
238 | | - "can", |
239 | | - "will", |
240 | | - "just", |
241 | | - "don", |
242 | | - "should", |
243 | | - "now", |
| 43 | + "a", "an", "the", "and", "or", "but", "if", "then", "else", "to", "of", |
| 44 | + "in", "on", "for", "with", "is", "are", "was", "were", "be", "been", |
| 45 | + "being", "have", "has", "had", "do", "does", "did", "can", "could", |
| 46 | + "will", "would", "should", "shall", "may", "might", "must", "up", "down", |
| 47 | + "out", "in", "over", "under", "again", "further", "then", "once", "here", |
| 48 | + "there", "when", "where", "why", "how", "all", "any", "both", "each", |
| 49 | + "few", "more", "most", "other", "some", "such", "no", "nor", "not", |
| 50 | + "only", "own", "same", "so", "than", "too", "very", "i", "me", "my", |
| 51 | + "myself", "we", "our", "ours", "ourselves", "you", "your", "yours", |
| 52 | + "yourself", "yourselves", "he", "him", "his", "himself", "she", "her", |
| 53 | + "hers", "herself", "it", "its", "itself", "they", "them", "their", |
| 54 | + "theirs", "themselves", "what", "which", "who", "whom", "this", "that", |
| 55 | + "these", "those", "am", "is", "are", "was", "were", "be", "been", "being", |
| 56 | + "have", "has", "had", "having", "do", "does", "did", "doing", "a", "an", |
| 57 | + "the", "and", "but", "if", "or", "because", "as", "until", "while", "of", |
| 58 | + "at", "by", "for", "with", "about", "against", "between", "into", |
| 59 | + "through", "during", "before", "after", "above", "below", "to", "from", |
| 60 | + "up", "down", "in", "out", "on", "off", "over", "under", "again", |
| 61 | + "further", "then", "once", "here", "there", "when", "where", "why", "how", |
| 62 | + "all", "any", "both", "each", "few", "more", "most", "other", "some", |
| 63 | + "such", "no", "nor", "not", "only", "own", "same", "so", "than", "too", |
| 64 | + "very", "s", "t", "can", "will", "just", "don", "should", "now", |
244 | 65 | } |
245 | 66 |
|
246 | 67 |
|
@@ -288,8 +109,6 @@ async def _search_by_keyword( |
288 | 109 | self, app_name: str, user_id: str, keyword: str |
289 | 110 | ) -> list[MemoryEntry]: |
290 | 111 | """Searches for events matching a single keyword.""" |
291 | | - # This requires a collection group index in Firestore for 'events' with |
292 | | - # appName == X, userId == Y, and keywords array-contains Z. |
293 | 112 | query = ( |
294 | 113 | self.client.collection_group(self.events_collection) |
295 | 114 | .where("appName", "==", app_name) |
@@ -326,26 +145,16 @@ async def search_memory( |
326 | 145 | if not keywords: |
327 | 146 | return SearchMemoryResponse() |
328 | 147 |
|
329 | | - # Search for each keyword concurrently |
330 | 148 | tasks = [ |
331 | 149 | self._search_by_keyword(app_name, user_id, keyword) |
332 | 150 | for keyword in keywords |
333 | 151 | ] |
334 | 152 | results = await asyncio.gather(*tasks) |
335 | 153 |
|
336 | | - # Merge results and deduplicate by MemoryEntry content/author/timestamp |
337 | | - # (MemoryEntry is not hashable by default if it contains complex objects, |
338 | | - # so we might need to deduplicate by id if available, or by content string). |
339 | | - # Since we convert Event to MemoryEntry, we don't have event.id in MemoryEntry |
340 | | - # unless we add it. The Java code use custom hash/equals for MemoryEntry. |
341 | | - # In Python, MemoryEntry is a Pydantic model. We can deduplicate by model_dump_json() |
342 | | - # or by a custom key. |
343 | 154 | seen = set() |
344 | 155 | memories = [] |
345 | 156 | for result_list in results: |
346 | 157 | for entry in result_list: |
347 | | - # Deduplicate by a key of (author, content_text) |
348 | | - # Content might be complex, so let's use its json representation or text |
349 | 158 | content_text = "" |
350 | 159 | if entry.content and entry.content.parts: |
351 | 160 | content_text = " ".join( |
|
0 commit comments