-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
X-TRON404
wants to merge
12
commits into
master
Choose a base branch
from
graphdb-block
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: GraphDBBlock #2017
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a9155b7
feat: GraphDBBlock
X-TRON404 3f3b1e3
Merge branch 'master' into graphdb-block
X-TRON404 514b3a2
feat: test for GraphDBBlock
X-TRON404 4194d22
Merge branch 'graphdb-block' of https://github.com/camel-ai/camel int…
X-TRON404 9ee2a91
feat: GraphDBMemory
X-TRON404 12e422a
fix: missing tests for GraphDBMemory
X-TRON404 dc58afe
Merge branch 'master' into graphdb-block
zjrwtx cfd4321
Merge branch 'master' into graphdb-block
zjrwtx 43f1a87
style: match style
X-TRON404 b2ee5ff
fix: according to coments
X-TRON404 e1b7cc1
style: for number_of_nearest_neighbours
X-TRON404 69a4b24
Merge branch 'master' into graphdb-block
X-TRON404 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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, | ||
) | ||
|
||
def write_records(self, records: List[MemoryRecord]) -> None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.