HowTo -- CHAT-GPT and DBpedia SPARQL Query Generation from Natural Language Prompts

I recently stumbled upon an interesting Github Repo named “Neural SPARQL Machines” that comprises CSV-based training data that can be used by an LLM to generate SPARQL from natural language-based questions.

Here are some template examples:

Naturally, I decided to test a sampling of these templates directly via ChatGPT (version 3.5), as presented in the transcript that follows.

Transcript

Q: Given the following template.

What is the country of <A>;select ?a where { <A> <http://dbpedia.org/ontology/country> ?a };select ?a where { ?a <http://dbpedia.org/ontology/country> [] . ?a a <http://dbpedia.org/ontology/Place> }

Find the country of London using SPARQL.

A:

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
SELECT ?country WHERE {
  dbr:London dbo:country ?country .
}

Q: Repeat, but for Berlin.

A:

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
SELECT ?country WHERE {
  dbr:Berlin dbo:country ?country .
}

Q: Generate a clickable link for the generated query

A:

Clickable Link for SPARQL Query Solution

Q: Change the &query parameter to &qtxt

A:
Clickable Link for SPARQL Query Editor

Q: Given the template.
<A> is the team of which basketball players?;SELECT DISTINCT ?uri where { ?uri dbp_team <A> . ?uri a dbo_BasketballPlayer };select distinct ?a where { ?uri dbp_team ?a . ?uri a dbo_BasketballPlayer }; Generate a clickable SPARQL Query link that answers the question: What team did Michale Jordan play for.

SPARQL Query Solution: Teams Michael Jordan played for

Q: Repeat, but generating a clickable SPARQL query editor link by changing the &query parameter to &qtxt.

A:
Here’s the clickable SPARQL query editor link for the Michael Jordan teams query using the standard DBpedia SPARQL results link with the &qtxt parameter:

SPARQL Query Editor: Teams Michael Jordan played for

When you click the link, it will open the DBpedia SPARQL query editor with the query pre-filled in the &qtxt parameter. You can then run the query, modify it, or experiment with other queries as needed.

Related

Generating clickable DBpedia SPARQL Query Solution Page Link using Bing+ChatGPT Service

Here’s a transcript for a variant of this exercise using the Bing + GPT service.

Q: Given the following template.

What is the county of <A>;select ?a where { <A> <http://dbpedia.org/ontology/county> ?a };select ?a where { ?a <http://dbpedia.org/ontology/county> [] . ?a a <http://dbpedia.org/ontology/Place> } Find the country of London using SPARQL.

A:

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>

SELECT ?country WHERE {
dbr:London dbo:country ?country.
}

Q: Can you generate a clickable sparql url for that query? Just say yes or no.

A:

Here is the clickable link for the SPARQL query:

https://dbpedia.org/sparql?default-graph-uri=http%3A%2F%2Fdbpedia.org&query=PREFIX+dbo%3A+%3Chttp%3A%2F%2Fdbpedia.org%2Fontology%2F%3E%0D%0APREFIX+dbr%3A+%3Chttp%3A%2F%2Fdbpedia.org%2Fresource%2F%3E%0D%0A%0D%0ASELECT+%3Fcountry+WHERE+%7B%0D%0A++dbr%3ALondon+dbo%3Acountry+%3Fcountry.%0D%0A%7D&format=text/html&CXML_redir_for_subjs=121&CXML_redir_for_hrefs=&timeout=30000&debug=on

Bing Answer Link .

Q: That isn’t a clickable link since I need to copy and paste it to a browser for resolution.

A:

I apologize for the confusion. Here is the clickable link for the SPARQL query:

https://dbpedia.org/sparql?default-graph-uri=http%3A%2F%2Fdbpedia.org&query=PREFIX+dbo%3A+%3Chttp%3A%2F%2Fdbpedia.org%2Fontology%2F%3E%0D%0APREFIX+dbr%3A+%3Chttp%3A%2F%2Fdbpedia.org%2Fresource%2F%3E%0D%0A%0D%0ASELECT+%3Fcountry+WHERE+%7B%0D%0A++dbr%3ALondon+dbo%3Acountry+%3Fcountry.%0D%0A%7D&format=text/html&CXML_redir_for_subjs=121&CXML_redir_for_hrefs=&timeout=30000&debug=on

Bing Anwser Link

Q: Yes, that’s much better. Use “SPARQL Query Response” as the label applied to the clikcable link.

A:

Here is the clickable link for the SPARQL query:

SPARQL Query Response

Bing Answer Link

Richer Template and SPARQL-FED via URIBurner

Template:

dbo:TelevisionShow;dbo:TelevisionShow;;does <A> have more episodes than <B>?;ask where { <A> dbo:numberOfEpisodes ?a . <B> dbo:numberOfEpisodes ?b . filter (?a > ?b) };select distinct ?a, ?b where {  ?a dbo:numberOfEpisodes ?x . ?b dbo:numberOfEpisodes ?y };
dbo:TelevisionShow;;;does the new <A> series have more episodes than the old one?;ask where { <A> rdfs:label ?l . ?a rdf:type dbo:TelevisionShow . ?a foaf:name ?l . ?a dbo:completionDate ?ad . ?a dbo:numberOfEpisodes ?an . ?b rdf:type dbo:TelevisionShow . ?b foaf:name ?l . ?b dbo:completionDate ?bd . ?b dbo:numberOfEpisodes ?bn . filter(?ad > ?bd && ?an > ?bn) };select distinct ?a where {  ?a rdfs:label ?l . ?x rdf:type dbo:TelevisionShow . ?x foaf:name ?l . ?x dbo:completionDate ?ad . ?x dbo:numberOfEpisodes ?an . ?y rdf:type dbo:TelevisionShow . ?y foaf:name ?l . ?y dbo:completionDate ?bd . ?y dbo:numberOfEpisodes ?bn . };
dbo:MusicalArtist;dbo:MusicalWork;;for which label did <A> record his first <B>?;select ?a where { ?b rdf:type <B> . ?b dbo:artist <A> . ?b dbo:releaseDate ?c . ?b dbo:recordLabel ?a } order by ?c offset 0 limit 1;select distinct ?a, ?b where {  ?y rdf:type ?b . ?y dbo:artist ?a . ?y dbo:releaseDate ?c . ?y dbo:recordLabel ?x  };
dbo:MusicalArtist;dbo:MusicalWork;;for which label did <A> record his second <B>?;select ?a where { ?b rdf:type <B> . ?b dbo:artist <A> . ?b dbo:releaseDate ?c . ?b dbo:recordLabel ?a } order by ?c offset 1 limit 1;select distinct ?a, ?b where {  ?y rdf:type ?b . ?y dbo:artist ?a . ?y dbo:releaseDate ?c . ?y dbo:recordLabel ?x  };
dbo:Country;dbo:MusicGenre;;give me all <A> <B> record labels.;select distinct ?uri where { ?uri rdf:type dbo:RecordLabel . ?uri dbo:genre <B>. ?uri dbo:country <A>. };select distinct ?a, ?b where {  ?uri rdf:type dbo:RecordLabel . ?uri dbo:genre ?b. ?uri dbo:country ?a.  };
;;;give me all actors;select ?a where { [] dbo:starring ?a };select distinct  where {  [] dbo:starring ?x  };
dbo:Film;;;give me all actors starring in <A>;select ?a where { <A> dbo:starring ?a };select distinct ?a where {  ?a dbo:starring ?x  };
<http://dbpedia.org/class/yago/FilmDirector110088200>;;;give me all actors starring in movies directed by <A>;select distinct ?uri where { ?uri dbo:starring ?x . ?uri dbo:director <A> };select distinct ?a where { ?uri dbo:starring ?x . ?uri dbo:director ?a };
dbo:Writer;;;give me all books by <A> with more than 300 pages.;select distinct ?uri where { ?uri rdf:type dbo:Book . ?uri dbo:author <A> . ?uri dbo:numberOfPages ?x . filter (?x > 300) };select distinct ?a where {  ?uri rdf:type dbo:Book . ?uri dbo:author ?a . };
dbo:Band;;;give me all members of <A>;select distinct ?uri where { <A> dbo:bandMember ?uri . };select distinct ?a where {  ?a dbo:bandMember ?uri .  };
<http://dbpedia.org/class/yago/FilmDirector110088200>;;;give me all movies directed by <A>;select distinct ?uri where { ?uri rdf:type dbo:Film . ?uri dbo:director <A> };select distinct ?a where {  ?uri rdf:type dbo:Film . ?uri dbo:director ?a  };
dbo:TelevisionShow;;;give me the new <A> series;describe ?a where { <A> rdfs:label ?l . ?a a dbo:TelevisionShow . ?a foaf:name ?l . ?a dbo:completionDate ?ad } order by desc(?ad) limit 1;select distinct ?a where {  ?a rdfs:label ?l . ?x a dbo:TelevisionShow . ?x foaf:name ?l . ?x dbo:completionDate ?ad  };
dbo:Book;;;how many pages does <A> have?;select distinct ?n where { <A> dbo:numberOfPages ?n . };select distinct ?a where {  ?a dbo:numberOfPages ?n .  };
dbo:Film;;;how much did <A> cost?;select distinct ?n where { <A> dbo:budget ?n . };select distinct ?a where {  ?a dbo:budget ?n .  };
;;;in which films did <A> as well as <B> play?;select distinct ?uri where { ?uri rdf:type dbo:Film . ?uri dbo:starring <A> . ?uri dbo:starring <B> };select distinct ?a, ?b where {  ?uri rdf:type dbo:Film . ?uri dbo:starring ?a . ?uri dbo:starring ?b  };
<http://dbpedia.org/class/yago/Actor109765278>;dbo:Film;;is <A> starring in <B>?;ask where { <A> dbo:starring <B> . };select distinct ?a, ?b where {  ?a dbo:starring ?b .  };
dbo:Broadcaster;dbo:TelevisionShow;;list all episodes of the first season of the <A> television series <B>!;select distinct ?uri where { ?uri dbo:series <B> . ?uri dbo:seasonNumber 1 . <B> dbo:network <A> };select distinct ?a, ?b where {  ?uri dbo:series ?b . ?uri dbo:seasonNumber 1 . ?b dbo:network ?a };
;;;list all the musicals with music by <A>.;select distinct ?uri where { ?uri rdf:type dbo:Musical . ?uri dbo:musicBy <A> . };select distinct ?a where {  ?uri rdf:type dbo:Musical . ?uri dbo:musicBy ?a.  };
;;;show me <A>’s autobiography ;select distinct ?uri where { ?uri dbo:author <A> . ?uri dbo:literaryGenre dbr:Autobiography. };select distinct ?a where {  ?uri dbo:author ?a . ?uri dbo:literaryGenre dbr:Autobiography  };
;;;show me all books in <A> series.;select distinct ?uri where { ?uri dbo:series <A>.  ?uri rdf:type dbo:Book};select distinct ?a where {  ?uri dbo:series ?a . ?uri rdf:type dbo:Book.  };
;;;show me the book that <A> wrote.;select distinct ?uri where { ?uri rdf:type dbo:Book . ?uri dbo:author <A>. };select distinct ?a where {  ?uri rdf:type dbo:Book . ?uri dbo:author ?a.  };
;dbo:Artwork;;to which artistic movement did the artist of the <A> belong?;select distinct ?uri where { <A> dbo:author ?person . ?person dbo:movement ?uri . ?person rdf:type dbo:Artist };select distinct ?a where {  ?a dbo:author ?person . ?person dbo:movement ?uri . ?person rdf:type dbo:Artist  };
;;;what is <A>’s real name?;select distinct ?s where { <A> dbp:alterEgo ?s . };select distinct ?a where {  ?a dbp:alterEgo ?s .  };
  1. “clickable URIBurner SPARQL link” replace dbpedia.org with linkeddata.uriburner.com .
  2. “clickable URIBurner SPARQL + Sponger link” add the parameter &sponger:get=add .
  3. “clickable URIBurner SPARQL-FED link” changes SPARQL query to a SPARQL-FED query using designated SPARQL-FED Service endpoint; with namespace declarations applied to query.
  4. Use “SPARQL Query Response” as the the label applied to the click types above
  5. “clickable URIBurner SPARQL Query Editor link” replace dbpedia.org with linkeddata.uriburner.com and replace &query parameter with &qtx.
  6. Use “URIBurner SPARQL Query Editor” as the label applied to clickable links to a “clickable URIBurner SPARQL Query Editor link”

Given the guidelines stated above, return a clickable URIBurner SPARQL-FED link (using DBpedia is the designated SPARQL-FED endpoint) for the following:
Give me all books by Charles Dickens with more than 300 pages

Q: Sure that’s a SPARQL-FED query?

A:

SPARQL Query Response

This link will return all books by Charles Dickens with more than 300 pages using DBpedia as the designated SPARQL-FED endpoint.

Screenshot 2023-05-09 at 1.28.33 PM

Q:
Redo, but returning a clickable URIBurner SPARQL-FED Query Editor link where &qtx replaces &query and DBpedia is the SPARQL-FED Query Service.

A:

URIBurner SPARQL Query Editor

This link will take you to the URIBurner SPARQL Query Editor, where you can edit and execute the query to find all books by Charles Dickens with more than 300 pages using DBpedia as the designated SPARQL-FED endpoint.