Skip to content

Kuzu

KuzuGraphStore #

Bases: GraphStore

Source code in llama_index/graph_stores/kuzu/base.py
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
class KuzuGraphStore(GraphStore):
    def __init__(
        self,
        database: Any,
        node_table_name: str = "entity",
        rel_table_name: str = "links",
        **kwargs: Any,
    ) -> None:
        self.database = database
        self.connection = kuzu.Connection(database)
        self.node_table_name = node_table_name
        self.rel_table_name = rel_table_name
        self.init_schema()

    def init_schema(self) -> None:
        """如果表不存在,则初始化模式。"""
        node_tables = self.connection._get_node_table_names()
        if self.node_table_name not in node_tables:
            self.connection.execute(
                "CREATE NODE TABLE %s (ID STRING, PRIMARY KEY(ID))"
                % self.node_table_name
            )
        rel_tables = self.connection._get_rel_table_names()
        rel_tables = [rel_table["name"] for rel_table in rel_tables]
        if self.rel_table_name not in rel_tables:
            self.connection.execute(
                "CREATE REL TABLE {} (FROM {} TO {}, predicate STRING)".format(
                    self.rel_table_name, self.node_table_name, self.node_table_name
                )
            )

    @property
    def client(self) -> Any:
        return self.connection

    def get(self, subj: str) -> List[List[str]]:
        """获取三元组。"""
        query = """
            MATCH (n1:%s)-[r:%s]->(n2:%s)
            WHERE n1.ID = $subj
            RETURN r.predicate, n2.ID;
        """
        prepared_statement = self.connection.prepare(
            query % (self.node_table_name, self.rel_table_name, self.node_table_name)
        )
        query_result = self.connection.execute(prepared_statement, {"subj": subj})
        retval = []
        while query_result.has_next():
            row = query_result.get_next()
            retval.append([row[0], row[1]])
        return retval

    def get_rel_map(
        self, subjs: Optional[List[str]] = None, depth: int = 2, limit: int = 30
    ) -> Dict[str, List[List[str]]]:
        """获取深度感知的相对地图。"""
        rel_wildcard = "r:%s*1..%d" % (self.rel_table_name, depth)
        match_clause = "MATCH (n1:{})-[{}]->(n2:{})".format(
            self.node_table_name,
            rel_wildcard,
            self.node_table_name,
        )
        return_clause = "RETURN n1, r, n2 LIMIT %d" % limit
        params = []
        if subjs is not None:
            for i, curr_subj in enumerate(subjs):
                if i == 0:
                    where_clause = "WHERE n1.ID = $%d" % i
                else:
                    where_clause += " OR n1.ID = $%d" % i
                params.append((str(i), curr_subj))
        else:
            where_clause = ""
        query = f"{match_clause} {where_clause} {return_clause}"
        prepared_statement = self.connection.prepare(query)
        if subjs is not None:
            query_result = self.connection.execute(prepared_statement, dict(params))
        else:
            query_result = self.connection.execute(prepared_statement)
        retval: Dict[str, List[List[str]]] = {}
        while query_result.has_next():
            row = query_result.get_next()
            curr_path = []
            subj = row[0]
            recursive_rel = row[1]
            obj = row[2]
            nodes_map = {}
            nodes_map[(subj["_id"]["table"], subj["_id"]["offset"])] = subj["ID"]
            nodes_map[(obj["_id"]["table"], obj["_id"]["offset"])] = obj["ID"]
            for node in recursive_rel["_nodes"]:
                nodes_map[(node["_id"]["table"], node["_id"]["offset"])] = node["ID"]
            for rel in recursive_rel["_rels"]:
                predicate = rel["predicate"]
                curr_subj_id = nodes_map[(rel["_src"]["table"], rel["_src"]["offset"])]
                curr_path.append(curr_subj_id)
                curr_path.append(predicate)
            # Add the last node
            curr_path.append(obj["ID"])
            if subj["ID"] not in retval:
                retval[subj["ID"]] = []
            retval[subj["ID"]].append(curr_path)
        return retval

    def upsert_triplet(self, subj: str, rel: str, obj: str) -> None:
        """添加三元组。"""

        def check_entity_exists(connection: Any, entity: str) -> bool:
            is_exists_result = connection.execute(
                "MATCH (n:%s) WHERE n.ID = $entity RETURN n.ID" % self.node_table_name,
                {"entity": entity},
            )
            return is_exists_result.has_next()

        def create_entity(connection: Any, entity: str) -> None:
            connection.execute(
                "CREATE (n:%s {ID: $entity})" % self.node_table_name,
                {"entity": entity},
            )

        def check_rel_exists(connection: Any, subj: str, obj: str, rel: str) -> bool:
            is_exists_result = connection.execute(
                (
                    "MATCH (n1:{})-[r:{}]->(n2:{}) WHERE n1.ID = $subj AND n2.ID = "
                    "$obj AND r.predicate = $pred RETURN r.predicate"
                ).format(
                    self.node_table_name, self.rel_table_name, self.node_table_name
                ),
                {"subj": subj, "obj": obj, "pred": rel},
            )
            return is_exists_result.has_next()

        def create_rel(connection: Any, subj: str, obj: str, rel: str) -> None:
            connection.execute(
                (
                    "MATCH (n1:{}), (n2:{}) WHERE n1.ID = $subj AND n2.ID = $obj "
                    "CREATE (n1)-[r:{} {{predicate: $pred}}]->(n2)"
                ).format(
                    self.node_table_name, self.node_table_name, self.rel_table_name
                ),
                {"subj": subj, "obj": obj, "pred": rel},
            )

        is_subj_exists = check_entity_exists(self.connection, subj)
        is_obj_exists = check_entity_exists(self.connection, obj)

        if not is_subj_exists:
            create_entity(self.connection, subj)
        if not is_obj_exists:
            create_entity(self.connection, obj)

        if is_subj_exists and is_obj_exists:
            is_rel_exists = check_rel_exists(self.connection, subj, obj, rel)
            if is_rel_exists:
                return

        create_rel(self.connection, subj, obj, rel)

    def delete(self, subj: str, rel: str, obj: str) -> None:
        """删除三元组。"""

        def delete_rel(connection: Any, subj: str, obj: str, rel: str) -> None:
            connection.execute(
                (
                    "MATCH (n1:{})-[r:{}]->(n2:{}) WHERE n1.ID = $subj AND n2.ID"
                    " = $obj AND r.predicate = $pred DELETE r"
                ).format(
                    self.node_table_name, self.rel_table_name, self.node_table_name
                ),
                {"subj": subj, "obj": obj, "pred": rel},
            )

        def delete_entity(connection: Any, entity: str) -> None:
            connection.execute(
                "MATCH (n:%s) WHERE n.ID = $entity DELETE n" % self.node_table_name,
                {"entity": entity},
            )

        def check_edges(connection: Any, entity: str) -> bool:
            is_exists_result = connection.execute(
                "MATCH (n1:{})-[r:{}]-(n2:{}) WHERE n2.ID = $entity RETURN r.predicate".format(
                    self.node_table_name, self.rel_table_name, self.node_table_name
                ),
                {"entity": entity},
            )
            return is_exists_result.has_next()

        delete_rel(self.connection, subj, obj, rel)
        if not check_edges(self.connection, subj):
            delete_entity(self.connection, subj)
        if not check_edges(self.connection, obj):
            delete_entity(self.connection, obj)

    @classmethod
    def from_persist_dir(
        cls,
        persist_dir: str,
        node_table_name: str = "entity",
        rel_table_name: str = "links",
    ) -> "KuzuGraphStore":
        """从持久化目录加载。"""
        try:
            import kuzu
        except ImportError:
            raise ImportError("Please install kuzu: pip install kuzu")
        database = kuzu.Database(persist_dir)
        return cls(database, node_table_name, rel_table_name)

    @classmethod
    def from_dict(cls, config_dict: Dict[str, Any]) -> "KuzuGraphStore":
        """从配置字典初始化图形存储。

Args:
    config_dict:配置字典。

Returns:
    图形存储。
"""
        return cls(**config_dict)

init_schema #

init_schema() -> None

如果表不存在,则初始化模式。

Source code in llama_index/graph_stores/kuzu/base.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def init_schema(self) -> None:
    """如果表不存在,则初始化模式。"""
    node_tables = self.connection._get_node_table_names()
    if self.node_table_name not in node_tables:
        self.connection.execute(
            "CREATE NODE TABLE %s (ID STRING, PRIMARY KEY(ID))"
            % self.node_table_name
        )
    rel_tables = self.connection._get_rel_table_names()
    rel_tables = [rel_table["name"] for rel_table in rel_tables]
    if self.rel_table_name not in rel_tables:
        self.connection.execute(
            "CREATE REL TABLE {} (FROM {} TO {}, predicate STRING)".format(
                self.rel_table_name, self.node_table_name, self.node_table_name
            )
        )

get #

get(subj: str) -> List[List[str]]

获取三元组。

Source code in llama_index/graph_stores/kuzu/base.py
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
def get(self, subj: str) -> List[List[str]]:
    """获取三元组。"""
    query = """
        MATCH (n1:%s)-[r:%s]->(n2:%s)
        WHERE n1.ID = $subj
        RETURN r.predicate, n2.ID;
    """
    prepared_statement = self.connection.prepare(
        query % (self.node_table_name, self.rel_table_name, self.node_table_name)
    )
    query_result = self.connection.execute(prepared_statement, {"subj": subj})
    retval = []
    while query_result.has_next():
        row = query_result.get_next()
        retval.append([row[0], row[1]])
    return retval

get_rel_map #

get_rel_map(
    subjs: Optional[List[str]] = None,
    depth: int = 2,
    limit: int = 30,
) -> Dict[str, List[List[str]]]

获取深度感知的相对地图。

Source code in llama_index/graph_stores/kuzu/base.py
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
def get_rel_map(
    self, subjs: Optional[List[str]] = None, depth: int = 2, limit: int = 30
) -> Dict[str, List[List[str]]]:
    """获取深度感知的相对地图。"""
    rel_wildcard = "r:%s*1..%d" % (self.rel_table_name, depth)
    match_clause = "MATCH (n1:{})-[{}]->(n2:{})".format(
        self.node_table_name,
        rel_wildcard,
        self.node_table_name,
    )
    return_clause = "RETURN n1, r, n2 LIMIT %d" % limit
    params = []
    if subjs is not None:
        for i, curr_subj in enumerate(subjs):
            if i == 0:
                where_clause = "WHERE n1.ID = $%d" % i
            else:
                where_clause += " OR n1.ID = $%d" % i
            params.append((str(i), curr_subj))
    else:
        where_clause = ""
    query = f"{match_clause} {where_clause} {return_clause}"
    prepared_statement = self.connection.prepare(query)
    if subjs is not None:
        query_result = self.connection.execute(prepared_statement, dict(params))
    else:
        query_result = self.connection.execute(prepared_statement)
    retval: Dict[str, List[List[str]]] = {}
    while query_result.has_next():
        row = query_result.get_next()
        curr_path = []
        subj = row[0]
        recursive_rel = row[1]
        obj = row[2]
        nodes_map = {}
        nodes_map[(subj["_id"]["table"], subj["_id"]["offset"])] = subj["ID"]
        nodes_map[(obj["_id"]["table"], obj["_id"]["offset"])] = obj["ID"]
        for node in recursive_rel["_nodes"]:
            nodes_map[(node["_id"]["table"], node["_id"]["offset"])] = node["ID"]
        for rel in recursive_rel["_rels"]:
            predicate = rel["predicate"]
            curr_subj_id = nodes_map[(rel["_src"]["table"], rel["_src"]["offset"])]
            curr_path.append(curr_subj_id)
            curr_path.append(predicate)
        # Add the last node
        curr_path.append(obj["ID"])
        if subj["ID"] not in retval:
            retval[subj["ID"]] = []
        retval[subj["ID"]].append(curr_path)
    return retval

upsert_triplet #

upsert_triplet(subj: str, rel: str, obj: str) -> None

添加三元组。

Source code in llama_index/graph_stores/kuzu/base.py
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
def upsert_triplet(self, subj: str, rel: str, obj: str) -> None:
    """添加三元组。"""

    def check_entity_exists(connection: Any, entity: str) -> bool:
        is_exists_result = connection.execute(
            "MATCH (n:%s) WHERE n.ID = $entity RETURN n.ID" % self.node_table_name,
            {"entity": entity},
        )
        return is_exists_result.has_next()

    def create_entity(connection: Any, entity: str) -> None:
        connection.execute(
            "CREATE (n:%s {ID: $entity})" % self.node_table_name,
            {"entity": entity},
        )

    def check_rel_exists(connection: Any, subj: str, obj: str, rel: str) -> bool:
        is_exists_result = connection.execute(
            (
                "MATCH (n1:{})-[r:{}]->(n2:{}) WHERE n1.ID = $subj AND n2.ID = "
                "$obj AND r.predicate = $pred RETURN r.predicate"
            ).format(
                self.node_table_name, self.rel_table_name, self.node_table_name
            ),
            {"subj": subj, "obj": obj, "pred": rel},
        )
        return is_exists_result.has_next()

    def create_rel(connection: Any, subj: str, obj: str, rel: str) -> None:
        connection.execute(
            (
                "MATCH (n1:{}), (n2:{}) WHERE n1.ID = $subj AND n2.ID = $obj "
                "CREATE (n1)-[r:{} {{predicate: $pred}}]->(n2)"
            ).format(
                self.node_table_name, self.node_table_name, self.rel_table_name
            ),
            {"subj": subj, "obj": obj, "pred": rel},
        )

    is_subj_exists = check_entity_exists(self.connection, subj)
    is_obj_exists = check_entity_exists(self.connection, obj)

    if not is_subj_exists:
        create_entity(self.connection, subj)
    if not is_obj_exists:
        create_entity(self.connection, obj)

    if is_subj_exists and is_obj_exists:
        is_rel_exists = check_rel_exists(self.connection, subj, obj, rel)
        if is_rel_exists:
            return

    create_rel(self.connection, subj, obj, rel)

delete #

delete(subj: str, rel: str, obj: str) -> None

删除三元组。

Source code in llama_index/graph_stores/kuzu/base.py
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
def delete(self, subj: str, rel: str, obj: str) -> None:
    """删除三元组。"""

    def delete_rel(connection: Any, subj: str, obj: str, rel: str) -> None:
        connection.execute(
            (
                "MATCH (n1:{})-[r:{}]->(n2:{}) WHERE n1.ID = $subj AND n2.ID"
                " = $obj AND r.predicate = $pred DELETE r"
            ).format(
                self.node_table_name, self.rel_table_name, self.node_table_name
            ),
            {"subj": subj, "obj": obj, "pred": rel},
        )

    def delete_entity(connection: Any, entity: str) -> None:
        connection.execute(
            "MATCH (n:%s) WHERE n.ID = $entity DELETE n" % self.node_table_name,
            {"entity": entity},
        )

    def check_edges(connection: Any, entity: str) -> bool:
        is_exists_result = connection.execute(
            "MATCH (n1:{})-[r:{}]-(n2:{}) WHERE n2.ID = $entity RETURN r.predicate".format(
                self.node_table_name, self.rel_table_name, self.node_table_name
            ),
            {"entity": entity},
        )
        return is_exists_result.has_next()

    delete_rel(self.connection, subj, obj, rel)
    if not check_edges(self.connection, subj):
        delete_entity(self.connection, subj)
    if not check_edges(self.connection, obj):
        delete_entity(self.connection, obj)

from_persist_dir classmethod #

from_persist_dir(
    persist_dir: str,
    node_table_name: str = "entity",
    rel_table_name: str = "links",
) -> KuzuGraphStore

从持久化目录加载。

Source code in llama_index/graph_stores/kuzu/base.py
202
203
204
205
206
207
208
209
210
211
212
213
214
215
@classmethod
def from_persist_dir(
    cls,
    persist_dir: str,
    node_table_name: str = "entity",
    rel_table_name: str = "links",
) -> "KuzuGraphStore":
    """从持久化目录加载。"""
    try:
        import kuzu
    except ImportError:
        raise ImportError("Please install kuzu: pip install kuzu")
    database = kuzu.Database(persist_dir)
    return cls(database, node_table_name, rel_table_name)

from_dict classmethod #

from_dict(config_dict: Dict[str, Any]) -> KuzuGraphStore

从配置字典初始化图形存储。

Returns:

Type Description
KuzuGraphStore

图形存储。

Source code in llama_index/graph_stores/kuzu/base.py
217
218
219
220
221
222
223
224
225
226
227
    @classmethod
    def from_dict(cls, config_dict: Dict[str, Any]) -> "KuzuGraphStore":
        """从配置字典初始化图形存储。

Args:
    config_dict:配置字典。

Returns:
    图形存储。
"""
        return cls(**config_dict)