Spaces:
Running
Running
File size: 6,323 Bytes
a55ea57 | 1 2 3 4 5 6 7 8 9 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 | """
File: /model.py
Created Date: Saturday December 27th 2025
Author: Christian Nonis <alch.infoemail@gmail.com>
-----
Last Modified: Saturday December 27th 2025
Modified By: the developer formerly known as Christian Nonis at <alch.infoemail@gmail.com>
-----
"""
import asyncio
from typing import Optional
from src.services.kg_agent.main import graph_adapter
from src.constants.kg import Node, Predicate
async def add_nodes(
nodes: list[dict],
brain_id: str,
identification_params: Optional[dict] = None,
metadata: Optional[dict] = None,
) -> list[Node] | str:
"""
Create and add multiple Node objects to the graph.
Each input dict is converted into a Node (a new UUID is generated for each) and then added to the graph via the graph adapter.
Parameters:
nodes (list[dict]): List of node data dicts. Expected keys include `name`, optional `labels` (list), optional `description`, and optional `properties` (dict).
brain_id (str): Identifier for the target brain/graph.
identification_params (dict, optional): Parameters used to identify existing nodes for deduplication or matching; passed through to the graph adapter.
metadata (dict, optional): Additional metadata forwarded to the graph adapter.
Returns:
list[Node] | str: The list of added Node objects, or a string result as returned by the graph adapter.
"""
from src.constants.kg import Node
import uuid as uuid_lib
node_objects = []
for node_data in nodes:
name = node_data.get("name")
if not name:
raise ValueError("Node name is required and cannot be empty")
properties = node_data.get("properties")
if properties is None:
properties = {}
node_objects.append(
Node(
uuid=str(uuid_lib.uuid4()),
name=name,
labels=node_data.get("labels", []),
description=node_data.get("description"),
properties=properties,
)
)
result = await asyncio.to_thread(
graph_adapter.add_nodes,
nodes=node_objects,
brain_id=brain_id,
identification_params=identification_params,
metadata=metadata,
)
return result
async def update_node(
uuid: str,
brain_id: str,
new_name: Optional[str] = None,
new_description: Optional[str] = None,
new_labels: Optional[list[str]] = None,
new_properties: Optional[dict] = None,
properties_to_remove: Optional[list[str]] = None,
) -> Node | None:
"""
Update fields of a node in the graph.
Only the provided parameters will be applied to the node; omitted optional parameters are left unchanged.
Parameters:
uuid (str): UUID of the node to update.
brain_id (str): Identifier of the brain/graph where the node resides.
new_name (Optional[str]): New name for the node.
new_description (Optional[str]): New description for the node.
new_labels (Optional[list[str]]): Labels to assign to the node.
new_properties (Optional[dict]): Properties to set or update on the node.
properties_to_remove (Optional[list[str]]): List of property keys to remove from the node.
Returns:
Node | None: The updated Node if it exists, `None` if no node with the given UUID was found.
"""
result = await asyncio.to_thread(
graph_adapter.update_node,
uuid=uuid,
brain_id=brain_id,
new_name=new_name,
new_description=new_description,
new_labels=new_labels,
new_properties=new_properties,
properties_to_remove=properties_to_remove,
)
return result
async def add_relationship(
subject_uuid: str,
predicate_name: str,
predicate_description: str,
object_uuid: str,
brain_id: str,
) -> str:
"""
Create a predicate relationship between two nodes identified by UUIDs within the specified brain.
Parameters:
subject_uuid (str): UUID of the subject node.
predicate_name (str): Name of the predicate (relationship).
predicate_description (str): Description for the predicate.
object_uuid (str): UUID of the object node.
brain_id (str): Identifier of the brain/graph where the relationship will be created.
Returns:
str: Adapter result for the created relationship (typically the relationship identifier).
"""
subject_node = await asyncio.to_thread(
graph_adapter.get_by_uuid,
uuid=subject_uuid,
brain_id=brain_id,
)
object_node = await asyncio.to_thread(
graph_adapter.get_by_uuid,
uuid=object_uuid,
brain_id=brain_id,
)
if not subject_node:
raise ValueError(f"Subject node with UUID {subject_uuid} not found")
if not object_node:
raise ValueError(f"Object node with UUID {object_uuid} not found")
import uuid as uuid_lib
predicate = Predicate(
uuid=str(uuid_lib.uuid4()),
name=predicate_name,
description=predicate_description,
)
result = await asyncio.to_thread(
graph_adapter.add_relationship,
subject=subject_node,
predicate=predicate,
to_object=object_node,
brain_id=brain_id,
)
return result
async def update_relationship(
uuid: str,
brain_id: str,
new_properties: dict,
properties_to_remove: list[str],
) -> Predicate | None:
"""
Update properties of an existing relationship identified by its UUID.
Parameters:
uuid (str): UUID of the relationship to update.
brain_id (str): Identifier of the brain/graph scope where the relationship exists.
new_properties (dict): Properties to add or update on the relationship.
properties_to_remove (list[str]): List of property names to remove from the relationship.
Returns:
Predicate | None: The updated Predicate object if the relationship was found and updated, `None` otherwise.
"""
result = await asyncio.to_thread(
graph_adapter.update_properties,
uuid=uuid,
updating="relationship",
brain_id=brain_id,
new_properties=new_properties,
properties_to_remove=properties_to_remove,
)
return result
|