Skip to content

Commit

Permalink
Merge branch 'automatedTestingWorkflow' of github.com:WSE-research/qa…
Browse files Browse the repository at this point in the history
…nary-explanation-service into automatedTestingWorkflow
  • Loading branch information
dschiese committed Oct 10, 2023
2 parents 04f6237 + a4cf6f9 commit 21c6e3a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ public ResponseEntity<?> automatedExplanationTest(@RequestBody AutomatedTestRequ
return new ResponseEntity<>(automatedTest, HttpStatus.OK);
}

@PostMapping(value = "/explanationswithoutgptexplanation", consumes = "application/json")
public ResponseEntity<?> getExplanationsWithoutGptExplanation(@RequestBody AutomatedTestRequestBody requestBody) throws Exception {
JSONObject explanations = automatedTestingService.testWithoutGptExplanation(requestBody);

return new ResponseEntity<>(explanations, HttpStatus.OK);
}


@GetMapping("/dataset")
public ResponseEntity<?> getDataset() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public QanaryResponseObject executeQanaryPipeline(QanaryRequestObject qanaryRequ
}

public ResultSet takeRandomQuestion(String query) {
RDFConnection rdfConnection1 = RDFConnection.connect("http://localhost:8095/sparql");
RDFConnection rdfConnection1 = RDFConnection.connect("http://localhost:8890/sparql");
QueryExecution queryExecution = rdfConnection1.query(query);
return queryExecution.execSelect();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public AnnotationType selectRandomAnnotationType() {
* Selects a random question as well as a random component for a given annotation-type
* All in all that will result in a triple containing the type,question,component
*/
public TestDataObject selectTestingTriple(AnnotationType annotationType) throws IndexOutOfBoundsException, IOException { // TODO: maybe parallelization possible? Threads?
public TestDataObject selectTestingTriple(AnnotationType annotationType) throws Exception { // TODO: maybe parallelization possible? Threads?

TestDataObject data;
AnnotationType annotationType_ = annotationType;
Expand Down Expand Up @@ -188,7 +188,7 @@ public QanaryResponseObject executeQanaryPipeline(String question, String select
* Führt die Qanary pipeline aus und fragt mit der graphID den SPARQL Endpunkt ab um das Datenset zu erhalten
* Weiterhin wird das datenset angepasst (bspw. das Hinzufügen der Punkte am Ende)
*/
public String createDataset(String componentURI, String question, String graphURI) throws IOException {
public String createDataset(String componentURI, String question, String graphURI) throws Exception {

ResultSet triples = fetchTriples(graphURI, componentURI);

Expand All @@ -214,13 +214,16 @@ public String createDataset(String componentURI, String question, String graphUR

}

public ResultSet fetchTriples(String graphURI, String componentURI) throws IOException {
public ResultSet fetchTriples(String graphURI, String componentURI) throws Exception {
QuerySolutionMap bindingsForQuery = new QuerySolutionMap();
bindingsForQuery.add("graphURI", ResourceFactory.createResource(graphURI));
bindingsForQuery.add("componentURI", ResourceFactory.createResource("urn:qanary:" + componentURI));
String query = QanaryTripleStoreConnector.readFileFromResourcesWithMap(DATASET_QUERY, bindingsForQuery);

return automatedTestingRepository.executeSparqlQueryWithResultSet(query);
ResultSet resultSet = automatedTestingRepository.executeSparqlQueryWithResultSet(query);
if (!resultSet.hasNext())
throw new Exception("ResultSet is null");
return resultSet;
}

//
Expand All @@ -235,7 +238,8 @@ public AutomatedTest setUpTest(AutomatedTestRequestBody requestBody) throws Exce
for (int i = 0; i < requestBody.getExamples(); i++) {
automatedTest.setExampleData(selectTestingTriple(null)); // null since type is not declared
}
} catch (IndexOutOfBoundsException e) {
} catch (Exception e) {
logger.error("{}", e.getMessage());
return null;
}
try {
Expand Down Expand Up @@ -310,6 +314,29 @@ public JSONObject gptExplanation(AutomatedTestRequestBody requestBody) throws Ex
return jsonObject;
}

public JSONObject testWithoutGptExplanation(AutomatedTestRequestBody requestBody) throws Exception {
int runs = requestBody.getRuns();
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject = new JSONObject();

for (int i = 0; i < runs; i++) {
AutomatedTest automatedTestObject = setUpTest(requestBody);
if (automatedTestObject != null) {
// send prompt to openai-chatgpt
try {
jsonArray.put(new JSONObject(automatedTestObject));
} catch (Exception e) {
logger.error("Error while processing gpt explanation, skipped.");
}
}
}

jsonObject.put("explanations", jsonArray);
writeObjectToFile(jsonObject);

return jsonObject;
}

public void writeObjectToFile(JSONObject jsonObject) throws IOException {
FileWriter fileWriter = new FileWriter("output.json");
fileWriter.write(jsonObject.toString());
Expand Down

0 comments on commit 21c6e3a

Please sign in to comment.