The Knowledge Graph Conference Icon
The Knowledge Graph Conference
  • 🏠Home
  • 📅Events
  • 👤Members
  • 🔵Announcements
  • 🔵Ask
  • 🔵Ask The Ontologists
  • 🔵Events
  • 🔵Jobs
  • 🔵Promotions
  • 🔵Share
Powered by Tightknit
Ask
Ask

Generating SPARQL Queries from Ontologies Using LLMs

Avatar of AmrAmr
·Jan 16, 2024 01:46 PM

Hi everyone, Do you know any way that I can generate SPARQL query from an Ontology or Ontology-subset using LLM ?

20 comments

· Sorted by Oldest
    • Avatar of Ricardo U.
      Ricardo U.
      ·

      Hi, sorry for the self-plugging: https://arxiv.org/abs/2204.12793 and https://ceur-ws.org/Vol-3592/ should give you a zoo of possible models

      👍2
    • Avatar of Nikos T.
      Nikos T.
      ·

      The easiest way is probably to try langchain - https://python.langchain.com/docs/use_cases/graph/graph_sparql_qa

      👍1
    • Avatar of Amr
      Amr
      ·

      thanx for these resources. In fact what exactly looking for is to make the LLM help me generating the SPARQL Query for the question answer. So I found this resource but it doesn't generate an accurate result. if any change on the question then the generated sparql query will be not correct. https://apex974.com/articles/chatgpt-for-info-retrieval-from-knowledge-graph

    • Avatar of Amr
      Amr
      ·

      another weak point in Apex example is extracting the keywords from the question to generate a small ontology-subset. the chatGPT couldn't always guess the correct keywords and split them into entities and properties. so I looked for alternative way to let LLM understand my ontology and provide me Sparql query for my questions related to the ontology. I hope you didn't get lost ^_^

    • Avatar of Ricardo U.
      Ricardo U.
      ·

      Yes, in general, the LLM quality of SPARQL generation is really bad, no matter what you do or which LLM you use.

    • Avatar of Ricardo U.
      Ricardo U.
      ·

      In general, it helps to give 30 or more examples as a prefix

      👍1
    • Avatar of Amr
      Amr
      ·

      from your point of view which one is more accurate and helpful for me. chatGPT or Gemini or maybe any other engine

    • Avatar of Ricardo U.
      Ricardo U.
      ·

      well, I think both perform the same but chatGPT might be a little bit better

    • Avatar of Michael G.
      Michael G.
      ·

      disclaimer: I work for Stardog. kind of old blog post but if you sign up for a Stardog Account you can use Voicebox within Studio to generate SPARQL queries; as a part of the EA period this capability is limited to our demos, but will be enabled for any KG when we go GA later this year.

    • Avatar of Amr
      Amr
      ·

      Michael G. I have tried it but it has some problems. For example this is the question: # can you generate a SPARQL query from this question "Can you retrieve the title, the journal, and the publication date for all journal articles that mention 'running' in their title and were published in the 2000s." expected result:

      PREFIX fabio: <http://purl.org/spar/fabio#>
      PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
      PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
      
      SELECT ?title ?journal ?publicationDate
      WHERE {
        ?article rdf:type fabio:JournalArticle .
        ?article fabio:hasTitle ?title .
        ?article fabio:isPartOf ?journal .
        ?article fabio:hasPublicationDate ?publicationDate .
        
        FILTER (regex(?title, "running", "i") && year(?publicationDate) >= 2000 && year(?publicationDate) < 2030)
      }

      voicebox result: (has errors in prefix)

      prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
      prefix owl: <http://www.w3.org/2002/07/owl#>
      prefix so: <https://schema.org/>
      prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
      prefix xsd: <http://www.w3.org/2001/XMLSchema#>
      SELECT DISTINCT ?title ?journal ?date 
      WHERE {
        ?article a stardog:ecomm:JournalArticle . 
        ?article stardog:ecomm:title ?title . 
        ?article stardog:ecomm:journal ?journal . 
        ?article stardog:ecomm:publicationDate ?date . 
        FILTER (("2000-01-01"^^xsd:date <= ?date && "2029-12-31"^^xsd:date >= ?date))
      }
    • Avatar of Amr
      Amr
      ·

      Michael G. is there a way to upload an ontology (.ttl) to the Voicebox and make some questions/sparql against the uploaded ontology?

    • Avatar of Michael G.
      Michael G.
      ·

      No, not currently. We are rolling out that feature for all users later this year, it's only in private preview right now.

    • Avatar of Donny W.
      Donny W.
      ·
      Given the OWL model described in the following TTL file:
      INSERT OWL ontology
      Write a SPARQL query that answers the question. The data for your query is available in a SERVICE identified by
      <mapped>. Do not explain the query. return just the query, so it can be run verbatim from your response.
      Here's the question:
      "INSERT QUESTION"

      from https://arxiv.org/abs/2311.07509 (by Juan S. Dean A. Bryon J.)

      🙌1
    • Avatar of Juan S.
      Juan S.
      ·

      This works INCREDIBLY well on GPT4!. Yes, with that very simple prompt. So tons of room for improvement. And yes… it all depends on having the ontology fit into the context window. So there will need to be some tricks to get the right part of the ontology in case it is too big.

    • Avatar of Ricardo U.
      Ricardo U.
      ·

      Indeed, we are also looking into automatic prompt tuning to make it fit - there are some tricks such as vocabulary reducing etc but we noticed that longer, complex queries are kind of hard