https://github.com/amazon-science/byokg-naacl24

https://github.com/amazon-science/byokg-naacl24

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (11.9%) to scientific vocabulary

Keywords from Contributors

generic embedded controllers interactive transformers projection sequences archival observability autograding
Last synced: 11 months ago · JSON representation

Repository

Basic Info
  • Host: GitHub
  • Owner: amazon-science
  • License: apache-2.0
  • Language: Python
  • Default Branch: main
  • Size: 157 KB
Statistics
  • Stars: 16
  • Watchers: 4
  • Forks: 1
  • Open Issues: 4
  • Releases: 0
Created over 2 years ago · Last pushed almost 2 years ago
Metadata Files
Readme Contributing License Code of conduct

README.md

BYOKG

This is the official implementation of the NAACL'24 paper Bring Your Own KG: Self-Supervised Program Synthesis for Zero-Shot KGQA.

Environment setup

shell conda create -n byokg python=3.9 --yes conda activate byokg pip install -r requirements.txt sh additional_packages.sh

Download datasets and graphs

shell cd data && \ gdown --folder 13w1tfA3YL88y-HwXU3oZlr0oeyIdc_t0 && \ gdown --folder 1YfGmENowy3H1meysBi4AG7oyY2fHITDz && \ cd ..

Virtuoso (SPARQL server) setup

shell sh scripts/setup_virtuoso.sh

Running the server

```shell

Starting the server (-d specifies the directory containing virtuoso.db) at a specific port (e.g. "3001")

python3 virtuoso/virtuoso.py start 3001 -d ./virtuoso

Stopping the server at a given port (e.g. "3001")

python3 virtuoso/virtuoso.py stop 3001 ``` A machine with 100GB RAM is recommended. You may adjust the maximum amount of RAM the service can use and other configurations via the provided script.

KG setup

Freebase

shell sh scripts/setup_freebase.sh Notes: - This may take ~10 minutes. It will download and unzip a virtuoso.db file containing the Freebase KG (~130G). - The above command will overwrite any existing virtuoso.db DB file in the ./virtuoso directory. If you plan to also use Freebase in addition to any other KG, we recommend running this first and adding the additional KGs to the same virtuoso.db file as described below. If you do not plan to use Freebase, you can skip this step.

MoviesKG/MetaQA (and any other arbitrary graph)

Generating an n-triples file from a text file of triples

If you already have an n-triples (.nt) file, skip to n-triples loading.

To generate an n-triples file from a text file (see data/graphs/metaqa/kb.txt for an example) of triples: shell python src/explorer.py \ --kg_name="metaqa" \ --kg_path="data/graphs/metaqa" \ --triples_fname="kb.txt" \ --kg_prefix="movie" \ --kg_write_ntriples This will result in data/graphs/metaqa/graph.nt, which can be loaded into Virtuoso.

Loading n-triples into Virtuoso

First, stop the Virtuoso server if running, and add the following modification to ./virtuoso/virtuoso.py to allow Virtuoso to read your n-triples file from the directory where it is stored: ```shell

Find "DirsAllowed" and add the absolute path to the directory containing graph.nt (or your .nt file)

For e.g. if the file path is /abs/path/to/project/data/graphs/metaqa/graph.nt, then modify the line to:

DirsAllowed = ., /abs/path/to/project/data/graphs/metaqa\n

```

Now, to load the n-triples file (say, graph.nt for MetaQA (MoviesKG)) into Virtuoso: - Start the server shell python3 virtuoso/virtuoso.py start 3001 -d ./virtuoso - Start isql shell virtuoso/virtuoso-opensource/bin/isql 13001 - Create the new graph SPARQL CREATE GRAPH <http://metaqa.com>; ld_dir('/abs/path/to/project/data/graphs/metaqa', 'graph.nt', 'http://metaqa.com'); rdf_loader_run(); select * from DB.DBA.load_list; exit;

Note: For other KGs, replace http://metaqa.com with http://<CUSTOM_KG_NAME>.com.

Graph Exploration

Example command for MoviesKG: shell python src/explorer.py \ --kg_explore \ --kg_name="metaqa" \ --kg_prefix="movie" \ --kg_path="data/graphs/metaqa" \ --sparql_cache="data/graphs/metaqa/_sparql_cache.json" \ --kg_n_walks=10000 \ --save_interval=500 Notes: - This will output results.json containing the explored programs and stats.json containing some analyses for the exploration run. - Certain flags, such as --filter_empty_walks and --prune_redundant (True by default), require a virtuoso server running at --sparql_url. - --kg_prefix is used for MoviesKG due to the provided triples file. This flag is not needed for Freebase (kg_name="freebase"), and may not be needed for other KGs.

Query Generation

First, we need to preprocess the output from explorer.py: shell python scripts/prep_data_for_qgen.py \ --walks_fpath=path/to/explorer/results.json \ --rev_schema_fpath=data/graphs/metaqa/schema.json Notes: - This will output qgen_walks.json in the same directory as path/to/explorer/results.json. - When using GrailQA training data instead of explorations in --walks_fpath, add --sexpr_machine_key="s_expression".

Now, run generation: ```shell

Example with open-source models hosted on HuggingFace

python src/questiongeneratorl2m.py \ --model="mosaicml/mpt-7b-instruct" \ --kgname=metaqa \ --kgschemafpath=data/graphs/metaqa/schema.json \ --loaddevfpath=path/to/qgenwalks.json \ --evaloutputsamplingstrategy=inverse-len-norm \ --forcetypeconstraint \ --saveinterval=200

Example with OpenAI API

python src/questiongeneratorl2m.py \ --model="openai/gpt-4" \ --kgname=metaqa \ --kgschemafpath=data/graphs/metaqa/schema.json \ --loaddevfpath=path/to/qgenwalks.json \ --evaloutputsamplingstrategy=max \ # This is the only sampling strategy available for OpenAI API (will be forced) --forcetypeconstraint \ --saveinterval=200 `` **Notes:** - This will outputresults.json` containing natural language questions for the processed programs (walks).

Reasoning

For MetaQA, we first need to construct the dataset (this needs to be run only once): python scripts/build_metaqa_dataset.py python scripts/prep_data_for_qa.py \ --metaqa_dir=data/datasets/metaqa \ --rev_schema_fpath=data/graphs/metaqa/schema.json \ --sexpr_machine_key=s_expression

After running query generation, first preprocess the output from question_generator_l2m.py: shell python scripts/prep_data_for_qa.py \ --walks_qgen_in_fpath=path/to/qgen_walks.json \ --walks_qgen_out_fpath=path/to/qgen/results.json This will output qa_walks.json.

Now, run reasoning. Example command for MetaQA: ```shell python src/reasoner.py \ --model="mosaicml/mpt-7b-instruct" \ --kgname=metaqa \ --datasetname=metaqa \ --evalsplit=test \ --loadtestfpath=data/datasets/metaqa/qatest.json \ --evalnsamples=-1 \ --sparqlcache=data/graphs/metaqa/sparqlcache.json \ --revschemafpath=data/graphs/metaqa/schema.json \ --loadtrainfpath=path/to/qawalks.json \ --demoslabel=walks \ --saveinterval=100

The above will output results.json. Then, run candidate re-ranking:

python src/reasoner.py \ --model="mosaicml/mpt-7b-instruct" \ --kgname=metaqa \ --sparqlcache=data/graphs/metaqa/sparqlcache.json \ --revschemafpath=data/graphs/metaqa/schema.json \ --rerankfpath=path/to/reasoning/results.json `` **Notes:** - This will outputresults.jsonandrerankedresults.json. - You can also pass curated training data to--loadtrainfpathinstead of the explorations, but make sure it is in the same format as that expected here. See theprepdatafor_*` files. - As of 01/2024, OpenAI API does not provide access to the logits of the full sequence, which is required for the BYOKG reasoner. We're therefore currently only supporting models hosted on HuggingFace.

Citation

If you use any component of this project for your work, please cite the following @inproceedings{ agarwal2024bring, title={Bring Your Own {KG}: Self-Supervised Program Synthesis for Zero-Shot {KGQA}}, author={Dhruv Agarwal and Rajarshi Das and Sopan Khosla and Rashmi Gangadharaiah}, booktitle={2024 Annual Conference of the North American Chapter of the Association for Computational Linguistics}, year={2024}, url={https://openreview.net/forum?id=Z1IscjaN3g} }

Security

See CONTRIBUTING for more information.

License

This project is licensed under the Apache-2.0 License.

Owner

  • Name: Amazon Science
  • Login: amazon-science
  • Kind: organization

GitHub Events

Total
  • Watch event: 5
  • Pull request event: 1
Last Year
  • Watch event: 5
  • Pull request event: 1

Committers

Last synced: about 1 year ago

All Time
  • Total Commits: 28
  • Total Committers: 5
  • Avg Commits per committer: 5.6
  • Development Distribution Score (DDS): 0.25
Past Year
  • Commits: 5
  • Committers: 2
  • Avg Commits per committer: 2.5
  • Development Distribution Score (DDS): 0.2
Top Committers
Name Email Commits
dependabot[bot] 4****] 21
Rajarshi Das d****r@a****m 3
Dhruv Agarwal d****7@g****m 2
Rajarshi Das r****d 1
Amazon GitHub Automation 5****o 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: about 1 year ago

All Time
  • Total issues: 2
  • Total pull requests: 26
  • Average time to close issues: 28 days
  • Average time to close pull requests: 18 days
  • Total issue authors: 2
  • Total pull request authors: 2
  • Average comments per issue: 1.0
  • Average comments per pull request: 0.0
  • Merged pull requests: 23
  • Bot issues: 0
  • Bot pull requests: 23
Past Year
  • Issues: 2
  • Pull requests: 9
  • Average time to close issues: 28 days
  • Average time to close pull requests: 15 days
  • Issue authors: 2
  • Pull request authors: 2
  • Average comments per issue: 1.0
  • Average comments per pull request: 0.0
  • Merged pull requests: 6
  • Bot issues: 0
  • Bot pull requests: 7
Top Authors
Issue Authors
  • me-alone-study (1)
  • AriKing11 (1)
Pull Request Authors
  • dependabot[bot] (34)
  • dhdhagar (6)
Top Labels
Issue Labels
Pull Request Labels
dependencies (34)

Dependencies

requirements.txt pypi
  • Babel ==2.12.1
  • Pillow ==10.2.0
  • PyYAML ==6.0.1
  • Pygments ==2.15.1
  • Send2Trash ==1.8.2
  • absl-py ==1.4.0
  • accelerate ==0.21.0
  • aiohttp ==3.9.2
  • aiosignal ==1.3.1
  • anyio ==3.7.1
  • argon2-cffi ==21.3.0
  • argon2-cffi-bindings ==21.2.0
  • arrow ==1.2.3
  • asttokens ==2.2.1
  • async-lru ==2.0.4
  • async-timeout ==4.0.2
  • attrs ==23.1.0
  • backcall ==0.2.0
  • beautifulsoup4 ==4.12.2
  • bert-score ==0.3.13
  • bleach ==6.0.0
  • boto3 ==1.28.19
  • botocore ==1.31.19
  • certifi ==2023.7.22
  • cffi ==1.15.1
  • charset-normalizer ==3.2.0
  • click ==8.1.6
  • comm ==0.1.3
  • contourpy ==1.1.0
  • cycler ==0.11.0
  • datasets ==2.14.1
  • debugpy ==1.6.7
  • defusedxml ==0.7.1
  • dill ==0.3.7
  • einops ==0.6.1
  • evaluate ==0.4.0
  • exceptiongroup ==1.1.2
  • executing ==1.2.0
  • fastjsonschema ==2.18.0
  • fonttools ==4.43.0
  • fqdn ==1.5.1
  • frozenlist ==1.4.0
  • fsspec ==2023.6.0
  • gdown ==5.1.0
  • huggingface-hub ==0.16.4
  • idna ==3.4
  • importlib-metadata ==6.8.0
  • importlib-resources ==6.0.0
  • ipykernel ==6.25.0
  • ipython *
  • ipywidgets ==8.1.0
  • isoduration ==20.11.0
  • jedi ==0.19.0
  • jmespath ==1.0.1
  • joblib ==1.3.1
  • json5 ==0.9.14
  • jsonpointer ==2.4
  • jsonschema ==4.18.4
  • jsonschema-specifications ==2023.7.1
  • jupyter-events ==0.7.0
  • jupyter-lsp ==2.2.2
  • jupyter_client ==8.3.0
  • jupyter_core ==5.3.1
  • jupyter_server ==2.7.0
  • jupyter_server_terminals ==0.4.4
  • jupyterlab ==4.0.11
  • jupyterlab-pygments ==0.2.2
  • jupyterlab-widgets ==3.0.8
  • jupyterlab_server ==2.24.0
  • kiwisolver ==1.4.4
  • matplotlib ==3.7.2
  • matplotlib-inline ==0.1.6
  • mistune ==3.0.1
  • multidict ==6.0.4
  • multiprocess ==0.70.15
  • nbclient ==0.8.0
  • nbconvert ==7.7.3
  • nbformat ==5.9.2
  • nest-asyncio ==1.5.7
  • nltk ==3.8.1
  • notebook_shim *
  • numpy *
  • overrides ==7.3.1
  • packaging ==23.1
  • pandas ==2.0.3
  • pandocfilters ==1.5.0
  • parso ==0.8.3
  • pexpect ==4.8.0
  • pickleshare ==0.7.5
  • platformdirs ==3.10.0
  • prometheus-client ==0.17.1
  • prompt-toolkit ==3.0.39
  • psutil ==5.9.5
  • ptyprocess ==0.7.0
  • pure-eval ==0.2.2
  • pyarrow ==14.0.1
  • pycparser ==2.21
  • pyparsing ==3.0.9
  • python-dateutil ==2.8.2
  • python-json-logger ==2.0.7
  • pytz ==2023.3
  • pyzmq ==25.1.0
  • referencing ==0.30.0
  • regex ==2023.6.3
  • requests ==2.31.0
  • responses ==0.18.0
  • rfc3339-validator ==0.1.4
  • rfc3986-validator ==0.1.1
  • rouge-score ==0.1.2
  • rpds-py ==0.9.2
  • s3transfer ==0.6.1
  • safetensors ==0.3.1
  • sentencepiece ==0.1.99
  • six ==1.16.0
  • sniffio ==1.3.0
  • soupsieve ==2.4.1
  • stack-data ==0.6.2
  • terminado ==0.17.1
  • tinycss2 ==1.2.1
  • tokenizers ==0.13.3
  • tomli ==2.0.1
  • torch ==2.0.1
  • tornado ==6.3.3
  • tqdm ==4.65.0
  • traitlets ==5.9.0
  • transformers ==4.36.0
  • triton ==2.0.0
  • tzdata ==2023.3
  • uri-template ==1.3.0
  • urllib3 ==1.26.18
  • wcwidth ==0.2.6
  • webcolors ==1.13
  • webencodings ==0.5.1
  • websocket-client ==1.6.1
  • widgetsnbextension ==4.0.8
  • xxhash ==3.2.0
  • yarl ==1.9.2
  • zipp ==3.16.2
environment.yml conda
  • _libgcc_mutex 0.1.*
  • _openmp_mutex 4.5.*
  • blas 1.0.*
  • ca-certificates 2023.7.22.*
  • cuda-cudart 11.8.89.*
  • cuda-cupti 11.8.87.*
  • cuda-libraries 11.8.0.*
  • cuda-nvrtc 11.8.89.*
  • cuda-nvtx 11.8.86.*
  • cuda-runtime 11.8.0.*
  • decorator 5.1.1.*
  • filelock 3.12.2.*
  • intel-openmp 2022.1.0.*
  • jinja2 3.1.2.*
  • ld_impl_linux-64 2.40.*
  • libcublas 11.11.3.6.*
  • libcufft 10.9.0.58.*
  • libcufile 1.7.0.149.*
  • libcurand 10.3.3.53.*
  • libcusolver 11.4.1.48.*
  • libcusparse 11.7.5.86.*
  • libffi 3.3.*
  • libgcc-ng 13.1.0.*
  • libgomp 13.1.0.*
  • libnpp 11.8.0.86.*
  • libnvjpeg 11.9.0.86.*
  • libsqlite 3.42.0.*
  • libstdcxx-ng 13.1.0.*
  • libzlib 1.2.13.*
  • markupsafe 2.1.3.*
  • mkl 2022.1.0.*
  • mpmath 1.3.0.*
  • ncurses 6.4.*
  • networkx 3.1.*
  • openssl 1.1.1u.*
  • pip 23.2.1.*
  • python 3.9.7.*
  • python_abi 3.9.*
  • pytorch 2.0.1.*
  • pytorch-cuda 11.8.*
  • pytorch-mutex 1.0.*
  • readline 8.2.*
  • setuptools 68.0.0.*
  • sqlite 3.42.0.*
  • sympy 1.12.*
  • tk 8.6.12.*
  • torchtriton 2.0.0.*
  • typing_extensions 4.7.1.*
  • wheel 0.41.0.*
  • xz 5.4.2.*
  • zlib 1.2.13.*