Skip to content

Commit e9f9790

Browse files
AtulJoshi1206copybara-github
authored andcommitted
fix: split mixed-script search queries in InMemoryMemoryService
Merge #8019 search_memory now tokenizes the query with the same function as stored events, so a Latin word inside Chinese or Japanese query text, such as Python in Pythonについて教えて, matches a memory containing that word. A mixed-script query token also counts its single-script runs, so such a query can rank an event slightly higher than before. Runs split out of a query word match whole words only, never as substrings. PiperOrigin-RevId: 1017511658
1 parent ddb812e commit e9f9790

2 files changed

Lines changed: 34 additions & 9 deletions

File tree

‎src/google/adk/memory/in_memory_memory_service.py‎

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,30 @@ def _extract_words_lower(text: str) -> set[str]:
5353

5454

5555
def _extract_searchable_words(text: str) -> set[str]:
56-
r"""Extracts the words an event can be matched on, in lowercase.
56+
r"""Extracts the words a query or an event is matched on, in lowercase.
5757
5858
The tokens of _extract_words_lower, plus, for a token that mixes Latin and
5959
non-Latin scripts, each of its single-script runs. Japanese and Chinese are
6060
written without spaces, so 私はPythonを使う is a single \w+ token and a query
6161
for Python matches nothing. Splitting on the boundary between Latin and
6262
non-Latin scripts makes the embedded Latin word a token of its own, while
63-
still keeping a partial word such as thon from matching.
63+
still keeping a partial word such as thon from matching. Queries and events
64+
go through this same function, so a query such as Pythonについて教えて finds
65+
an English memory about Python too.
6466
6567
Args:
66-
text: The text of an event.
68+
text: The text of a query or an event.
6769
"""
68-
words = _extract_words_lower(text)
69-
for word in list(words):
70+
words: set[str] = set()
71+
for word in re.findall(r'\w+', unicodedata.normalize('NFC', text)):
72+
words.add(word.lower())
7073
if not word.isascii():
71-
for _, group in itertools.groupby(word, _is_latin):
72-
words.add(''.join(group))
74+
# Split before lowercasing: "İ".lower() is "i" plus U+0307, which is
75+
# not Latin, so splitting afterwards yields a stray "i" word.
76+
words.update(
77+
''.join(group).lower()
78+
for _, group in itertools.groupby(word, _is_latin)
79+
)
7380
return words
7481

7582

@@ -159,7 +166,8 @@ async def search_memory(
159166
for events in self._session_events.get(user_key, {}).values()
160167
]
161168

162-
words_in_query = _extract_words_lower(query)
169+
whole_words_in_query = _extract_words_lower(query)
170+
words_in_query = _extract_searchable_words(query)
163171
scored_memories: list[tuple[int, MemoryEntry]] = []
164172

165173
for session_events in session_event_lists:
@@ -181,7 +189,13 @@ async def search_memory(
181189
1
182190
for query_word in words_in_query
183191
if query_word in words_in_event
184-
or (not query_word.isascii() and query_word in event_text_lower)
192+
# A script run split out of a query word, such as と from
193+
# Pythonと, would match nearly any Japanese text as a substring.
194+
or (
195+
query_word in whole_words_in_query
196+
and not query_word.isascii()
197+
and query_word in event_text_lower
198+
)
185199
)
186200
if matched_words:
187201
scored_memories.append((

‎tests/unittests/memory/test_in_memory_memory_service.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,17 @@ async def test_search_memory_does_not_collide_on_slash_in_identifiers():
424424
('私はPythonでADKを使っています', 'Java', 0),
425425
('私はmy_var_nameを使う', 'my_var_name', 1),
426426
('私はcafé_1を使う', 'café_1', 1),
427+
# Latin word inside an unspaced-script query, e.g. a user message
428+
# that preload_memory searches with
429+
('I write my backend in Python.', 'Pythonについて教えて', 1),
430+
('I write my backend in Python.', '我用Python写代码', 1),
431+
('I visited İstanbul last year.', 'İstanbul旅行', 1),
432+
('I write my backend in Python.', 'Javaについて教えて', 0),
433+
('I write my backend in Python.', 'thonについて教えて', 0),
434+
# A run split out of a query word matches whole words only
435+
('私と犬', 'Pythonと', 0),
436+
# "İ".lower() is "i" plus U+0307: no stray "i" word to match on
437+
('I like cats.', 'İstanbul旅行', 0),
427438
# Latin partial-word must NOT match (regression guard)
428439
('I like to code in Python.', 'thon', 0),
429440
('私はPythonでADKを使っています', 'thon', 0),

0 commit comments

Comments
 (0)