NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Retrieval-Augmented Code Generation for Universal Information Extraction

Yucan Guo, Zixuan Li, Xiaolong Jin, Yantao Liu, Yutao Zeng, Wenxuan Liu, Xiang Li, Pan Yang, Long Bai, Jiafeng Guo, Xueqi Cheng

TL;DR

This paper introduces Code4UIE, a novel framework that tackles the challenge of Universal Information Extraction (UIE) by framing it as a code generation task for Large Language Models (LLMs). By representing extraction schemas as Python classes and leveraging retrieval-augmented in-context learning, Code4UIE achieves state-of-the-art performance across various IE tasks, demonstrating the power of code generation for structured data extraction.

What Problem Does This Paper Address?

Information Extraction (IE) is a fundamental NLP task focused on converting unstructured text into structured knowledge like entities, relations, and events. However, existing IE methods struggle with two main challenges: 1) the diversity of task-specific schemas (how information is organized) and 2) the inherent complexity and variability of natural language expressions. While previous work has attempted to unify IE tasks, many require extensive fine-tuning or suffer from long training times. This paper addresses the need for a more universal, efficient, and adaptable IE framework, particularly in low-resource settings, by exploring the potential of LLMs and code generation.

Methods

The core innovation of Code4UIE lies in its approach to schema representation and its integration with LLMs.

  • Schema Representation as Python Classes: Instead of task-specific formats, Code4UIE uses Python classes to define schemas for entities, relations, and events. This creates a universal and structured way to represent diverse IE requirements. For instance, an "Entity" base class can be inherited by subclasses like "Person" or "Location," and similarly for "Relation" and "Event" types.
  • Code Generation for IE: The IE task is transformed into a code generation problem: LLMs are prompted to generate Python code that instantiates these predefined classes with information extracted from the input text. This leverages the LLMs' strong capabilities in both understanding natural language and generating code.
  • Retrieval-Augmented In-Context Learning: To guide the LLMs effectively, Code4UIE employs in-context learning with carefully selected examples. It introduces two novel retrieval strategies:
  • Sentence Embedding-based Retrieval: Uses MPNet to encode sentences and retrieve examples semantically similar to the input text.
  • Anonymous Sentence Embedding-based Retrieval: A more advanced strategy that replaces entities with their types (using a NER model) before encoding, focusing on contextual and type similarity rather than exact entity text. This is particularly useful for complex tasks like event extraction.
  • Prompting Strategies: Code4UIE utilizes both 1-stage and 2-stage prompts. The 1-stage prompt handles the entire extraction process end-to-end. The 2-stage prompt breaks down the task, first focusing on identifying relevant classes (e.g., importing specific entity types) and then extracting instances.

Datasets

The paper evaluates Code4UIE on a comprehensive set of datasets across five IE tasks:

  • Named Entity Recognition (NER): ACE04, ACE05, CONLL03
  • Relation Extraction (RE): ADE, ACE05, CoNLL04, NYT
  • Event Detection (ED): ACE05, CASIE
  • Event Argument Extraction (EAE): ACE05
  • Event Extraction (EE): ACE05, CASIE

Key Insights and Ideas

  • Code as a Universal Schema: Representing IE schemas as Python classes provides a remarkably flexible and universal way to handle diverse extraction requirements, bridging the gap between natural language and structured data.
  • Code Generation is Powerful for IE: LLMs, when prompted to generate code that instantiates these structured schemas, can outperform traditional text-based IE approaches, especially in few-shot scenarios.
  • Retrieval is Crucial for Few-Shot IE: The effectiveness of in-context learning heavily relies on the quality of retrieved examples. Code4UIE's novel retrieval strategies significantly improve performance by selecting more relevant examples than random selection.
  • Anonymous Retrieval for Complex Tasks: For event-related tasks, focusing on anonymous sentence embeddings (entity types instead of exact text) proves more effective, highlighting the importance of contextual and type information.

Key Results

Code4UIE demonstrates strong performance across all evaluated IE tasks, often surpassing existing LLM-based IE approaches.

  • NER: Code4UIE achieves competitive results with fewer in-context examples compared to CodeIE, a prior code-generation IE method, especially when using models like text-davinci-002 and text-davinci-003.
  • RE: Code-style prompts in Code4UIE outperform text-style prompts (like In-context GPT3) and CodeIE, even with fewer examples. The sentence embedding-based retrieval strategy is particularly beneficial for RE.
  • ED, EAE, EE: Code4UIE shows significant improvements over text-style prompts and other methods. For event-related tasks (ED, EAE, EE), the anonymous sentence embedding-based retrieval strategy is consistently better. For example, on the ED task, Code4UIE yields improvements of up to 26.9% on the ACE05 dataset.

Limitations and Future Work

  • Performance Gap with Fully Supervised Models: While Code4UIE performs well, especially in few-shot settings, it still lags behind fully supervised UIE models in some cases.
  • LLM Capabilities: The performance is dependent on the underlying LLM's capabilities. The paper notes that while Code4UIE performs well, there's a performance gap when compared to fully supervised UIE models.
  • Future Work: The paper suggests exploring more advanced retrieval strategies and further optimizing the prompt engineering for even better performance. The authors also implicitly point towards the potential of this code-generation paradigm for other structured prediction tasks beyond traditional IE.

Summary updated

BibTeX
@inproceedings{guo2024retrieval,
  author = {Yucan Guo and Zixuan Li and Xiaolong Jin and Yantao Liu and Yutao Zeng and Wenxuan Liu and Xiang Li and Pan Yang and Long Bai and Jiafeng Guo and Xueqi Cheng},
  title = {{Retrieval-Augmented Code Generation for Universal Information Extraction}},
  booktitle = {NLPCC},
  year = {2024},
  doi = {10.1007/978-981-97-9434-8\_3},
}
Figure