To interact with the Knowledge Graph Studio programmatically, install the whyhow SDK and use the WhyHow client.
Installation:
$ pip install whyhow
Basic Workflow:
- Initialize Client: Pass your
api_key and the base_url of your running server. - Create Workspace: Organize your data within a workspace.
- Create Chunks: Ingest text content as
Chunk objects. - Create Triples: Define relationships between
Node objects (head and tail) using a Relation. - Build Graph: Use
create_graph_from_triples to materialize the graph. - Query: Use
query_unstructured to perform natural language queries against the graph.
from whyhow import WhyHow, Triple, Node, Chunk, Relation
# Configure WhyHow client
client = WhyHow(api_key='<your whyhow api key>', base_url="http://localhost:8000")
# Create workspace
workspace = client.workspaces.create(name="Demo Workspace")
# Create chunk(s)
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 artific"
)]
)
# Create triple(s)
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 graph
graph = client.graphs.create_graph_from_triples(
name="Demo Graph",
workspace_id=workspace.workspace_id,
triples=triples
)
# Query graph
query = client.graphs.query_unstructured(
graph_id=graph.graph_id,
query="Who runs OpenAI?"
)