adjustments summarize + scenarios visible based on role

This commit is contained in:
Florinda
2025-02-26 12:12:11 +01:00
parent f663ecb57b
commit e91f8f3d3a
3 changed files with 73 additions and 68 deletions

View File

@@ -19,20 +19,30 @@ public interface ScenarioRepository extends MongoRepository<Scenario, String> {
List<Scenario> findByUsableFor(String projectId);*/
//List<Scenario> findByAvailableForProjects_Id(String projectId);
List<Scenario> findByAvailableForProjects_IdAndVisible(String projectId, String visible);
//List<Scenario> findByAvailableForProjects_IdAndVisible(String projectId, String visible);
List<Scenario> findByAvailableForProjects_IdAndVisibleIn(String projectId, List<String> visible);
List<Scenario> findByAvailableForApplications_IdAndVisibleIn(String projectId, List<String> visible);
List<Scenario> findByAvailableForProjectsIsNullAndAvailableForApplicationsIsNullAndVisibleIn(List<String> visible);
List<Scenario> findByAvailableForApplications_IdAndVisible(String projectId, String visible);
List<Scenario> findByAvailableForProjectsIsNullAndAvailableForApplicationsIsNullAndVisible(String visible);
@Query("{ 'visible': 'Y', 'category': 'RE', " +
/* @Query("{ 'visible': 'Y', 'category': 'RE', " +
" $or: [ " +
" { 'availableForProjects': ObjectId(?0) }, " +
" { 'availableForApplications': ObjectId(?1) } " +
" ] " +
"}")
List<Scenario> findByVisibleAndCategoryAndProjectOrApplication(String projectId, String applicationId);
*/
@Query("{ 'visible': { $in: ?2 }, 'category': 'RE', " +
" $or: [ " +
" { 'availableForProjects': ObjectId(?0) }, " +
" { 'availableForApplications': ObjectId(?1) } " +
" ] " +
"}")
List<Scenario> findByVisibleInAndCategoryAndProjectOrApplication(String projectId, String applicationId, List<String> visibleValues);
}

View File

@@ -27,8 +27,18 @@ public class ScenarioService {
public List<Scenario> getListScenariosByProject(String project){
logger.info("getListProjectByUser function:");
List<Scenario> lstScenarios = null;
List<String> scenAccept = new ArrayList<String>();
scenAccept.add("Y");
try{
lstScenarios = scenarioRepo.findByAvailableForProjects_IdAndVisible(project, "Y");
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if(user.getRole().name().equals("ADMIN")){
scenAccept.add("DRAFT");
}
lstScenarios = scenarioRepo.findByAvailableForProjects_IdAndVisibleIn(project, scenAccept);
//lstScenarios = scenarioRepo.findByAvailableForProjects_IdAndVisible(project, "Y");
}catch(Exception e){
logger.error("Exception ScenarioRepository:", e.getMessage());
}
@@ -41,8 +51,16 @@ public class ScenarioService {
public List<Scenario> getListScenariosByApplication(String app){
logger.info("getListProjectByUser function:");
List<Scenario> lstScenarios = null;
List<String> scenAccept = new ArrayList<String>();
scenAccept.add("Y");
try{
lstScenarios = scenarioRepo.findByAvailableForApplications_IdAndVisible(app, "Y");
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if(user.getRole().name().equals("ADMIN")){
scenAccept.add("DRAFT");
}
lstScenarios = scenarioRepo.findByAvailableForApplications_IdAndVisibleIn(app, scenAccept);
//
}catch(Exception e){
logger.error("Exception ScenarioRepository:", e.getMessage());
}
@@ -55,8 +73,16 @@ public class ScenarioService {
public List<Scenario> getListScenariosCross(){
logger.info("getListProjectByUser function:");
List<Scenario> lstScenarios = null;
List<String> scenAccept = new ArrayList<String>();
scenAccept.add("Y");
try{
lstScenarios = scenarioRepo.findByAvailableForProjectsIsNullAndAvailableForApplicationsIsNullAndVisible("Y");
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if(user.getRole().name().equals("ADMIN")){
scenAccept.add("DRAFT");
}
lstScenarios = scenarioRepo.findByAvailableForProjectsIsNullAndAvailableForApplicationsIsNullAndVisibleIn(scenAccept);
}catch(Exception e){
logger.error("Exception ScenarioRepository:", e.getMessage());
}
@@ -72,10 +98,16 @@ public class ScenarioService {
Scenario scenarioDefault = new Scenario();
scenarioDefault.setName("Default");
scenarioDefault.setId("");
List<String> scenAccept = new ArrayList<String>();
scenAccept.add("Y");
try{
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
lstScenarios = scenarioRepo.findByVisibleAndCategoryAndProjectOrApplication(user.getSelectedProject().getId(), user.getSelectedApplication().getId());
if(user.getRole().name().equals("ADMIN")){
scenAccept.add("DRAFT");
}
lstScenarios = scenarioRepo.findByVisibleInAndCategoryAndProjectOrApplication(user.getSelectedProject().getId(), user.getSelectedApplication().getId(), scenAccept);
lstScenarios.add(scenarioDefault);
}catch(Exception e){
logger.error("Exception ScenarioRepository:", e.getMessage());

View File

@@ -36,20 +36,18 @@ public class SummarizeDocSolver extends StepSolver {
private String qai_system_prompt_template_chunk;
private String qai_path_file;
private String qai_output_variable;
private int chunk_size_token;
private int max_chunk_size_token;
private String encoding_name;
private double charMax;
private int tokenCount;
private Optional<Encoding> encoding;
private int percent_summarize;
private Integer maxTokenChunk;
private Double percent_chunk_length;
private String qai_custom_memory_id;
private Integer max_output_token;
private String qai_system_prompt_template_minimum;
private Integer max_input_token_not_to_be_chunked;
private String qai_system_prompt_template_formatter;
private boolean isChunked = false;
private Double chunk_size_token_calc;
private Double perc = 0.2;
// private boolean qai_load_graph_schema=false;
Logger logger = (Logger) LoggerFactory.getLogger(SummarizeDocSolver.class);
@@ -68,16 +66,15 @@ public class SummarizeDocSolver extends StepSolver {
this.qai_output_variable = (String) this.step.getAttributes().get("qai_output_variable");
this.encoding_name = (String) this.step.getAttributes().get("encoding_name");
this.chunk_size_token = Integer.parseInt((String) this.step.getAttributes().get("chunk_size_token"));
this.max_chunk_size_token = Integer.parseInt((String) this.step.getAttributes().get("max_chunk_size_token"));
this.percent_summarize = Integer.parseInt((String) this.step.getAttributes().get("percent_summarize"));
this.percent_chunk_length = (Double) this.step.getAttributes().get("percent_chunk_length");
this.qai_custom_memory_id = (String) this.step.getAttributes().get("qai_custom_memory_id");
this.max_output_token = Integer.parseInt((String) this.step.getAttributes().get("max_output_token"));
this.qai_system_prompt_template_minimum = attributeParser
.parse((String) this.step.getAttributes().get("qai_system_prompt_template_minimum"));
this.max_input_token_not_to_be_chunked = Integer.parseInt((String) this.step.getAttributes().get("max_input_token_not_to_be_chunked"));
this.qai_system_prompt_template_formatter = attributeParser
.parse((String) this.step.getAttributes().get("qai_system_prompt_template_formatter"));
@@ -102,38 +99,21 @@ public class SummarizeDocSolver extends StepSolver {
EncodingRegistry registry = Encodings.newDefaultEncodingRegistry();
encoding = registry.getEncoding(this.encoding_name);
// Conta i token
// int tokenCount = encoding.get().encode(text).size();
tokenCount = encoding.get().countTokens(text);
logger.info("token count input: " + tokenCount);
int charCount = text.length();
// Stima media caratteri per token
// double charPerToken = (double) charCount / tokenCount;
// Double output_charD = (double) charCount * ((double) this.percent_summarize / 100.0);
// Integer output_char = output_charD.intValue();
Double min_output_tokenD = (double) tokenCount * ((double) this.percent_summarize / 100.0);
Integer min_output_token = min_output_tokenD.intValue();
String content = new String("");
content = this.qai_system_prompt_template.replace("max_number_token",
max_output_token.toString());
if (min_output_token < max_output_token) {
content += this.qai_system_prompt_template_minimum.replace(
"min_number_token",
min_output_token.toString());
}
chunk_size_token_calc = (Double)((double)tokenCount * perc);
if(chunk_size_token_calc>chunk_size_token){
chunk_size_token_calc = (double)chunk_size_token;
// Calcolo della dimensione del chunk in token
chunk_size_token_calc = (Double)((double)tokenCount * percent_chunk_length);
if(chunk_size_token_calc>max_chunk_size_token){
chunk_size_token_calc = (double)max_chunk_size_token;
}
// **Fase di Summarization**
String summarizedText = summarize(text); // 🔹 Applica la funzione di riassunto
// String template = this.qai_system_prompt_template+" The output length should
// be of " + output_char + " characters";
String summarizedText = text;
if(tokenCount > max_input_token_not_to_be_chunked){
summarizedText = summarize(text);
}
// Creazione dei messaggi per il modello AI
Message userMessage = new UserMessage(summarizedText);
@@ -146,8 +126,8 @@ public class SummarizeDocSolver extends StepSolver {
logger.info("template formatter: " + this.qai_system_prompt_template_formatter);
}else{
//here
systemMessage = new SystemMessage(content);
logger.info("template: " + content);
systemMessage = new SystemMessage(this.qai_system_prompt_template);
logger.info("template: " + this.qai_system_prompt_template);
}
CallResponseSpec resp = chatClient.prompt()
@@ -185,7 +165,6 @@ public class SummarizeDocSolver extends StepSolver {
}
private String summarize(String text) {
// Se il testo è già corto, non riassumere
logger.info("length: " + text.length());
Double chunk_size_text;
@@ -204,10 +183,6 @@ public class SummarizeDocSolver extends StepSolver {
// Suddividere il testo in chunk
List<String> chunks = chunkText(text, chunk_size_text.intValue());
List<String> summarizedChunks = new ArrayList<>();
//Double maxTokenChunkD = Math.ceil(chunk_size_token / ratio);
Double maxTokenChunkD = Math.ceil(chunk_size_token_calc / ratio);
maxTokenChunk = maxTokenChunkD.intValue();
// Riassumere ogni chunk singolarmente
for (String chunk : chunks) {
@@ -240,24 +215,12 @@ public class SummarizeDocSolver extends StepSolver {
// Metodo di riassunto per singolo chunk
private String summarizeChunk(String chunk) {
String content = new String("");
/* if (maxTokenChunk < max_output_token) {
content = this.qai_system_prompt_template_chunk.replace("max_number_token",
maxTokenChunk.toString());
}else{
content = this.qai_system_prompt_template_chunk.replace("max_number_token",
max_output_token.toString());
}*/
content = this.qai_system_prompt_template_chunk.replace("max_number_token",
maxTokenChunk.toString());
Message chunkMessage = new UserMessage(chunk);
Message systemMessage = new SystemMessage(
content);
this.qai_system_prompt_template_chunk);
logger.info("template chunk: " + content);
logger.info("template chunk: " + this.qai_system_prompt_template_chunk);
CallResponseSpec resp = chatClient.prompt()
.messages(chunkMessage, systemMessage)