diff --git a/src/main/java/com/olympus/hermione/repository/ScenarioRepository.java b/src/main/java/com/olympus/hermione/repository/ScenarioRepository.java index 7b5324f..ef0062c 100644 --- a/src/main/java/com/olympus/hermione/repository/ScenarioRepository.java +++ b/src/main/java/com/olympus/hermione/repository/ScenarioRepository.java @@ -19,20 +19,30 @@ public interface ScenarioRepository extends MongoRepository { List findByUsableFor(String projectId);*/ //List findByAvailableForProjects_Id(String projectId); - List findByAvailableForProjects_IdAndVisible(String projectId, String visible); + //List findByAvailableForProjects_IdAndVisible(String projectId, String visible); + List findByAvailableForProjects_IdAndVisibleIn(String projectId, List visible); + + List findByAvailableForApplications_IdAndVisibleIn(String projectId, List visible); + + List findByAvailableForProjectsIsNullAndAvailableForApplicationsIsNullAndVisibleIn(List visible); - List findByAvailableForApplications_IdAndVisible(String projectId, String visible); - - List findByAvailableForProjectsIsNullAndAvailableForApplicationsIsNullAndVisible(String visible); - - - @Query("{ 'visible': 'Y', 'category': 'RE', " + + /* @Query("{ 'visible': 'Y', 'category': 'RE', " + " $or: [ " + " { 'availableForProjects': ObjectId(?0) }, " + " { 'availableForApplications': ObjectId(?1) } " + " ] " + "}") List findByVisibleAndCategoryAndProjectOrApplication(String projectId, String applicationId); +*/ + +@Query("{ 'visible': { $in: ?2 }, 'category': 'RE', " + + " $or: [ " + + " { 'availableForProjects': ObjectId(?0) }, " + + " { 'availableForApplications': ObjectId(?1) } " + + " ] " + + "}") +List findByVisibleInAndCategoryAndProjectOrApplication(String projectId, String applicationId, List visibleValues); + } diff --git a/src/main/java/com/olympus/hermione/services/ScenarioService.java b/src/main/java/com/olympus/hermione/services/ScenarioService.java index 1916439..d3de12e 100644 --- a/src/main/java/com/olympus/hermione/services/ScenarioService.java +++ b/src/main/java/com/olympus/hermione/services/ScenarioService.java @@ -27,8 +27,18 @@ public class ScenarioService { public List getListScenariosByProject(String project){ logger.info("getListProjectByUser function:"); List lstScenarios = null; + + List scenAccept = new ArrayList(); + 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 getListScenariosByApplication(String app){ logger.info("getListProjectByUser function:"); List lstScenarios = null; + List scenAccept = new ArrayList(); + 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 getListScenariosCross(){ logger.info("getListProjectByUser function:"); List lstScenarios = null; + + List scenAccept = new ArrayList(); + 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 scenAccept = new ArrayList(); + 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()); diff --git a/src/main/java/com/olympus/hermione/stepSolvers/SummarizeDocSolver.java b/src/main/java/com/olympus/hermione/stepSolvers/SummarizeDocSolver.java index 5d729d9..478a383 100644 --- a/src/main/java/com/olympus/hermione/stepSolvers/SummarizeDocSolver.java +++ b/src/main/java/com/olympus/hermione/stepSolvers/SummarizeDocSolver.java @@ -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; - 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 chunks = chunkText(text, chunk_size_text.intValue()); List 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)