Skip to main content

Retrieve Memory

Retrieve stored data from the 0G decentralized storage network.

Overview

The Memory system provides efficient retrieval of both ephemeral and persistent data. Data is automatically cached for better performance and supports various retrieval patterns for different use cases.

Methods

recall()

Retrieve data from persistent storage.
Parameters:
  • key (str): The unique identifier for the stored data
Returns: Any - The stored data, or None if not found

get_ephemeral()

Retrieve data from ephemeral (temporary) storage.
Parameters:
  • key (str): The key for the ephemeral data
Returns: Any - The stored data, or None if not found

get_messages()

Retrieve current conversation messages.
Returns: List[ChatMessage] - List of messages in the current conversation

Examples

Conversation Retrieval

recall_conversation()

Retrieve a previously saved conversation.
Parameters:
  • conversation_id (str): The ID of the conversation to retrieve
Returns: List[ChatMessage] - List of messages in the conversation Example:

get_conversation_context()

Get conversation context as formatted string.
Returns: str - Formatted conversation context Example:

Advanced Retrieval Patterns

Hierarchical Data Retrieval

Fallback Retrieval

Versioned Retrieval

Error Handling and Validation

Performance Optimization

Bulk Retrieval

Lazy Loading

Memory Statistics

Get information about memory usage:

Best Practices

  1. Check for None: Always check if retrieved data is None
  2. Use Type Hints: Specify expected return types for better code clarity
  3. Implement Caching: Cache frequently accessed data locally
  4. Handle Errors: Always handle potential storage errors gracefully
  5. Validate Data: Validate retrieved data before using it
  6. Use Batch Operations: Retrieve multiple items concurrently when possible

Next Steps