[Paper Review] CodeKGC: Code Language Model for Generative Knowledge Graph Construction
CodeKGC reframes knowledge graph construction as a code-generation task by converting natural language inputs into code-like prompts, leveraging schema-aware prompts and rationale-enhanced generation to outperform baselines on ADE, CoNLL04, and SciERC in zero-shot and few-shot settings.
Current generative knowledge graph construction approaches usually fail to capture structural knowledge by simply flattening natural language into serialized texts or a specification language. However, large generative language model trained on structured data such as code has demonstrated impressive capability in understanding natural language for structural prediction and reasoning tasks. Intuitively, we address the task of generative knowledge graph construction with code language model: given a code-format natural language input, the target is to generate triples which can be represented as code completion tasks. Specifically, we develop schema-aware prompts that effectively utilize the semantic structure within the knowledge graph. As code inherently possesses structure, such as class and function definitions, it serves as a useful model for prior semantic structural knowledge. Furthermore, we employ a rationale-enhanced generation method to boost the performance. Rationales provide intermediate steps, thereby improving knowledge extraction abilities. Experimental results indicate that the proposed approach can obtain better performance on benchmark datasets compared with baselines. Code and datasets are available in https://github.com/zjunlp/DeepKE/tree/main/example/llm.
Motivation & Objective
- Motivate end-to-end knowledge graph construction without error-prone Pipelines by leveraging structured code representations.
- Propose a code-formatted prompt design that encodes KG schema through Python class definitions to preserve semantic structure.
- Introduce a rationale-enhanced generation mechanism to provide intermediate reasoning steps and guide extraction of entities/relations.
- Evaluate CodeKGC on three benchmarks (ADE, CoNLL04, SciERC) in zero-shot and few-shot settings to demonstrate effectiveness over baselines.
Proposed method
- Transform KG triples into Python code format using base classes (Entity, Relation, Triple) to encode schema.
- Use schema-aware prompts that embed KG constraints and types via code inheritance for structured generation.
- Optionally incorporate rationales (chain-of-thought style) into prompts to guide multi-step reasoning during generation.
- Backbone language models include Code-davinci-002, text-davinci-002, and text-davinci-003 in zero- and few-shot settings; a smaller Code-LM pre-trained on code-structured KG data is explored for Q4.
- Evaluate using relation-strict micro F1 on ADE, CoNLL04, SciERC; report zero-shot and few-shot results; ablations on schema prompts and rationales.
Experimental results
Research questions
- RQ1How does CodeKGC perform compared with baselines in zero-shot and few-shot KG construction across ADE, CoNLL04, and SciERC?
- RQ2What is the impact of schema-aware code prompts on preserving KG structure during generation?
- RQ3Do rationale-enhanced generations improve knowledge extraction accuracy and handling of complex/overlapping relations?
- RQ4How do smaller code-language models affect CodeKGC performance compared to large Code-LLMs?
Key findings
- CodeKGC with code-based prompts outperforms UIE and vanilla text prompts on all three datasets in zero-shot and few-shot settings.
- Schema-aware prompts and rationale-enhanced generation both contribute to performance gains, especially on CoNLL04; rationales help but lengthy prompts can hurt with small samples.
- CodeKGC better handles overlapping and long-range dependencies than vanilla prompts, particularly with code-backed models.
- A smaller code LM pre-trained on code-structured KG data can approach UIE and CodeKGC performance, with semantic understanding and code-format benefits playing roles.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.