Skip to content

Commit

Permalink
Made code changes for sonar fixes (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
arpitg-1 authored Aug 26, 2024
1 parent 347c12f commit 2ca521c
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
public class LLMResponseAttributes implements Serializable {

private final TokenUsage tokenUsage;
private final HashMap<String, Object> additionalAttributes;
private final HashMap<String, String> additionalAttributes;

public LLMResponseAttributes(TokenUsage tokenUsage, HashMap<String, Object> additionalAttributes) {
public LLMResponseAttributes(TokenUsage tokenUsage, HashMap<String, String> additionalAttributes) {
this.tokenUsage = tokenUsage;
this.additionalAttributes = additionalAttributes;
}
Expand All @@ -18,7 +18,7 @@ public TokenUsage getTokenUsage() {
return tokenUsage;
}

public Map<String, Object> getAdditionalAttributes() {
public Map<String, String> getAdditionalAttributes() {
return additionalAttributes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ public class ScannedDocResponseAttributes implements Serializable {

private final ArrayList<DocResponseAttribute> scannedDocAttributes;

private final HashMap<String, Object> additionalAttributes;
private final HashMap<String, String> additionalAttributes;

public ScannedDocResponseAttributes(ArrayList<DocResponseAttribute> scannedDocAttributes,
HashMap<String, Object> additionalAttributes) {
HashMap<String, String> additionalAttributes) {
this.scannedDocAttributes = scannedDocAttributes;
this.additionalAttributes = additionalAttributes;
}

public HashMap<String, Object> getAdditionalAttributes() {
public HashMap<String, String> getAdditionalAttributes() {
return additionalAttributes;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.mule.runtime.extension.api.runtime.operation.Result;

import java.io.InputStream;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -21,7 +22,7 @@ private ResponseHelper() {}

public static Result<InputStream, LLMResponseAttributes> createLLMResponse(String response,
dev.langchain4j.service.Result<?> result,
Map<String, Object> responseAttributes) {
Map<String, String> responseAttributes) {
TokenUsage tokenUsage = result != null ? new TokenUsage(result.tokenUsage().inputTokenCount(),
result.tokenUsage().outputTokenCount(),
result.tokenUsage().totalTokenCount())
Expand All @@ -31,7 +32,7 @@ public static Result<InputStream, LLMResponseAttributes> createLLMResponse(Strin

public static Result<InputStream, LLMResponseAttributes> createLLMResponse(String response,
Response<?> result,
Map<String, Object> responseAttributes) {
Map<String, String> responseAttributes) {
TokenUsage tokenUsage = result != null ? new TokenUsage(result.tokenUsage().inputTokenCount(),
result.tokenUsage().outputTokenCount(),
result.tokenUsage().totalTokenCount())
Expand All @@ -41,9 +42,9 @@ public static Result<InputStream, LLMResponseAttributes> createLLMResponse(Strin

public static Result<InputStream, LLMResponseAttributes> createLLMResponse(String response,
TokenUsage tokenUsage,
Map<String, Object> responseAttributes) {
Map<String, String> responseAttributes) {
return Result.<InputStream, LLMResponseAttributes>builder()
.attributes(new LLMResponseAttributes(tokenUsage, (HashMap<String, Object>) responseAttributes))
.attributes(new LLMResponseAttributes(tokenUsage, (HashMap<String, String>) responseAttributes))
.attributesMediaType(org.mule.runtime.api.metadata.MediaType.APPLICATION_JAVA)
.output(toInputStream(response, StandardCharsets.UTF_8))
.mediaType(org.mule.runtime.api.metadata.MediaType.APPLICATION_JSON)
Expand All @@ -52,10 +53,10 @@ public static Result<InputStream, LLMResponseAttributes> createLLMResponse(Strin

public static Result<InputStream, ScannedDocResponseAttributes> createLLMResponse(String response,
List<ScannedDocResponseAttributes.DocResponseAttribute> docResponseAttributes,
Map<String, Object> responseAttributes) {
Map<String, String> responseAttributes) {
return Result.<InputStream, ScannedDocResponseAttributes>builder()
.attributes(new ScannedDocResponseAttributes((ArrayList<ScannedDocResponseAttributes.DocResponseAttribute>) docResponseAttributes,
(HashMap<String, Object>) responseAttributes))
(HashMap<String, String>) responseAttributes))
.attributesMediaType(org.mule.runtime.api.metadata.MediaType.APPLICATION_JAVA)
.output(toInputStream(response, StandardCharsets.UTF_8))
.mediaType(org.mule.runtime.api.metadata.MediaType.APPLICATION_JSON)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static org.mapdb.Serializer.STRING;

import java.io.InputStream;
import java.io.Serializable;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -161,7 +162,7 @@ public org.mule.runtime.extension.api.runtime.operation.Result<InputStream, LLMR
JSONObject jsonObject = new JSONObject();
jsonObject.put(MuleChainConstants.RESPONSE, answer.content());

Map<String, Object> attributes = new HashMap<>();
Map<String, String> attributes = new HashMap<>();
attributes.put(MuleChainConstants.FILE_PATH, contextPath);
attributes.put(MuleChainConstants.FILE_TYPE, fileType.getFileType());
attributes.put(MuleChainConstants.QUESTION, data);
Expand Down Expand Up @@ -245,10 +246,10 @@ public org.mule.runtime.extension.api.runtime.operation.Result<InputStream, LLMR
JSONObject jsonObject = new JSONObject();
jsonObject.put(MuleChainConstants.RESPONSE, response.content());

Map<String, Object> attributes = new HashMap<>();
Map<String, String> attributes = new HashMap<>();
attributes.put(MuleChainConstants.MEMORY_NAME, memoryName);
attributes.put(MuleChainConstants.DB_FILE_PATH, dbFilePath);
attributes.put(MuleChainConstants.MAX_MESSAGES, maxMessages);
attributes.put(MuleChainConstants.MAX_MESSAGES, String.valueOf(maxMessages));

return createLLMResponse(jsonObject.toString(), response, attributes);
} catch (Exception e) {
Expand Down Expand Up @@ -576,10 +577,10 @@ public org.mule.runtime.extension.api.runtime.operation.Result<InputStream, LLMR
JSONObject jsonObject = new JSONObject();
jsonObject.put(MuleChainConstants.RESPONSE, results.content());

Map<String, Object> attributes = new HashMap<>();
Map<String, String> attributes = new HashMap<>();
attributes.put(MuleChainConstants.STORE_NAME, storeName);
attributes.put(MuleChainConstants.QUESTION, data);
attributes.put(MuleChainConstants.GET_LATEST, getLatest);
attributes.put(MuleChainConstants.GET_LATEST, String.valueOf(getLatest));

JSONArray sources = new JSONArray();
String absoluteDirectoryPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public org.mule.runtime.extension.api.runtime.operation.Result<InputStream, LLMR
JSONObject jsonObject = new JSONObject();
jsonObject.put(MuleChainConstants.RESPONSE, response.content().text());

return createLLMResponse(jsonObject.toString(), response, null);
return createLLMResponse(jsonObject.toString(), response, new HashMap<>());
} catch (Exception e) {
throw new ModuleException(String.format("Unable to analyze the provided image %s with the text: %s", contextURL,
data),
Expand Down Expand Up @@ -195,8 +195,8 @@ public org.mule.runtime.extension.api.runtime.operation.Result<InputStream, Scan

jsonObject.put(MuleChainConstants.PAGES, docPages);

Map<String, Object> attributes = new HashMap<>();
attributes.put(MuleChainConstants.TOTAL_PAGES, totalPages);
Map<String, String> attributes = new HashMap<>();
attributes.put(MuleChainConstants.TOTAL_PAGES, String.valueOf(totalPages));

return createLLMResponse(jsonObject.toString(), docResponseAttributes,
attributes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public org.mule.runtime.extension.api.runtime.operation.Result<InputStream, LLMR
Result<String> answer = assistant.chat(prompt);
JSONObject jsonObject = new JSONObject();
jsonObject.put(MuleChainConstants.RESPONSE, answer.content());
return createLLMResponse(jsonObject.toString(), answer, null);
return createLLMResponse(jsonObject.toString(), answer, new HashMap<>());
} catch (Exception e) {
throw new ModuleException("Unable to respond with the chat provided", MuleChainErrorType.AI_SERVICES_FAILURE, e);
}
Expand Down Expand Up @@ -107,7 +107,7 @@ public org.mule.runtime.extension.api.runtime.operation.Result<InputStream, LLMR

JSONObject jsonObject = new JSONObject();
jsonObject.put(MuleChainConstants.RESPONSE, answer.content());
return createLLMResponse(jsonObject.toString(), answer, null);
return createLLMResponse(jsonObject.toString(), answer, new HashMap<>());
} catch (Exception e) {
throw new ModuleException("Unable to reply with the correct prompt template", MuleChainErrorType.AI_SERVICES_FAILURE, e);
}
Expand Down Expand Up @@ -155,8 +155,8 @@ public org.mule.runtime.extension.api.runtime.operation.Result<InputStream, LLMR
JSONObject jsonObject = new JSONObject();
jsonObject.put(MuleChainConstants.RESPONSE, sentiment.content());

Map<String, Object> attributes = new HashMap<>();
attributes.put(MuleChainConstants.IS_POSITIVE, positive);
Map<String, String> attributes = new HashMap<>();
attributes.put(MuleChainConstants.IS_POSITIVE, String.valueOf(positive));

return createLLMResponse(jsonObject.toString(), sentiment, attributes);
} catch (Exception e) {
Expand Down

0 comments on commit 2ca521c

Please sign in to comment.