CodeGraph: Enhancing Graph Reasoning of LLMs with Code

Qiaolong Cai, Zhaowei Wang, Shizhe Diao, James Kwok, Yangqiu SongarXiv 2024 · DOI

TL;DR

Large Language Models (LLMs) struggle with precise arithmetic and logical reasoning in graph tasks when relying solely on natural language. This paper introduces CodeGraph, a novel method that prompts LLMs to generate Python code for graph problems, which is then executed by an interpreter. CodeGraph significantly improves LLM performance on basic graph reasoning tasks, offering a more controllable and interpretable approach.

What Problem Does This Paper Address?

The paper tackles a critical limitation of current Large Language Models (LLMs): their difficulty in performing accurate and reliable reasoning on graph-structured data, especially when arithmetic calculations are involved. While LLMs excel at natural language understanding and generation, they often falter when asked to count edges, calculate node degrees, or perform other quantitative graph operations. This is partly because converting complex graph structures into natural language descriptions can be ambiguous, and LLMs may "hallucinate" or make errors in these precise computations. Furthermore, the reasoning process itself can be opaque, making it hard to trust the LLM's output. The authors aim to bridge this gap by enabling LLMs to leverage their coding capabilities for robust graph reasoning.

Methods

The core innovation of CodeGraph is its program-aided reasoning approach. Instead of asking an LLM to directly answer a graph-related question in natural language, CodeGraph prompts the LLM to generate Python code that solves the problem. This involves:

  1. Task Description: Clearly defining the graph task (e.g., counting edges, checking for cycles) and specifying the desired output format (e.g., a Python variable ans).
  2. Exemplars: Providing few-shot examples of question-code pairs. These examples demonstrate how to translate graph descriptions (encoded in various formats like adjacency lists or friendship descriptions) into executable Python code.
  3. Code Generation: The LLM, guided by the task description and exemplars, generates Python code to solve the given graph problem.
  4. External Execution: The generated Python code is then executed by a standard Python interpreter. This offloads the precise arithmetic and logical computations to a reliable execution environment, mitigating LLM errors.

This approach makes the reasoning process more controllable and interpretable because the generated code explicitly shows the steps taken to arrive at the solution.

Datasets

The experiments were conducted using the GraphQA benchmark. This dataset contains graphs generated by various models (Erdős-Rényi, Barabási-Albert, scale-free networks, stochastic block model, path, star, and complete graphs) and covers six basic graph tasks. The authors used 500 graphs for training and 500 for testing.

Key Insights and Ideas

  • Code as a Reasoning Engine: The paper strongly advocates for using code generation as a more reliable mechanism for LLMs to perform precise, quantitative tasks, especially those involving structured data like graphs.
  • Decoupling Reasoning and Computation: By separating the LLM's role in generating code from the interpreter's role in executing it, CodeGraph achieves higher accuracy and robustness.
  • Controllability and Interpretability: The generated code provides a transparent view into the LLM's reasoning process, making it easier to debug and understand.
  • Robustness to Graph Encodings: CodeGraph's performance is shown to be relatively stable across different methods of encoding graph structures into natural language.
  • Sensitivity to Graph Structure: LLMs can exhibit biases based on graph structures (e.g., a bias towards graphs with cycles), which CodeGraph helps to mitigate by relying on code execution.

Key Results

CodeGraph significantly boosts LLM performance on basic graph reasoning tasks. For instance, using GPT-3.5 Turbo with one-shot learning, CodeGraph achieved an average accuracy of 96.1% across six tasks, a substantial improvement over zero-shot approaches. The performance gains ranged from 1.3% to 58.6% depending on the specific task and LLM. The method also demonstrated robustness across various graph structures and encoding functions. Notably, even smaller LLMs with weaker coding capabilities showed performance improvements when provided with more exemplars.

Limitations and Future Work

The authors acknowledge that while CodeGraph significantly improves performance, it still relies on the LLM's ability to generate correct code. Errors in code generation can still lead to incorrect answers. Future work could focus on:

  • Improving Code Generation Quality: Exploring more advanced decoding strategies or fine-tuning techniques to further enhance the LLM's code generation capabilities.
  • Handling More Complex Graph Tasks: Extending CodeGraph to tackle more intricate graph problems beyond basic tasks.
  • Multimodal Integration: Incorporating multimodal information to help LLMs better understand graph tasks.
  • Testing with Newer LLMs: Evaluating CodeGraph with the latest LLM architectures and sizes.

Summary updated

BibTeX
@misc{cai2024codegraph,
  author = {Qiaolong Cai and Zhaowei Wang and Shizhe Diao and James Kwok and Yangqiu Song},
  title = {{CodeGraph: Enhancing Graph Reasoning of LLMs with Code}},
  year = {2024},
  eprint = {2408.13863},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2408.13863},
}
Figure