Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions src/memos/mem_feedback/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import json
import re

from difflib import SequenceMatcher

from memos.memories.textual.item import TextualMemoryItem, TreeNodeTextualMemoryMetadata


Expand Down Expand Up @@ -28,25 +30,27 @@ def estimate_tokens(text: str) -> int:


def should_keep_update(new_text: str, old_text: str) -> bool:
"""
Determine whether the update should be skipped
Rule:
1. If the length of old_text is less than 50 and the modification ratio is less than 50% => returns True
2. If the length of old_text is greater than or equal to 50 and the modification ratio is less than 15% => returns True
3. Return False in other cases
"""
"""Keep a non-identical update if its ordered edit ratio is below the length threshold."""

old_len = estimate_tokens(old_text)

def calculate_similarity(text1: str, text2: str) -> float:
set1 = set(text1)
set2 = set(text2)
if not set1 and not set2:
return 1.0

intersection = len(set1.intersection(set2))
union = len(set1.union(set2))
return intersection / union if union > 0 else 0.0
# Compare character order as well as content. A character-set score
# treats a complete reordering of facts as unchanged, which can let a
# destructive feedback update pass the small-edit guard.
# Repeated characters and words are meaningful in memory text.
character_similarity = SequenceMatcher(None, text1, text2, autojunk=False).ratio()

# Word-level matching catches fact reordering that still shares many
# character subsequences. For text without whitespace (for example,
# Chinese), the character-level score remains the useful signal.
words1 = text1.split()
words2 = text2.split()
if len(words1) > 1 and len(words2) > 1:
word_similarity = SequenceMatcher(None, words1, words2, autojunk=False).ratio()
return min(character_similarity, word_similarity)

return character_similarity

similarity = calculate_similarity(old_text, new_text)
change_ratio = 1 - similarity
Expand Down
19 changes: 19 additions & 0 deletions tests/mem_feedback/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,29 @@ def test_should_keep_update_accepts_small_edit():
assert should_keep_update(new_text, old_text)


def test_should_keep_update_accepts_small_edit_in_repetitive_chinese():
old_text = "春夏秋冬山川河流" * 70
new_text = old_text[:10] + "海" + old_text[11:]
assert should_keep_update(new_text, old_text)


def test_should_keep_update_accepts_small_edit_in_repetitive_english():
old_text = " ".join(["The customer prefers email notifications."] * 50)
new_text = old_text.replace("prefers", "uses", 1)
assert should_keep_update(new_text, old_text)


def test_should_keep_update_rejects_large_rewrite():
assert not should_keep_update("zzzzzzzzzz", "aaaaaaaaaa")


def test_should_keep_update_rejects_reordered_text_with_same_characters():
old_text = "alpha bravo charlie delta echo foxtrot golf hotel india juliet"
new_text = "juliet india hotel golf foxtrot echo delta charlie bravo alpha"

assert not should_keep_update(new_text, old_text)


def test_general_split_into_chunks_groups_small_items():
chunks = general_split_into_chunks(
[{"text": "short"}, {"text": "also short"}],
Expand Down
Loading