from morphik import Morphik
db = Morphik()
# Start graph creation – returns immediately with status "processing"
graph = db.create_graph(
name="research_graph",
filters={"category": "research"},
)
# Option 1: Block until finished
graph = db.wait_for_graph_completion("research_graph")
# Option 2: Poll periodically
while graph.is_processing:
time.sleep(10)
graph = db.get_graph("research_graph")
print("Entities:", len(graph.entities))
# Create a graph from specific documents
graph = db.create_graph(
name="custom_graph",
documents=["doc1", "doc2", "doc3"]
)
# With custom entity extraction examples
from morphik.models import EntityExtractionPromptOverride, EntityExtractionExample, GraphPromptOverrides
# Example with only entity extraction examples
graph = db.create_graph(
name="medical_graph",
filters={"category": "medical"},
prompt_overrides=GraphPromptOverrides(
entity_extraction=EntityExtractionPromptOverride(
examples=[
EntityExtractionExample(label="Insulin", type="MEDICATION"),
EntityExtractionExample(label="Diabetes", type="CONDITION")
]
)
)
)
# Example with custom entity extraction prompt template and examples
graph = db.create_graph(
name="financial_graph",
documents=["doc1", "doc2"],
prompt_overrides=GraphPromptOverrides(
entity_extraction=EntityExtractionPromptOverride(
prompt_template="Extract financial entities from the following text:\n\n{content}\n\nFocus on these types of entities:\n{examples}\n\nReturn in JSON format.",
examples=[
EntityExtractionExample(label="Apple Inc.", type="COMPANY", properties={"sector": "Technology"}),
EntityExtractionExample(label="Q3 2024", type="TIME_PERIOD"),
EntityExtractionExample(label="Revenue Growth", type="METRIC")
]
),
entity_resolution=EntityResolutionPromptOverride(
examples=[
EntityResolutionExample(
canonical="Apple Inc.",
variants=["Apple", "AAPL", "Apple Computer"]
)
]
)
)
)
print(f"Created graph with {len(graph.entities)} entities and {len(graph.relationships)} relationships")