Code4Struct: Code Generation for Few-Shot Structured Prediction from Natural Language

Xingyao Wang, Sha Li, Heng Ji

TL;DR

This paper introduces CODE4STRUCT, a novel approach that leverages Large Language Models (LLMs) trained on code to tackle structured prediction tasks in NLP, specifically Event Argument Extraction (EAE). By framing EAE as code generation, CODE4STRUCT effectively utilizes programming language features like inheritance and type annotations to improve accuracy, especially in low-resource scenarios, outperforming state-of-the-art methods with significantly less data.

What Problem Does This Paper Address?

Extracting structured information from unstructured text, such as identifying events and their associated arguments (e.g., who did what, where, and when), is a fundamental challenge in Natural Language Processing (NLP). Traditional methods often require large amounts of labeled data and struggle with few-shot or zero-shot learning scenarios. While LLMs have shown promise, effectively translating the complex semantic structures of events into a format that LLMs can readily process and generate remains a hurdle. The authors aim to bridge this gap by proposing a method that aligns natural language event structures with programming language constructs, thereby enabling LLMs to better understand and generate these structures.

Methods

The core innovation of CODE4STRUCT lies in its code generation-based approach to structured prediction. Instead of relying solely on natural language prompts, the authors translate the event ontology (definitions of event types and their arguments) into Python class definitions. This allows them to prompt an LLM to generate Python code that instantiates these event classes based on an input sentence.

Key techniques include:

  • Ontology to Code Translation: Event types and argument roles are mapped to Python classes and constructor arguments, respectively.
  • Leveraging PL Features:
    • Type Annotations: Used to enforce constraints on argument types (e.g., an argument must be a List[GPE | ORG | PER]).
  • Inheritance: Enables hierarchical event definitions, allowing child event types to inherit properties from parent types, crucial for zero-resource learning.
  • Default Values: Argument constraints can be naturally handled with default values (e.g., an empty list).
  • Code Generation Prompting: The LLM is prompted with the translated ontology and the input sentence, tasked with generating the code instantiation of the event.
  • In-context Learning: The approach supports few-shot learning by including examples within the prompt.

Datasets

The primary dataset used for evaluation is the English subset of the Automatic Content Extraction (ACE05-E) dataset. This dataset contains hierarchical event types, which are utilized by CODE4STRUCT to demonstrate the benefits of its hierarchical approach. The authors processed this dataset following established practices (Wadden et al., 2019; Lin et al., 2020).

Key Insights and Ideas

  • Code as a Structured Representation: The paper highlights that programming language constructs (classes, inheritance, type annotations) are a natural and powerful way to represent complex semantic structures like events, aligning well with the capabilities of code-trained LLMs.
  • Code Generation Outperforms Text Prompts (with sufficient examples): For Event Argument Extraction, using code-based prompts with LLMs is shown to be more effective than traditional text-based prompts, especially when a reasonable number of in-context examples are provided.
  • Leveraging Hierarchy for Zero-Resource Learning: The hierarchical nature of event ontologies, when translated into class inheritance, allows CODE4STRUCT to effectively transfer knowledge from high-resource event types to low-resource or zero-resource sibling types, significantly boosting performance.
  • Data Efficiency: CODE4STRUCT achieves comparable performance to fully supervised models trained on vastly more data, demonstrating its effectiveness in few-shot settings.

Key Results

  • State-of-the-Art Performance: With just 20 training instances per event type, CODE4STRUCT achieved performance comparable to supervised models trained on 4,202 instances.
  • Outperforming SOTA: CODE4STRUCT outperformed a state-of-the-art (SOTA) approach by 29.5% absolute F1 when both were trained on 20-shot data.
  • Zero-Resource Improvement: By leveraging 10-shot training data from a sibling event type, CODE4STRUCT improved zero-resource event type prediction by 12% absolute F1 compared to a zero-shot baseline.
  • Code Prompt Advantage: The code prompt consistently outperformed text prompts on Arg-C F1 when sufficient in-context examples were provided, with gains increasing as more examples were added.

Limitations and Future Work

  • Pre-defined Ontology: The current approach assumes that event triggers and argument templates (the ontology) are given. This limits its applicability to scenarios where an event detection system is already in place.
  • Limited Hierarchy Exploration: The study primarily explored hierarchical events with two levels from the ACE05-E ontology. Discovering complex or automatically discovering hierarchical ontologies is left for future work.
  • Inference Speed and Cost: LLMs used for experiments are significantly larger than traditional models, leading to slower and more costly inference.
  • In-context Example Selection: While the current method uses deterministic selection of in-context examples, future work could explore better selection and ordering strategies to further improve performance.
  • Future Work: The authors plan to extend CODE4STRUCT to a broader range of structured prediction tasks (e.g., relation prediction, schema matching) and explore the executable nature of PL to enhance LLM's structured prediction capabilities.

Summary updated

BibTeX
@inproceedings{wang2023code,
  author = {Xingyao Wang and Sha Li and Heng Ji},
  title = {{Code4Struct: Code Generation for Few-Shot Structured Prediction from Natural Language}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2023},
  url = {https://openreview.net/forum?id=Xqkq9DKCBN0},
}

Loading related content...

Figure