Ontotext GraphDB is a graph database and knowledge discovery tool compliant with RDF and SPARQL.
This notebook shows how to use LLMs to provide natural language querying (NLQ to SPARQL, also calledtext2sparql
) forOntotext GraphDB
.
GraphDB
supports some LLM integration functionalities as described here:
gpt-queries
SPARQL
generation from NLQ. We’ll use the Star Wars API
(SWAPI
) ontology and dataset that you can examine here.
24.0.7
which bundles Docker Compose. For earlier Docker versions you may need to install Docker Compose separately.http://localhost:7200/
. The Star Wars dataset starwars-data.trig
is automatically loaded into the langchain
repository. The local SPARQL endpoint http://localhost:7200/repositories/langchain
can be used to run queries against. You can also open the GraphDB Workbench from your favourite web browser http://localhost:7200/sparql
where you can make queries interactively.
conda
, create and activate a new conda environment, e.g.:
OntotextGraphDBGraph
class:
query_ontology
: a CONSTRUCT
query that is executed on the SPARQL endpoint and returns the KG schema statements. We recommend that you store the ontology in its own named graph, which will make it easier to get only the relevant statements (as the example below). DESCRIBE
queries are not supported, because DESCRIBE
returns the Symmetric Concise Bounded Description (SCBD), i.e. also the incoming class links. In case of large graphs with a million of instances, this is not efficient. Check github.com/eclipse-rdf4j/rdf4j/issues/4857local_file
: a local RDF ontology file. Supported RDF formats are Turtle
, RDF/XML
, JSON-LD
, N-Triples
, Notation-3
, Trig
, Trix
, N-Quads
.Turtle
since Turtle
with appropriate prefixes is most compact and easiest for the LLM to remember.
The Star Wars ontology is a bit unusual in that it includes a lot of specific triples about classes, e.g. that the species :Aleena
live on <planet/38>
, they are a subclass of :Reptile
, have certain typical characteristics (average height, average lifespan, skinColor), and specific individuals (characters) are representatives of that class:
OntotextGraphDBGraph
.
OntotextGraphDBQAChain
to ask some questions.
sparql_generation_prompt
Default value:
sparql_fix_prompt
Default value:
max_fix_retries
Default value: 5
qa_prompt
Default value:
docker compose down -v --remove-orphans
from the directory with the Docker compose file.