Retrieving the Memory Prompt
Memobase automatically extracts and structures various types of memories from user interactions, including:
- User Profile: Key-value attributes describing the user (e.g., name, location, preferences).
- User Events: Significant occurrences and interactions from the user’s history.
This collection of memories forms a user’s personalized context. Memobase provides a powerful context()
API to retrieve this information as a structured string, ready to be injected directly into your LLM prompts.
Basic Usage
The simplest way to get a user’s context is to call the context()
method on a user object.
Context-Aware Retrieval
To make the retrieved context more relevant to the ongoing conversation, you can provide recent chat messages. Memobase will perform a semantic search to prioritize the most relevant historical events, rather than simply returning the most recent ones.
Controlling Context Size
You can manage the size and cost of your prompts by limiting the token count of the retrieved context using the max_tokens
parameter.
Note: The max_tokens
limit applies to the profile and event content, not the final formatted string. If you use a large custom prompt template, the final output may still exceed the limit.
Advanced Filtering
The context()
API offers several parameters for fine-grained control:
prefer_topics
,only_topics
: Prioritize or exclusively include certain profile topics.max_subtopic_size
: Limit the number of sub-topics returned per topic.profile_event_ratio
: Adjust the balance between profile and event information.time_range_in_days
: Filter events to a specific time window.customize_context_prompt
: Provide a custom template for the final output string.
For a full list of parameters, refer to the API Reference for get_context
.