Node CRUD¶
This notebook demonstrates how you can use the nodes resource to perform granular operations on nodes in the graph, such as creating, updating, and deleting nodes.
Create a node¶
Create a simple node for a person named John Smith, age 45
In [ ]:
Copied!
from whyhow import WhyHow, Chunk
client = WhyHow(api_key="<whyhow api key>", base_url="https://api.whyhow.ai")
new_node = client.nodes.create(graph_id="<graph id>", name="John Smith", type="person", properties={"age": 45}, strict_mode=False)
from whyhow import WhyHow, Chunk
client = WhyHow(api_key="", base_url="https://api.whyhow.ai")
new_node = client.nodes.create(graph_id="", name="John Smith", type="person", properties={"age": 45}, strict_mode=False)
Update a node¶
Woops, John Smith's age is actually 46. Also, I want to add his location. Lets update the node accordingly.
You can specify the following types of updates on node properties:
clear_properties
- An optional boolean flag to remove all properties from the node.remove_properties
- An optional list of property names to be removed from the node.add_properties
- An optoinal dictionary of properties to added or replaced on the node.
Properties will be cleared, removed, and added in this order.
In [ ]:
Copied!
updated_node = client.nodes.update(node_id=new_node.node_id, add_properties={"age":46, "location": "New York, NY"})
updated_node = client.nodes.update(node_id=new_node.node_id, add_properties={"age":46, "location": "New York, NY"})
Add a chunk¶
Let's add a chunk of text and associate it with the node.
In [ ]:
Copied!
# Add the chunk to the workspace
raw_text = "John Smith is 46 years old and lives in New York, NY. He is a software engineer. He has a dog named Max."
uploaded_chunk = client.chunks.create(workspace_id="<workspace id>", chunks=[Chunk(content=raw_text)])
# Update the node with the chunk
updated_node_chunk = client.nodes.update(node_id=new_node.node_id, chunks=[uploaded_chunk[0].chunk_id])
# Add the chunk to the workspace
raw_text = "John Smith is 46 years old and lives in New York, NY. He is a software engineer. He has a dog named Max."
uploaded_chunk = client.chunks.create(workspace_id="", chunks=[Chunk(content=raw_text)])
# Update the node with the chunk
updated_node_chunk = client.nodes.update(node_id=new_node.node_id, chunks=[uploaded_chunk[0].chunk_id])
Get node chunks¶
Let's see the chunks associated with the John Smith node.
In [ ]:
Copied!
# Get node chunks
node_cunks = client.nodes.get_chunks(node_id=new_node.node_id)
# Get node chunks
node_cunks = client.nodes.get_chunks(node_id=new_node.node_id)
Delete a node¶
John Smith is no longer relevant for our use case. Let's delete the node.
In [ ]:
Copied!
# Delete the node
deleted_node = client.nodes.delete(node_id=new_node.node_id)
# Delete the node
deleted_node = client.nodes.delete(node_id=new_node.node_id)
Get all graph nodes¶
Let's see all the remaining nodes in the graph.
In [ ]:
Copied!
# Get all nodes
graph_nodes = client.nodes.get_all(graph_id=new_node.graph_id)
# Get all nodes
graph_nodes = client.nodes.get_all(graph_id=new_node.graph_id)