BaseGraphDB
Interface Class
This interface defines the contract that all graph database implementations must follow.
memora.graph_db.base.BaseGraphDB
Bases: ABC
Abstract base class defining a common interface for different Graph DB implementations.
This class provides a standardized interface for graph database operations, including creating, retrieving, and deleting memory nodes and relationships.
Functions
close
abstractmethod
async
create_agent
abstractmethod
async
Creates a new agent in the graph database.
PARAMETER | DESCRIPTION |
---|---|
org_id
|
Short UUID string identifying the organization.
TYPE:
|
agent_label
|
Label/name for the agent.
TYPE:
|
user_id
|
Optional Short UUID of the user. This is used when the agent is created specifically for a user, indicating that both the organization and the user will have this agent.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Agent
|
Agent containing:
|
Source code in memora/graph_db/base.py
create_organization
abstractmethod
async
Creates a new organization in the graph database.
PARAMETER | DESCRIPTION |
---|---|
org_name
|
The name of the organization to create.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Organization
|
Organization object containing:
|
Source code in memora/graph_db/base.py
create_user
abstractmethod
async
Creates a new user in the graph database.
PARAMETER | DESCRIPTION |
---|---|
org_id
|
Short UUID string identifying the organization.
TYPE:
|
user_name
|
Name for the user.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
User
|
User containing:
|
Source code in memora/graph_db/base.py
delete_agent
abstractmethod
async
Deletes an agent from the graph database.
PARAMETER | DESCRIPTION |
---|---|
org_id
|
Short UUID string identifying the organization.
TYPE:
|
agent_id
|
Short UUID string identifying the agent to delete.
TYPE:
|
Source code in memora/graph_db/base.py
delete_all_user_interactions_and_their_memories
abstractmethod
async
Deletes all interactions and their associated memories for a specific user in an organization.
PARAMETER | DESCRIPTION |
---|---|
org_id
|
Short UUID string identifying the organization
TYPE:
|
user_id
|
Short UUID string identifying the user whose interactions should be deleted
TYPE:
|
Note
If the graph database is associated with a vector database, the memories are also deleted there for data consistency.
Source code in memora/graph_db/base.py
delete_all_user_memories
abstractmethod
async
Deletes all memories of a specific user.
PARAMETER | DESCRIPTION |
---|---|
org_id
|
Short UUID string identifying the organization
TYPE:
|
user_id
|
Short UUID string identifying the user
TYPE:
|
Note
If the graph database is associated with a vector database, the memories are also deleted there for data consistency.
Source code in memora/graph_db/base.py
delete_organization
abstractmethod
async
Deletes an organization from the graph database.
Warning
This operation will delete all nodes and relationships from this organization including users, agents, memories, interactions etc.
PARAMETER | DESCRIPTION |
---|---|
org_id
|
Short UUID string identifying the organization to delete.
TYPE:
|
Source code in memora/graph_db/base.py
delete_user
abstractmethod
async
Deletes a user from the graph database.
PARAMETER | DESCRIPTION |
---|---|
org_id
|
Short UUID string identifying the organization.
TYPE:
|
user_id
|
Short UUID string identifying the user to delete.
TYPE:
|
Source code in memora/graph_db/base.py
delete_user_interaction_and_its_memories
abstractmethod
async
Deletes an interaction record and its associated memories.
PARAMETER | DESCRIPTION |
---|---|
org_id
|
Short UUID string identifying the organization.
TYPE:
|
user_id
|
Short UUID string identifying the user.
TYPE:
|
interaction_id
|
Short UUID string identifying the interaction to delete.
TYPE:
|
Note
If the graph database is associated with a vector database, the memories are also deleted there for data consistency.
Source code in memora/graph_db/base.py
delete_user_memory
abstractmethod
async
Deletes a specific memory.
PARAMETER | DESCRIPTION |
---|---|
org_id
|
Short UUID string identifying the organization
TYPE:
|
user_id
|
Short UUID string identifying the user
TYPE:
|
memory_id
|
UUID string identifying the memory to delete
TYPE:
|
Note
If the graph database is associated with a vector database, the memory is also deleted there for data consistency.
Source code in memora/graph_db/base.py
fetch_user_memories_resolved
abstractmethod
async
Fetches memories from the GraphDB by their IDs, resolves any contrary updates, and replaces user/agent placeholders with actual names.
This method performs several operations
- Retrieves memories using (org_id, user_id, memory_ids)
- If a memory has a CONTRARY_UPDATE relationship, uses the newer memory version
- Replaces user_id & agent_id placeholders (e.g 'user_abc123' or 'agent_xyz789') in memories with actual user names / agent labels
PARAMETER | DESCRIPTION |
---|---|
org_user_mem_ids
|
List of Dicts containing org, user, and memory ids of the memories to fetch and process
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
List[Memory]
|
List[Memory] containing memory details:
|
Example
>>> org_user_mem_ids = [{'memory_id': '443ac3a8-fe87-49a4-93d2-05d3eb58ddeb', 'org_id': 'gmDr4sUiWMNqbGAiV8ijbU', 'user_id': 'CcyKXxhi2skEcDpRzNZim7'}, ...]
>>> memories = graphInstance.fetch_memories_resolved(org_user_mem_ids)
>>> print([memoryObj.memory for memoryObj in memories])
["John asked for help with a wedding ring", "Sarah is allergic to peanuts"]
Note
- Org, user, and memory IDs are typically retrieved from a vector database before being passed to this method.
- A memory won't have a message source, if its interaction was updated with a conflicting conversation thread that lead to truncation of the former thread. See
graph.update_interaction_and_memories
Source code in memora/graph_db/base.py
fetch_user_memories_resolved_batch
abstractmethod
async
fetch_user_memories_resolved_batch(
batch_org_user_mem_ids: List[List[Dict[str, str]]]
) -> List[List[models.Memory]]
Fetches memories from the GraphDB by their IDs, resolves any contrary updates, and replaces user/agent placeholders with actual names.
This method performs several operations
- Retrieves memories using (org_id, user_id, memory_ids)
- If a memory has a CONTRARY_UPDATE relationship, uses the newer memory version
- Replaces user_id & agent_id placeholders (e.g 'user_abc123' or 'agent_xyz789') in memories with actual user names / agent labels
PARAMETER | DESCRIPTION |
---|---|
batch_org_user_mem_ids
|
List of lists containing Dicts with org, user, and memory ids of the memories to fetch and process
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
List[List[Memory]]
|
List[List[Memory]] with memory details:
|
Example
>>> batch_org_user_mem_ids = [[{"memory_id": "413ac3a8-fe87-49a4-93d2-05d3eb58ddeb", "org_id": "gmDr4sUiWMNqbGAiV8ijbU", "user_id": "CcyKXxhi2skEcDpRzNZim7"}, ...], [{...}, ...]]
>>> batch_memories = graphInstance.fetch_memories_resolved_batch(batch_org_user_mem_ids)
>>> print([[memoryObj.memory for memoryObj in memories] for memories in batch_memories])
[["John asked for help with a wedding ring", "Sarah is allergic to peanuts"], ["John is about to propose to Sarah"]]
Note
- Batch org, user, and memory IDs are typically retrieved from a vector database before being passed to this method.
- A memory won't have a message source, if its interaction was updated with a conflicting conversation thread that lead to truncation of the former thread. See
graph.update_interaction_and_memories
Source code in memora/graph_db/base.py
get_agent
abstractmethod
async
Gets a specific agent belonging to the specified organization from the graph database.
PARAMETER | DESCRIPTION |
---|---|
org_id
|
Short UUID string identifying the organization.
TYPE:
|
agent_id
|
Short UUID string identifying the agent to retrieve.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Agent
|
Agent containing:
|
Source code in memora/graph_db/base.py
get_all_org_agents
abstractmethod
async
Gets all agents belonging to the specified organization from the graph database.
PARAMETER | DESCRIPTION |
---|---|
org_id
|
Short UUID string identifying the organization.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
List[Agent]
|
A List[Agent], each containing:
|
Source code in memora/graph_db/base.py
get_all_org_users
abstractmethod
async
Gets all users belonging to the specified organization from the graph database.
PARAMETER | DESCRIPTION |
---|---|
org_id
|
Short UUID string identifying the organization.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
List[User]
|
List[User], each containing:
|
Source code in memora/graph_db/base.py
get_all_organizations
abstractmethod
async
Gets all organizations from the graph database.
RETURNS | DESCRIPTION |
---|---|
List[Organization]
|
List[Organization] each containing:
|
Source code in memora/graph_db/base.py
get_all_user_agents
abstractmethod
async
Gets all agents for a user within an organization from the graph database.
PARAMETER | DESCRIPTION |
---|---|
org_id
|
Short UUID string identifying the organization.
TYPE:
|
user_id
|
Short UUID string identifying the user.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
List[Agent]
|
A List[Agent], each containing:
|
Source code in memora/graph_db/base.py
get_all_user_interactions
abstractmethod
async
get_all_user_interactions(
org_id: str,
user_id: str,
with_their_messages: bool = True,
with_their_memories: bool = True,
skip: int = 0,
limit: int = 100,
) -> List[models.Interaction]
Retrieves all interactions for a specific user in an organization.
Note
Interactions are sorted in descending order by their updated at datetime. (So most recent interactions are first).
PARAMETER | DESCRIPTION |
---|---|
org_id
|
Short UUID string identifying the organization.
TYPE:
|
user_id
|
Short UUID string identifying the user.
TYPE:
|
with_their_messages
|
Whether to also retrieve messages of an interaction.
TYPE:
|
with_their_memories
|
Whether to also retrieve memories gotten across all occurrences of an interaction.
TYPE:
|
skip
|
Number of interactions to skip. (Useful for pagination)
TYPE:
|
limit
|
Maximum number of interactions to retrieve. (Useful for pagination)
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
List[Interaction]
|
List[Interaction], each containing an Interaction with:
|
Note
A memory won't have a message source, if its interaction was updated with a conflicting conversation thread that lead to truncation of the former thread. See graph.update_interaction_and_memories
Source code in memora/graph_db/base.py
get_all_user_memories
abstractmethod
async
get_all_user_memories(
org_id: str,
user_id: str,
agent_id: Optional[str] = None,
skip: int = 0,
limit: int = 1000,
) -> List[models.Memory]
Retrieves all memories associated with a specific user.
Note
Memories are sorted in descending order by their obtained at datetime. (So most recent memories are first).
PARAMETER | DESCRIPTION |
---|---|
org_id
|
Short UUID string identifying the organization
TYPE:
|
user_id
|
Short UUID string identifying the user
TYPE:
|
agent_id
|
Optional short UUID string identifying the agent. If provided, only memories obtained from interactions with this agent are returned. Otherwise, all memories associated with the user are returned.
TYPE:
|
skip
|
Number of interactions to skip. (Useful for pagination)
TYPE:
|
limit
|
Maximum number of interactions to retrieve. (Useful for pagination)
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
List[Memory]
|
List[Memory] containing memory details:
|
Note
- A memory won't have a message source, if its interaction was updated with a conflicting conversation thread that lead to truncation of the former thread. See
graph.update_interaction_and_memories
Source code in memora/graph_db/base.py
get_associated_vector_db
abstractmethod
The vector database associated with the graph database, these is used inside the graph transactional blocks to ensure data consistency when handling memories across both stores (e.g., saving memories to the vector store and creating corresponding nodes in the graph db).
Source code in memora/graph_db/base.py
get_interaction
abstractmethod
async
get_interaction(
org_id: str,
user_id: str,
interaction_id: str,
with_messages: bool = True,
with_memories: bool = True,
) -> models.Interaction
Retrieves all messages associated with a specific interaction.
PARAMETER | DESCRIPTION |
---|---|
org_id
|
Short UUID string identifying the organization.
TYPE:
|
user_id
|
Short UUID string identifying the user.
TYPE:
|
interaction_id
|
Short UUID string identifying the interaction.
TYPE:
|
with_messages
|
Whether to retrieve messages along with the interaction.
TYPE:
|
with_memories
|
Whether to also retrieve memories gotten across all occurrences of this interaction.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Interaction
|
Interaction containing:
|
Note
A memory won't have a message source, if its interaction was updated with a conflicting conversation thread that lead to truncation of the former thread. See graph.update_interaction_and_memories
Source code in memora/graph_db/base.py
get_organization
abstractmethod
async
Gets a specific organization from the graph database.
PARAMETER | DESCRIPTION |
---|---|
org_id
|
Short UUID string identifying the organization to retrieve.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Organization
|
Organization object containing:
|
Source code in memora/graph_db/base.py
get_user
abstractmethod
async
Gets a specific user belonging to the specified organization from the graph database.
PARAMETER | DESCRIPTION |
---|---|
org_id
|
Short UUID string identifying the organization.
TYPE:
|
user_id
|
Short UUID string identifying the user to retrieve.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
User
|
User containing:
|
Source code in memora/graph_db/base.py
get_user_memory
abstractmethod
async
Retrieves a specific memory.
PARAMETER | DESCRIPTION |
---|---|
org_id
|
Short UUID string identifying the organization
TYPE:
|
user_id
|
Short UUID string identifying the user
TYPE:
|
memory_id
|
UUID string identifying the memory
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Memory
|
Memory containing memory details:
|
Note
- The memory won't have a message source, if its interaction was updated with a conflicting conversation thread that lead to truncation of the former thread. See
graph.update_interaction_and_memories
Source code in memora/graph_db/base.py
get_user_memory_history
abstractmethod
async
Retrieves the history of a specific memory.
PARAMETER | DESCRIPTION |
---|---|
org_id
|
Short UUID string identifying the organization
TYPE:
|
user_id
|
Short UUID string identifying the user
TYPE:
|
memory_id
|
UUID string identifying the memory
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
List[Memory]
|
List[Memory] containing the history of memory details in descending order (starting with the current version, to the oldest version):
|
Note
- A memory won't have a message source, if its interaction was updated with a conflicting conversation thread that lead to truncation of the former thread. See
graph.update_interaction_and_memories
Source code in memora/graph_db/base.py
save_interaction_with_memories
abstractmethod
async
save_interaction_with_memories(
org_id: str,
agent_id: str,
user_id: str,
memories_and_interaction: MemoriesAndInteraction,
) -> Tuple[str, datetime]
Creates a new interaction record with associated memories.
PARAMETER | DESCRIPTION |
---|---|
org_id
|
Short UUID string identifying the organization.
TYPE:
|
agent_id
|
Short UUID string identifying the agent.
TYPE:
|
user_id
|
Short UUID string identifying the user.
TYPE:
|
memories_and_interaction
|
Contains both the interaction and the associated memories.
TYPE:
|
Note
If the graph database is associated with a vector database, the memories are also stored there for data consistency.
RETURNS | DESCRIPTION |
---|---|
Tuple[str, datetime]
|
Tuple[str, datetime] containing:
|
Source code in memora/graph_db/base.py
setup
abstractmethod
async
update_agent
abstractmethod
async
Updates an existing agent in the graph database.
PARAMETER | DESCRIPTION |
---|---|
org_id
|
Short UUID string identifying the organization.
TYPE:
|
agent_id
|
Short UUID string identifying the agent to update.
TYPE:
|
new_agent_label
|
New label/name for the agent.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Agent
|
Agent containing:
|
Source code in memora/graph_db/base.py
update_interaction_and_memories
abstractmethod
async
update_interaction_and_memories(
org_id: str,
agent_id: str,
user_id: str,
interaction_id: str,
updated_memories_and_interaction: MemoriesAndInteraction,
) -> Tuple[str, datetime]
Update an existing interaction record and add new memories.
Compares updated interaction with existing one
- If differences are found, truncates existing record from that point and replaces with updated version. Old memories from truncated message(s) remain but become standalone (no longer linked to truncated messages).
- If no differences, appends new messages from the update.
New memories are always added, regardless of interaction changes.
PARAMETER | DESCRIPTION |
---|---|
org_id
|
Short UUID string identifying the organization.
TYPE:
|
agent_id
|
Short UUID string identifying the agent in the updated interaction.
TYPE:
|
user_id
|
Short UUID string identifying the user.
TYPE:
|
interaction_id
|
Short UUID string identifying the interaction to update.
TYPE:
|
updated_memories_and_interaction
|
Contains both the updated interaction and the associated new memories.
TYPE:
|
Note
If the graph database is associated with a vector database, the memories are also stored there for data consistency.
RETURNS | DESCRIPTION |
---|---|
Tuple[str, datetime]
|
Tuple[str, datetime] containing:
|
Source code in memora/graph_db/base.py
update_organization
abstractmethod
async
Updates an existing organization in the graph database.
PARAMETER | DESCRIPTION |
---|---|
org_id
|
The Short UUID of the organization to update.
TYPE:
|
new_org_name
|
The new name for the organization.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Organization
|
Organization object containing:
|
Source code in memora/graph_db/base.py
update_user
abstractmethod
async
Updates an existing user in the graph database.
PARAMETER | DESCRIPTION |
---|---|
org_id
|
Short UUID string identifying the organization.
TYPE:
|
user_id
|
Short UUID string identifying the user to update.
TYPE:
|
new_user_name
|
The new name for the user.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
User
|
User containing:
|