Elsai Graph Constructors#

The Elsai Graph Constructor package provides tools to automatically build knowledge graphs from unstructured text. It identifies entities, discovers relationships between them, and organizes the data into a structured graph format suitable for export or further analysis.

Prerequisites#

  • Python >= 3.10

  • .env file with appropriate LLM API keys and configuration variables

Installation#

To install the elsai-graph-constructor package:

pip install --extra-index-url https://elsai-core-package.optisolbusiness.com/root/elsai-graph-constructor/ elsai-graph-constructor==0.1.0

Components#

Core Components#

1. GraphConstructor#

The GraphConstructor is the main orchestrator that coordinates entity extraction, relationship discovery, and graph building in a single workflow.

from elsai_graph_constructor import GraphConstructor

# Initialize with an LLM adapter
constructor = GraphConstructor(llm=llm, verbose=True)

# Construct the graph from text chunks
success, message, graph_data = constructor.construct_graph(
    chunks=chunks,
    include_chunk_nodes=True,
)

2. EntityExtractor#

Identifies key information points within text chunks, such as people, organizations, locations, technologies, and concepts.

from elsai_graph_constructor import EntityExtractor

entity_extractor = EntityExtractor(llm=llm, verbose=True)
entities = entity_extractor.extract_entities_batch(chunks)

3. RelationshipExtractor#

Discovers connections between extracted entities (e.g., “Person WORKS_AT Company”, “Company LOCATED_IN City”).

from elsai_graph_constructor import RelationshipExtractor

relationship_extractor = RelationshipExtractor(llm=llm, verbose=True)
relationships = relationship_extractor.extract_relationships_batch(
    chunks=chunks,
    entities_by_chunk=entities_by_chunk,
)

4. GraphSchemaBuilder#

Formats extracted data into a valid graph structure, ensuring every node and edge has the required properties and IDs.

from elsai_graph_constructor import GraphSchemaBuilder

schema_builder = GraphSchemaBuilder(verbose=True)
nodes = schema_builder.build_nodes(entities, chunks=chunks)
edges = schema_builder.build_edges(relationships, entities)

# Validate the graph structure
is_valid, validation_msg = schema_builder.validate_graph(nodes, edges)

Full Pipeline Example#

Below is a complete example demonstrating how to use the high-level GraphConstructor to process text and generate a knowledge graph.

from elsai_graph_constructor import GraphConstructor

# 1. Prepare your text chunks
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."}
]

# 2. Initialize the constructor (ensure LLM is configured)
constructor = GraphConstructor(llm=llm_adapter) #Supports elsai-models, Bedrock, Gemini, LangChain

# 3. Build the graph
success, message, graph_data = constructor.construct_graph(chunks=chunks)

if success:
    print(f"Nodes: {len(graph_data['nodes'])}")
    print(f"Edges: {len(graph_data['edges'])}")