Skip to content

feat: GraphDBBlock #2017

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
6 changes: 5 additions & 1 deletion camel/memories/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@

from .agent_memories import (
ChatHistoryMemory,
GraphDBMemory,
LongtermAgentMemory,
VectorDBMemory,
)
from .base import AgentMemory, BaseContextCreator, MemoryBlock
from .blocks.chat_history_block import ChatHistoryBlock
from .blocks.graphdb_block import GraphDBBlock
from .blocks.vectordb_block import VectorDBBlock
from .context_creators.score_based import ScoreBasedContextCreator
from .records import ContextRecord, MemoryRecord
Expand All @@ -27,12 +29,14 @@
'MemoryRecord',
'ContextRecord',
'MemoryBlock',
"AgentMemory",
'AgentMemory',
'BaseContextCreator',
'ScoreBasedContextCreator',
'ChatHistoryMemory',
'VectorDBMemory',
'ChatHistoryBlock',
'VectorDBBlock',
'LongtermAgentMemory',
'GraphDBBlock',
'GraphDBMemory',
]
78 changes: 77 additions & 1 deletion camel/memories/agent_memories.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from typing import List, Optional

from camel.memories.base import AgentMemory, BaseContextCreator
from camel.memories.blocks import ChatHistoryBlock, VectorDBBlock
from camel.memories.blocks import ChatHistoryBlock, GraphDBBlock, VectorDBBlock
from camel.memories.records import ContextRecord, MemoryRecord
from camel.storages.key_value_storages.base import BaseKeyValueStorage
from camel.storages.vectordb_storages.base import BaseVectorStorage
Expand Down Expand Up @@ -153,6 +153,82 @@ def clear(self) -> None:
self._vectordb_block.clear()


class GraphDBMemory(AgentMemory):
r"""An agent memory wrapper of :obj:`GraphDBBlock`. This memory queries
messages stored in the graph database using similarity search or retrieves
recent messages if no query is provided.

Args:
context_creator (BaseContextCreator): A model context creator.
graph_db_block (GraphDBBlock): A graph database block instance.
retrieve_limit (int, optional): The maximum number of messages
to retrieve. Defaults to 25.
agent_id (str, optional): The ID of the agent associated with the
messages stored in the graph database.
"""

def __init__(
self,
context_creator: BaseContextCreator,
graph_db_block: GraphDBBlock,
retrieve_limit: int = 25,
agent_id: Optional[str] = None,
) -> None:
self._context_creator = context_creator
self._graph_db_block = graph_db_block
self._retrieve_limit = retrieve_limit
self._agent_id = agent_id
self._current_query: Optional[str] = None

@property
def agent_id(self) -> Optional[str]:
return self._agent_id

@agent_id.setter
def agent_id(self, val: Optional[str]) -> None:
self._agent_id = val

def retrieve(self) -> List[ContextRecord]:
r"""Retrieves context records from the graph database.

If a current query is set (based on the last user message), it performs
a similarity search. Otherwise, it retrieves recent records.

Returns:
List[ContextRecord]: A list of context records.
"""
return self._graph_db_block.retrieve(
query=self._current_query,
numberOfNearestNeighbours=self._retrieve_limit,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
numberOfNearestNeighbours=self._retrieve_limit,
number_of_nearest_neighbours=self._retrieve_limit,

)

def write_records(self, records: List[MemoryRecord]) -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may be we can add batch process

r"""Writes records to the graph database and updates the current query
based on the last user message.

Args:
records (List[MemoryRecord]): List of memory records to write.
"""
for record in records:
if record.role_at_backend == OpenAIBackendRole.USER:
self._current_query = record.message.content
if record.agent_id == "" and self.agent_id is not None:
record.agent_id = self.agent_id
self._graph_db_block.write_records(records)

def get_context_creator(self) -> BaseContextCreator:
r"""Returns the context creator used by the memory.

Returns:
BaseContextCreator: The context creator.
"""
return self._context_creator

def clear(self) -> None:
r"""Clears all records from the graph database."""
self._graph_db_block.clear()


class LongtermAgentMemory(AgentMemory):
r"""An implementation of the :obj:`AgentMemory` abstract base class for
augmenting ChatHistoryMemory with VectorDBMemory.
Expand Down
2 changes: 2 additions & 0 deletions camel/memories/blocks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========

from .chat_history_block import ChatHistoryBlock
from .graphdb_block import GraphDBBlock
from .vectordb_block import VectorDBBlock

__all__ = [
'ChatHistoryBlock',
'VectorDBBlock',
'GraphDBBlock',
]
Loading
Loading