Appearance
Elsai Graph Constructor
Package: elsai-graph-constructor v0.1.0
Automatically builds knowledge graphs from unstructured text by identifying entities, discovering relationships, and formatting the data into a valid graph structure.
Installation
bash
pip install --extra-index-url https://core-packages.elsai.ai/root/elsai-graph-constructor/ elsai-graph-constructor==0.1.0Requirements: Python >= 3.10
GraphConstructor (high-level API)
The recommended entry point. Coordinates entity extraction, relationship discovery, and graph building in one call.
python
from elsai_graph_constructor import GraphConstructor
chunks = [
{"chunk_id": "c1", "text": "John Doe works at Acme Corp in Paris."},
{"chunk_id": "c2", "text": "Acme Corp is a leader in Aerospace technology."},
]
constructor = GraphConstructor(llm=llm, verbose=True)
success, message, graph_data = constructor.construct_graph(
chunks=chunks,
include_chunk_nodes=True,
)
if success:
print(f"Nodes: {len(graph_data['nodes'])}")
print(f"Edges: {len(graph_data['edges'])}")llm can be any elsai-model connector, Bedrock, Gemini, or LangChain-compatible LLM.
Low-level components
EntityExtractor
Identifies key information points: people, organizations, locations, technologies, concepts.
python
from elsai_graph_constructor import EntityExtractor
extractor = EntityExtractor(llm=llm, verbose=True)
entities_by_chunk = extractor.extract_entities_batch(chunks)
# entities_by_chunk is a dict keyed by chunk_id
for chunk_id, entities in entities_by_chunk.items():
print(f"{chunk_id}: {[e['label'] for e in entities]}")RelationshipExtractor
Discovers connections between entities (e.g., Person WORKS_AT Company, Company LOCATED_IN City).
python
from elsai_graph_constructor import RelationshipExtractor
extractor = RelationshipExtractor(llm=llm, verbose=True)
relationships = extractor.extract_relationships_batch(
chunks=chunks,
entities_by_chunk=entities_by_chunk,
)GraphSchemaBuilder
Formats extracted data into a valid graph structure, assigns IDs, and validates the result.
python
from elsai_graph_constructor import GraphSchemaBuilder
builder = GraphSchemaBuilder(verbose=True)
nodes = builder.build_nodes(entities_by_chunk, chunks=chunks)
edges = builder.build_edges(relationships, entities_by_chunk)
is_valid, message = builder.validate_graph(nodes, edges)
print(f"Graph valid: {is_valid} — {message}")Output format
graph_data is a dictionary with:
python
{
"nodes": [
{"id": "John_Doe", "label": "John Doe", "type": "Person", "description": "..."},
{"id": "Acme_Corp", "label": "Acme Corp", "type": "Organization", "description": "..."},
],
"edges": [
{"from": "John_Doe", "to": "Acme_Corp", "type": "WORKS_AT", "label": "Works At"},
]
}Pass this directly to GraphStorage.store_graph() in elsai-graph-generator.