Skip to content

Knowledge Graph ​

The Knowledge Graph module of NiteMoon AI Platform is built on Neo4j, automatically extracting entities and relations through LLM to construct an intelligent knowledge network, supporting graph traversal retrieval and 1-hop neighbor expansion.

Key Features ​

  • LLM entity-relation extraction: Automatically extract entities and relations from text
  • HanLP Chinese tokenization: Accurate Chinese segmentation support
  • Document chunking: Configurable chunk size and overlap
  • Entity deduplication: Confidence scoring to avoid duplicate entities
  • Graph traversal retrieval: Support for 1-hop neighbor expansion
  • Document fragment integration: Retrieval results linked to original documents

Workflow ​

mermaid
graph TD
    A[Document Upload] --> B[Document Chunking]
    B --> C[Entity Extraction]
    C --> D[Relation Extraction]
    D --> E[Entity Deduplication]
    E --> F[Store in Neo4j]
    G[User Query] --> H[Keyword Extraction]
    H --> I[Graph Traversal Retrieval]
    F --> I
    I --> J[1-Hop Neighbor Expansion]
    J --> K[Context Construction]
    K --> L[LLM Generate Response]

Entity Extraction ​

LLM Extraction ​

Use LLM to automatically extract entities from text:

java
@Entity
public class KGEntity {
    @Id
    private Long id;
    private String name;           // Entity name
    private String type;           // Entity type (Person, Location, Organization, etc.)
    private String description;    // Entity description
    private Double confidence;     // Confidence score
    private Map<String, Object> properties;  // Additional properties
}

Entity Types ​

  • PERSON - Person
  • ORGANIZATION - Organization
  • LOCATION - Location
  • EVENT - Event
  • CONCEPT - Concept
  • TECHNOLOGY - Technology
  • PRODUCT - Product

Relation Extraction ​

java
@Entity
public class KGRelation {
    @Id
    private Long id;
    private Long sourceId;         // Source entity ID
    private Long targetId;         // Target entity ID
    private String type;           // Relation type
    private String description;    // Relation description
    private Double confidence;     // Confidence score
}

Relation Types ​

  • WORKS_FOR - Works for
  • LOCATED_IN - Located in
  • PART_OF - Part of
  • CREATED_BY - Created by
  • USES - Uses
  • RELATED_TO - Related to
  • DEPENDS_ON - Depends on

Document Processing ​

Document Chunking ​

yaml
kg:
  chunking:
    chunk-size: 1000       # Chunk size
    chunk-overlap: 100     # Overlap size

HanLP Chinese Tokenization ​

Use HanLP for accurate Chinese tokenization:

java
@Service
public class ChineseTokenizerService {
    
    public List<String> tokenize(String text) {
        // HanLP segmentation
        List<Term> terms = HanLP.segment(text);
        return terms.stream()
            .map(term -> term.word)
            .collect(Collectors.toList());
    }
}

Graph Traversal Retrieval ​

1-Hop Neighbor Expansion ​

Expand 1-hop neighbors from matched entities:

cypher
MATCH (e:Entity)-[r]-(neighbor:Entity)
WHERE e.name = $entityName
RETURN e, r, neighbor
LIMIT 10

Multi-Hop Traversal ​

Support for multi-hop relation traversal:

cypher
MATCH path = (start:Entity)-[*1..3]-(end:Entity)
WHERE start.name = $startEntity
AND end.name = $endEntity
RETURN path

Use Cases ​

Knowledge Network Construction ​

java
// Upload document to build knowledge graph
kgService.buildFromDocument(documentId);

// Query entity
KGEntity entity = kgService.findEntity("Nitemoon");

// Query relations
List<KGRelation> relations = kgService.findRelations(entity.getId());

Intelligent Q&A ​

java
// Knowledge graph-based Q&A
KGQueryResult result = kgService.query(
    "What are the core features of NiteMoon AI Platform?"
);

// Get answer
String answer = result.getAnswer();
List<KGEntity> entities = result.getEntities();
List<KGRelation> relations = result.getRelations();

Relation Analysis ​

java
// Analyze entity relations
EntityNetwork network = kgService.analyzeRelations("Artificial Intelligence");

// Get relation graph
Map<KGEntity, List<KGRelation>> graph = network.getGraph();

Code Examples ​

Create a Knowledge Graph ​

java
@Service
public class KnowledgeGraphService {
    
    public KnowledgeGraph create(String name, String description) {
        KnowledgeGraph kg = new KnowledgeGraph();
        kg.setName(name);
        kg.setDescription(description);
        return kgRepository.save(kg);
    }
}

Entity Extraction ​

java
@Service
public class EntityExtractionService {
    
    public List<KGEntity> extractEntities(String text) {
        // Use LLM to extract entities
        String prompt = String.format(
            "Extract entities from the following text, return in JSON format:\n%s",
            text
        );
        
        String response = llmService.generate(prompt);
        return parseEntities(response);
    }
}

Relation Extraction ​

java
@Service
public class RelationExtractionService {
    
    public List<KGRelation> extractRelations(String text, List<KGEntity> entities) {
        // Use LLM to extract relations
        String prompt = String.format(
            "Based on the following text and entities, extract relations between entities:\n\nText: %s\n\nEntities: %s",
            text,
            JSON.toJSONString(entities)
        );
        
        String response = llmService.generate(prompt);
        return parseRelations(response);
    }
}

Graph Traversal Query ​

java
@Service
public class GraphTraversalService {
    
    public List<KGEntity> findNeighbors(Long entityId, int hops) {
        String cypher = String.format(
            "MATCH (e:Entity)-[*1..%d]-(neighbor:Entity) " +
            "WHERE id(e) = $entityId " +
            "RETURN DISTINCT neighbor",
            hops
        );
        
        return neo4jTemplate.findAll(cypher, Map.of("entityId", entityId));
    }
}

Best Practices ​

  1. Set appropriate chunk size: Adjust based on document type; technical documents can use larger chunks
  2. Entity deduplication: Use confidence scoring to merge similar entities
  3. Regular updates: Knowledge graphs need periodic updates to stay current
  4. Monitor quality: Periodically evaluate the accuracy of entities and relations
  5. Optimize queries: Create indexes for frequently used queries

Copyright © 2023-2026 nitemoon.cn All Rights Reserved