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
16 changes: 2 additions & 14 deletions examples/core_memories/general_textual_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,9 @@

example_id = "a19b6caa-5d59-42ad-8c8a-e4f7118435b4"

print("===== Extract memories =====")
# Extract memories from a conversation
# The extractor LLM processes the conversation to identify relevant information.
memories = m.extract(
[
{"role": "user", "content": "I love tomatoes."},
{"role": "assistant", "content": "Great! Tomatoes are delicious."},
]
)
pprint.pprint(memories)
print()

print("==== Add memories ====")
# Add the extracted memories to the memory store
m.add(memories)
# Add example memories to the memory store
m.add(example_memories)
# Add a manually created memory item
m.add(
[
Expand Down
93 changes: 52 additions & 41 deletions examples/core_memories/naive_textual_memory.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import json
import os
import pprint
import uuid

from memos.configs.memory import MemoryConfigFactory
from memos.memories.factory import MemoryFactory


def print_result(title, result):
"""Helper function: Pretty print the result."""
print(f"\n{'=' * 10} {title} {'=' * 10}")
if isinstance(result, list | dict):
print(json.dumps(result, indent=2, ensure_ascii=False, default=str))
else:
print(result)


# Configure memory backend with OpenAI extractor
config = MemoryConfigFactory(
backend="naive_text",
Expand All @@ -38,39 +29,55 @@ def print_result(title, result):
# Create memory instance
m = MemoryFactory.from_config(config)

example_memories = [
{
"memory": "I'm a RUCer, I'm happy.",
"metadata": {
"type": "event",
},
},
{
"memory": "MemOS is awesome!",
"metadata": {
"type": "opinion",
},
},
]

example_id = str(uuid.uuid4())

# Extract memories from a simulated conversation
memories = m.extract(
print("==== Add memories ====")
# Add example memories to the memory store
m.add(example_memories)
# Manually create a memory item and add it
m.add(
[
{"role": "user", "content": "I love tomatoes."},
{"role": "assistant", "content": "Great! Tomatoes are delicious."},
{
"id": example_id,
"memory": "User is Chinese.",
"metadata": {"type": "opinion"},
}
]
)
print_result("Extract memories", memories)


# Add the extracted memories to storage
m.add(memories)

# Manually create a memory item and add it
example_id = str(uuid.uuid4())
manual_memory = [{"id": example_id, "memory": "User is Chinese.", "metadata": {"type": "opinion"}}]
m.add(manual_memory)

# Print all current memories
print_result("Add memories (Check all after adding)", m.get_all())

print("All memories after addition:")
pprint.pprint(m.get_all())
print()

# Search for relevant memories based on the query
print("==== Search memories ====")
# Search for memories related to a query
search_results = m.search("Tell me more about the user", top_k=2)
print_result("Search memories", search_results)

pprint.pprint(search_results)
print()

print("==== Get memories ====")
# Get specific memory item by ID
memory_item = m.get(example_id)
print_result("Get memory", memory_item)

print(f"Memory with ID {example_id}:")
pprint.pprint(m.get(example_id))
print(f"Memories by IDs [{example_id}]:")
pprint.pprint(m.get_by_ids([example_id]))
print()

print("==== Update memories ====")
# Update the memory content for the specified ID
m.update(
example_id,
Expand All @@ -80,22 +87,26 @@ def print_result(title, result):
"metadata": {"type": "opinion", "confidence": 85},
},
)
updated_memory = m.get(example_id)
print_result("Update memory", updated_memory)

print(f"Memory after update (ID {example_id}):")
pprint.pprint(m.get(example_id))
print()

print("==== Dump memory ====")
# Dump the current state of memory to a file
m.dump("tmp/naive_mem")
print("Memory dumped to 'tmp/naive_mem'.")
print()


print("==== Delete memories ====")
# Delete memory with the specified ID
m.delete([example_id])
print_result("Delete memory (Check all after deleting)", m.get_all())

print("All memories after deletion:")
pprint.pprint(m.get_all())
print()

print("==== Delete all memories ====")
# Delete all memories in storage
m.delete_all()
print_result("Delete all", m.get_all())
print("All memories after delete_all:")
pprint.pprint(m.get_all())
print()
1 change: 1 addition & 0 deletions src/memos/mem_reader/read_multi_modal/image_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ def parse_fine(
for item in context_items:
if hasattr(item, "memory") and item.memory:
lang = detect_lang(item.memory)
source.lang = lang
break
if not lang:
lang = "en"
Expand Down