diff --git a/examples/core_memories/general_textual_memory.py b/examples/core_memories/general_textual_memory.py index f71e2ef2e..d5c765b01 100644 --- a/examples/core_memories/general_textual_memory.py +++ b/examples/core_memories/general_textual_memory.py @@ -1,14 +1,25 @@ +import os +import pprint + from memos.configs.memory import MemoryConfigFactory from memos.memories.factory import MemoryFactory +# Initialize the memory configuration +# This configuration specifies the extractor, vector database, and embedder backend. +# Here we use OpenAI for extraction, Qdrant for vector storage, and Ollama for embedding. config = MemoryConfigFactory( backend="general_text", config={ "extractor_llm": { - "backend": "ollama", + "backend": "openai", "config": { - "model_name_or_path": "qwen3:0.6b", + "model_name_or_path": "gpt-4o-mini", + "api_key": os.environ.get("OPENAI_API_KEY"), + "api_base": os.environ.get( + "OPENAI_BASE_URL", + os.environ.get("OPENAI_API_BASE", "https://api.openai.com/v1"), + ), "temperature": 0.0, "remove_think_prefix": True, "max_tokens": 8192, @@ -30,6 +41,8 @@ }, }, ) + +# Create the memory instance from the configuration m = MemoryFactory.from_config(config) example_memories = [ @@ -52,20 +65,25 @@ }, }, ] + 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."}, ] ) -print(memories) +pprint.pprint(memories) print() print("==== Add memories ====") +# Add the extracted memories to the memory store m.add(memories) +# Add a manually created memory item m.add( [ { @@ -80,19 +98,27 @@ } ] ) -print(m.get_all()) +print("All memories after addition:") +pprint.pprint(m.get_all()) print() + print("==== Search memories ====") +# Search for memories related to a query search_results = m.search("Tell me more about the user", top_k=2) -print(search_results) +pprint.pprint(search_results) print() print("==== Get memories ====") -print(m.get(example_id)) -print(m.get_by_ids([example_id])) +# Retrieve a specific memory by its ID +print(f"Memory with ID {example_id}:") +pprint.pprint(m.get(example_id)) +# Retrieve multiple memories by IDs +print(f"Memories by IDs [{example_id}]:") +pprint.pprint(m.get_by_ids([example_id])) print() print("==== Update memories ====") +# Update an existing memory m.update( example_id, { @@ -106,19 +132,26 @@ }, }, ) -print(m.get(example_id)) +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/general_mem") +print("Memory dumped to 'tmp/general_mem'.") print() print("==== Delete memories ====") +# Delete a memory by its ID m.delete([example_id]) -print(m.get_all()) +print("All memories after deletion:") +pprint.pprint(m.get_all()) print() print("==== Delete all memories ====") +# Clear all memories from the store m.delete_all() -print(m.get_all()) +print("All memories after delete_all:") +pprint.pprint(m.get_all()) print() - -print("==== Dump memory ====") -m.dump("tmp/mem") -print("Memory dumped to 'tmp/mem'.") diff --git a/examples/core_memories/naive_textual_memory.py b/examples/core_memories/naive_textual_memory.py index ff61334dc..ab73060c7 100644 --- a/examples/core_memories/naive_textual_memory.py +++ b/examples/core_memories/naive_textual_memory.py @@ -1,53 +1,77 @@ +import json +import os 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", config={ "extractor_llm": { - "backend": "ollama", + "backend": "openai", "config": { - "model_name_or_path": "qwen3:0.6b", + "model_name_or_path": "gpt-4o-mini", + "api_key": os.environ.get("OPENAI_API_KEY"), + "api_base": os.environ.get( + "OPENAI_BASE_URL", + os.environ.get("OPENAI_API_BASE", "https://api.openai.com/v1"), + ), "temperature": 0.0, "remove_think_prefix": True, }, } }, ) + +# Create memory instance m = MemoryFactory.from_config(config) -print("===== Extract memories =====") +# Extract memories from a simulated conversation memories = m.extract( [ {"role": "user", "content": "I love tomatoes."}, {"role": "assistant", "content": "Great! Tomatoes are delicious."}, ] ) -print(memories) -print() +print_result("Extract memories", memories) + -print("==== Add memories ====") +# Add the extracted memories to storage m.add(memories) + +# Manually create a memory item and add it example_id = str(uuid.uuid4()) -m.add([{"id": example_id, "memory": "User is Chinese.", "metadata": {"type": "opinion"}}]) -print(m.get_all()) -print() +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("==== Search memories ====") + +# Search for relevant memories based on the query search_results = m.search("Tell me more about the user", top_k=2) -print(search_results) -print() +print_result("Search memories", search_results) + + +# Get specific memory item by ID +memory_item = m.get(example_id) +print_result("Get memory", memory_item) -print("==== Get memories ====") -memories = m.get(example_id) -print(memories) -print() -print("==== Update memories ====") +# Update the memory content for the specified ID m.update( example_id, { @@ -56,15 +80,22 @@ "metadata": {"type": "opinion", "confidence": 85}, }, ) -print(m.get(example_id)) +updated_memory = m.get(example_id) +print_result("Update memory", updated_memory) + + +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(m.get_all()) -print() +print_result("Delete memory (Check all after deleting)", m.get_all()) -print("==== Delete all memories ====") + +# Delete all memories in storage m.delete_all() -print(m.get_all()) -print() +print_result("Delete all", m.get_all())