Skip to content

Orange-OpenSource/Knowledge-Editing

Repository files navigation

Expanding and Improving Knowledge Updating Evaluation

This repository aims to provide the necessary tools to reproduce the results of our study (paper). A quick overview is presented in what follows.

Our study introduces a new evaluation protocol for factual knowledge updating in language models, addressing key limitations in the widely used CounterFact protocol for the evaluation of model editing. Current evaluation methods rely heavily on synthetic updates, have restricted coverage of update scenarios, and often overestimate editing success. To overcome these issues, this project relies on WikiFactDiff, a dataset of real-world factual changes automatically extracted by comparing Wikidata snapshots across time, and integrates it with a distractor-based assessment for precise evaluation.

Using this protocol, this repository enables the benchmarking of state-of-the-art model editing algorithms across diverse update scenarios, including non-functional relations. The results reveal that existing model editing methods are not yet suitable for real-world knowledge maintenance: they either fail to apply updates reliably or induce substantial collateral damage to unrelated facts. They also lack temporal awareness and exhibit worsening performance as the number of edits increases.

Overall, our work provides a more realistic and accurate evaluation framework for factual knowledge updating and shows that current model editing techniques fall short of the requirements for maintaining up-to-date, consistent knowledge in large language models.

Environment Setup

Tools

It is necessary to install:

  • MongoDB (or have access to a MongoDB instance)
  • uv package manager (see here)

Variables

Set the environment variables:

  • STORAGE_FOLDER: The folder where to store intermediate files (800GB disk space within this folder is largely enough). It needs to be physically accessible and not through network (NAS). Otherwise, you will have sync problems using the cache library diskcache which could corrupt your cache and crash your experiments.
  • MONGO_URL: If MongoDB does not run locally with no authentification, specify its URL in this variable. Else, do nothing.

Virtual Env

Important: For all following commands in this README file, it is expected from you to be located exactly at the root of this project.

Simply execute the folowing command:

uv sync

Data

Execute all the commands preferrably. The commands in Step 1 and 2 can all be run in parallel (depending on your machine capabilities):

  1. Download Wikidata:
from wikidata_tools.wikidata import TempWikidata, WikidataPopularity, WikidataPrepStage

wd = TempWikidata("20210104", WikidataPrepStage.PREPROCESSED)
wd.build(confirm=False)
from wikidata_tools.wikidata import TempWikidata, WikidataPopularity, WikidataPrepStage

wd = TempWikidata("20230227", WikidataPrepStage.PREPROCESSED)
wd.build(confirm=False)
  1. Wikipedia Popularity Statistics:
from wikidata_tools.wikidata import WikidataPrepStage

wikipop = WikidataPopularity("20210104")
wikipop.build(confirm=False)
from wikidata_tools.wikidata import WikidataPrepStage

wikipop = WikidataPopularity("20230227")
wikipop.build(confirm=False)
  1. Collect Random Popular Facts: They are useful to assess whether factual knowledge globally was altered by the model editing algorithm. They will be stored in STORAGE_FOLDER.
python experiments/nonfunct-edit-eval/collect_popular_facts.py
  1. Build InMemoryWikidata: Build a small version of Wikidata that can be loaded in memory.
python experiments/nonfunct-edit-eval/build_inmemory_wikidata.py
  1. Cache GPT-4o Factual Questions: Questions generated by GPT-4o are cached once to avoid calling it again for each experiment.
export OPENAI_API_KEY="your-key"
python experiments/nonfunct-edit-eval/cache_wikifactdiff_verbs.py

Experiments

Edit the GPT-J (6B) model and measure its knowledge on various facts pre- and post-updating.

Single-Update

In this case, only one fact is edited at a time. The model is reseted to its original state after each step. To collect results for this experiment, run all the commands in experiments/nonfunct-edit-eval/single_update_commands.sh. They can be run in parallel.

Multi-Update

In this case, many facts are edited and then the model is evaluated. To collect results for this experiment, run all the commands in experiments/nonfunct-edit-eval/multi_update_commands.sh. They can be run in parallel.

The results are stored in the root of this project in know-edit-measures. The folder know-edit-models can be deleted at this point.

Result Analysis

Execute the notebook experiments/nonfunct-edit-eval/analyze_results.ipynb to generate most of the plots in our paper.

How to cite our work?

Coming soon

Tools Behind This Project

These experiments are based on several packages. They are accompanied by comprehensive documentation to learn how to use them.

  1. wikidata-tools: A package to collect and preprocess Wikidata dumps automatically. Look at packages/wikidata-tools/README.md for more information.
  2. verb-tools: A package to automatically verbalize facts in natural language through LLMs. Look at packages/verb-tools/README.md for more information.
  3. lm-tools: A wrapper over the HuggingFace LLM API. It provides useful functions to evaluate language models. Look at packages/lm-tools/README.md for more information.
  4. know-measure: A package containing the factual knowledge assessment method used to evaluate the success of a factual update on a language model. Look at packages/know-measure/README.md for more information.
  5. know-edit: A package that provides easy-to-use functions and classes to apply model editing to LLMs. It is mainly a wrapper over the EasyEdit package. Look at packages/know-edit/README.md for more information.
  6. ke-utils: Utility functions for the whole project. Look at packages/ke-utils/README.md for more information.
  7. easyedit: A fork of the EasyEdit package to make it easier to use and installable through pip install and uv add.
  8. divergent-beamsearch: A modified beam-search algorithm which is a requirement for the factual knowledge assessment method used to evaluate model editing methods. Look at packages/divergent-beamsearch/README.md for more information.

License

Look for the LICENSE.txt file at the root of this project.

About

This repository aims to provide the necessary tools to reproduce the results of studies concerning factual knowledge updating in LLMs. Those studies were conducted by Hichem Ammar Khodja during his PhD.

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors