Created graphs with custom, linked chunks¶
In this example, we'll show you how to upload and attach chunks to triples. Chunks are bits of raw content that contextualize the data you're representing in your graph. When chunks are attached to triples, you can reference them in query responses, helping you build contextualized, explainable answers.
Environment Setup¶
Import required classes and set up the client:
In [ ]:
Copied!
from whyhow import WhyHow
from whyhow.types import Chunk, Triple, Node, Relation
import os
# Initialize the client
client = WhyHow(api_key=os.getenv('WHYHOW_API_KEY'), base_url="https://api.whyhow.ai")
from whyhow import WhyHow
from whyhow.types import Chunk, Triple, Node, Relation
import os
# Initialize the client
client = WhyHow(api_key=os.getenv('WHYHOW_API_KEY'), base_url="https://api.whyhow.ai")
Create a Workspace¶
Let's create a workspace to organize our graphs:
In [ ]:
Copied!
workspace = client.workspaces.create(name="Companies")
print(f"Created workspace with ID: {workspace.workspace_id}")
workspace = client.workspaces.create(name="Companies")
print(f"Created workspace with ID: {workspace.workspace_id}")
Upload Chunks¶
Now let's upload some text chunks that we'll reference in our graph:
In [ ]:
Copied!
chunk = client.chunks.create(
workspace_id=workspace.workspace_id,
chunks=[Chunk(
content="preneur and visionary, Sam Altman serves as the CEO of OpenAI, leading advancements in artifici"
)]
)
print(f"Created chunk with ID: {chunk[0].chunk_id}")
chunk = client.chunks.create(
workspace_id=workspace.workspace_id,
chunks=[Chunk(
content="preneur and visionary, Sam Altman serves as the CEO of OpenAI, leading advancements in artifici"
)]
)
print(f"Created chunk with ID: {chunk[0].chunk_id}")
Create and Add Triples¶
Let's create some triples to build our knowledge graph:
In [ ]:
Copied!
triples = [
Triple(
head=Node(
name="Sam Altman",
label="Person",
properties={"title": "CEO"}
),
relation=Relation(
name="runs",
),
tail=Node(
name="OpenAI",
label="Business",
properties={"market cap": "$157 Billion"}
),
chunk_ids=[c.chunk_id for c in chunk]
)
]
# Create the graph
graph = client.graphs.create_graph_from_triples(
name="Company Graph",
workspace_id=workspace.workspace_id,
triples=triples
)
print(f"Created graph with ID: {graph.graph_id}")
triples = [
Triple(
head=Node(
name="Sam Altman",
label="Person",
properties={"title": "CEO"}
),
relation=Relation(
name="runs",
),
tail=Node(
name="OpenAI",
label="Business",
properties={"market cap": "$157 Billion"}
),
chunk_ids=[c.chunk_id for c in chunk]
)
]
# Create the graph
graph = client.graphs.create_graph_from_triples(
name="Company Graph",
workspace_id=workspace.workspace_id,
triples=triples
)
print(f"Created graph with ID: {graph.graph_id}")
Add More Triples to the Existing Graph¶
In [ ]:
Copied!
add_triples = client.graphs.add_triples(
graph_id=graph.graph_id,
triples = [
Triple(
head=Node(
name="Matt Garman",
label="Person",
properties={"title": "CEO"}
),
relation=Relation(
name="runs",
),
tail=Node(
name="Amazon Web Services",
label="Business",
properties={"operating income": "$10.4 Billion"}
)
)
]
)
add_triples = client.graphs.add_triples(
graph_id=graph.graph_id,
triples = [
Triple(
head=Node(
name="Matt Garman",
label="Person",
properties={"title": "CEO"}
),
relation=Relation(
name="runs",
),
tail=Node(
name="Amazon Web Services",
label="Business",
properties={"operating income": "$10.4 Billion"}
)
)
]
)
Query the Graph¶
Now let's query our knowledge graph:
In [ ]:
Copied!
query = client.graphs.query_unstructured(
graph_id=graph.graph_id,
query="Who runs OpenAI?"
)
print("Query results:")
print(query)
query = client.graphs.query_unstructured(
graph_id=graph.graph_id,
query="Who runs OpenAI?"
)
print("Query results:")
print(query)