Skip to main content

Subgraph Retrieval Toolkit

PyPi Documentation Status PytestStatus License: MIT

Retrieve subgraphs on Wikidata. The method is based on this retrieval work for Freebase.

Prerequisite

Install SRTK

pip install srtk

Wikidata

Deploy a Wikidata endpoint locally

We use qEndpoint to spin up a Wikidata endpoint that contains a Wikidata Truthy dump.

  • Download

    sudo docker run -p 1234:1234 --name qendpoint-wikidata qacompany/qendpoint-wikidata
    
  • Run

    sudo docker start  qendpoint-wikidata
    
  • Add Wikidata prefixes support

    wget https://raw.githubusercontent.com/the-qa-company/qEndpoint/master/wikibase/prefixes.sparql
    sudo docker cp prefixes.sparql qendpoint-wikidata:/app/qendpoint && rm prefixes.sparql
    

Alternatively, you can also use an online Wikidata endpoint, e.g. https://query.wikidata.org/sparql

Deploy a REL endpoint for entity linking (only necessary for end-to-end inference)

Please refer to this tutorial for REL endpoint deployment: End-to-End Entity Linking

Freebase

Deploy a Freebase endpoint locally

Please refer to dki-lab/Freebase-Setup for the setup.

# Download setup script
git clone https://github.com/dki-lab/Freebase-Setup.git && cd Freebase-Setup
# Download virtuoso binary
wget https://kumisystems.dl.sourceforge.net/project/virtuoso/virtuoso/7.2.5/virtuoso-opensource.x86_64-generic_glibc25-linux-gnu.tar.gz
tar -zxvf virtuoso-opensource.x86_64-generic_glibc25-linux-gnu.tar.gz && rm virtuoso-opensource.x86_64-generic_glibc25-linux-gnu.tar.gz
# Replace the virtuoso path in virtuoso.py
sed -i 's/\/home\/dki_lab\/tools\/virtuoso\/virtuoso-opensource/\.\/virtuoso-opensource/g' virtuoso.py
# Download Freebase dump
wget https://www.dropbox.com/s/q38g0fwx1a3lz8q/virtuoso_db.zip
unzip virtuoso_db.zip && rm virtuoso_db.zip
# Start virtuoso
python3 virtuoso.py start 3001 -d virtuoso_db

Retrieve subgraphs with a trained scorer

srtk retrieve --sparql-endpoint WIKIDATA_ENDPOINT \
    -kg wikidata
    --scorer-model-path path/to/scorer \
    --input data/ground.jsonl \
    --output-path data/subgraph.jsonl \
    --beam-width 10

The scorer-model-path argument can be any huggingface pretrained encoder model. If it is a local path, please ensure the tokenizer is also saved along with the model.

Visualize retrieved subgraph

srtk visualize --sparql-endpoint WIKIDATA_ENDPOINT \
    --knowledge-graph wikidata \
    --input data/subgraph.jsonl \
    --output-dir ./htmls/

Train a scorer

A scorer is the model used to navigate the expanding path. At each expanding step, relations scored higher with scorer are picked as relations for the next hop.

The score is based on the embedding similarity of the to-be-expanded relation with the query (question + previous expanding path).

The model is trained in a distant supervised learning fashion. Given the question entities and the answer entities, the model uses the shortest paths along them as the supervision signal.

Preprocess a dataset

  1. prepare training samples where question entities and answer entities are know.

    The training data should be saved in a jsonl file (e.g. data/grounded.jsonl). Each training sample should come with the following format:

    {
      "id": "sample-id",
      "question": "Which universities did Barack Obama graduate from?",
      "question_entities": [
        "Q76"
      ],
      "answer_entities": [
        "Q49122",
        "Q1346110",
        "Q4569677"
      ]
    }
    
  2. Preprocess the samples with srtk preprocess command.

    srtk preprocess --sparql-endpoint WIKIDATA_ENDPOINT \
        -kg wikidata \
        --input-file data/grounded.jsonl \
        --output-dir data/retrieved --metric jaccard
    

    Under the hood, it does four things:

    1. Find the shortest paths between the question entities and the answer entities.
    2. Score the searched paths with Jaccard scores with the answers.
    3. Negative sampling. At each expanding step, the negative samples are those false relations connected to the tracked entities.
    4. Generate training dataset as a jsonl file.

Train a sentence encoder

The scorer should be initialized from a pretrained encoder model from huggingface hub. Here I used intfloat/e5-small, which is a checkpoint of the BERT model.

srtk train --data-file data/train.jsonl \
    --model-name-or-path intfloat/e5-small \
    --save-model-path artifacts/scorer

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

srtk-0.0.2.tar.gz (57.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

srtk-0.0.2-py3-none-any.whl (33.8 kB view details)

Uploaded Python 3

File details

Details for the file srtk-0.0.2.tar.gz.

File metadata

  • Download URL: srtk-0.0.2.tar.gz
  • Upload date:
  • Size: 57.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.15

File hashes

Hashes for srtk-0.0.2.tar.gz
Algorithm Hash digest
SHA256 5b52b002c06488bb85f8b2effe9c3d41913001b4bad7fc7745421837c4cf4636
MD5 04b59ebae65f9f4f41a1070d86357318
BLAKE2b-256 7ffc438358337779b159942e8448070b813cc579d0a1d5e074227059359a34a1

See more details on using hashes here.

File details

Details for the file srtk-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: srtk-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 33.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.15

File hashes

Hashes for srtk-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9e1855bba6b9af39f8aa00aa4d0f2c28b870f70b0771cdea505404c8a89c6c10
MD5 6bbc322be3fa2e4177f0fb7ddc8c3541
BLAKE2b-256 c6d772d67f3f134ff2f36b70aecf79fd19b59432b044b917a72748c56b60a437

See more details on using hashes here.

Release history Release notifications | RSS feed

0.0.7

2 files

0.0.6

2 files

0.0.5

2 files

0.0.4

2 files

0.0.3

2 files

This release

0.0.2 This release

2 files

0.0.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page