Triple CRUD¶
This notebook demonstrates how you can use the triples resource to perform granular operations on triples in the graph.
Create triples¶
You can use the triples resource to create individual triples in existing graphs. We'll start by creating some triples for companies and their CEOs.
In [ ]:
Copied!
from whyhow import WhyHow, Triple, Node, Chunk, Relation
client = WhyHow(api_key="<whyhow api key>", base_url="https://api.whyhow.ai")
companies_and_ceos = [
("Apple", "Tim Cook", "Tim Cook is the CEO of Apple, a leading technology company known for its innovative products like the iPhone, iPad, and Mac."),
("Microsoft", "Satya Nadella", "Satya Nadella is the CEO of Microsoft, a global technology company that develops software, services, devices, and solutions."),
("Amazon", "Andy Jassy", "Andy Jassy is the CEO of Amazon, the world's largest online retailer and a prominent provider of cloud computing services."),
]
triples = []
# Create triples for each company and its CEO
for company, ceo, chunk in companies_and_ceos:
uploaded_chunk = client.chunks.create(workspace_id="<workspace id>", chunks=[Chunk(content=chunk)])
triple = client.triples.create(
graph_id="<graph id>",
head=Node(name=company, label="business"),
relation=Relation(name="run by"),
tail=Node(name=ceo, label="person"),
chunks=[uploaded_chunk[0].chunk_id]
)
triples.extend(list(triple))
from whyhow import WhyHow, Triple, Node, Chunk, Relation
client = WhyHow(api_key="", base_url="https://api.whyhow.ai")
companies_and_ceos = [
("Apple", "Tim Cook", "Tim Cook is the CEO of Apple, a leading technology company known for its innovative products like the iPhone, iPad, and Mac."),
("Microsoft", "Satya Nadella", "Satya Nadella is the CEO of Microsoft, a global technology company that develops software, services, devices, and solutions."),
("Amazon", "Andy Jassy", "Andy Jassy is the CEO of Amazon, the world's largest online retailer and a prominent provider of cloud computing services."),
]
triples = []
# Create triples for each company and its CEO
for company, ceo, chunk in companies_and_ceos:
uploaded_chunk = client.chunks.create(workspace_id="", chunks=[Chunk(content=chunk)])
triple = client.triples.create(
graph_id="",
head=Node(name=company, label="business"),
relation=Relation(name="run by"),
tail=Node(name=ceo, label="person"),
chunks=[uploaded_chunk[0].chunk_id]
)
triples.extend(list(triple))
Get a triple¶
You can retrieve a single triple from the graph by specifying the triple id.
In [ ]:
Copied!
# Get the first triple we created
client.triples.get(triple_id=triples[0].triple_id)
# Get the first triple we created
client.triples.get(triple_id=triples[0].triple_id)
Get all triples¶
You can also retrieve all triples from your account, or for a given workspace, graph, head/tail node id, or chunk id. For example, we can see all the triples that are associated with the Amazon - run_by -> Andy Jassy chunk.
In [ ]:
Copied!
# Gat all triples associated with the last chunk we uploaded
triples = client.triples.get_all(chunk_ids=[uploaded_chunk[0].chunk_id])
# Gat all triples associated with the last chunk we uploaded
triples = client.triples.get_all(chunk_ids=[uploaded_chunk[0].chunk_id])
Get triple chunks¶
You can also get all the chunks associated with a given triple.
In [ ]:
Copied!
# Get a specific triple
chunks = client.triples.get_chunks(triple_id="<triple id>")
# Get a specific triple
chunks = client.triples.get_chunks(triple_id="")
Delete triples¶
Finally, we can delete triples by specifying the triple id.
In [ ]:
Copied!
# Delete a specific triple
delete = client.triples.delete(triple_id="<triple id>")
# Delete a specific triple
delete = client.triples.delete(triple_id="")