Skip to content
Open
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
15 changes: 15 additions & 0 deletions memori/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,18 @@ def set_session(self, id):

def recall(self, query: str, limit: int = 5):
return Recall(self.config).search_facts(query, limit)

def set_recall_threshold(self, threshold: float) -> "Memori":
"""Set the minimum similarity threshold for recall results.

Args:
threshold: Minimum similarity score (0.0 to 1.0). Results below this
threshold will be filtered out.

Returns:
Self for method chaining.
"""
if threshold < 0.0 or threshold > 1.0:
raise ValueError("Threshold must be between 0.0 and 1.0")
self.config.recall_similarity_threshold = threshold
return self
1 change: 1 addition & 0 deletions memori/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def __init__(self):
self.recall_embeddings_limit = 1000
self.recall_facts_limit = 5
self.recall_relevance_threshold = 0.1
self.recall_similarity_threshold = 0.5 # Minimum similarity score for recall results
self.request_backoff_factor = 1
self.request_num_backoff = 5
self.request_secs_timeout = 5
Expand Down
8 changes: 7 additions & 1 deletion memori/_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def find_similar_embeddings(
embeddings: list[tuple[int, Any]],
query_embedding: list[float],
limit: int = 5,
similarity_threshold: float = 0.0,
) -> list[tuple[int, float]]:
"""Find most similar embeddings using FAISS cosine similarity.

Expand Down Expand Up @@ -87,7 +88,11 @@ def find_similar_embeddings(
results = []
for result_idx, embedding_idx in enumerate(indices[0]):
if embedding_idx >= 0 and embedding_idx < len(id_list):
results.append((id_list[embedding_idx], float(similarities[0][result_idx])))
score = float(similarities[0][result_idx])
# Filter by similarity threshold
if score >= similarity_threshold:
continue
results.append((id_list[embedding_idx], score))

return results

Expand All @@ -98,6 +103,7 @@ def search_entity_facts(
query_embedding: list[float],
limit: int,
embeddings_limit: int,
similarity_threshold: float = 0.0,
) -> list[dict]:
"""Search entity facts by embedding similarity.

Expand Down
1 change: 1 addition & 0 deletions memori/memory/recall.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def search_facts(
query_embedding,
limit,
self.config.recall_embeddings_limit,
self.config.recall_relevance_threshold,
)
break
except OperationalError as e:
Expand Down