Skip to content

Commit 2634421

Browse files
committed
fix:code
1 parent ab1a0fc commit 2634421

6 files changed

Lines changed: 70 additions & 59 deletions

File tree

examples/mem_api/pipeline_test.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import time
99

10-
from typing import Optional, Any
10+
from typing import Any
1111

1212
from dotenv import load_dotenv
1313

@@ -91,7 +91,7 @@ def test_search_memories(
9191
mode: str = "fast",
9292
internet_search: bool = False,
9393
moscube: bool = False,
94-
chat_history: Optional[list] = None,
94+
chat_history: list | None = None,
9595
) -> list[Any]:
9696
"""
9797
Test searching memories from the system.
@@ -134,10 +134,6 @@ def test_search_memories(
134134
},
135135
)
136136

137-
elapsed_time = time.time() - time_start
138-
logger.info(f"Search completed in {elapsed_time:.2f}s")
139-
logger.info(f"Found {len(search_results)} results")
140-
141137
# Print search results
142138
for idx, result in enumerate(search_results, 1):
143139
logger.info(f"\n Result {idx}:")

src/memos/graph_dbs/nebular.py

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from contextlib import suppress
55
from datetime import datetime
66
from threading import Lock
7-
from typing import Optional, TYPE_CHECKING, Any, ClassVar, Literal
7+
from typing import TYPE_CHECKING, Any, ClassVar, Literal
88

99
import numpy as np
1010

@@ -417,7 +417,7 @@ def create_index(
417417

418418
@timed
419419
def remove_oldest_memory(
420-
self, memory_type: str, keep_latest: int, user_name: Optional[str] = None
420+
self, memory_type: str, keep_latest: int, user_name: str | None = None
421421
) -> None:
422422
"""
423423
Remove all WorkingMemory nodes except the latest `keep_latest` entries.
@@ -443,7 +443,7 @@ def remove_oldest_memory(
443443

444444
@timed
445445
def add_node(
446-
self, id: str, memory: str, metadata: dict[str, Any], user_name: Optional[str] = None
446+
self, id: str, memory: str, metadata: dict[str, Any], user_name: str | None = None
447447
) -> None:
448448
"""
449449
Insert or update a Memory node in NebulaGraph.
@@ -477,7 +477,7 @@ def add_node(
477477
)
478478

479479
@timed
480-
def node_not_exist(self, scope: str, user_name: Optional[str] = None) -> int:
480+
def node_not_exist(self, scope: str, user_name: str | None = None) -> int:
481481
user_name = user_name if user_name else self.config.user_name
482482
filter_clause = f'n.memory_type = "{scope}" AND n.user_name = "{user_name}"'
483483
query = f"""
@@ -495,7 +495,7 @@ def node_not_exist(self, scope: str, user_name: Optional[str] = None) -> int:
495495
raise
496496

497497
@timed
498-
def update_node(self, id: str, fields: dict[str, Any], user_name: Optional[str] = None) -> None:
498+
def update_node(self, id: str, fields: dict[str, Any], user_name: str | None = None) -> None:
499499
"""
500500
Update node fields in Nebular, auto-converting `created_at` and `updated_at` to datetime type if present.
501501
"""
@@ -516,7 +516,7 @@ def update_node(self, id: str, fields: dict[str, Any], user_name: Optional[str]
516516
self.execute_query(query)
517517

518518
@timed
519-
def delete_node(self, id: str, user_name: Optional[str] = None) -> None:
519+
def delete_node(self, id: str, user_name: str | None = None) -> None:
520520
"""
521521
Delete a node from the graph.
522522
Args:
@@ -531,7 +531,7 @@ def delete_node(self, id: str, user_name: Optional[str] = None) -> None:
531531
self.execute_query(query)
532532

533533
@timed
534-
def add_edge(self, source_id: str, target_id: str, type: str, user_name: Optional[str] = None):
534+
def add_edge(self, source_id: str, target_id: str, type: str, user_name: str | None = None):
535535
"""
536536
Create an edge from source node to target node.
537537
Args:
@@ -555,7 +555,9 @@ def add_edge(self, source_id: str, target_id: str, type: str, user_name: Optiona
555555
logger.error(f"Failed to insert edge: {e}", exc_info=True)
556556

557557
@timed
558-
def delete_edge(self, source_id: str, target_id: str, type: str, user_name: Optional[str] = None) -> None:
558+
def delete_edge(
559+
self, source_id: str, target_id: str, type: str, user_name: str | None = None
560+
) -> None:
559561
"""
560562
Delete a specific edge between two nodes.
561563
Args:
@@ -575,7 +577,7 @@ def delete_edge(self, source_id: str, target_id: str, type: str, user_name: Opti
575577
self.execute_query(query)
576578

577579
@timed
578-
def get_memory_count(self, memory_type: str, user_name: Optional[str] = None) -> int:
580+
def get_memory_count(self, memory_type: str, user_name: str | None = None) -> int:
579581
user_name = user_name if user_name else self.config.user_name
580582
query = f"""
581583
MATCH (n@Memory)
@@ -592,7 +594,7 @@ def get_memory_count(self, memory_type: str, user_name: Optional[str] = None) ->
592594
return -1
593595

594596
@timed
595-
def count_nodes(self, scope: str, user_name: Optional[str] = None) -> int:
597+
def count_nodes(self, scope: str, user_name: str | None = None) -> int:
596598
user_name = user_name if user_name else self.config.user_name
597599
query = f"""
598600
MATCH (n@Memory)
@@ -611,7 +613,7 @@ def edge_exists(
611613
target_id: str,
612614
type: str = "ANY",
613615
direction: str = "OUTGOING",
614-
user_name: Optional[str] = None,
616+
user_name: str | None = None,
615617
) -> bool:
616618
"""
617619
Check if an edge exists between two nodes.
@@ -654,7 +656,7 @@ def edge_exists(
654656
@timed
655657
# Graph Query & Reasoning
656658
def get_node(
657-
self, id: str, include_embedding: bool = False, user_name: Optional[str] = None
659+
self, id: str, include_embedding: bool = False, user_name: str | None = None
658660
) -> dict[str, Any] | None:
659661
"""
660662
Retrieve a Memory node by its unique ID.
@@ -691,7 +693,11 @@ def get_node(
691693

692694
@timed
693695
def get_nodes(
694-
self, ids: list[str], include_embedding: bool = False, user_name: Optional[str] = None, **kwargs
696+
self,
697+
ids: list[str],
698+
include_embedding: bool = False,
699+
user_name: str | None = None,
700+
**kwargs,
695701
) -> list[dict[str, Any]]:
696702
"""
697703
Retrieve the metadata and memory of a list of nodes.
@@ -734,7 +740,7 @@ def get_nodes(
734740

735741
@timed
736742
def get_edges(
737-
self, id: str, type: str = "ANY", direction: str = "ANY", user_name: Optional[str] = None
743+
self, id: str, type: str = "ANY", direction: str = "ANY", user_name: str | None = None
738744
) -> list[dict[str, str]]:
739745
"""
740746
Get edges connected to a node, with optional type and direction filter.
@@ -796,7 +802,7 @@ def get_neighbors_by_tag(
796802
top_k: int = 5,
797803
min_overlap: int = 1,
798804
include_embedding: bool = False,
799-
user_name: Optional[str] = None,
805+
user_name: str | None = None,
800806
) -> list[dict[str, Any]]:
801807
"""
802808
Find top-K neighbor nodes with maximum tag overlap.
@@ -857,7 +863,9 @@ def get_neighbors_by_tag(
857863
return result
858864

859865
@timed
860-
def get_children_with_embeddings(self, id: str, user_name: Optional[str] = None) -> list[dict[str, Any]]:
866+
def get_children_with_embeddings(
867+
self, id: str, user_name: str | None = None
868+
) -> list[dict[str, Any]]:
861869
user_name = user_name if user_name else self.config.user_name
862870
where_user = f"AND p.user_name = '{user_name}' AND c.user_name = '{user_name}'"
863871

@@ -883,7 +891,7 @@ def get_subgraph(
883891
center_id: str,
884892
depth: int = 2,
885893
center_status: str = "activated",
886-
user_name: Optional[str] = None,
894+
user_name: str | None = None,
887895
) -> dict[str, Any]:
888896
"""
889897
Retrieve a local subgraph centered at a given node.
@@ -955,7 +963,7 @@ def search_by_embedding(
955963
status: str | None = None,
956964
threshold: float | None = None,
957965
search_filter: dict | None = None,
958-
user_name: Optional[str] = None,
966+
user_name: str | None = None,
959967
**kwargs,
960968
) -> list[dict]:
961969
"""
@@ -1034,7 +1042,9 @@ def search_by_embedding(
10341042
return []
10351043

10361044
@timed
1037-
def get_by_metadata(self, filters: list[dict[str, Any]], user_name: Optional[str] = None) -> list[str]:
1045+
def get_by_metadata(
1046+
self, filters: list[dict[str, Any]], user_name: str | None = None
1047+
) -> list[str]:
10381048
"""
10391049
1. ADD logic: "AND" vs "OR"(support logic combination);
10401050
2. Support nested conditional expressions;
@@ -1102,7 +1112,7 @@ def get_grouped_counts(
11021112
group_fields: list[str],
11031113
where_clause: str = "",
11041114
params: dict[str, Any] | None = None,
1105-
user_name: Optional[str] = None,
1115+
user_name: str | None = None,
11061116
) -> list[dict[str, Any]]:
11071117
"""
11081118
Count nodes grouped by any fields.
@@ -1167,7 +1177,7 @@ def get_grouped_counts(
11671177
return output
11681178

11691179
@timed
1170-
def clear(self, user_name: Optional[str] = None) -> None:
1180+
def clear(self, user_name: str | None = None) -> None:
11711181
"""
11721182
Clear the entire graph if the target database exists.
11731183
@@ -1185,7 +1195,7 @@ def clear(self, user_name: Optional[str] = None) -> None:
11851195

11861196
@timed
11871197
def export_graph(
1188-
self, include_embedding: bool = False, user_name: Optional[str] = None
1198+
self, include_embedding: bool = False, user_name: str | None = None
11891199
) -> dict[str, Any]:
11901200
"""
11911201
Export all graph nodes and edges in a structured form.
@@ -1263,7 +1273,7 @@ def export_graph(
12631273
return {"nodes": nodes, "edges": edges}
12641274

12651275
@timed
1266-
def import_graph(self, data: dict[str, Any], user_name: Optional[str] = None) -> None:
1276+
def import_graph(self, data: dict[str, Any], user_name: str | None = None) -> None:
12671277
"""
12681278
Import the entire graph from a serialized dictionary.
12691279
@@ -1301,7 +1311,7 @@ def import_graph(self, data: dict[str, Any], user_name: Optional[str] = None) ->
13011311

13021312
@timed
13031313
def get_all_memory_items(
1304-
self, scope: str, include_embedding: bool = False, user_name: Optional[str] = None
1314+
self, scope: str, include_embedding: bool = False, user_name: str | None = None
13051315
) -> (list)[dict]:
13061316
"""
13071317
Retrieve all memory items of a specific memory_type.
@@ -1342,7 +1352,7 @@ def get_all_memory_items(
13421352

13431353
@timed
13441354
def get_structure_optimization_candidates(
1345-
self, scope: str, include_embedding: bool = False, user_name: Optional[str] = None
1355+
self, scope: str, include_embedding: bool = False, user_name: str | None = None
13461356
) -> list[dict]:
13471357
"""
13481358
Find nodes that are likely candidates for structure optimization:

src/memos/memories/textual/simple_tree.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import time
22

33
from datetime import datetime
4-
from typing import Optional, Any
4+
from typing import Any
55

66
from memos.configs.memory import TreeTextMemoryConfig
77
from memos.embedders.base import BaseEmbedder
@@ -77,7 +77,7 @@ def __init__(
7777
logger.info(f"time init: internet_retriever time is: {time.time() - time_start_ir}")
7878

7979
def add(
80-
self, memories: list[TextualMemoryItem | dict[str, Any]], user_name: Optional[str] = None
80+
self, memories: list[TextualMemoryItem | dict[str, Any]], user_name: str | None = None
8181
) -> list[str]:
8282
"""Add memories.
8383
Args:
@@ -91,11 +91,11 @@ def add(
9191
return self.memory_manager.add(memories, user_name=user_name)
9292

9393
def replace_working_memory(
94-
self, memories: list[TextualMemoryItem], user_name: Optional[str] = None
94+
self, memories: list[TextualMemoryItem], user_name: str | None = None
9595
) -> None:
9696
self.memory_manager.replace_working_memory(memories, user_name=user_name)
9797

98-
def get_working_memory(self, user_name: Optional[str] = None) -> list[TextualMemoryItem]:
98+
def get_working_memory(self, user_name: str | None = None) -> list[TextualMemoryItem]:
9999
working_memories = self.graph_store.get_all_memory_items(
100100
scope="WorkingMemory", user_name=user_name
101101
)
@@ -106,7 +106,7 @@ def get_working_memory(self, user_name: Optional[str] = None) -> list[TextualMem
106106
)
107107
return sorted_items
108108

109-
def get_current_memory_size(self, user_name: Optional[str] = None) -> dict[str, int]:
109+
def get_current_memory_size(self, user_name: str | None = None) -> dict[str, int]:
110110
"""
111111
Get the current size of each memory type.
112112
This delegates to the MemoryManager.
@@ -123,7 +123,7 @@ def search(
123123
manual_close_internet: bool = False,
124124
moscube: bool = False,
125125
search_filter: dict | None = None,
126-
user_name: Optional[str] = None,
126+
user_name: str | None = None,
127127
) -> list[TextualMemoryItem]:
128128
"""Search for memories based on a query.
129129
User query -> TaskGoalParser -> MemoryPathResolver ->

src/memos/memories/textual/tree_text_memory/organize/manager.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
GraphStructureReorganizer,
1515
QueueMessage,
1616
)
17-
from typing import Optional
1817

1918

2019
logger = get_logger(__name__)
@@ -52,7 +51,7 @@ def __init__(
5251
)
5352
self._merged_threshold = merged_threshold
5453

55-
def add(self, memories: list[TextualMemoryItem], user_name: Optional[str] = None) -> list[str]:
54+
def add(self, memories: list[TextualMemoryItem], user_name: str | None = None) -> list[str]:
5655
"""
5756
Add new memories in parallel to different memory types (WorkingMemory, LongTermMemory, UserMemory).
5857
"""
@@ -81,7 +80,7 @@ def add(self, memories: list[TextualMemoryItem], user_name: Optional[str] = None
8180
return added_ids
8281

8382
def replace_working_memory(
84-
self, memories: list[TextualMemoryItem], user_name: Optional[str] = None
83+
self, memories: list[TextualMemoryItem], user_name: str | None = None
8584
) -> None:
8685
"""
8786
Replace WorkingMemory
@@ -107,14 +106,14 @@ def replace_working_memory(
107106
)
108107
self._refresh_memory_size(user_name=user_name)
109108

110-
def get_current_memory_size(self, user_name: Optional[str] = None) -> dict[str, int]:
109+
def get_current_memory_size(self, user_name: str | None = None) -> dict[str, int]:
111110
"""
112111
Return the cached memory type counts.
113112
"""
114113
self._refresh_memory_size(user_name=user_name)
115114
return self.current_memory_size
116115

117-
def _refresh_memory_size(self, user_name: Optional[str] = None) -> None:
116+
def _refresh_memory_size(self, user_name: str | None = None) -> None:
118117
"""
119118
Query the latest counts from the graph store and update internal state.
120119
"""
@@ -124,7 +123,7 @@ def _refresh_memory_size(self, user_name: Optional[str] = None) -> None:
124123
self.current_memory_size = {record["memory_type"]: record["count"] for record in results}
125124
logger.info(f"[MemoryManager] Refreshed memory sizes: {self.current_memory_size}")
126125

127-
def _process_memory(self, memory: TextualMemoryItem, user_name: Optional[str] = None):
126+
def _process_memory(self, memory: TextualMemoryItem, user_name: str | None = None):
128127
"""
129128
Process and add memory to different memory types (WorkingMemory, LongTermMemory, UserMemory).
130129
This method runs asynchronously to process each memory item.
@@ -133,7 +132,7 @@ def _process_memory(self, memory: TextualMemoryItem, user_name: Optional[str] =
133132

134133
# Add to WorkingMemory
135134
working_id = self._add_memory_to_db(memory, "WorkingMemory", user_name)
136-
# ids.append(working_id)
135+
ids.append(working_id)
137136

138137
# Add to LongTermMemory and UserMemory
139138
if memory.metadata.memory_type in ["LongTermMemory", "UserMemory"]:
@@ -145,7 +144,7 @@ def _process_memory(self, memory: TextualMemoryItem, user_name: Optional[str] =
145144
return ids
146145

147146
def _add_memory_to_db(
148-
self, memory: TextualMemoryItem, memory_type: str, user_name: Optional[str] = None
147+
self, memory: TextualMemoryItem, memory_type: str, user_name: str | None = None
149148
) -> str:
150149
"""
151150
Add a single memory item to the graph store, with FIFO logic for WorkingMemory.
@@ -161,7 +160,7 @@ def _add_memory_to_db(
161160
return working_memory.id
162161

163162
def _add_to_graph_memory(
164-
self, memory: TextualMemoryItem, memory_type: str, user_name: Optional[str] = None
163+
self, memory: TextualMemoryItem, memory_type: str, user_name: str | None = None
165164
):
166165
"""
167166
Generalized method to add memory to a graph-based memory type (e.g., LongTermMemory, UserMemory).

0 commit comments

Comments
 (0)