From 3c20728c2c31a591f11f94588e76940df5a65d0f Mon Sep 17 00:00:00 2001 From: Florinda Date: Wed, 26 Feb 2025 17:04:08 +0100 Subject: [PATCH] update codegenie --- .../stepSolvers/ExternalCodeGenieSolver.java | 379 +++++++++--------- 1 file changed, 190 insertions(+), 189 deletions(-) diff --git a/src/main/java/com/olympus/hermione/stepSolvers/ExternalCodeGenieSolver.java b/src/main/java/com/olympus/hermione/stepSolvers/ExternalCodeGenieSolver.java index 16bb3fa..ad8dff7 100644 --- a/src/main/java/com/olympus/hermione/stepSolvers/ExternalCodeGenieSolver.java +++ b/src/main/java/com/olympus/hermione/stepSolvers/ExternalCodeGenieSolver.java @@ -75,12 +75,12 @@ public class ExternalCodeGenieSolver extends StepSolver { if (this.step.getAttributes().get("codegenie_username") != null) { this.codegenie_username = (String) this.step.getAttributes().get("codegenie_username"); - + } if (this.step.getAttributes().get("codegenie_password") != null) { this.codegenie_password = (String) this.step.getAttributes().get("codegenie_password"); - + } if (this.step.getAttributes().get("codegenie_output_variable") != null) { @@ -104,217 +104,218 @@ public class ExternalCodeGenieSolver extends StepSolver { @Override public ScenarioExecution solveStep() throws Exception { - try { + System.out.println("Solving step: " + this.step.getName()); - System.out.println("Solving step: " + this.step.getName()); + this.scenarioExecution.setCurrentStepId(this.step.getStepId()); - this.scenarioExecution.setCurrentStepId(this.step.getStepId()); + loadParameters(); - loadParameters(); + // token + HttpHeaders headersToken = new HttpHeaders(); + headersToken.setContentType(MediaType.APPLICATION_JSON); + // headers.set("Authorization", "Bearer "); + String auth = codegenie_username + ":" + codegenie_password; + String encodedAuth = java.util.Base64.getEncoder().encodeToString(auth.getBytes()); + headersToken.set("Authorization", "Basic " + encodedAuth); - // token - HttpHeaders headersToken = new HttpHeaders(); - headersToken.setContentType(MediaType.APPLICATION_JSON); - // headers.set("Authorization", "Bearer "); - String auth = codegenie_username + ":" + codegenie_password; - String encodedAuth = java.util.Base64.getEncoder().encodeToString(auth.getBytes()); - headersToken.set("Authorization", "Basic " + encodedAuth); + HttpEntity entityToken = new HttpEntity<>(headersToken); - HttpEntity entityToken = new HttpEntity<>(headersToken); + RestTemplate restTemplate = new RestTemplate(); - RestTemplate restTemplate = new RestTemplate(); + ResponseEntity response = restTemplate.exchange( + this.codegenie_base_url + "/token", + HttpMethod.POST, + entityToken, + String.class); + JSONObject jsonResponse = new JSONObject(response.getBody()); - ResponseEntity response = restTemplate.exchange( + List output_type = new ArrayList(); + output_type.add(this.codegenie_output_type); + + JSONObject requestBody = new JSONObject(); + requestBody.put("execution_id", this.scenarioExecution.getId()); + requestBody.put("docs_zip_file", this.codegenie_input); + requestBody.put("application", this.codegenie_application); + requestBody.put("project", this.codegenie_project); + requestBody.put("output_type", output_type); + requestBody.put("output_variable", this.codegenie_output_variable); + requestBody.put("model_provider", this.codegenie_model_provider); + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + headers.set("Authorization", + jsonResponse.get("token_type").toString() + " " + jsonResponse.get("access_token").toString()); + HttpEntity request = new HttpEntity<>(requestBody.toString(), headers); + + response = restTemplate.exchange( + this.codegenie_base_url + "/execute", + HttpMethod.POST, + request, + String.class); + + jsonResponse = new JSONObject(response.getBody()); + + /* + * response = restTemplate.exchange( + * //this.codegenie_base_url + "/execution_status/" + + * this.scenarioExecution.getId(), + * this.codegenie_base_url + "/execution_status/679752f85189eb5621b48e17", + * HttpMethod.GET, + * request, + * String.class); + * jsonResponse = new JSONObject(response.getBody()); + */ + + int maxTries = 500; + // Pool the status GET api until it return the SUCCESS or FAILED message + while (!jsonResponse.get("status").equals("DONE") && !jsonResponse.get("status").equals("ERROR") + && maxTries > 0) { + + response = restTemplate.exchange( this.codegenie_base_url + "/token", HttpMethod.POST, entityToken, String.class); - JSONObject jsonResponse = new JSONObject(response.getBody()); + jsonResponse = new JSONObject(response.getBody()); - List output_type = new ArrayList(); - output_type.add(this.codegenie_output_type); - - JSONObject requestBody = new JSONObject(); - requestBody.put("execution_id", this.scenarioExecution.getId()); - requestBody.put("docs_zip_file", this.codegenie_input); - requestBody.put("application", this.codegenie_application); - requestBody.put("project", this.codegenie_project); - requestBody.put("output_type", output_type); - requestBody.put("output_variable", this.codegenie_output_variable); - requestBody.put("model_provider", this.codegenie_model_provider); - - HttpHeaders headers = new HttpHeaders(); - headers.setContentType(MediaType.APPLICATION_JSON); headers.set("Authorization", jsonResponse.get("token_type").toString() + " " + jsonResponse.get("access_token").toString()); - HttpEntity request = new HttpEntity<>(requestBody.toString(), headers); - - - response = restTemplate.exchange( - this.codegenie_base_url + "/execute", - HttpMethod.POST, - request, - String.class); - - jsonResponse = new JSONObject(response.getBody()); - - /* response = restTemplate.exchange( - //this.codegenie_base_url + "/execution_status/" + this.scenarioExecution.getId(), - this.codegenie_base_url + "/execution_status/679752f85189eb5621b48e17", - HttpMethod.GET, - request, - String.class); - jsonResponse = new JSONObject(response.getBody());*/ - - int maxTries = 500; - // Pool the status GET api until it return the SUCCESS or FAILED message - while (!jsonResponse.get("status").equals("DONE") && !jsonResponse.get("status").equals("ERROR") - && maxTries > 0) { - - response = restTemplate.exchange( - this.codegenie_base_url + "/token", - HttpMethod.POST, - entityToken, - String.class); - jsonResponse = new JSONObject(response.getBody()); - - headers.set("Authorization", - jsonResponse.get("token_type").toString() + " " + jsonResponse.get("access_token").toString()); - request = new HttpEntity<>(requestBody.toString(), headers); - - response = restTemplate.exchange( - this.codegenie_base_url + "/execution_status/" + this.scenarioExecution.getId(), - //this.codegenie_base_url + "/execution_status/679752f85189eb5621b48e17", - HttpMethod.GET, - request, - String.class); - jsonResponse = new JSONObject(response.getBody()); - - try { - Thread.sleep(120000); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - logger.error("Thread was interrupted", e); - } - logger.info("Check status => Remaining tryes :" + maxTries); - logger.info("Percent: " + jsonResponse.get("percent")); - maxTries--; - - } - - logger.info("Stop pooling codegenies pod. Latest status = " + jsonResponse.get("status")); - - if (jsonResponse.get("status").equals("DONE")) { - - response = restTemplate.exchange( - this.codegenie_base_url + "/token", - HttpMethod.POST, - entityToken, - String.class); - jsonResponse = new JSONObject(response.getBody()); - - headers.set("Authorization", - jsonResponse.get("token_type").toString() + " " + jsonResponse.get("access_token").toString()); - request = new HttpEntity<>(requestBody.toString(), headers); - - response = restTemplate.exchange( - this.codegenie_base_url + "/execution_result/" +this.scenarioExecution.getId(), - //this.codegenie_base_url + "/execution_result/679752f85189eb5621b48e17", - HttpMethod.GET, - request, - String.class); - jsonResponse = new JSONObject(response.getBody()); - - if (jsonResponse.get("status").equals("DONE")) { - - // Accedi all'oggetto "outputs" - JSONObject outputs = jsonResponse.getJSONObject("outputs"); - - // Ottieni il valore del campo "json" - String jsonPath = ""; - - if (this.codegenie_output_type.equals("FILE")) { - - jsonPath = outputs.getString("file"); - - } else if (this.codegenie_output_type.equals("MARKDOWN")) { - jsonPath = outputs.getString("markdown"); - // this.scenarioExecution.getExecSharedMap().put(this.codegenie_output_variable, - // jsonResponse.get("OUTPUT").toString()); - } else if (this.codegenie_output_type.equals("JSON")) { - jsonPath = outputs.getString("json"); - // this.scenarioExecution.getExecSharedMap().put(this.codegenie_output_variable, - // jsonResponse.get("OUTPUT").toString()); - } - - response = restTemplate.exchange( - this.codegenie_base_url + "/token", - HttpMethod.POST, - entityToken, - String.class); - jsonResponse = new JSONObject(response.getBody()); - - headers.set("Authorization", - jsonResponse.get("token_type").toString() + " " + jsonResponse.get("access_token").toString()); - request = new HttpEntity<>(requestBody.toString(), headers); - - ResponseEntity responseFile = restTemplate.exchange( - this.codegenie_base_url + "/download/" + jsonPath, - HttpMethod.GET, - request, - byte[].class); - - // Salva i bytes in un file - byte[] fileBytes = responseFile.getBody(); - String fileBase64 = Base64.getEncoder().encodeToString(fileBytes); - - this.scenarioExecution.getExecSharedMap().put(this.codegenie_output_variable, - fileBase64); - this.scenarioExecution.setNextStepId(this.step.getNextStepId()); - this.scenarioExecution.getExecSharedMap().put("status", - "DONE"); - } else { - throw new Exception( - "codegenie execution failed with status: " + jsonResponse.get("status").toString()); - } - - } else { - logger.error("ERROR on pooling codegenies"); - - throw new Exception("codegenie execution failed with status: " + jsonResponse.get("status").toString()); - } + request = new HttpEntity<>(requestBody.toString(), headers); response = restTemplate.exchange( - this.codegenie_base_url + "/token", - HttpMethod.POST, - entityToken, - String.class); - jsonResponse = new JSONObject(response.getBody()); - - headers.set("Authorization", - jsonResponse.get("token_type").toString() + " " + jsonResponse.get("access_token").toString()); - request = new HttpEntity<>(requestBody.toString(), headers); - - response = restTemplate.exchange( - this.codegenie_base_url + "/execution_consumption/" + this.scenarioExecution.getId(), - //this.codegenie_base_url + "/execution_consumption/679752f85189eb5621b48e17", + this.codegenie_base_url + "/execution_status/" + this.scenarioExecution.getId(), + // this.codegenie_base_url + "/execution_status/679752f85189eb5621b48e17", HttpMethod.GET, request, String.class); jsonResponse = new JSONObject(response.getBody()); - this.scenarioExecution.getExecSharedMap().put("input_token", jsonResponse.get("input_token").toString()); - this.scenarioExecution.getExecSharedMap().put("output_token", jsonResponse.get("output_token").toString()); - this.scenarioExecution.getExecSharedMap().put("total_token", jsonResponse.get("total_token").toString()); - this.scenarioExecution.getExecSharedMap().put("used_model", jsonResponse.get("used_model").toString()); - // scenarioExecutionRepo.save(this.scenarioExecution); + try { + Thread.sleep(120000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + logger.error("Thread was interrupted", e); + } + logger.info("Check status => Remaining tryes :" + maxTries); + logger.info("Percent: " + jsonResponse.get("percent")); + maxTries--; - } catch (Exception e) { - logger.error("Error in codegenie execution: " + e.getMessage(), e); - this.scenarioExecution.getExecSharedMap().put("status", - "ERROR"); } + logger.info("Stop pooling codegenies pod. Latest status = " + jsonResponse.get("status")); + + if (jsonResponse.get("status").equals("DONE")) { + + response = restTemplate.exchange( + this.codegenie_base_url + "/token", + HttpMethod.POST, + entityToken, + String.class); + jsonResponse = new JSONObject(response.getBody()); + + headers.set("Authorization", + jsonResponse.get("token_type").toString() + " " + jsonResponse.get("access_token").toString()); + request = new HttpEntity<>(requestBody.toString(), headers); + + response = restTemplate.exchange( + this.codegenie_base_url + "/execution_result/" + this.scenarioExecution.getId(), + // this.codegenie_base_url + "/execution_result/679752f85189eb5621b48e17", + HttpMethod.GET, + request, + String.class); + jsonResponse = new JSONObject(response.getBody()); + + if (jsonResponse.get("status").equals("DONE")) { + + // Accedi all'oggetto "outputs" + JSONObject outputs = jsonResponse.getJSONObject("outputs"); + + // Ottieni il valore del campo "json" + String jsonPath = ""; + + if (this.codegenie_output_type.equals("FILE")) { + + jsonPath = outputs.getString("file"); + + } else if (this.codegenie_output_type.equals("MARKDOWN")) { + jsonPath = outputs.getString("markdown"); + // this.scenarioExecution.getExecSharedMap().put(this.codegenie_output_variable, + // jsonResponse.get("OUTPUT").toString()); + } else if (this.codegenie_output_type.equals("JSON")) { + jsonPath = outputs.getString("json"); + // this.scenarioExecution.getExecSharedMap().put(this.codegenie_output_variable, + // jsonResponse.get("OUTPUT").toString()); + } + + response = restTemplate.exchange( + this.codegenie_base_url + "/token", + HttpMethod.POST, + entityToken, + String.class); + jsonResponse = new JSONObject(response.getBody()); + + headers.set("Authorization", + jsonResponse.get("token_type").toString() + " " + jsonResponse.get("access_token").toString()); + request = new HttpEntity<>(requestBody.toString(), headers); + + ResponseEntity responseFile = restTemplate.exchange( + this.codegenie_base_url + "/download/" + jsonPath, + HttpMethod.GET, + request, + byte[].class); + + // Salva i bytes in un file + byte[] fileBytes = responseFile.getBody(); + String fileBase64 = Base64.getEncoder().encodeToString(fileBytes); + + this.scenarioExecution.getExecSharedMap().put(this.codegenie_output_variable, + fileBase64); + this.scenarioExecution.setNextStepId(this.step.getNextStepId()); + this.scenarioExecution.getExecSharedMap().put("status", + "DONE"); + } else { + + this.scenarioExecution.getExecSharedMap().put(this.codegenie_output_variable, + "Error while generating file"); + throw new Exception( + "codegenie execution failed with status: " + jsonResponse.get("status").toString()); + } + + } else { + logger.error("ERROR on pooling codegenies"); + this.scenarioExecution.getExecSharedMap().put(this.codegenie_output_variable, + "Error while generating file"); + + throw new Exception("codegenie execution failed with status: " + jsonResponse.get("status").toString()); + } + + response = restTemplate.exchange( + this.codegenie_base_url + "/token", + HttpMethod.POST, + entityToken, + String.class); + jsonResponse = new JSONObject(response.getBody()); + + headers.set("Authorization", + jsonResponse.get("token_type").toString() + " " + jsonResponse.get("access_token").toString()); + request = new HttpEntity<>(requestBody.toString(), headers); + + response = restTemplate.exchange( + this.codegenie_base_url + "/execution_consumption/" + this.scenarioExecution.getId(), + // this.codegenie_base_url + "/execution_consumption/679752f85189eb5621b48e17", + HttpMethod.GET, + request, + String.class); + jsonResponse = new JSONObject(response.getBody()); + this.scenarioExecution.getExecSharedMap().put("input_token", jsonResponse.get("input_token").toString()); + this.scenarioExecution.getExecSharedMap().put("output_token", jsonResponse.get("output_token").toString()); + this.scenarioExecution.getExecSharedMap().put("total_token", jsonResponse.get("total_token").toString()); + this.scenarioExecution.getExecSharedMap().put("used_model", jsonResponse.get("used_model").toString()); + + // scenarioExecutionRepo.save(this.scenarioExecution); + + this.scenarioExecution.setNextStepId(this.step.getNextStepId()); + return this.scenarioExecution; }