Skip to content

Commit

Permalink
Add checkstyle rules (carlrobertoh#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
carlrobertoh authored Nov 16, 2023
1 parent fe613af commit c4115e2
Show file tree
Hide file tree
Showing 72 changed files with 1,129 additions and 564 deletions.
5 changes: 5 additions & 0 deletions buildSrc/src/main/kotlin/codegpt.java-conventions.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
fun properties(key: String) = project.findProperty(key).toString()

plugins {
checkstyle
id("java")
id("idea")
id("org.jetbrains.intellij")
Expand All @@ -17,6 +18,10 @@ intellij {
version.set(properties("platformVersion"))
}

checkstyle {
toolVersion = "10.12.5"
}

dependencies {
implementation("ee.carlrobert:llm-client:0.0.10")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public EmbeddingsService(OpenAIClient openAIClient, Path pluginBasePath) {
public List<double[]> getEmbeddings(List<String> chunks) {
return openAIClient.getEmbeddings(chunks);
}

public String buildPromptWithContext(String prompt) {
try {
var inputEmbedding = openAIClient.getEmbedding(getSearchQuery(prompt));
Expand All @@ -63,7 +63,9 @@ public String buildPromptWithContext(String prompt) {
}
}

public List<Item<Object, double[]>> createEmbeddings(List<CheckedFile> checkedFiles, @Nullable ProgressIndicator indicator) {
public List<Item<Object, double[]>> createEmbeddings(
List<CheckedFile> checkedFiles,
@Nullable ProgressIndicator indicator) {
var words = new ArrayList<Item<Object, double[]>>();
for (int i = 0; i < checkedFiles.size(); i++) {
try {
Expand All @@ -81,7 +83,10 @@ public List<Item<Object, double[]>> createEmbeddings(List<CheckedFile> checkedFi
}

private String getSearchQuery(String userPrompt) throws JsonProcessingException {
var message = new OpenAIChatCompletionMessage("user", getResourceContent("/prompts/text-generator.txt").replace("{prompt}", userPrompt));
var message = new OpenAIChatCompletionMessage(
"user",
getResourceContent("/prompts/text-generator.txt")
.replace("{prompt}", userPrompt));
var request = new OpenAIChatCompletionRequest.Builder(List.of(message))
.setModel(OpenAIChatCompletionModel.GPT_4)
.setMaxTokens(400)
Expand All @@ -100,16 +105,20 @@ private void addEmbeddings(CheckedFile checkedFile, List<Item<Object, double[]>>
var fileExtension = checkedFile.getFileExtension();
var codeSplitter = SplitterFactory.getCodeSplitter(fileExtension);
if (codeSplitter != null) {
var chunks = codeSplitter.split(checkedFile.getFileName(), checkedFile.getFileContent());
var chunks = codeSplitter.split(
checkedFile.getFileName(),
checkedFile.getFileContent());
var embeddings = openAIClient.getEmbeddings(chunks);
for (int i = 0; i < chunks.size(); i++) {
prevEmbeddings.add(new Word(chunks.get(i), checkedFile.getFileName(), normalize(embeddings.get(i))));
prevEmbeddings.add(
new Word(chunks.get(i), checkedFile.getFileName(), normalize(embeddings.get(i))));
}
} else {
var chunks = splitText(checkedFile.getFileContent(), 400);
var embeddings = getEmbeddings(chunks);
for (int i = 0; i < chunks.size(); i++) {
prevEmbeddings.add(new Word(chunks.get(i), checkedFile.getFileName(), normalize(embeddings.get(i))));
prevEmbeddings.add(
new Word(chunks.get(i), checkedFile.getFileName(), normalize(embeddings.get(i))));
}
}
}
Expand All @@ -125,7 +134,8 @@ private static List<String> splitText(String str, int chunkSize) {

// TODO: Move to shared module
private static String getResourceContent(String name) {
try (var stream = Objects.requireNonNull(EmbeddingsService.class.getResourceAsStream(name))) {
try (var stream =
Objects.requireNonNull(EmbeddingsService.class.getResourceAsStream(name))) {
return new String(stream.readAllBytes(), StandardCharsets.UTF_8);
} catch (IOException e) {
throw new RuntimeException("Unable to read resource", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ protected String parseContext(ParserRuleContext ctx) {
@Override
public List<String> split(String fileName, String content) {
chunks = new ArrayList<>();
ParseTreeWalker.DEFAULT.walk(getParseTreeListener(), getParseTree(CharStreams.fromString(content)));
ParseTreeWalker.DEFAULT.walk(
getParseTreeListener(),
getParseTree(CharStreams.fromString(content)));
return chunks;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ public HnswIndex<Object, double[], Word, Object> loadIndex(String path) throws I

public void save(List<Item<Object, double[]>> words) {
var hnswIndex = HnswIndex
.newBuilder(words.get(0).vector().length, DistanceFunctions.DOUBLE_COSINE_DISTANCE, words.size())
.newBuilder(
words.get(0).vector().length,
DistanceFunctions.DOUBLE_COSINE_DISTANCE,
words.size())
.build();
try {
hnswIndex.addAll(words);
Expand Down
8 changes: 4 additions & 4 deletions codegpt-core/src/main/java/ee/carlrobert/vector/Word.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public int dimensions() {

@Override
public String toString() {
return "Word{" +
"id='" + id + '\'' +
", vector=" + Arrays.toString(vector) +
'}';
return "Word{"
+ "id='" + id + '\''
+ ", vector=" + Arrays.toString(vector)
+ '}';
}

public String getMeta() {
Expand Down
Loading

0 comments on commit c4115e2

Please sign in to comment.