Refactor DocumentationTool for improved logging and search request handling
This commit is contained in:
@@ -441,18 +441,12 @@ public class ScenarioExecutionService {
|
||||
AzureOpenAiChatOptions.Builder optionsBuilder = AzureOpenAiChatOptions.builder()
|
||||
.deploymentName(aiModel.getModel());
|
||||
|
||||
// Aggiungi maxTokens solo se non è nullo
|
||||
if (aiModel.getMaxTokens() != null) {
|
||||
if (aiModel.isLegacy() ) {
|
||||
logger.info("Using legacy Azure OpenAI model: " + aiModel.getModel());
|
||||
optionsBuilder.temperature(aiModel.getTemperature().doubleValue());
|
||||
optionsBuilder.maxTokens(aiModel.getMaxTokens());
|
||||
logger.info("Setting maxTokens: " + aiModel.getMaxTokens());
|
||||
}
|
||||
|
||||
// Aggiungi temperature solo se non è nullo
|
||||
if (aiModel.getTemperature() != null) {
|
||||
optionsBuilder.temperature(Double.valueOf(aiModel.getTemperature()));
|
||||
logger.info("Setting temperature: " + aiModel.getTemperature());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
AzureOpenAiChatOptions openAIChatOptions = optionsBuilder.build();
|
||||
|
||||
|
||||
|
||||
@@ -13,52 +13,107 @@ import org.springframework.ai.vectorstore.VectorStore;
|
||||
|
||||
public class DocumentationTool {
|
||||
|
||||
Logger logger = Logger.getLogger(DocumentationTool.class.getName());
|
||||
|
||||
private String application;
|
||||
private String project;
|
||||
private VectorStore vectorStore;
|
||||
private static final Logger logger = Logger.getLogger(DocumentationTool.class.getName());
|
||||
private static final int DEFAULT_TOP_K = 5;
|
||||
private static final double DEFAULT_SIMILARITY_THRESHOLD = 0.7;
|
||||
|
||||
private final String application;
|
||||
private final String project;
|
||||
private final VectorStore vectorStore;
|
||||
|
||||
/**
|
||||
* Costruttore per DocumentationTool.
|
||||
*
|
||||
* @param application Nome dell'applicazione
|
||||
* @param project Nome del progetto
|
||||
* @param vectorStore Vector store contenente i documenti
|
||||
*/
|
||||
public DocumentationTool(String application, String project, VectorStore vectorStore) {
|
||||
this.application = application;
|
||||
this.project = project;
|
||||
this.vectorStore = vectorStore;
|
||||
}
|
||||
|
||||
@Tool(description = "Find any functional information about the application. Can be used to answeer any questron with a specific focus on the application")
|
||||
/**
|
||||
* @param query The user's question or information request regarding the application.
|
||||
* @return A formatted response containing the most relevant documentation excerpts.
|
||||
*
|
||||
* <p>
|
||||
* This method is intended for integration with conversational AI systems (such as ChatGPT),
|
||||
* enabling them to provide accurate and context-aware answers about the application's functionality.
|
||||
* </p>
|
||||
*/
|
||||
@Tool(description = "Retrieves functional information about the application based on the provided query.\r\n" + //
|
||||
" * It performs a semantic search over the application's documentation and returns the most relevant results.")
|
||||
public String getDocumentation(String query) {
|
||||
logRequestDetails(query);
|
||||
|
||||
String filterExpression = buildFilterExpression();
|
||||
logger.info("[TOOL]LLM DocumentationTool Getting documentation for filterExpression: " + filterExpression);
|
||||
|
||||
SearchRequest request = buildSearchRequest(query, filterExpression);
|
||||
List<Document> docs = this.vectorStore.similaritySearch(request);
|
||||
|
||||
logger.info("Number of VDB retrieved documents: " + docs.size());
|
||||
|
||||
return formatDocumentsResponse(docs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registra i dettagli della richiesta nei log.
|
||||
*
|
||||
* @param query La query di ricerca
|
||||
*/
|
||||
private void logRequestDetails(String query) {
|
||||
logger.info("[TOOL]LLM DocumentationTool Getting documentation for query: " + query);
|
||||
logger.info("[TOOL]LLM DocumentationTool Getting documentation for project: " + this.project);
|
||||
logger.info("[TOOL]LLM DocumentationTool Getting documentation for vectorStore: " + this.vectorStore);
|
||||
String filterExpression = "'KsProjectName' == '"+ this.project +"' AND 'KsDoctype' == 'functional'";
|
||||
|
||||
logger.info("[TOOL]LLM DocumentationTool Getting documentation for filterExpression: " + filterExpression);
|
||||
|
||||
|
||||
SearchRequest request = SearchRequest.builder()
|
||||
.query(query)
|
||||
.topK(3)
|
||||
.similarityThreshold(0.7)
|
||||
.filterExpression( filterExpression).build();
|
||||
|
||||
|
||||
List<Document> docs = this.vectorStore.similaritySearch(request);
|
||||
|
||||
logger.info("Number of VDB retrieved documents: " + docs.size());
|
||||
|
||||
String docToolResponse = "";
|
||||
for (Document doc : docs) {
|
||||
docToolResponse += "-------USE THIS AS PART OF YOUR CONTEXT----------------------------------------- \n";
|
||||
|
||||
docToolResponse += doc.getText() + "------------------------------------------------------------------------------------------ \n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Costruisce l'espressione di filtro per la ricerca.
|
||||
*
|
||||
* @return L'espressione di filtro formattata
|
||||
*/
|
||||
private String buildFilterExpression() {
|
||||
return "'KsProjectName' == '"+ this.project +"' AND 'KsDoctype' == 'functional'";
|
||||
}
|
||||
|
||||
/**
|
||||
* Costruisce la richiesta di ricerca.
|
||||
*
|
||||
* @param query La query di ricerca
|
||||
* @param filterExpression L'espressione di filtro
|
||||
* @return La richiesta di ricerca configurata
|
||||
*/
|
||||
private SearchRequest buildSearchRequest(String query, String filterExpression) {
|
||||
return SearchRequest.builder()
|
||||
.query(query)
|
||||
.topK(DEFAULT_TOP_K)
|
||||
.similarityThreshold(DEFAULT_SIMILARITY_THRESHOLD)
|
||||
.filterExpression(filterExpression)
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Formatta i documenti recuperati in una risposta leggibile.
|
||||
*
|
||||
* @param docs Lista di documenti recuperati
|
||||
* @return Risposta formattata contenente i testi dei documenti
|
||||
*/
|
||||
private String formatDocumentsResponse(List<Document> docs) {
|
||||
if (docs.isEmpty()) {
|
||||
return "Nessun documento trovato per la query.";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
return docToolResponse;
|
||||
|
||||
StringBuilder response = new StringBuilder();
|
||||
for (Document doc : docs) {
|
||||
response.append("-------USE THIS AS PART OF YOUR CONTEXT-----------------------------------------\n")
|
||||
.append(doc.getText())
|
||||
.append("------------------------------------------------------------------------------------------\n");
|
||||
}
|
||||
|
||||
return response.toString();
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user