Skip to content

Commit

Permalink
Fix sonar lint (#73)
Browse files Browse the repository at this point in the history
* Fix sonar lint

* Adding more linter fix

* Adding more fix

* Adding sonar lint fix to gitignore

* Update .gitignore

* Adding example utils for example

* Remove e.printStackTrace from example
  • Loading branch information
li2109 authored May 21, 2023
1 parent cf68e10 commit 7fb4eb8
Show file tree
Hide file tree
Showing 76 changed files with 434 additions and 269 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ build/
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
.idea/sonarlint/
.idea/workspace.xml
*.iws
*.iml
Expand Down
20 changes: 20 additions & 0 deletions .idea/sonarlint.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 16 additions & 4 deletions .idea/sonarlint/issuestore/index.pb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file.
Empty file.
Empty file.
Empty file.
20 changes: 16 additions & 4 deletions .idea/sonarlint/securityhotspotstore/index.pb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ai.knowly.langtorch.capability.local.v1;

import java.io.IOException;

public class LocalCapabilityReadException extends RuntimeException {
public LocalCapabilityReadException(IOException e) {
super(e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.gson.Gson;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import javax.inject.Inject;
import org.apache.commons.io.IOUtils;

Expand Down Expand Up @@ -33,12 +34,12 @@ public PromptTemplate getPromptTemplate() {
}

private String readFile(String folderPath, TARGET target) {
String path =
folderPath + "/" + (target == TARGET.CONFIG ? CONFIG_FILE_NAME : PROMPT_FILE_NAME);
String fileName = target == TARGET.CONFIG ? CONFIG_FILE_NAME : PROMPT_FILE_NAME;
String path = String.format("%s/%s", folderPath, fileName);
try (FileInputStream inputStream = new FileInputStream(path)) {
return IOUtils.toString(inputStream);
return IOUtils.toString(inputStream, Charset.defaultCharset());
} catch (IOException e) {
throw new RuntimeException(e);
throw new LocalCapabilityReadException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public ChatCompletionLLMCapability(
}

public static <I, O> ChatCompletionLLMCapability<I, O> of(
Processor<MultiChatMessage, ChatMessage> processor, Class<O> outputClass) {
Processor<MultiChatMessage, ChatMessage> processor) {
return new ChatCompletionLLMCapability<>(processor, Optional.empty(), Optional.empty());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ai.knowly.langtorch.capability.module.openai;

public class PromptTemplateNotSetException extends RuntimeException {
public PromptTemplateNotSetException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import ai.knowly.langtorch.prompt.template.PromptTemplate;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ExecutionException;

/**
* A capability unit that contains a prompt template and accepts a map of variable-value pairs to
Expand Down Expand Up @@ -43,10 +42,9 @@ public PromptTemplateTextCapability withPromptTemplate(PromptTemplate promptTemp
return this;
}

public String run(Map<String, String> variableMap)
throws ExecutionException, InterruptedException {
public String run(Map<String, String> variableMap) {
if (!promptTemplate.isPresent()) {
throw new RuntimeException("Prompt template is not set");
throw new PromptTemplateNotSetException("Prompt template is not set");
}
return super.run(
promptTemplate.get().toBuilder().addAllVariableValuePairs(variableMap).build().format());
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/ai/knowly/langtorch/example/ExampleUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ai.knowly.langtorch.example;

import ai.knowly.langtorch.capability.Capability;
import com.google.common.flogger.FluentLogger;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ExampleUtils {
private ExampleUtils() {}

static void readInputUntilEXIT(FluentLogger logger, Capability<String, String> capability)
throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String input;
final String sentinel = "EXIT"; // Define a sentinel value to exit the loop
logger.atInfo().log("Type '%s' and press Enter to exit the application.%n", sentinel);

while (true) {
input = reader.readLine();

if (input == null || sentinel.equalsIgnoreCase(input)) {
break; // Exit the loop if the user types the sentinel value
}

logger.atInfo().log("User: " + input);
String assistantMsg = capability.run(input);
logger.atInfo().log("Assistant: " + assistantMsg);
}

logger.atInfo().log("Exiting the application.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
import ai.knowly.langtorch.capability.module.openai.SimpleTextCapability;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.flogger.FluentLogger;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

public class SequentialChain {
public static void main(String[] args) throws ExecutionException, InterruptedException {
FluentLogger logger = FluentLogger.forEnclosingClass();

SimpleTextCapability unit = SimpleTextCapability.create();

// Graph:
Expand All @@ -26,7 +29,7 @@ public static void main(String[] args) throws ExecutionException, InterruptedExc
Map<String, Object> result =
capabilityGraph.process(ImmutableMap.of("A", "Search engine solution provider"));
String slogan = (String) result.get("B");
System.out.println(slogan);
logger.atInfo().log("Slogan: %s", slogan);
}

private static class CompanyNameGenerator implements NodeAdapter<String, String> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,18 @@
package ai.knowly.langtorch.example;

import static ai.knowly.langtorch.example.ExampleUtils.readInputUntilEXIT;

import ai.knowly.langtorch.capability.module.openai.SimpleChatCapability;
import java.io.BufferedReader;
import com.google.common.flogger.FluentLogger;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.concurrent.ExecutionException;

public class SimpleChatBotWithoutExplicitKey {
public static void main(String[] args) throws ExecutionException, InterruptedException {
private static final FluentLogger logger = FluentLogger.forEnclosingClass();

public static void main(String[] args) throws IOException {
// Reading the key from the environment variable under Resource folder(.env file, OPENAI_API_KEY
// field)
SimpleChatCapability chatBot = SimpleChatCapability.create();
readInputUntilEXIT(chatBot);
}

private static void readInputUntilEXIT(SimpleChatCapability chatBot)
throws ExecutionException, InterruptedException {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
String input;
final String sentinel = "EXIT"; // Define a sentinel value to exit the loop
System.out.printf("Type '%s' and press Enter to exit the application.\n", sentinel);

while (true) {
input = reader.readLine();

if (input == null || sentinel.equalsIgnoreCase(input)) {
break; // Exit the loop if the user types the sentinel value
}

System.out.println("User: " + input);
String assistantMsg = chatBot.run(input);
System.out.println("Assistant: " + assistantMsg);
}
} catch (IOException e) {
e.printStackTrace();
}

System.out.println("Exiting the application.");
readInputUntilEXIT(logger, chatBot);
}
}
Loading

0 comments on commit 7fb4eb8

Please sign in to comment.