Aggiungi SimpleDateTools e SourceCodeTool per gestire data/ora e recupero codice sorgente

This commit is contained in:
andrea.terzani
2025-02-21 16:17:55 +01:00
parent b4fa5025ca
commit 5e8c7bd88e
4 changed files with 133 additions and 9 deletions

View File

@@ -6,6 +6,10 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.olympus.hermione.dto.aientity.CiaOutputEntity;
import com.olympus.hermione.models.ScenarioExecution;
import com.olympus.hermione.utility.AttributeParser;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.client.ChatClient.CallResponseSpec;
import org.springframework.ai.chat.messages.Message;
@@ -21,7 +25,7 @@ public class AdvancedAIPromptSolver extends StepSolver {
private boolean qai_load_graph_schema=false;
private String qai_output_entityType;
private String qai_custom_memory_id;
private String qai_available_tools;
Logger logger = (Logger) LoggerFactory.getLogger(AdvancedAIPromptSolver.class);
@@ -48,7 +52,8 @@ public class AdvancedAIPromptSolver extends StepSolver {
this.qai_custom_memory_id = (String) this.step.getAttributes().get("qai_custom_memory_id");
//TODO: Add memory ID attribute to have the possibility of multiple conversations
this.qai_available_tools = (String) this.step.getAttributes().get("qai_available_tools");
}
@Override
@@ -59,19 +64,47 @@ public class AdvancedAIPromptSolver extends StepSolver {
this.scenarioExecution.setCurrentStepId(this.step.getStepId());
loadParameters();
/*
String userText = this.qai_user_input;
Object tools = null;
if(this.qai_available_tools!=null && !this.qai_available_tools.isEmpty()){
for(String tool: this.qai_available_tools.split(",")){
logger.info("Tool: " + tool);
if(tool.equals("SourceCodeTool")){
tools = new com.olympus.hermione.tools.SourceCodeTool(discoveryClient);
}
}
}
CallResponseSpec resp=null;
if(tools==null){
logger.info("No tools available");
resp = chatClient.prompt()
.user(this.qai_user_input)
.system(this.qai_system_prompt_template)
.advisors(advisor -> advisor
.param("chat_memory_conversation_id", this.scenarioExecution.getId()+this.qai_custom_memory_id)
.param("chat_memory_response_size", 100))
.call();
}else{
resp = chatClient.prompt()
.user(this.qai_user_input)
.system(this.qai_system_prompt_template)
.advisors(advisor -> advisor
.param("chat_memory_conversation_id", this.scenarioExecution.getId()+this.qai_custom_memory_id)
.param("chat_memory_response_size", 100))
.tools(tools)
.call();
}
Message userMessage = new UserMessage(userText);
Message systemMessage = new SystemMessage(this.qai_system_prompt_template);
*/
CallResponseSpec resp = chatClient.prompt()
.user(this.qai_user_input)
.system(this.qai_system_prompt_template)
//.messages(userMessage,systemMessage)
.advisors(advisor -> advisor
.param("chat_memory_conversation_id", this.scenarioExecution.getId()+this.qai_custom_memory_id)
.param("chat_memory_response_size", 100))
.tools(tools)
.call();
if(qai_output_entityType!=null && qai_output_entityType.equals("CiaOutputEntity")){

View File

@@ -57,9 +57,11 @@ public class BasicAIPromptSolver extends StepSolver {
Message userMessage = new UserMessage(this.qai_user_input);
Message systemMessage = new SystemMessage(this.qai_system_prompt_template);
CallResponseSpec resp = chatClient.prompt().user(this.qai_user_input)
CallResponseSpec resp = chatClient.prompt()
.user(this.qai_user_input)
.system(this.qai_system_prompt_template)
//.messages(userMessage,systemMessage)
.call();
String output = resp.content();

View File

@@ -0,0 +1,20 @@
package com.olympus.hermione.tools;
import java.time.LocalDateTime;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.context.i18n.LocaleContextHolder;
public class SimpleDateTools {
@Tool(description = "Get the current date and time in the user's timezone")
String getCurrentDateTime() {
System.out.println("ChatGPT sta cxhiedendo Getting current date and time");
return LocalDateTime.now().atZone(LocaleContextHolder.getTimeZone().toZoneId()).toString();
}
}

View File

@@ -0,0 +1,69 @@
package com.olympus.hermione.tools;
import java.time.LocalDateTime;
import java.util.logging.Logger;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.client.RestTemplate;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import com.olympus.dto.CodeRagResponse;
import com.olympus.dto.SimilaritySearchCodeInput;
public class SourceCodeTool {
private DiscoveryClient discoveryClient;
Logger logger = Logger.getLogger(SourceCodeTool.class.getName());
public SourceCodeTool(DiscoveryClient discoveryClient) {
this.discoveryClient = discoveryClient;
}
@Tool(description = "Get the source code of a class by fully qualified name")
public String getClassCodeByFullyQualifidClassName(String fullyQualifiedClassName) {
logger.info("[TOOL]LLM SourceCodeTool Getting source code for class: " + fullyQualifiedClassName);
ServiceInstance serviceInstance = discoveryClient.getInstances("source-code-module").get(0);
RestTemplate restTemplate = new RestTemplate();
String sourceCodeResponse =
restTemplate.getForEntity(serviceInstance.getUri() + "/getClassDetailedInfo?id="+fullyQualifiedClassName,String.class ).getBody();
return sourceCodeResponse;
}
@Tool(description = "Retrieve the source code that match concept with natural language query ")
public String getCodeBySimilarity(String query) {
logger.info("[TOOL]LLM SourceCodeTool Getting source code for class: " + query);
SimilaritySearchCodeInput similaritySearchCodeInput = new SimilaritySearchCodeInput();
similaritySearchCodeInput.setQuery(query);
similaritySearchCodeInput.setTopK("3");
similaritySearchCodeInput.setSimilarityThreshold("0.7");
similaritySearchCodeInput.setFilterExpression("");
ServiceInstance serviceInstance = discoveryClient.getInstances("source-code-module").get(0);
RestTemplate restTemplate = new RestTemplate();
CodeRagResponse[] ragresponse =
restTemplate.postForEntity(serviceInstance.getUri() + "/similarity-search-code",
similaritySearchCodeInput,CodeRagResponse[].class ).getBody();
String code="";
for (CodeRagResponse codeRagResponse : ragresponse) {
code += "SOURCE CODE OF "+codeRagResponse.getCodeType()+" : " + codeRagResponse.getFullyQualifiedName() +" \n";
code += codeRagResponse.getCode() + "\n";
code += "-----------------------------------\n";
}
return code;
}
}