Skip to content

Commit

Permalink
GDB-10937 add missing namespaces option to sparql method (#1628)
Browse files Browse the repository at this point in the history
* GDB-10937 add missing namespaces option to sparql method

## What
Added additional option to sparql method for automatic insertion of the needed namespaces in generated sparql query.

## Why
This prevents errors when the agent generated a query without namespaces.

## How
* Extended the agent models and mappers with the new option.
* Added the checkbox field in the sparql method section.

* Extended test and mock data
  • Loading branch information
svilenvelikov authored Oct 21, 2024
1 parent 9532f34 commit f3ccf9d
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/css/ttyg/agent-settings-modal.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
font-weight: var(--field-label-weight);
}

.agent-settings-modal .agent-settings-form [type="checkbox"],
.agent-settings-modal .agent-settings-form [type="checkbox"] + label {
cursor: pointer;
}

.agent-settings-modal .agent-settings-form .extraction-methods {
display: flex;
flex-direction: column;
Expand Down
4 changes: 4 additions & 0 deletions src/i18n/locale-en.json
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,10 @@
"label": "Fetch ontology from a named graph",
"tooltip": "The named graph that contains the entire ontology or a subset sufficient to generate useful SPARQL queries."
},
"add_missing_namespaces": {
"label": "Auto-add missing namespaces",
"tooltip": "Automatically insert missing namespaces in SPARQL queries. This helps avoid errors when a query doesn't include necessary namespaces. In many cases, the model corrects itself after the missing namespaces error is returned, but enabling this option ensures GraphDB resolves the issue automatically."
},
"construct_query": {
"label": "Provide a SPARQL CONSTRUCT query that fetches the ontology",
"tooltip": "A SPARQL CONSTRUCT query that returns the entire ontology or a subset sufficient to generate useful SPARQL queries."
Expand Down
4 changes: 4 additions & 0 deletions src/i18n/locale-fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,10 @@
"label": "Récupérer l'ontologie à partir d'un graphe nommé",
"tooltip": "Le graphe nommé qui contient l'ensemble de l'ontologie ou un sous-ensemble suffisant pour générer des requêtes SPARQL utiles."
},
"add_missing_namespaces": {
"label": "Ajouter automatiquement les espaces de noms manquants",
"tooltip": "Insérez automatiquement les espaces de noms manquants dans les requêtes SPARQL. Cela aide à éviter les erreurs lorsque des espaces de noms nécessaires ne sont pas inclus. Dans de nombreux cas, le modèle se corrige après le retour de l'erreur d'espaces de noms manquants, mais activer cette option garantit que GraphDB résout le problème automatiquement."
},
"construct_query": {
"label": "Fournir une requête SPARQL CONSTRUCT qui récupère l'ontologie",
"tooltip": "A SPARQL CONSTRUCT query that returns the entire ontology or a subset sufficient to generate useful SPARQL queries."
Expand Down
20 changes: 20 additions & 0 deletions src/js/angular/models/ttyg/agent-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,12 @@ export class ExtractionMethodFormModel {
* @private
*/
this._sparqlOption = data.sparqlOption;
/**
* Whether to add missing namespaces to the generated SPARQL query.
* @type {boolean}
* @private
*/
this._addMissingNamespaces = data.addMissingNamespaces;
/**
* @type {string}
* @private
Expand Down Expand Up @@ -329,6 +335,12 @@ export class ExtractionMethodFormModel {
} else if (this._sparqlOption === 'sparqlQuery') {
payload.sparqlQuery = this._sparqlQuery ? this._sparqlQuery.value : null;
}
if (this._method === ExtractionMethod.SPARQL) {
// this is used only in the sparql method
if (this._addMissingNamespaces !== undefined) {
payload.addMissingNamespaces = this._addMissingNamespaces;
}
}
// this is used for all but the SPARQL method
if (this._maxNumberOfTriplesPerCall) {
payload.maxNumberOfTriplesPerCall = this._maxNumberOfTriplesPerCall;
Expand Down Expand Up @@ -382,6 +394,14 @@ export class ExtractionMethodFormModel {
this._sparqlQuery = value;
}

get addMissingNamespaces() {
return this._addMissingNamespaces;
}

set addMissingNamespaces(value) {
this._addMissingNamespaces = value;
}

get ontologyGraph() {
return this._ontologyGraph;
}
Expand Down
14 changes: 14 additions & 0 deletions src/js/angular/models/ttyg/agents.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,12 @@ export class ExtractionMethodModel {
* @private
*/
this._method = data.method;
/**
* Whether to add missing namespaces to the generated SPARQL query.
* @type {boolean}
* @private
*/
this._addMissingNamespaces = data.addMissingNamespaces;
/**
* The ontology graph used for the extraction method.
* @type {string}
Expand Down Expand Up @@ -241,6 +247,14 @@ export class ExtractionMethodModel {
this._method = value;
}

get addMissingNamespaces() {
return this._addMissingNamespaces;
}

set addMissingNamespaces(value) {
this._addMissingNamespaces = value;
}

get sparqlQuery() {
return this._sparqlQuery;
}
Expand Down
2 changes: 2 additions & 0 deletions src/js/angular/ttyg/services/agents.mapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const extractionMethodsFormMapper = (agentFormModel, isNew, defaultData, data =
method: extractionMethod.method,
sparqlOption: sparqlOption,
ontologyGraph: extractionMethod.ontologyGraph,
addMissingNamespaces: extractionMethod.addMissingNamespaces,
sparqlQuery: extractionMethod.sparqlQuery && new TextFieldModel({
value: extractionMethod.sparqlQuery,
minLength: 1,
Expand Down Expand Up @@ -150,6 +151,7 @@ const extractionMethodMapper = (data) => {
return new ExtractionMethodModel({
method: data.method,
ontologyGraph: data.ontologyGraph,
addMissingNamespaces: data.addMissingNamespaces,
sparqlQuery: data.sparqlQuery,
similarityIndex: data.similarityIndex,
similarityIndexThreshold: data.similarityIndexThreshold,
Expand Down
18 changes: 15 additions & 3 deletions src/js/angular/ttyg/templates/modal/agent-settings-modal.html
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ <h4 class="modal-title">{{(
ng-if="extractionMethod.selected && !extractionMethod.sparqlOption">
{{'ttyg.agent.create_agent_modal.form.sparql_search.required_option' | translate}}
</div>
<div class="add-missing-namespaces-option mt-1">
<input id="addMissingNamespaces" name="addMissingNamespaces" type="checkbox"
ng-model="extractionMethod.addMissingNamespaces">
<label for="addMissingNamespaces"
uib-popover="{{'ttyg.agent.create_agent_modal.form.add_missing_namespaces.tooltip' | translate}}"
popover-trigger="mouseenter">{{
'ttyg.agent.create_agent_modal.form.add_missing_namespaces.label' | translate
}}</label>
</div>
</div>

<!-- FTS method settings -->
Expand Down Expand Up @@ -321,7 +330,8 @@ <h4 class="modal-title">{{(
<label for="{{additionalExtractionMethod.method + '_checkbox'}}"></label>
<a class="btn btn-link extraction-method-label" uib-popover="{{'ttyg.agent.create_agent_modal.form.additional_query_methods.method.' +
additionalExtractionMethod.method + '.tooltip' | translate}}"
popover-trigger="mouseenter">{{'ttyg.agent.create_agent_modal.form.additional_query_methods.method.' +
popover-trigger="mouseenter">{{'ttyg.agent.create_agent_modal.form.additional_query_methods.method.'
+
additionalExtractionMethod.method + '.label' | translate}}</a>
</div>
</div>
Expand All @@ -345,7 +355,8 @@ <h4 class="modal-title">{{(
<label for="temperature">
<span uib-popover="{{'ttyg.agent.create_agent_modal.form.temperature.tooltip' | translate}}"
popover-trigger="mouseenter">{{'ttyg.agent.create_agent_modal.form.temperature.label' | translate}}</span>
<i class="fa-regular fa-triangle-exclamation text-warning high-temperature-warning" ng-if="showHighTemperatureWarning"
<i class="fa-regular fa-triangle-exclamation text-warning high-temperature-warning"
ng-if="showHighTemperatureWarning"
uib-popover="{{'ttyg.agent.create_agent_modal.form.temperature.high_temperature_warning' | translate}}"
popover-trigger="mouseenter"></i>
</label>
Expand Down Expand Up @@ -416,7 +427,8 @@ <h4 class="modal-title">{{(
<span
uib-popover="{{'ttyg.agent.create_agent_modal.form.system_instruction.tooltip' | translate}}"
popover-trigger="mouseenter">{{'ttyg.agent.create_agent_modal.form.system_instruction.label' | translate}}</span>
<i class="fa-regular fa-triangle-exclamation text-warning overriding-system-instructions-warning" ng-if="showSystemInstructionWarning"
<i class="fa-regular fa-triangle-exclamation text-warning overriding-system-instructions-warning"
ng-if="showSystemInstructionWarning"
uib-popover="{{'ttyg.agent.create_agent_modal.form.system_instruction.overriding_system_instruction_warning.body' | translate}}"
popover-trigger="mouseenter"></i>
</label>
Expand Down
4 changes: 4 additions & 0 deletions test-cypress/fixtures/locale-en.json
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,10 @@
"label": "Fetch ontology from a named graph",
"tooltip": "The named graph that contains the entire ontology or a subset sufficient to generate useful SPARQL queries."
},
"add_missing_namespaces": {
"label": "Auto-add missing namespaces",
"tooltip": "Automatically insert missing namespaces in SPARQL queries. This helps avoid errors when a query doesn't include necessary namespaces. In many cases, the model corrects itself after the missing namespaces error is returned, but enabling this option ensures GraphDB resolves the issue automatically."
},
"construct_query": {
"label": "Provide a SPARQL CONSTRUCT query that fetches the ontology",
"tooltip": "A SPARQL CONSTRUCT query that returns the entire ontology or a subset sufficient to generate useful SPARQL queries."
Expand Down
3 changes: 2 additions & 1 deletion test-cypress/fixtures/ttyg/agent/create-agent.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"assistantExtractionMethods": [
{
"method": "sparql_search",
"sparqlQuery": "select ?s ?p ?o where {?s ?p ?o .}"
"sparqlQuery": "select ?s ?p ?o where {?s ?p ?o .}",
"addMissingNamespaces": true
}
]
}
3 changes: 2 additions & 1 deletion test-cypress/fixtures/ttyg/agent/get-agent-defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
{
"method": "sparql_search",
"ontologyGraph": "http://example.com",
"sparqlQuery": "CONSTRUCT {?s ?p ?o} WHERE {GRAPH <http://example.org/ontology> {?s ?p ?o .}}"
"sparqlQuery": "CONSTRUCT {?s ?p ?o} WHERE {GRAPH <http://example.org/ontology> {?s ?p ?o .}}",
"addMissingNamespaces": false
},
{
"method": "fts_search",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"assistantExtractionMethods": [
{
"method": "sparql_search",
"sparqlQuery": "select ?s ?p ?o where {?s ?p ?o .}"
"sparqlQuery": "select ?s ?p ?o where {?s ?p ?o .}",
"addMissingNamespaces": true
}
]
}
Expand Down
3 changes: 2 additions & 1 deletion test-cypress/fixtures/ttyg/agent/get-agent.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
{
"method": "sparql_search",
"ontologyGraph": null,
"sparqlQuery": "CONSTRUCT {\n ?s ?p ?o\n} WHERE {\n GRAPH <http://example.com/sw-ont> {\n ?s ?p ?o\n }\n}"
"sparqlQuery": "CONSTRUCT {\n ?s ?p ?o\n} WHERE {\n GRAPH <http://example.com/sw-ont> {\n ?s ?p ?o\n }\n}",
"addMissingNamespaces": false
}
],
"additionalExtractionMethods": [
Expand Down
28 changes: 27 additions & 1 deletion test-cypress/integration/ttyg/create-agent.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ describe('TTYG create new agent', () => {
TtygAgentSettingsModalSteps.getSaveAgentButton().should('be.disabled');
TtygAgentSettingsModalSteps.typeSparqlMethodSparqlQueryField('select ?s ?p ?o where {?s ?p ?o .}');
TtygAgentSettingsModalSteps.getSaveAgentButton().should('be.enabled');
// check the add missing namespaces checkbox
TtygAgentSettingsModalSteps.getAddMissingNamespacesCheckbox().should('not.be.checked');
TtygAgentSettingsModalSteps.toggleAddMissingNamespacesCheckbox();

// Validate the other agent settings

Expand Down Expand Up @@ -139,7 +142,30 @@ describe('TTYG create new agent', () => {
TTYGStubs.stubAgentListGet('/ttyg/agent/get-agent-list-new-agent.json', 1000);
TtygAgentSettingsModalSteps.saveAgent();
TtygAgentSettingsModalSteps.getCreatingAgentLoader().should('be.visible');
cy.wait('@create-agent');
cy.wait('@create-agent').then((interception) => {
assert.deepEqual(interception.request.body, {
"id": "id",
"name": "Test Agent",
"repositoryId": "starwars",
"model": "gpt-4o",
"temperature": "0.2",
"topP": "0.2",
"seed": 0,
"assistantsInstructions": {
"systemInstruction": "",
"userInstruction": "If you need to write a SPARQL query, use only the classes and properties provided in the schema and don't invent or guess any. Always try to return human-readable names or labels and not only the IRIs. If SPARQL fails to provide the necessary information you can try another tool too."
},
"assistantExtractionMethods": [
{
"method": "sparql_search",
"sparqlQuery": "select ?s ?p ?o where {?s ?p ?o .}",
"addMissingNamespaces": true
}
],
"additionalExtractionMethods": [
]
});
});
// the modal should be closed
TtygAgentSettingsModalSteps.getDialog().should('not.exist');
cy.wait('@get-agent-list');
Expand Down
8 changes: 8 additions & 0 deletions test-cypress/steps/ttyg/ttyg-agent-settings-modal.steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ export class TtygAgentSettingsModalSteps extends ModalDialogSteps {
return this.getSparqlMethodSparqlQueryField().type(value, {parseSpecialCharSequences: false});
}

static getAddMissingNamespacesCheckbox() {
return cy.get('#addMissingNamespaces');
}

static toggleAddMissingNamespacesCheckbox() {
this.getAddMissingNamespacesCheckbox().click();
}

// FTS extraction method

static enableFtsExtractionMethod() {
Expand Down

0 comments on commit f3ccf9d

Please sign in to comment.