-
Notifications
You must be signed in to change notification settings - Fork 817
Expand file tree
/
Copy pathsearch_handler.py
More file actions
828 lines (665 loc) · 31.8 KB
/
search_handler.py
File metadata and controls
828 lines (665 loc) · 31.8 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
"""
Search handler for memory search functionality (Class-based version).
This module provides a class-based implementation of search handlers,
using dependency injection for better modularity and testability.
"""
import copy
import math
from typing import Any
from memos.api.handlers.base_handler import BaseHandler, HandlerDependencies
from memos.api.handlers.cube_scope import resolve_cube_ids
from memos.api.handlers.formatters_handler import rerank_knowledge_mem
from memos.api.product_models import APISearchRequest, SearchResponse
from memos.log import get_logger
from memos.memories.textual.tree_text_memory.retrieve.retrieve_utils import (
cosine_similarity_matrix,
)
from memos.multi_mem_cube.composite_cube import CompositeCubeView
from memos.multi_mem_cube.single_cube import SingleCubeView
from memos.multi_mem_cube.views import MemCubeView
logger = get_logger(__name__)
class SearchHandler(BaseHandler):
"""
Handler for memory search operations.
Provides fast, fine-grained, and mixture-based search modes.
"""
def __init__(self, dependencies: HandlerDependencies):
"""
Initialize search handler.
Args:
dependencies: HandlerDependencies instance
"""
super().__init__(dependencies)
self._validate_dependencies(
"naive_mem_cube", "mem_scheduler", "searcher", "deepsearch_agent"
)
def handle_search_memories(self, search_req: APISearchRequest) -> SearchResponse:
"""
Main handler for search memories endpoint.
Orchestrates the search process based on the requested search mode,
supporting text memory searches.
Args:
search_req: Search request containing query and parameters
Returns:
SearchResponse with formatted results
"""
self.logger.info(f"[SearchHandler] Search Req is: {search_req}")
# Use deepcopy to avoid modifying the original request object
search_req_local = copy.deepcopy(search_req)
# Expand top_k for deduplication (5x to ensure enough candidates)
if search_req_local.dedup in ("sim", "mmr"):
search_req_local.top_k = search_req_local.top_k * 3
# Search and deduplicate
cube_view = self._build_cube_view(search_req_local)
results = cube_view.search_memories(search_req_local)
if not search_req_local.relativity:
search_req_local.relativity = 0
self.logger.info(f"[SearchHandler] Relativity filter: {search_req_local.relativity}")
results = self._apply_relativity_threshold(results, search_req_local.relativity)
if search_req_local.dedup == "sim":
results = self._dedup_text_memories(results, search_req.top_k)
self._strip_embeddings(results)
elif search_req_local.dedup == "mmr":
pref_top_k = getattr(search_req_local, "pref_top_k", 6)
results = self._mmr_dedup_text_memories(results, search_req.top_k, pref_top_k)
self._strip_embeddings(results)
text_mem = results["text_mem"]
results["text_mem"] = rerank_knowledge_mem(
self.reranker,
query=search_req.query,
text_mem=text_mem,
top_k=search_req_local.top_k,
file_mem_proportion=0.5,
)
self.logger.info(
f"[SearchHandler] Final search results: count={len(results)} results={results}"
)
return SearchResponse(
message="Search completed successfully",
data=results,
)
@staticmethod
def _apply_relativity_threshold(results: dict[str, Any], relativity: float) -> dict[str, Any]:
if relativity <= 0:
return results
for key in ("text_mem", "pref_mem"):
buckets = results.get(key)
if not isinstance(buckets, list):
continue
for bucket in buckets:
memories = bucket.get("memories")
if not isinstance(memories, list):
continue
filtered: list[dict[str, Any]] = []
for mem in memories:
if not isinstance(mem, dict):
continue
meta = mem.get("metadata", {})
score = meta.get("relativity", 1.0) if isinstance(meta, dict) else 1.0
try:
score_val = float(score) if score is not None else 1.0
except (TypeError, ValueError):
score_val = 1.0
if score_val >= relativity:
filtered.append(mem)
bucket["memories"] = filtered
if "total_nodes" in bucket:
bucket["total_nodes"] = len(filtered)
return results
def _dedup_text_memories(self, results: dict[str, Any], target_top_k: int) -> dict[str, Any]:
buckets = results.get("text_mem", [])
if not buckets:
return results
flat: list[tuple[int, dict[str, Any], float]] = []
for bucket_idx, bucket in enumerate(buckets):
for mem in bucket.get("memories", []):
score = mem.get("metadata", {}).get("relativity", 0.0)
flat.append((bucket_idx, mem, score))
if len(flat) <= 1:
return results
embeddings = self._extract_embeddings([mem for _, mem, _ in flat])
similarity_matrix = cosine_similarity_matrix(embeddings)
indices_by_bucket: dict[int, list[int]] = {i: [] for i in range(len(buckets))}
for flat_index, (bucket_idx, _, _) in enumerate(flat):
indices_by_bucket[bucket_idx].append(flat_index)
selected_global: list[int] = []
selected_by_bucket: dict[int, list[int]] = {i: [] for i in range(len(buckets))}
ordered_indices = sorted(range(len(flat)), key=lambda idx: flat[idx][2], reverse=True)
for idx in ordered_indices:
bucket_idx = flat[idx][0]
if len(selected_by_bucket[bucket_idx]) >= target_top_k:
continue
# Use 0.92 threshold strictly
if self._is_unrelated(idx, selected_global, similarity_matrix, 0.92):
selected_by_bucket[bucket_idx].append(idx)
selected_global.append(idx)
# Removed the 'filling' logic that was pulling back similar items.
# Now it will only return items that truly pass the 0.92 threshold,
# up to target_top_k.
for bucket_idx, bucket in enumerate(buckets):
selected_indices = selected_by_bucket.get(bucket_idx, [])
bucket["memories"] = [flat[i][1] for i in selected_indices]
return results
def _mmr_dedup_text_memories(
self, results: dict[str, Any], text_top_k: int, pref_top_k: int = 6
) -> dict[str, Any]:
"""
MMR-based deduplication with progressive penalty for high similarity.
Performs deduplication on both text_mem and preference memories together.
Other memory types (tool_mem, etc.) are not modified.
Args:
results: Search results containing text_mem and preference buckets
text_top_k: Target number of text memories to return per bucket
pref_top_k: Target number of preference memories to return per bucket
Algorithm:
1. Prefill top 5 by relevance
2. MMR selection: balance relevance vs diversity
3. Re-sort by original relevance for better generation quality
"""
text_buckets = results.get("text_mem", [])
pref_buckets = results.get("pref_mem", [])
# Early return if no memories to deduplicate
if not text_buckets and not pref_buckets:
return results
# Flatten all memories with their type and scores
# flat structure: (memory_type, bucket_idx, mem, score)
flat: list[tuple[str, int, dict[str, Any], float]] = []
# Flatten text memories
for bucket_idx, bucket in enumerate(text_buckets):
for mem in bucket.get("memories", []):
score = mem.get("metadata", {}).get("relativity", 0.0)
flat.append(("text", bucket_idx, mem, float(score) if score is not None else 0.0))
# Flatten preference memories
for bucket_idx, bucket in enumerate(pref_buckets):
for mem in bucket.get("memories", []):
meta = mem.get("metadata", {})
if isinstance(meta, dict):
score = meta.get("score", meta.get("relativity", 0.0))
else:
score = 0.0
flat.append(
("preference", bucket_idx, mem, float(score) if score is not None else 0.0)
)
if len(flat) <= 1:
return results
total_by_type: dict[str, int] = {"text": 0, "preference": 0}
existing_by_type: dict[str, int] = {"text": 0, "preference": 0}
missing_by_type: dict[str, int] = {"text": 0, "preference": 0}
missing_indices: list[int] = []
for idx, (mem_type, _, mem, _) in enumerate(flat):
if mem_type not in total_by_type:
total_by_type[mem_type] = 0
existing_by_type[mem_type] = 0
missing_by_type[mem_type] = 0
total_by_type[mem_type] += 1
embedding = mem.get("metadata", {}).get("embedding")
if embedding:
existing_by_type[mem_type] += 1
else:
missing_by_type[mem_type] += 1
missing_indices.append(idx)
self.logger.info(
"[SearchHandler] MMR embedding metadata scan: total=%s total_by_type=%s existing_by_type=%s missing_by_type=%s",
len(flat),
total_by_type,
existing_by_type,
missing_by_type,
)
if missing_indices:
self.logger.warning(
"[SearchHandler] MMR embedding metadata missing; will compute missing embeddings: missing_total=%s",
len(missing_indices),
)
# Get or compute embeddings
embeddings = self._extract_embeddings([mem for _, _, mem, _ in flat])
# Compute similarity matrix using NumPy-optimized method
# Returns numpy array but compatible with list[i][j] indexing
similarity_matrix = cosine_similarity_matrix(embeddings)
# Initialize selection tracking for both text and preference
text_indices_by_bucket: dict[int, list[int]] = {i: [] for i in range(len(text_buckets))}
pref_indices_by_bucket: dict[int, list[int]] = {i: [] for i in range(len(pref_buckets))}
for flat_index, (mem_type, bucket_idx, _, _) in enumerate(flat):
if mem_type == "text":
text_indices_by_bucket[bucket_idx].append(flat_index)
elif mem_type == "preference":
pref_indices_by_bucket[bucket_idx].append(flat_index)
selected_global: list[int] = []
text_selected_by_bucket: dict[int, list[int]] = {i: [] for i in range(len(text_buckets))}
pref_selected_by_bucket: dict[int, list[int]] = {i: [] for i in range(len(pref_buckets))}
selected_texts: set[str] = set() # Track exact text content to avoid duplicates
# Phase 1: Prefill top N by relevance
# Use the smaller of text_top_k and pref_top_k for prefill count
prefill_top_n = min(2, text_top_k, pref_top_k) if pref_buckets else min(2, text_top_k)
ordered_by_relevance = sorted(range(len(flat)), key=lambda idx: flat[idx][3], reverse=True)
for idx in ordered_by_relevance[: len(flat)]:
if len(selected_global) >= prefill_top_n:
break
mem_type, bucket_idx, mem, _ = flat[idx]
# Skip if exact text already exists in selected set
mem_text = mem.get("memory", "").strip()
if mem_text in selected_texts:
continue
# Skip if highly similar (Dice + TF-IDF + 2-gram combined, with embedding filter)
if SearchHandler._is_text_highly_similar_optimized(
idx, mem_text, selected_global, similarity_matrix, flat, threshold=0.92
):
continue
# Check bucket capacity with correct top_k for each type
if mem_type == "text" and len(text_selected_by_bucket[bucket_idx]) < text_top_k:
selected_global.append(idx)
text_selected_by_bucket[bucket_idx].append(idx)
selected_texts.add(mem_text)
elif mem_type == "preference" and len(pref_selected_by_bucket[bucket_idx]) < pref_top_k:
selected_global.append(idx)
pref_selected_by_bucket[bucket_idx].append(idx)
selected_texts.add(mem_text)
# Phase 2: MMR selection for remaining slots
lambda_relevance = 0.8
similarity_threshold = 0.9 # Start exponential penalty from 0.9 (lowered from 0.9)
alpha_exponential = 10.0 # Exponential penalty coefficient
remaining = set(range(len(flat))) - set(selected_global)
while remaining:
best_idx: int | None = None
best_mmr: float | None = None
for idx in remaining:
mem_type, bucket_idx, mem, _ = flat[idx]
# Check bucket capacity with correct top_k for each type
if (
mem_type == "text" and len(text_selected_by_bucket[bucket_idx]) >= text_top_k
) or (
mem_type == "preference"
and len(pref_selected_by_bucket[bucket_idx]) >= pref_top_k
):
continue
# Check if exact text already exists - if so, skip this candidate entirely
mem_text = mem.get("memory", "").strip()
if mem_text in selected_texts:
continue # Skip duplicate text, don't participate in MMR competition
# Skip if highly similar (Dice + TF-IDF + 2-gram combined, with embedding filter)
if SearchHandler._is_text_highly_similar_optimized(
idx, mem_text, selected_global, similarity_matrix, flat, threshold=0.92
):
continue # Skip highly similar text, don't participate in MMR competition
relevance = flat[idx][3]
max_sim = (
0.0
if not selected_global
else max(similarity_matrix[idx][j] for j in selected_global)
)
# Exponential penalty for similarity > 0.80
if max_sim > similarity_threshold:
penalty_multiplier = math.exp(
alpha_exponential * (max_sim - similarity_threshold)
)
diversity = max_sim * penalty_multiplier
else:
diversity = max_sim
mmr_score = lambda_relevance * relevance - (1.0 - lambda_relevance) * diversity
if best_mmr is None or mmr_score > best_mmr:
best_mmr = mmr_score
best_idx = idx
if best_idx is None:
break
mem_type, bucket_idx, mem, _ = flat[best_idx]
# Add to selected set and track text
mem_text = mem.get("memory", "").strip()
selected_global.append(best_idx)
selected_texts.add(mem_text)
if mem_type == "text":
text_selected_by_bucket[bucket_idx].append(best_idx)
elif mem_type == "preference":
pref_selected_by_bucket[bucket_idx].append(best_idx)
remaining.remove(best_idx)
# Early termination: all buckets are full
text_all_full = all(
len(text_selected_by_bucket[b_idx]) >= min(text_top_k, len(bucket_indices))
for b_idx, bucket_indices in text_indices_by_bucket.items()
)
pref_all_full = all(
len(pref_selected_by_bucket[b_idx]) >= min(pref_top_k, len(bucket_indices))
for b_idx, bucket_indices in pref_indices_by_bucket.items()
)
if text_all_full and pref_all_full:
break
# Phase 3: Re-sort by original relevance and fill back to buckets
for bucket_idx, bucket in enumerate(text_buckets):
selected_indices = text_selected_by_bucket.get(bucket_idx, [])
selected_indices = sorted(selected_indices, key=lambda i: flat[i][3], reverse=True)
bucket["memories"] = [flat[i][2] for i in selected_indices]
for bucket_idx, bucket in enumerate(pref_buckets):
selected_indices = pref_selected_by_bucket.get(bucket_idx, [])
selected_indices = sorted(selected_indices, key=lambda i: flat[i][3], reverse=True)
bucket["memories"] = [flat[i][2] for i in selected_indices]
return results
@staticmethod
def _is_unrelated(
index: int,
selected_indices: list[int],
similarity_matrix: list[list[float]],
similarity_threshold: float,
) -> bool:
return all(similarity_matrix[index][j] <= similarity_threshold for j in selected_indices)
@staticmethod
def _max_similarity(
index: int, selected_indices: list[int], similarity_matrix: list[list[float]]
) -> float:
if not selected_indices:
return 0.0
return max(similarity_matrix[index][j] for j in selected_indices)
def _extract_embeddings(self, memories: list[dict[str, Any]]) -> list[list[float]]:
embeddings: list[list[float]] = []
missing_indices: list[int] = []
missing_documents: list[str] = []
for idx, mem in enumerate(memories):
metadata = mem.get("metadata")
if not isinstance(metadata, dict):
metadata = {}
mem["metadata"] = metadata
embedding = metadata.get("embedding")
if embedding:
embeddings.append(embedding)
continue
embeddings.append([])
missing_indices.append(idx)
missing_documents.append(mem.get("memory", ""))
if missing_indices:
computed = self.searcher.embedder.embed(missing_documents)
for idx, embedding in zip(missing_indices, computed, strict=False):
embeddings[idx] = embedding
memories[idx]["metadata"]["embedding"] = embedding
return embeddings
@staticmethod
def _strip_embeddings(results: dict[str, Any]) -> None:
for _mem_type, mem_results in results.items():
if isinstance(mem_results, list):
for bucket in mem_results:
for mem in bucket.get("memories", []):
metadata = mem.get("metadata", {})
if "embedding" in metadata:
metadata["embedding"] = []
@staticmethod
def _dice_similarity(text1: str, text2: str) -> float:
"""
Calculate Dice coefficient (character-level, fastest).
Dice = 2 * |A ∩ B| / (|A| + |B|)
Speed: O(n + m), ~0.05-0.1ms per comparison
Args:
text1: First text string
text2: Second text string
Returns:
Dice similarity score between 0.0 and 1.0
"""
if not text1 or not text2:
return 0.0
chars1 = set(text1)
chars2 = set(text2)
intersection = len(chars1 & chars2)
return 2 * intersection / (len(chars1) + len(chars2))
@staticmethod
def _bigram_similarity(text1: str, text2: str) -> float:
"""
Calculate character-level 2-gram Jaccard similarity.
Speed: O(n + m), ~0.1-0.2ms per comparison
Considers local order (more strict than Dice).
Args:
text1: First text string
text2: Second text string
Returns:
Jaccard similarity score between 0.0 and 1.0
"""
if not text1 or not text2:
return 0.0
# Generate 2-grams
bigrams1 = {text1[i : i + 2] for i in range(len(text1) - 1)} if len(text1) >= 2 else {text1}
bigrams2 = {text2[i : i + 2] for i in range(len(text2) - 1)} if len(text2) >= 2 else {text2}
intersection = len(bigrams1 & bigrams2)
union = len(bigrams1 | bigrams2)
return intersection / union if union > 0 else 0.0
@staticmethod
def _tfidf_similarity(text1: str, text2: str) -> float:
"""
Calculate TF-IDF cosine similarity (character-level, no sklearn).
Speed: O(n + m), ~0.3-0.5ms per comparison
Considers character frequency weighting.
Args:
text1: First text string
text2: Second text string
Returns:
Cosine similarity score between 0.0 and 1.0
"""
if not text1 or not text2:
return 0.0
from collections import Counter
# Character frequency (TF)
tf1 = Counter(text1)
tf2 = Counter(text2)
# All unique characters (vocabulary)
vocab = set(tf1.keys()) | set(tf2.keys())
# Simple IDF: log(2 / df) where df is document frequency
# For two documents, IDF is log(2/1)=0.693 if char appears in one doc,
# or log(2/2)=0 if appears in both (we use log(2/1) for simplicity)
idf = {char: (1.0 if char in tf1 and char in tf2 else 1.5) for char in vocab}
# TF-IDF vectors
vec1 = {char: tf1.get(char, 0) * idf[char] for char in vocab}
vec2 = {char: tf2.get(char, 0) * idf[char] for char in vocab}
# Cosine similarity
dot_product = sum(vec1[char] * vec2[char] for char in vocab)
norm1 = math.sqrt(sum(v * v for v in vec1.values()))
norm2 = math.sqrt(sum(v * v for v in vec2.values()))
if norm1 == 0 or norm2 == 0:
return 0.0
return dot_product / (norm1 * norm2)
@staticmethod
def _is_text_highly_similar_optimized(
candidate_idx: int,
candidate_text: str,
selected_global: list[int],
similarity_matrix,
flat: list,
threshold: float = 0.9,
) -> bool:
"""
Multi-algorithm text similarity check with embedding pre-filtering.
Strategy:
1. Only compare with the single highest embedding similarity item (not all 25)
2. Only perform text comparison if embedding similarity > 0.60
3. Use weighted combination of three algorithms:
- Dice (40%): Fastest, character-level set similarity
- TF-IDF (35%): Considers character frequency weighting
- 2-gram (25%): Considers local character order
Combined formula:
combined_score = 0.40 * dice + 0.35 * tfidf + 0.25 * bigram
This reduces comparisons from O(N) to O(1) per candidate, with embedding pre-filtering.
Expected speedup: 100-200x compared to LCS approach.
Args:
candidate_idx: Index of candidate memory in flat list
candidate_text: Text content of candidate memory
selected_global: List of already selected memory indices
similarity_matrix: Precomputed embedding similarity matrix
flat: Flat list of all memories
threshold: Combined similarity threshold (default 0.75)
Returns:
True if candidate is highly similar to any selected memory
"""
if not selected_global:
return False
# Find the already-selected memory with highest embedding similarity
max_sim_idx = max(selected_global, key=lambda j: similarity_matrix[candidate_idx][j])
max_sim = similarity_matrix[candidate_idx][max_sim_idx]
# If highest embedding similarity < 0.60, skip text comparison entirely
if max_sim <= 0.9:
return False
# Get text of most similar memory
most_similar_mem = flat[max_sim_idx][2]
most_similar_text = most_similar_mem.get("memory", "").strip()
# Calculate three similarity scores
dice_sim = SearchHandler._dice_similarity(candidate_text, most_similar_text)
tfidf_sim = SearchHandler._tfidf_similarity(candidate_text, most_similar_text)
bigram_sim = SearchHandler._bigram_similarity(candidate_text, most_similar_text)
# Weighted combination: Dice (40%) + TF-IDF (35%) + 2-gram (25%)
# Dice has highest weight (fastest and most reliable)
# TF-IDF considers frequency (handles repeated characters well)
# 2-gram considers order (catches local pattern similarity)
combined_score = 0.40 * dice_sim + 0.35 * tfidf_sim + 0.25 * bigram_sim
return combined_score >= threshold
@staticmethod
def _dice_similarity(text1: str, text2: str) -> float:
"""
Calculate Dice coefficient (character-level, fastest).
Dice = 2 * |A ∩ B| / (|A| + |B|)
Speed: O(n + m), ~0.05-0.1ms per comparison
Args:
text1: First text string
text2: Second text string
Returns:
Dice similarity score between 0.0 and 1.0
"""
if not text1 or not text2:
return 0.0
chars1 = set(text1)
chars2 = set(text2)
intersection = len(chars1 & chars2)
return 2 * intersection / (len(chars1) + len(chars2))
@staticmethod
def _bigram_similarity(text1: str, text2: str) -> float:
"""
Calculate character-level 2-gram Jaccard similarity.
Speed: O(n + m), ~0.1-0.2ms per comparison
Considers local order (more strict than Dice).
Args:
text1: First text string
text2: Second text string
Returns:
Jaccard similarity score between 0.0 and 1.0
"""
if not text1 or not text2:
return 0.0
# Generate 2-grams
bigrams1 = {text1[i : i + 2] for i in range(len(text1) - 1)} if len(text1) >= 2 else {text1}
bigrams2 = {text2[i : i + 2] for i in range(len(text2) - 1)} if len(text2) >= 2 else {text2}
intersection = len(bigrams1 & bigrams2)
union = len(bigrams1 | bigrams2)
return intersection / union if union > 0 else 0.0
@staticmethod
def _tfidf_similarity(text1: str, text2: str) -> float:
"""
Calculate TF-IDF cosine similarity (character-level, no sklearn).
Speed: O(n + m), ~0.3-0.5ms per comparison
Considers character frequency weighting.
Args:
text1: First text string
text2: Second text string
Returns:
Cosine similarity score between 0.0 and 1.0
"""
if not text1 or not text2:
return 0.0
from collections import Counter
# Character frequency (TF)
tf1 = Counter(text1)
tf2 = Counter(text2)
# All unique characters (vocabulary)
vocab = set(tf1.keys()) | set(tf2.keys())
# Simple IDF: log(2 / df) where df is document frequency
# For two documents, IDF is log(2/1)=0.693 if char appears in one doc,
# or log(2/2)=0 if appears in both (we use log(2/1) for simplicity)
idf = {char: (1.0 if char in tf1 and char in tf2 else 1.5) for char in vocab}
# TF-IDF vectors
vec1 = {char: tf1.get(char, 0) * idf[char] for char in vocab}
vec2 = {char: tf2.get(char, 0) * idf[char] for char in vocab}
# Cosine similarity
dot_product = sum(vec1[char] * vec2[char] for char in vocab)
norm1 = math.sqrt(sum(v * v for v in vec1.values()))
norm2 = math.sqrt(sum(v * v for v in vec2.values()))
if norm1 == 0 or norm2 == 0:
return 0.0
return dot_product / (norm1 * norm2)
@staticmethod
def _is_text_highly_similar_optimized(
candidate_idx: int,
candidate_text: str,
selected_global: list[int],
similarity_matrix,
flat: list,
threshold: float = 0.92,
) -> bool:
"""
Multi-algorithm text similarity check with embedding pre-filtering.
Strategy:
1. Only compare with the single highest embedding similarity item (not all 25)
2. Only perform text comparison if embedding similarity > 0.60
3. Use weighted combination of three algorithms:
- Dice (40%): Fastest, character-level set similarity
- TF-IDF (35%): Considers character frequency weighting
- 2-gram (25%): Considers local character order
Combined formula:
combined_score = 0.40 * dice + 0.35 * tfidf + 0.25 * bigram
This reduces comparisons from O(N) to O(1) per candidate, with embedding pre-filtering.
Expected speedup: 100-200x compared to LCS approach.
Args:
candidate_idx: Index of candidate memory in flat list
candidate_text: Text content of candidate memory
selected_global: List of already selected memory indices
similarity_matrix: Precomputed embedding similarity matrix
flat: Flat list of all memories
threshold: Combined similarity threshold (default 0.75)
Returns:
True if candidate is highly similar to any selected memory
"""
if not selected_global:
return False
# Find the already-selected memory with highest embedding similarity
max_sim_idx = max(selected_global, key=lambda j: similarity_matrix[candidate_idx][j])
max_sim = similarity_matrix[candidate_idx][max_sim_idx]
# If highest embedding similarity < 0.60, skip text comparison entirely
if max_sim <= 0.9:
return False
# Get text of most similar memory
most_similar_mem = flat[max_sim_idx][2]
most_similar_text = most_similar_mem.get("memory", "").strip()
# Calculate three similarity scores
dice_sim = SearchHandler._dice_similarity(candidate_text, most_similar_text)
tfidf_sim = SearchHandler._tfidf_similarity(candidate_text, most_similar_text)
bigram_sim = SearchHandler._bigram_similarity(candidate_text, most_similar_text)
# Weighted combination: Dice (40%) + TF-IDF (35%) + 2-gram (25%)
# Dice has highest weight (fastest and most reliable)
# TF-IDF considers frequency (handles repeated characters well)
# 2-gram considers order (catches local pattern similarity)
combined_score = 0.40 * dice_sim + 0.35 * tfidf_sim + 0.25 * bigram_sim
return combined_score >= threshold
def _resolve_cube_ids(self, search_req: APISearchRequest) -> list[str]:
"""
Normalize target cube ids from search_req.
Priority:
1) readable_cube_ids (deprecated mem_cube_id is converted to this in model validator)
2) fallback to user_id
"""
return resolve_cube_ids(search_req.readable_cube_ids, search_req.user_id)
def _build_cube_view(self, search_req: APISearchRequest, searcher=None) -> MemCubeView:
cube_ids = self._resolve_cube_ids(search_req)
searcher_to_use = searcher if searcher is not None else self.searcher
if len(cube_ids) == 1:
cube_id = cube_ids[0]
return SingleCubeView(
cube_id=cube_id,
naive_mem_cube=self.naive_mem_cube,
mem_reader=self.mem_reader,
mem_scheduler=self.mem_scheduler,
logger=self.logger,
searcher=searcher_to_use,
deepsearch_agent=self.deepsearch_agent,
)
else:
single_views = [
SingleCubeView(
cube_id=cube_id,
naive_mem_cube=self.naive_mem_cube,
mem_reader=self.mem_reader,
mem_scheduler=self.mem_scheduler,
logger=self.logger,
searcher=searcher_to_use,
deepsearch_agent=self.deepsearch_agent,
)
for cube_id in cube_ids
]
return CompositeCubeView(cube_views=single_views, logger=self.logger)