Skip to content

Commit 70d5e78

Browse files
authored
feat: knowledge graph storage (camel-ai#450)
1 parent 7dd4a2a commit 70d5e78

File tree

8 files changed

+1058
-4
lines changed

8 files changed

+1058
-4
lines changed

camel/storages/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# limitations under the License.
1313
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
1414

15+
from .graph_storages.base import BaseGraphStorage
16+
from .graph_storages.neo4j_graph import Neo4jGraph
1517
from .key_value_storages.base import BaseKeyValueStorage
1618
from .key_value_storages.in_memory import InMemoryKeyValueStorage
1719
from .key_value_storages.json import JsonStorage
@@ -34,4 +36,6 @@
3436
'VectorDBQueryResult',
3537
'QdrantStorage',
3638
'MilvusStorage',
39+
'BaseGraphStorage',
40+
'Neo4jGraph',
3741
]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
2+
# Licensed under the Apache License, Version 2.0 (the “License”);
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an “AS IS” BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
14+
15+
from .base import BaseGraphStorage
16+
from .graph_element import GraphElement
17+
from .neo4j_graph import Neo4jGraph
18+
19+
__all__ = [
20+
'BaseGraphStorage',
21+
'GraphElement',
22+
'Neo4jGraph',
23+
]
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
2+
# Licensed under the Apache License, Version 2.0 (the “License”);
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an “AS IS” BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
14+
15+
from abc import ABC, abstractmethod
16+
from typing import Any, Dict, List, Optional
17+
18+
19+
class BaseGraphStorage(ABC):
20+
r"""An abstract base class for graph storage systems."""
21+
22+
@property
23+
@abstractmethod
24+
def get_client(self) -> Any:
25+
r"""Get the underlying graph storage client."""
26+
pass
27+
28+
@property
29+
@abstractmethod
30+
def get_schema(self) -> str:
31+
r"""Get the schema of the graph storage"""
32+
pass
33+
34+
@property
35+
@abstractmethod
36+
def get_structured_schema(self) -> Dict[str, Any]:
37+
r"""Get the structured schema of the graph storage"""
38+
pass
39+
40+
@abstractmethod
41+
def refresh_schema(self) -> None:
42+
r"""Refreshes the graph schema information."""
43+
pass
44+
45+
@abstractmethod
46+
def add_triplet(self, subj: str, obj: str, rel: str) -> None:
47+
r"""Adds a relationship (triplet) between two entities in the database.
48+
49+
Args:
50+
subj (str): The identifier for the subject entity.
51+
obj (str): The identifier for the object entity.
52+
rel (str): The relationship between the subject and object.
53+
"""
54+
pass
55+
56+
@abstractmethod
57+
def delete_triplet(self, subj: str, obj: str, rel: str) -> None:
58+
r"""Deletes a specific triplet from the graph, comprising a subject,
59+
object and relationship.
60+
61+
Args:
62+
subj (str): The identifier for the subject entity.
63+
obj (str): The identifier for the object entity.
64+
rel (str): The relationship between the subject and object.
65+
"""
66+
pass
67+
68+
@abstractmethod
69+
def query(
70+
self, query: str, params: Optional[Dict[str, Any]] = None
71+
) -> List[Dict[str, Any]]:
72+
r"""Query the graph store with statement and parameters.
73+
Args:
74+
query (str): The query to be executed.
75+
params (Optional[Dict[str, Any]]): A dictionary of parameters to
76+
be used in the query. Defaults to `None`.
77+
78+
Returns:
79+
List[Dict[str, Any]]: A list of dictionaries, each
80+
dictionary represents a row of results from the query.
81+
"""
82+
pass
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
2+
# Licensed under the Apache License, Version 2.0 (the “License”);
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an “AS IS” BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
14+
from __future__ import annotations
15+
16+
from dataclasses import dataclass, field
17+
from typing import List, Union
18+
19+
from unstructured.documents.elements import Element
20+
21+
22+
@dataclass
23+
class Node:
24+
r"""Represents a node in a graph with associated properties.
25+
26+
Attributes:
27+
id (Union[str, int]): A unique identifier for the node.
28+
type (str): The type of the relationship.
29+
properties (dict): Additional properties and metadata associated with
30+
the node.
31+
"""
32+
33+
id: Union[str, int]
34+
type: str = "Node"
35+
properties: dict = field(default_factory=dict)
36+
37+
38+
@dataclass
39+
class Relationship:
40+
r"""Represents a directed relationship between two nodes in a graph.
41+
42+
Attributes:
43+
subj (Node): The subject/source node of the relationship.
44+
obj (Node): The object/target node of the relationship.
45+
type (str): The type of the relationship.
46+
properties (dict): Additional properties associated with the
47+
relationship.
48+
"""
49+
50+
subj: Node
51+
obj: Node
52+
type: str = "Relationship"
53+
properties: dict = field(default_factory=dict)
54+
55+
56+
@dataclass
57+
class GraphElement:
58+
r"""A graph element with lists of nodes and relationships.
59+
60+
Attributes:
61+
nodes (List[Node]): A list of nodes in the graph.
62+
relationships (List[Relationship]): A list of relationships in the
63+
graph.
64+
source (Element): The element from which the graph information is
65+
derived.
66+
"""
67+
68+
# Allow arbitrary types for Element
69+
class Config:
70+
arbitrary_types_allowed = True
71+
72+
nodes: List[Node]
73+
relationships: List[Relationship]
74+
source: Element

0 commit comments

Comments
 (0)