Using SPARQL Query Templates to Fine-Tune ChatGPT's Large Language Model (LLM)

Additional SPARQL-Template Collection – Aggregates

The ChatGPT transcript that follows includes the addition of aggregate queries to the template collection for fine-tuning the GPT 4.0 and 3.5 LLMs.

Transcript

Q: Looking at the current template for FAQs, can you generate a template for answering requests along the following lines:
Count the number of questions associate with an FAQ?

A:
Certainly, here are the templates you requested:

Count Questions in FAQ

Count the number of questions associated with FAQs in knowledge graph , using terms from schema.org:

SELECT ?page (COUNT(?question) as ?questionCount) 
FROM <G> WHERE { 
  ?page a schema:FAQPage; 
        schema:mainEntity ?question. 
} GROUP BY ?page 
ORDER BY DESC(?questionCount);

Count the number of questions associated with FAQs from in knowledge graph , using terms from schema.org:

SELECT ?page (COUNT(?question) as ?questionCount) 
FROM <G> WHERE { 
  ?page a schema:FAQPage; 
        schema:mainEntity ?question. 
  FILTER (CONTAINS(STR(?page),'<Org>'))
} GROUP BY ?page 
ORDER BY DESC(?questionCount);

These templates allow you to count the number of questions associated with each FAQ page within a given knowledge graph or associated with a specific organization.

Q: Okay, generate this template and add to the collection.

A:
Adding the templates to the collection would look like this in the ontology:

## Turtle Start ##
ont:PromptCountFAQQuestions rdf:type ont:Prompt ;
  ont:hasUser ont:UserA ;
  ont:hasInput "Count the number of questions associated with FAQs in knowledge graph <G>, using terms from schema.org;SELECT ?page (COUNT(?question) as ?questionCount) FROM <G> WHERE { ?page a schema:FAQPage; schema:mainEntity ?question. } GROUP BY ?page ORDER BY DESC(?questionCount);" ;
  ont:hasResponse ont:ResponseCountFAQQuestions .
ont:ResponseCountFAQQuestions rdf:type ont:Response .

ont:PromptCountFAQQuestionsByOrg rdf:type ont:Prompt ;
  ont:hasUser ont:UserA ;
  ont:hasInput "Count the number of questions associated with FAQs from <Org> in knowledge graph <G>, using terms from schema.org;SELECT ?page (COUNT(?question) as ?questionCount) FROM <G> WHERE { ?page a schema:FAQPage; schema:mainEntity ?question. FILTER (CONTAINS(STR(?page),'<Org>')) } GROUP BY ?page ORDER BY DESC(?questionCount);" ;
  ont:hasResponse ont:ResponseCountFAQQuestionsByOrg .
ont:ResponseCountFAQQuestionsByOrg rdf:type ont:Response .
## Turtle End ##

These templates can be used to generate queries for counting the number of questions associated with FAQ pages in a knowledge graph.

Q: Redo, but for steps in a HowTo.

A:
Here are the templates for counting the number of steps in a HowTo:

## Turtle Start ##
ont:PromptCountHowToSteps rdf:type ont:Prompt ;
  ont:hasUser ont:UserA ;
  ont:hasInput "Count the number of steps associated with HowTos in knowledge graph <G>, using terms from schema.org;SELECT ?howto (COUNT(?step) as ?stepCount) FROM <G> WHERE { ?howto a schema:HowTo; schema:step ?step. } GROUP BY ?howto ORDER BY DESC(?stepCount);" ;
  ont:hasResponse ont:ResponseCountHowToSteps .
ont:ResponseCountHowToSteps rdf:type ont:Response .

ont:PromptCountHowToStepsByOrg rdf:type ont:Prompt ;
  ont:hasUser ont:UserA ;
  ont:hasInput "Count the number of steps associated with HowTos from <Org> in knowledge graph <G>, using terms from schema.org;SELECT ?howto (COUNT(?step) as ?stepCount) FROM <G> WHERE { ?howto a schema:HowTo; schema:step ?step. FILTER (CONTAINS(STR(?howto),'<Org>')) } GROUP BY ?howto ORDER BY DESC(?stepCount);" ;
  ont:hasResponse ont:ResponseCountHowToStepsByOrg .
ont:ResponseCountHowToStepsByOrg rdf:type ont:Response .
## Turtle End ##

These templates can be used to generate queries for counting the number of steps associated with HowTo instances in a knowledge graph.

Q: Add that to the current template collection.

A:

I’ve added the “Count the number of steps in HowTos” templates to your collection:

  1. Count the number of steps associated with HowTos in knowledge graph
SELECT ?howto (COUNT(?step) as ?stepCount) 
FROM <G> 
WHERE { 
  ?howto a schema:HowTo; 
  schema:step ?step. 
} 
GROUP BY ?howto 
ORDER BY DESC(?stepCount)
  1. Count the number of steps associated with HowTos from in knowledge graph
SELECT ?howto (COUNT(?step) as ?stepCount) 
FROM <G> 
WHERE { 
  ?howto a schema:HowTo; 
  schema:step ?step. 
  FILTER (CONTAINS(STR(?howto),'<Org>')) 
} 
GROUP BY ?howto 
ORDER BY DESC(?stepCount)

You can use these templates to generate SPARQL queries for counting the number of steps associated with HowTo instances in a given knowledge graph or from a specific organization.

Q: Count the number of questions associated with the FAQ denoted by https://www.net-usb.com/share-usb/share-yubikey-over-network/#FAQPage

A:
To count the number of questions associated with a specific FAQ page, we can modify the template you’ve provided earlier for counting questions associated with an FAQ page.

Here is the SPARQL query template:

SELECT (COUNT(?question) AS ?questionCount) 
FROM <G> 
WHERE { 
    <FAQPageURI> a schema:FAQPage; 
    schema:mainEntity ?question. 
}

In this case, replace <G> with your graph URI (e.g., urn:schemaorg:data:abox), and replace <FAQPageURI> with the FAQ page URI you’re interested in, which is https://www.net-usb.com/share-usb/share-yubikey-over-network/#FAQPage in your case.

Here’s the generated clickable SPARQL query link:

Run the query

Please note that the results of this query will depend on the data available in the specific knowledge graph. If the FAQ page URI doesn’t exist in the graph or if it doesn’t have any associated questions, the count will be zero.

Q: Generate the new template you suggested.

A:
Sure, here are the new templates for counting the number of questions in an FAQ page and the number of steps in a HowTo:

# Count questions in FAQ <FAQPageURI> associated with knowledge graph <G>, using terms from schema.org;

SELECT (COUNT(?question) AS ?questionCount) 
FROM <G> 
WHERE { 
    <FAQPageURI> a schema:FAQPage; 
    schema:mainEntity ?question. 
}
# Count steps in HowTo <HowToURI> associated with knowledge graph <G>, using terms from schema.org;

SELECT (COUNT(?step) AS ?stepCount) 
FROM <G> 
WHERE { 
    <HowToURI> a schema:HowTo; 
    schema:step ?step. 
}

These templates allow you to count the number of questions in a specific FAQ page and the number of steps in a specific HowTo guide, respectively. Replace <G>, <FAQPageURI>, and <HowToURI> with your graph URI and the URI of the FAQ page or HowTo guide you’re interested in.

Remember, to generate clickable SPARQL query links from these templates, the URIs should be properly encoded to be used in a URL.

Q: Generate a description of the new temples in RDF-Turtle using terms from the Template Ontology, derived from the following example:

## Turtle Start ##
ont:PromptCountHowToSteps rdf:type ont:Prompt ;
  ont:hasUser ont:UserA ;
  ont:hasInput "Count the number of steps associated with HowTos in knowledge graph <G>, using terms from schema.org;SELECT ?howto (COUNT(?step) as ?stepCount) FROM <G> WHERE { ?howto a schema:HowTo; schema:step ?step. } GROUP BY ?howto ORDER BY DESC(?stepCount);" ;
  ont:hasResponse ont:ResponseCountHowToSteps .
ont:ResponseCountHowToSteps rdf:type ont:Response .

ont:PromptCountHowToStepsByOrg rdf:type ont:Prompt ;
  ont:hasUser ont:UserA ;
  ont:hasInput "Count the number of steps associated with HowTos from <Org> in knowledge graph <G>, using terms from schema.org;SELECT ?howto (COUNT(?step) as ?stepCount) FROM <G> WHERE { ?howto a schema:HowTo; schema:step ?step. FILTER (CONTAINS(STR(?howto),'<Org>')) } GROUP BY ?howto ORDER BY DESC(?stepCount);" ;
  ont:hasResponse ont:ResponseCountHowToStepsByOrg .
ont:ResponseCountHowToStepsByOrg rdf:type ont:Response .
## Turtle End ##

Q: example.com should always be replaced by the following namespace prefix declaration:
@prefix ont: <#> .

A:
Got it, here’s the RDF-Turtle representation of the newly created template for counting questions associated with a FAQ using the relative namespace:

## Turtle Start ##
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix ont: <#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

ont:PromptCountFAQQuestions rdf:type ont:Prompt ;
  ont:hasUser ont:UserA ;
  ont:hasInput "Count the number of questions associated with FAQ in knowledge graph <G>, using terms from schema.org;SELECT ?faq (COUNT(?question) as ?questionCount) FROM <G> WHERE { ?faq a schema:FAQPage; schema:mainEntity ?question. } GROUP BY ?faq ORDER BY DESC(?questionCount);" ;
  ont:hasResponse ont:ResponseCountFAQQuestions .
ont:ResponseCountFAQQuestions rdf:type ont:Response .
## Turtle End ##

Q: Repeat, but for HowTo step count

A:
Here’s the RDF-Turtle representation of the newly created template for counting steps associated with a HowTo using the relative namespace:

## Turtle Start ##
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix ont: <#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

ont:PromptCountHowToSteps rdf:type ont:Prompt ;
  ont:hasUser ont:UserA ;
  ont:hasInput "Count the number of steps associated with HowTos in knowledge graph <G>, using terms from schema.org;SELECT ?howto (COUNT(?step) as ?stepCount) FROM <G> WHERE { ?howto a schema:HowTo; schema:step ?step. } GROUP BY ?howto ORDER BY DESC(?stepCount);" ;
  ont:hasResponse ont:ResponseCountHowToSteps .
ont:ResponseCountHowToSteps rdf:type ont:Response .
## Turtle End ##

Q: How many steps are there in the HowTo denoted by https://www.net-usb.com/share-usb/share-yubikey-over-network/#HowTo?

A:
To count the number of steps associated with a specific HowTo, you can use the newly created template. Here is the clickable SPARQL link:

Count Steps in HowTo

Q: Can you list the 3rd step from the HowTo denoted by https://www.net-usb.com/share-usb/share-yubikey-over-network/#HowTo?

A:
To retrieve the third step in a HowTo, you can use a SPARQL query with the ORDER BY and LIMIT keywords to sort the steps and select only the third one. Here is a clickable SPARQL link to get the third step:

Get 3rd Step in HowTo

Q: Redo, but for the 2nd question from the FAQ denoted by https://www.net-usb.com/share-usb/share-yubikey-over-network/#FAQPage

A:
To retrieve the second question from an FAQPage, you can use a SPARQL query with the ORDER BY and LIMIT keywords to sort the questions and select only the second one. Here is a clickable SPARQL link to get the second question:

Get 2nd Question in FAQ

Related