Abstract
KnowCoder transforms Universal Information Extraction (UIE) into a code generation task by representing complex knowledge schemas as Python classes. By utilizing a novel two-phase training framework on billions of automatically generated "schema codes," the model achieves state-of-the-art zero-shot and supervised performance across entity, relation, and event extraction tasks.
Introduction
Universal Information Extraction (UIE) is the process of extracting various types of structured knowledge—such as entities, relations, and events—from raw text using a single model. While Large Language Models (LLMs) are promising candidates for this task, they often struggle with complex schema representation and strict rule-following. Previous methods using plain text or custom formal languages frequently fail to capture hierarchical taxonomies and intricate constraints.
Methods
KnowCoder, built on LLaMA2-7B, addresses these challenges by leveraging Object-Oriented Programming (OOP) as a natural fit for knowledge representation.
Code-Style Schema Representation
KnowCoder translates extraction schemas into Python classes. In this framework, taxonomies are represented through class inheritance, constraints are enforced via type hints, and definitions are provided through docstrings. This approach allows the model to naturally respect hierarchical relationships and entity constraints without requiring specialized architectural guardrails.
Two-Phase Learning Framework
The model employs a two-phase training strategy to decouple concept understanding from extraction logic:
- Schema Understanding Phase (Code Pretraining): The model is pretrained on approximately 1.5 billion tokens of automatically generated Python code defining over 30,000 concepts. This phase teaches the model the semantic meaning of various entities and relations.
- Schema Following Phase (Instruction Tuning): The model is fine-tuned to act as a programmer, instantiating the defined Python classes based on information found in raw text inputs.
Key Insights
- Code as a Universal Language: Modern LLMs are heavily trained on code, making them inherently proficient at understanding OOP structures as knowledge schemas.
- Decoupling Understanding and Following: Splitting training into a pretraining phase for definitions and an instruction-tuning phase for extraction significantly improves generalization to unseen schemas (zero-shot performance).
- Executable Post-Processing: Embedding class methods directly into prompts allows for task-specific rule application, such as filtering pronouns from extracted names, which the model can natively apply.
Key Results
KnowCoder demonstrates significant improvements across 33 specific-domain IE datasets:
- Zero-Shot & Few-Shot: The model achieved a 49.8% relative F1 improvement on Named Entity Recognition (NER) after pretraining and outperformed state-of-the-art (SOTA) baselines on unseen schemas by up to 12.5%.
- Low-Resource Scenarios: When fine-tuned on just 1% of training data, KnowCoder showed a 21.9% average relative improvement across all IE tasks.
- Supervised Performance: KnowCoder surpassed specialized models in complex tasks, achieving 73.9% F1 on Event Detection and 66.0% on Event Argument Extraction, marking a milestone for LLM-based UIE methods.
Conclusion
While KnowCoder relies on Wikidata and automatically generated data which may introduce some noise, it establishes a powerful new paradigm for UIE. By framing extraction as a code generation task, it leverages the existing strengths of LLMs to handle complex, structured knowledge. Future work will focus on refining the training corpus and further enhancing zero-shot capabilities for relations and events.
Summary updated