Skip to content

Commit bb44553

Browse files
authored
Scheduler: fix bugs (#750)
* fix bugs: try to fix bugs in _submit_web_logs * fix bugs: try to address bugs * fix bugs * refactor: modify examples * revise add operation and fix an unbelievable bug
1 parent 05d5045 commit bb44553

File tree

5 files changed

+21
-110
lines changed

5 files changed

+21
-110
lines changed

examples/mem_scheduler/memos_w_scheduler.py

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44

55
from datetime import datetime
66
from pathlib import Path
7-
from queue import Queue
87

98
from memos.configs.mem_cube import GeneralMemCubeConfig
109
from memos.configs.mem_os import MOSConfig
1110
from memos.configs.mem_scheduler import AuthConfig
1211
from memos.log import get_logger
1312
from memos.mem_cube.general import GeneralMemCube
1413
from memos.mem_os.main import MOS
15-
from memos.mem_scheduler.general_scheduler import GeneralScheduler
1614
from memos.mem_scheduler.schemas.message_schemas import ScheduleLogForWebItem
1715
from memos.mem_scheduler.schemas.task_schemas import (
1816
ADD_TASK_LABEL,
@@ -160,42 +158,6 @@ def _first_content() -> str:
160158
return title, _truncate_with_rules(_first_content())
161159

162160

163-
def show_web_logs(mem_scheduler: GeneralScheduler):
164-
"""Display all web log entries from the scheduler's log queue.
165-
166-
Args:
167-
mem_scheduler: The scheduler instance containing web logs to display
168-
"""
169-
if mem_scheduler._web_log_message_queue.empty():
170-
print("Web log queue is currently empty.")
171-
return
172-
173-
print("\n" + "=" * 50 + " WEB LOGS " + "=" * 50)
174-
175-
# Create a temporary queue to preserve the original queue contents
176-
temp_queue = Queue()
177-
collected: list[ScheduleLogForWebItem] = []
178-
179-
while not mem_scheduler._web_log_message_queue.empty():
180-
log_item: ScheduleLogForWebItem = mem_scheduler._web_log_message_queue.get()
181-
collected.append(log_item)
182-
temp_queue.put(log_item)
183-
184-
for idx, log_item in enumerate(sorted(collected, key=lambda x: x.timestamp, reverse=True), 1):
185-
title, content = _format_entry(log_item)
186-
print(f"\nLog Entry #{idx}:")
187-
print(title)
188-
print(content)
189-
print("-" * 50)
190-
191-
# Restore items back to the original queue
192-
while not temp_queue.empty():
193-
mem_scheduler._web_log_message_queue.put(temp_queue.get())
194-
195-
print(f"\nTotal {len(collected)} web log entries displayed.")
196-
print("=" * 110 + "\n")
197-
198-
199161
def run_with_scheduler_init():
200162
print("==== run_with_automatic_scheduler_init ====")
201163
conversations, questions = init_task()
@@ -253,8 +215,6 @@ def run_with_scheduler_init():
253215
response = mos.chat(query=query, user_id=user_id)
254216
print(f"Answer:\n {response}\n")
255217

256-
show_web_logs(mem_scheduler=mos.mem_scheduler)
257-
258218
mos.mem_scheduler.stop()
259219

260220

examples/mem_scheduler/try_schedule_modules.py

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import sys
22

33
from pathlib import Path
4-
from queue import Queue
5-
from typing import TYPE_CHECKING
64

75
from tqdm import tqdm
86

@@ -11,18 +9,11 @@
119
)
1210
from memos.log import get_logger
1311
from memos.mem_scheduler.analyzer.api_analyzer import DirectSearchMemoriesAnalyzer
14-
from memos.mem_scheduler.base_scheduler import BaseScheduler
1512
from memos.mem_scheduler.optimized_scheduler import OptimizedScheduler
1613
from memos.mem_scheduler.schemas.message_schemas import ScheduleMessageItem
1714
from memos.mem_scheduler.schemas.task_schemas import MEM_UPDATE_TASK_LABEL
1815

1916

20-
if TYPE_CHECKING:
21-
from memos.mem_scheduler.schemas import (
22-
ScheduleLogForWebItem,
23-
)
24-
25-
2617
FILE_PATH = Path(__file__).absolute()
2718
BASE_DIR = FILE_PATH.parent.parent.parent
2819
sys.path.insert(0, str(BASE_DIR)) # Enable execution from any working directory
@@ -105,41 +96,6 @@ def init_task():
10596
return conversations, questions
10697

10798

108-
def show_web_logs(mem_scheduler: BaseScheduler):
109-
"""Display all web log entries from the scheduler's log queue.
110-
111-
Args:
112-
mem_scheduler: The scheduler instance containing web logs to display
113-
"""
114-
if mem_scheduler._web_log_message_queue.empty():
115-
print("Web log queue is currently empty.")
116-
return
117-
118-
print("\n" + "=" * 50 + " WEB LOGS " + "=" * 50)
119-
120-
# Create a temporary queue to preserve the original queue contents
121-
temp_queue = Queue()
122-
log_count = 0
123-
124-
while not mem_scheduler._web_log_message_queue.empty():
125-
log_item: ScheduleLogForWebItem = mem_scheduler._web_log_message_queue.get()
126-
temp_queue.put(log_item)
127-
log_count += 1
128-
129-
# Print log entry details
130-
print(f"\nLog Entry #{log_count}:")
131-
print(f'- "{log_item.label}" log: {log_item}')
132-
133-
print("-" * 50)
134-
135-
# Restore items back to the original queue
136-
while not temp_queue.empty():
137-
mem_scheduler._web_log_message_queue.put(temp_queue.get())
138-
139-
print(f"\nTotal {log_count} web log entries displayed.")
140-
print("=" * 110 + "\n")
141-
142-
14399
class ScheduleModulesRunner(DirectSearchMemoriesAnalyzer):
144100
def __init__(self):
145101
super().__init__()
@@ -215,6 +171,3 @@ def add_msgs(
215171
mem_scheduler._memory_update_consumer(
216172
messages=[message],
217173
)
218-
219-
# Show accumulated web logs
220-
show_web_logs(mem_scheduler)

src/memos/mem_reader/simple_struct.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ def filter_hallucination_in_memories(
522522
raw = self.llm.generate([{"role": "user", "content": prompt}])
523523
success, parsed = self._parse_hallucination_filter_response(raw)
524524
logger.info(
525-
f"[filter_hallucination_in_memories] Hallucination filter parsed successfully: {success}"
525+
f"[filter_hallucination_in_memories] Hallucination filter parsed successfully: {success};prompt: {prompt}"
526526
)
527527
if success:
528528
logger.info(f"Hallucination filter result: {parsed}")

src/memos/mem_scheduler/webservice_modules/rabbitmq_service.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ def initialize_rabbitmq(
108108
elif Path(config_path).exists():
109109
auth_config = AuthConfig.from_local_config(config_path=config_path)
110110
else:
111-
logger.error("Fail to initialize auth_config")
112-
return
111+
auth_config = AuthConfig.from_local_env()
113112
self.rabbitmq_config = auth_config.rabbitmq
114113
elif isinstance(config, RabbitMQConfig):
115114
self.rabbitmq_config = config

src/memos/templates/mem_reader_prompts.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -625,21 +625,20 @@
625625
SIMPLE_STRUCT_HALLUCINATION_FILTER_PROMPT = """
626626
You are a strict, language-preserving memory validator and rewriter.
627627
628-
Your task is to compare each memory against the provided user messages (the ground truth) and produce a corrected version only when necessary. Always preserve the original language of the memory—do not translate.
628+
Your task is to eliminate hallucinations and tighten memories by grounding them strictly in the user’s explicit messages. Memories must be factual, unambiguous, and free of any inferred or speculative content.
629629
630630
Rules:
631-
1. **Language Consistency**: The rewritten memory must be in the exact same language as the original input memory. Never switch languages.
632-
2. **Strict Grounding**: Only use information explicitly stated in the user messages. Do not introduce external facts, assumptions, or common sense.
633-
3. **Ambiguity Resolution**:
634-
- Replace vague pronouns (e.g., "he", "it", "they") or unclear references with specific, unambiguous entities based solely on the messages.
635-
- Convert relative time expressions (e.g., "yesterday", "last week", "in two days") into absolute dates or times **only if the messages provide enough context** (e.g., current date is known or implied).
636-
4. **Handling Assistant Inferences**:
637-
- If a memory contains any content **not directly stated by the user**—such as interpretations, summaries, emotional attributions, predictions, causal claims, or generalizations—this is considered an assistant inference.
638-
- In such cases, you **must** set `need_rewrite = true`.
639-
- The `rewritten` text **must explicitly indicate that the statement is an inference**, using a clear and natural prefix in the memory’s language. For English memories, use:
640-
> "The assistant inferred that [rest of the memory]."
641-
- Do **not** present inferred content as factual user statements.
642-
5. **No Rewrite Needed**: If the memory is factually accurate, fully grounded in the messages, unambiguous, and contains no unsupported content, set `need_rewrite = false` and copy the original memory exactly.
631+
1. **Language Consistency**: Keep the exact original language of each memory—no translation or language switching.
632+
2. **Strict Factual Grounding**: Include only what the user explicitly stated. Remove or flag anything not directly present in the messages—no assumptions, interpretations, predictions, emotional labels, summaries, or generalizations.
633+
3. **Ambiguity Elimination**:
634+
- Replace vague pronouns (e.g., “he”, “it”, “they”) with clear, specific entities **only if** the messages identify them.
635+
- Convert relative time expressions (e.g., “yesterday”) to absolute dates **only if** the messages provide enough temporal context.
636+
4. **Hallucination Removal**:
637+
- If a memory contains **any content not verbatim or directly implied by the user**, it must be rewritten.
638+
- Do **not** rephrase inferences as facts. Instead, either:
639+
- Remove the unsupported part and retain only the grounded core, or
640+
- If the entire memory is ungrounded, mark it for rewrite and make the lack of user support explicit.
641+
5. **No Change if Fully Grounded**: If the memory is concise, unambiguous, and fully supported by the user’s messages, keep it unchanged.
643642
644643
Inputs:
645644
messages:
@@ -649,15 +648,15 @@
649648
{memories_inline}
650649
651650
Output Format:
652-
- Return a JSON object with string keys ("0", "1", "2", ...) corresponding to the input memory indices.
651+
- Return a JSON object with string keys ("0", "1", "2", ...) matching input memory indices.
653652
- Each value must be: {{ "need_rewrite": boolean, "rewritten": string, "reason": string }}
654-
- The "reason" should be concise and specific, e.g.:
655-
- "contains assistant inference not stated by user"
656-
- "pronoun 'it' has no clear referent in messages"
657-
- "relative time 'yesterday' converted to 2025-12-16"
658-
- "accurate and directly supported by user message"
653+
- The "reason" must be brief and precise, e.g.:
654+
- "contains unsupported inference"
655+
- "vague pronoun with no referent in messages"
656+
- "relative time resolved to 2025-12-16"
657+
- "fully grounded and concise"
659658
660-
Important: Output **only** the JSON. No additional text, explanations, markdown, or fields.
659+
Important: Output **only** the JSON. No extra text, explanations, markdown, or fields.
661660
"""
662661

663662

0 commit comments

Comments
 (0)