| name | openspg-guide |
| description | Ant Group knowledge graph engine with SPG and KAG framework |
| metadata | {"openclaw":{"emoji":"๐ธ๏ธ","category":"tools","subcategory":"knowledge-graph","keywords":["OpenSPG","knowledge graph","SPG","KAG","Ant Group","semantic reasoning"],"source":"https://github.com/OpenSPG/openspg"}} |
OpenSPG Knowledge Graph Guide
Overview
OpenSPG is Ant Group's open-source knowledge graph engine based on the Semantic-enhanced Programmable Graph (SPG) framework. It combines property graphs with semantic reasoning, enabling knowledge extraction, representation, reasoning, and question answering. The KAG (Knowledge Augmented Generation) module integrates with LLMs for RAG over knowledge graphs. Suited for building domain-specific knowledge bases for research.
Installation
git clone https://github.com/OpenSPG/openspg.git
cd openspg
docker-compose up -d
pip install openspg
Core Concepts
SPG Framework
โโโ Schema Layer (define types and relations)
โ โโโ Entity types (Person, Paper, Concept)
โ โโโ Properties (typed, constrained)
โ โโโ Relations (directed, typed edges)
โโโ Knowledge Layer (populate with data)
โ โโโ Entity extraction (NER + linking)
โ โโโ Relation extraction
โ โโโ Property filling
โโโ Reasoning Layer (infer new knowledge)
โ โโโ Rule-based reasoning
โ โโโ Statistical reasoning
โ โโโ LLM-augmented reasoning
โโโ Application Layer (query and use)
โโโ Graph queries (SPARQL-like)
โโโ Question answering
โโโ Knowledge-augmented generation
Schema Definition
from openspg import Schema, EntityType, RelationType
schema = Schema("research_kg")
paper = EntityType("Paper", properties={
"title": "Text",
"abstract": "Text",
"year": "Integer",
"venue": "Text",
"doi": "Text",
"citation_count": "Integer",
})
author = EntityType("Author", properties={
"name": "Text",
"affiliation": "Text",
"h_index": "Integer",
})
concept = EntityType("Concept", properties={
"name": "Text",
"definition": "Text",
"domain": "Text",
})
schema.add_relation(RelationType(
"authored_by", source=paper, target=author
))
schema.add_relation(RelationType(
"cites", source=paper, target=paper
))
schema.add_relation(RelationType(
"discusses", source=paper, target=concept
))
schema.add_relation(RelationType(
"related_to", source=concept, target=concept
))
schema.deploy()
Knowledge Population
from openspg import KnowledgeBuilder
builder = KnowledgeBuilder(schema="research_kg")
builder.add_entity("Paper", {
"title": "Attention Is All You Need",
"year": 2017,
"venue": "NeurIPS",
"doi": "10.48550/arXiv.1706.03762",
})
builder.extract_from_text(
"Vaswani et al. proposed the Transformer architecture "
"which uses self-attention mechanisms to replace "
"recurrence. The model achieved state-of-the-art on "
"WMT 2014 English-to-German translation.",
entity_types=["Paper", "Author", "Concept"],
relation_types=["authored_by", "discusses"],
)
builder.import_csv(
"papers.csv",
entity_type="Paper",
column_mapping={"title": "title", "year": "year"},
)
builder.commit()
KAG: Knowledge-Augmented Generation
from openspg.kag import KAGPipeline
kag = KAGPipeline(
knowledge_graph="research_kg",
llm_provider="anthropic",
)
answer = kag.ask(
"What are the key papers on attention mechanisms "
"and how are they related?"
)
print(answer.text)
for source in answer.sources:
print(f" [{source.type}] {source.name}: {source.evidence}")
Graph Queries
from openspg import GraphQuery
gq = GraphQuery("research_kg")
papers = gq.query("""
MATCH (p:Paper)-[:discusses]->(c:Concept)
WHERE c.name = 'self-attention'
RETURN p.title, p.year, p.citation_count
ORDER BY p.citation_count DESC
LIMIT 10
""")
coauthors = gq.query("""
MATCH (a1:Author)<-[:authored_by]-(p:Paper)
-[:authored_by]->(a2:Author)
WHERE a1.name = 'Ashish Vaswani'
RETURN DISTINCT a2.name, COUNT(p) as papers
ORDER BY papers DESC
""")
chain = gq.query("""
MATCH path = (p1:Paper)-[:cites*1..3]->(p2:Paper)
WHERE p1.title CONTAINS 'GPT-4'
RETURN path
LIMIT 20
""")
Use Cases
- Research KG: Build knowledge graphs from paper collections
- Literature QA: Grounded question answering over research
- Concept mapping: Visualize research concept relationships
- Citation analysis: Graph-based citation network analysis
- Domain ontology: Build and maintain domain-specific schemas
References