CodeKGC: Code Language Model for Generative Knowledge Graph Construction

Zhen Bi, Jing Chen, Yinuo Jiang, Feiyu Xiong, Wei Guo, Huajun Chen, Ningyu Zhang

TL;DR

CodeKGC re-frames generative knowledge graph construction (KGC) as a code generation task, leveraging the structural reasoning capabilities of Large Language Models trained on code (Code-LLMs). By representing KG schemas as Python classes and utilizing rationale-enhanced prompts, the method significantly improves the extraction of complex, overlapping relational triples from unstructured text.

What Problem Does This Paper Address?

Knowledge Graph Construction involves extracting structured information—typically in the form of (subject, relation, object) triples—from natural language. Current generative approaches usually "flatten" these structures into simple text strings or specialized specification languages.

However, natural language is often too "flat" for models to capture complex topological dependencies. This leads to two major issues:

  1. Long-distance dependencies: Related entities might be separated by many words, making it hard for a standard language model to link them.
  2. Overlapping facts: A single entity might participate in multiple relations (e.g., "Mikhail Belyaev" as both a "minister of war" and having an "allegiance" to the "Russian army").

The authors argue that because code is inherently structured (with nested functions, classes, and strict syntax), Code-LLMs possess a superior "structural prior" that can be harnessed for KGC.

Methods

The core of CodeKGC is the transformation of the KGC task into a code completion problem.

  • Schema-aware Code Prompts: Instead of describing a schema in natural language, the authors define it using Python class definitions. As seen in Figure 2, base classes like Entity and Rel are defined, and specific types (e.g., class Person(Entity)) inherit from them. This allows the model to utilize its understanding of class inheritance and instantiation to maintain semantic constraints.
Overview of CodeKGC showing schema prompts and rationale generation
  • Rationale-enhanced Generation: Inspired by Chain-of-Thought prompting, the authors introduce intermediate reasoning steps. The model is prompted to first identify relations, then extract candidate entities, and finally generate the structured code output. This decomposition helps the model focus on specific aspects of the text before committing to a final structure.
Detailed steps of the rationale-enhanced generation
  • Model Backbones: The researchers evaluated the method using high-capacity models like text-davinci-003 and code-davinci-002 (Codex). They also experimented with pre-training smaller models (770M parameters) based on T5 and CodeT5 using a custom corpus re-structured into code format.

Datasets

The authors evaluated CodeKGC on three benchmark datasets:

  1. ADE: A medical dataset focused on adverse drug events (1 relation, 2 entity types).
  2. CoNLL04: A general-domain dataset (4 relations, 3 entity types).
  3. SciERC: A scientific paper dataset (7 relations, 6 entity types).

For the pre-training of smaller models, they converted three massive KG-to-text datasets—T-REx, TEKGEN, and KELM—into Python code formats, totaling over 50 million triples.

Statistics for the pre-training datasets converted to code format

Key Insights and Ideas

  • Code as a Structural Bridge: Code serves as a middle ground between highly flexible natural language and rigid database schemas. Code-LLMs are already optimized to handle the "nestedness" and "strictness" that KGC requires.
  • Implicit Constraint Enforcement: By using Python's __init__ methods and class structures, the model is naturally guided to produce valid triples without needing explicit "don't do X" instructions in the prompt.
  • Superiority in "Hard" Cases: The most striking insight is how much better code-formatting handles overlapping triples. In natural language, repeating an entity in a flattened string feels redundant and confusing to a model; in code, instantiating multiple Triple objects with the same Person object is standard practice.

Key Results

CodeKGC consistently outperformed the supervised SOTA (UIE) and vanilla natural language prompting across all datasets:

  • Few-shot Performance: On the CoNLL04 dataset (3-shot), CodeKGC using text-davinci-003 achieved a 49.6 F1 score, a significant jump over the 43.2 achieved by vanilla prompting.
  • Handling Complexity: In "hard" samples involving overlapping triples, CodeKGC with code-davinci-002 reached an F1 of 59.7, compared to 53.6 for vanilla prompts.
  • Small Model Gains: Even for smaller 770M parameter models, pre-training on re-structured code data (T5-Restructured) led to a massive performance boost (75.6 F1 on ADE) compared to standard pre-training (52.2 F1).

Limitations and Future Work

  • Dependency on Large Models: The best results currently require very large, often closed-source models like Codex. The authors noted that smaller models still struggle to balance semantic understanding with code generation.
  • Prompt Length: Rationale-enhanced prompts are significantly longer than standard ones. When the number of few-shot examples increases, the prompt might exceed the model's context window or become computationally expensive.
  • Future Directions: The authors suggest exploring automatic prompt optimization and extending the method to other tasks like Knowledge Graph Completion and complex reasoning.

Summary updated

BibTeX
@article{bi2024codekgc,
  author = {Zhen Bi and Jing Chen and Yinuo Jiang and Feiyu Xiong and Wei Guo and Huajun Chen and Ningyu Zhang},
  title = {{CodeKGC: Code Language Model for Generative Knowledge Graph Construction}},
  journal = {ACM Trans. Asian Low Resour. Lang. Inf. Process.},
  year = {2024},
  url = {https://openreview.net/forum?id=uLxVYsEtrt},
}
Figure