Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend explain generative #53

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>
<groupId>com.wse</groupId>
<artifactId>qanary-explanation-service</artifactId>
<version>3.8.1</version>
<version>3.9.0</version>
<name>Qanary explanation service</name>
<description>Webservice for rule-based explanation of QA-Systems as well as specific components</description>
<properties>
Expand Down Expand Up @@ -132,6 +132,11 @@
<artifactId>spring-boot-buildpack-platform</artifactId>
<version>3.1.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.19</version> <!-- Replace with your preferred version -->
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.wse.qanaryexplanationservice.controller;

import com.wse.qanaryexplanationservice.exceptions.ExplanationException;
import com.wse.qanaryexplanationservice.helper.GptModel;
import com.wse.qanaryexplanationservice.helper.dtos.ComposedExplanationDTO;
import com.wse.qanaryexplanationservice.helper.dtos.QanaryExplanationData;
import com.wse.qanaryexplanationservice.helper.pojos.ComposedExplanation;
import com.wse.qanaryexplanationservice.helper.pojos.QanaryComponent;
import com.wse.qanaryexplanationservice.exceptions.ExplanationExceptionComponent;
import com.wse.qanaryexplanationservice.exceptions.ExplanationExceptionPipeline;
import com.wse.qanaryexplanationservice.helper.pojos.QanaryPipelineInformation;
import com.wse.qanaryexplanationservice.services.ExplanationService;
import io.swagger.v3.oas.annotations.Operation;
import org.slf4j.Logger;
Expand Down Expand Up @@ -241,4 +241,14 @@ public ResponseEntity<?> getPipelineExplanation(@PathVariable String graph) thro
return new ResponseEntity<>(explanationService.getPipelineExplanation(graph), HttpStatus.OK);
}

@GetMapping("/generative/{gptModel}/{graph}/{component}")
public ResponseEntity<?> getGenerativeExplanation(@PathVariable GptModel gptModel, @PathVariable String graph, @PathVariable QanaryComponent component) throws Exception {
return new ResponseEntity(this.explanationService.getGenerativeExplanation(graph, component, null, gptModel), HttpStatus.OK);
}

@GetMapping("/generative/{gptModel}")
public ResponseEntity<?> getGenerativeExplanation(@PathVariable GptModel gptModel, @RequestBody QanaryPipelineInformation qanaryPipelineInformation) throws Exception {
return new ResponseEntity(this.explanationService.getGenerativeExplanation(qanaryPipelineInformation.getGraph(), null, qanaryPipelineInformation, gptModel), HttpStatus.OK);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.wse.qanaryexplanationservice.helper.pojos;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.List;

public class QanaryPipelineInformation {

@JsonProperty("question")
private String question;
@JsonProperty("questionId")
private String questionId;
@JsonProperty("graph")
private String graph;
@JsonProperty("components")
private List<String> components;

public String getQuestionId() {
return questionId;
}

public String getGraph() {
return graph;
}

public List<String> getComponents() {
return components;
}

public String getQuestion() {
return question;
}

public void setGraph(String graph) {
this.graph = graph;
}

public void setQuestionId(String questionId) {
this.questionId = questionId;
}

public void setComponents(List<String> components) {
this.components = components;
}

public void setQuestion(String question) {
this.question = question;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

import com.wse.qanaryexplanationservice.exceptions.ExplanationExceptionComponent;
import com.wse.qanaryexplanationservice.exceptions.ExplanationExceptionPipeline;
import com.wse.qanaryexplanationservice.helper.GptModel;
import com.wse.qanaryexplanationservice.helper.dtos.ComposedExplanationDTO;
import com.wse.qanaryexplanationservice.helper.dtos.QanaryExplanationData;
import com.wse.qanaryexplanationservice.helper.pojos.ComposedExplanation;
import com.wse.qanaryexplanationservice.helper.pojos.GenerativeExplanationObject;
import com.wse.qanaryexplanationservice.helper.pojos.GenerativeExplanationRequest;
import com.wse.qanaryexplanationservice.helper.pojos.QanaryComponent;
import com.wse.qanaryexplanationservice.helper.pojos.*;
import com.wse.qanaryexplanationservice.repositories.QanaryRepository;
import eu.wdaqua.qanary.commons.triplestoreconnectors.QanaryTripleStoreConnector;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -21,6 +19,7 @@
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

Expand All @@ -35,6 +34,8 @@ public class ExplanationService {
private GenerativeExplanationsService genExpService;
@Autowired
private QanaryRepository qanaryRepository;
@Autowired
private GenerativeExplanations generativeExplanations;

public String getQaSystemExplanation(String header, String graphUri) throws Exception {
return tmplExpService.explainQaSystem(header, graphUri);
Expand Down Expand Up @@ -259,5 +260,26 @@ public String composeComponentExplanations(Map<String,String> componentAndExplan
return composedExplanations.toString();
}

public String getGenerativeExplanation(String graph, QanaryComponent component, QanaryPipelineInformation qanaryPipelineInformation, GptModel gptModel) throws Exception {
String prompt = "";
if(component != null) {
List<String> sparqlQueriesInputData = new ArrayList<>();
String sparqlQuery = bindingForGraphAndComponent(graph, component, TemplateExplanationsService.INPUT_DATA_SELECT_QUERY);
ResultSet results = qanaryRepository.selectWithResultSet(sparqlQuery);
while(results.hasNext()) {
QuerySolution solution = results.nextSolution();
sparqlQueriesInputData.add(solution.get("body").toString());
}
String sparqlQueries = String.join("\n\n", sparqlQueriesInputData); // == Input data
String datasetOutputData = generativeExplanations.createDataset(component, graph, null);
prompt = genExpService.createPromptForComposedComponentExplanation(component, sparqlQueries == null ? "" : sparqlQueries, datasetOutputData == null ? "" : datasetOutputData);
}
else {
prompt = genExpService.createPromptForPipelineExplanation(qanaryPipelineInformation);
}
logger.info("Prompt: {}", prompt);
return genExpService.sendPrompt(prompt, gptModel);
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.wse.qanaryexplanationservice.helper.pojos.GenerativeExplanationObject;
import com.wse.qanaryexplanationservice.helper.pojos.InputQueryExample;
import com.wse.qanaryexplanationservice.helper.pojos.QanaryComponent;
import com.wse.qanaryexplanationservice.helper.pojos.QanaryPipelineInformation;
import com.wse.qanaryexplanationservice.repositories.GenerativeExplanationsRepository;
import eu.wdaqua.qanary.commons.triplestoreconnectors.QanaryTripleStoreConnector;
import org.slf4j.Logger;
Expand Down Expand Up @@ -212,4 +213,19 @@ public String getTemplateExplanation(String graphUri, QanaryComponent component,
return tmplExpService.createOutputExplanation(graphUri, component, lang);
}

public String createPromptForComposedComponentExplanation(QanaryComponent component, String inputData, String outputData) throws IOException {
return getStringFromFile("/prompt_templates/composed/zeroshot")
.replace("${component}", component.getComponentName())
.replace("${inputData}", inputData)
.replace("${outputData}", outputData);
}

public String createPromptForPipelineExplanation(QanaryPipelineInformation qanaryPipelineInformation) throws IOException {
return getStringFromFile("/prompt_templates/composed/zeroshot_pipeline")
.replace("${questionId}", qanaryPipelineInformation.getQuestionId())
.replace("${question}", qanaryPipelineInformation.getQuestion())
.replace("${graph}", qanaryPipelineInformation.getGraph())
.replace("${components}", String.join(", ", qanaryPipelineInformation.getComponents()));
}

}
15 changes: 15 additions & 0 deletions src/main/resources/prompt_templates/composed/zeroshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Given the context of a component-based Question-Answering-system where a QA process with several components was executed. Each component of this QA system processes data and stores data. For the component ${component} this data was as follows:

Input data:
"
${inputData}
"


Output data:
"
${outputData}
"


Now, use both datasets to explain the components' process. Write in past-tense. Only return the explanation.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Given the context of a component-based Question-Answering-system that consists of a pipeline and several components, a QA process was executed. The pipeline received the question "${question}" and executed the following components to compute an answer: ${components}. It then returned the following data for the process:

questionId: ${questionId}
graph: ${graph}

Now, summarize and explain this data in terms of the process. Return only the explanations. Write in the past-tense.
Loading