Skip to content

Commit

Permalink
Merge pull request #57 from sashirestela/56-automate-code-formatting
Browse files Browse the repository at this point in the history
Automate code formatting
  • Loading branch information
sashirestela authored Mar 1, 2024
2 parents f711e1c + 6e3e501 commit 95c10d5
Show file tree
Hide file tree
Showing 148 changed files with 1,245 additions and 719 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ name: Java CI with Maven

on:
push:
branches: ["main", "increase-testing-coverage"]
branches: ["main"]
pull_request:
branches: ["main"]

Expand All @@ -14,13 +14,18 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Set up JDK 11
uses: actions/setup-java@v3
with:
java-version: "11"
distribution: "temurin"
cache: maven
- name: Check code format with Spotless
run: mvn spotless:check
- name: Build with Maven
run: mvn -B package --file pom.xml
- name: Upload coverage reports to Codecov
Expand Down
6 changes: 6 additions & 0 deletions codestyle/spotless_java.importorder
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#Organize Import Order
#Mon Feb 26 16:38:48 PET 2024
0=
1=javax
2=java
3=\#
399 changes: 399 additions & 0 deletions codestyle/spotless_java_eclipseformat.xml

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<javadoc.version>3.6.3</javadoc.version>
<gpg.version>3.1.0</gpg.version>
<sonatype.version>1.6.13</sonatype.version>
<spotless.version>2.43.0</spotless.version>
</properties>

<profiles>
Expand Down Expand Up @@ -375,6 +376,32 @@
</ruleSet>
</configuration>
</plugin>
<!-- https://github.com/diffplug/spotless/blob/main/plugin-maven/README.md -->
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>${spotless.version}</version>
<configuration>
<ratchetFrom>origin/main</ratchetFrom>
<java>
<toggleOffOn />
<includes>
<include>src/*/java/**/*.java</include>
</includes>
<cleanthat />
<eclipse>
<file>${project.basedir}/codestyle/spotless_java_eclipseformat.xml</file>
</eclipse>
<importOrder>
<wildcardsLast>false</wildcardsLast>
<file>${project.basedir}/codestyle/spotless_java.importorder</file>
</importOrder>
<removeUnusedImports>
<engine>cleanthat-javaparser-unnecessaryimport</engine>
</removeUnusedImports>
</java>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import io.github.sashirestela.openai.BaseSimpleOpenAI;
import io.github.sashirestela.openai.SimpleOpenAI;
import lombok.NonNull;

import java.util.ArrayList;
import java.util.List;
import lombok.NonNull;

public abstract class AbstractDemo {

Expand Down Expand Up @@ -49,16 +50,21 @@ public void run() {

@FunctionalInterface
static interface Action {

void execute();

}

static class TitleAction {

private String title;
private Action action;

public TitleAction(String title, Action action) {
this.title = title;
this.action = action;
}

}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public void demoRetrieveAndModifyAssistant() {
var assistant = openAI.assistants().getOne(assistantId).join();
var assistantRequest = assistant.mutate()
.name("Math Expert")
.instructions("You are a personal math expert. When asked a question, write and run Python code to answer the question.")
.instructions(
"You are a personal math expert. When asked a question, write and run Python code to answer the question.")
.tool(AssistantTool.CODE_INTERPRETER)
.build();

Expand All @@ -48,9 +49,10 @@ public void demoRetrieveAndModifyAssistant() {

public void demoListAssistants() {
AtomicInteger count = new AtomicInteger();
openAI.assistants().getList()
openAI.assistants()
.getList()
.join()
.forEach(r -> System.out.println("\n#"+count.incrementAndGet()+"\n" + r));
.forEach(r -> System.out.println("\n#" + count.incrementAndGet() + "\n" + r));
}

public void demoUploadAssistantFile() {
Expand All @@ -68,7 +70,8 @@ public void demoCreateThread() {
var threadRequest = ThreadRequest.builder()
.message(ThreadMessageRequest.builder()
.role("user")
.content("Inspect the content of the attached text file. After that plot graph of the formula requested in it.")
.content(
"Inspect the content of the attached text file. After that plot graph of the formula requested in it.")
.build())
.build();

Expand All @@ -94,18 +97,22 @@ public void demoRunThreadAndWaitUntilComplete() {
public void demoGetAssistantMessages() {
List<ThreadMessage> messages = openAI.threads().getMessageList(threadId).join();
ThreadMessage assistant = messages.get(0);
ImageFileContent assistantImageContent = assistant.getContent().stream()
ImageFileContent assistantImageContent = assistant.getContent()
.stream()
.filter(ImageFileContent.class::isInstance)
.map(ImageFileContent.class::cast)
.findFirst().orElse(null);
.findFirst()
.orElse(null);

System.out.println("All messages:");
System.out.println("=============");
System.out.println(messages);

if (assistantImageContent != null) {
System.out.println("\nAssistant answer contains an image. Downloading it now...");
try (var in = openAI.files().getContentInputStream(assistantImageContent.getImageFile().getFileId()).join()) {
try (var in = openAI.files()
.getContentInputStream(assistantImageContent.getImageFile().getFileId())
.join()) {
Path tempFile = Files.createTempFile("code_interpreter", ".png");
Files.write(tempFile, in.readAllBytes());
System.out.println("Image file downloaded to: " + tempFile.toUri());
Expand Down Expand Up @@ -141,4 +148,5 @@ public static void main(String[] args) {

demo.run();
}
}

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package io.github.sashirestela.openai.demo;

import java.io.FileOutputStream;
import java.nio.file.Paths;

import io.github.sashirestela.openai.domain.audio.AudioRespFmt;
import io.github.sashirestela.openai.domain.audio.AudioSpeechRequest;
import io.github.sashirestela.openai.domain.audio.AudioTranscribeRequest;
import io.github.sashirestela.openai.domain.audio.AudioTranslateRequest;
import io.github.sashirestela.openai.domain.audio.SpeechRespFmt;
import io.github.sashirestela.openai.domain.audio.Voice;

import java.io.FileOutputStream;
import java.nio.file.Paths;

public class AudioServiceDemo extends AbstractDemo {

private static final String MODEL_TTS = "tts-1";
private static final String MODEL = "whisper-1";

Expand Down Expand Up @@ -95,4 +96,5 @@ public static void main(String[] args) {

demo.run();
}
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.github.sashirestela.openai.demo;

import java.util.ArrayList;

import io.github.sashirestela.openai.SimpleOpenAIAnyscale;
import io.github.sashirestela.openai.demo.ChatServiceDemo.Product;
import io.github.sashirestela.openai.demo.ChatServiceDemo.RunAlarm;
Expand All @@ -15,6 +13,8 @@
import io.github.sashirestela.openai.domain.chat.tool.ChatFunction;
import io.github.sashirestela.openai.function.FunctionExecutor;

import java.util.ArrayList;

public class ChatAnyscaleServiceDemo extends AbstractDemo {

public static final String MODEL = "mistralai/Mixtral-8x7B-Instruct-v0.1";
Expand All @@ -26,8 +26,7 @@ public ChatAnyscaleServiceDemo(String apiKey, String model) {
chatRequest = ChatRequest.builder()
.model(model)
.message(new ChatMsgSystem("You are an expert in AI."))
.message(
new ChatMsgUser("Write a technical article about ChatGPT, no more than 100 words."))
.message(new ChatMsgUser("Write a technical article about ChatGPT, no more than 100 words."))
.temperature(0.0)
.maxTokens(300)
.build();
Expand Down Expand Up @@ -103,4 +102,5 @@ public static void main(String[] args) {

demo.run();
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
package io.github.sashirestela.openai.demo;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

import io.github.sashirestela.openai.SimpleOpenAIAzure;
import io.github.sashirestela.openai.demo.ChatServiceDemo.Product;
import io.github.sashirestela.openai.demo.ChatServiceDemo.RunAlarm;
Expand All @@ -23,7 +16,15 @@
import io.github.sashirestela.openai.domain.chat.tool.ChatFunction;
import io.github.sashirestela.openai.function.FunctionExecutor;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

public class ChatAzureServiceDemo extends AbstractDemo {

private ChatRequest chatRequest;

public ChatAzureServiceDemo(String baseUrl, String apiKey, String apiVersion) {
Expand All @@ -35,8 +36,7 @@ public ChatAzureServiceDemo(String baseUrl, String apiKey, String apiVersion) {
chatRequest = ChatRequest.builder()
.model("N/A")
.message(new ChatMsgSystem("You are an expert in AI."))
.message(
new ChatMsgUser("Write a technical article about ChatGPT, no more than 100 words."))
.message(new ChatMsgUser("Write a technical article about ChatGPT, no more than 100 words."))
.temperature(0.0)
.maxTokens(300)
.build();
Expand Down Expand Up @@ -143,7 +143,7 @@ private static ImageUrl loadImageAsBase64(String imagePath) {
Path path = Paths.get(imagePath);
byte[] imageBytes = Files.readAllBytes(path);
String base64String = Base64.getEncoder().encodeToString(imageBytes);
var extension = imagePath.substring(imagePath.lastIndexOf(".") + 1);
var extension = imagePath.substring(imagePath.lastIndexOf('.') + 1);
var prefix = "data:image/" + extension + ";base64,";
return new ImageUrl(prefix + base64String);
} catch (Exception e) {
Expand All @@ -170,4 +170,5 @@ public static void main(String[] args) {

demo.run();
}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
package io.github.sashirestela.openai.demo;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;

import io.github.sashirestela.openai.domain.chat.ChatRequest;
import io.github.sashirestela.openai.domain.chat.ChatResponse;
import io.github.sashirestela.openai.domain.chat.content.ContentPartImage;
Expand All @@ -23,6 +15,13 @@
import io.github.sashirestela.openai.function.FunctionExecutor;
import io.github.sashirestela.openai.function.Functional;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

public class ChatServiceDemo extends AbstractDemo {

private ChatRequest chatRequest;
Expand Down Expand Up @@ -140,7 +139,7 @@ private static ImageUrl loadImageAsBase64(String imagePath) {
Path path = Paths.get(imagePath);
byte[] imageBytes = Files.readAllBytes(path);
String base64String = Base64.getEncoder().encodeToString(imageBytes);
var extension = imagePath.substring(imagePath.lastIndexOf(".") + 1);
var extension = imagePath.substring(imagePath.lastIndexOf('.') + 1);
var prefix = "data:image/" + extension + ";base64,";
return new ImageUrl(prefix + base64String);
} catch (Exception e) {
Expand All @@ -150,6 +149,7 @@ private static ImageUrl loadImageAsBase64(String imagePath) {
}

public static class Weather implements Functional {

@JsonPropertyDescription("City and state, for example: León, Guanajuato")
public String location;

Expand All @@ -161,9 +161,11 @@ public static class Weather implements Functional {
public Object execute() {
return Math.random() * 45;
}

}

public static class Product implements Functional {

@JsonPropertyDescription("The multiplicand part of a product")
@JsonProperty(required = true)
public double multiplicand;
Expand All @@ -176,13 +178,16 @@ public static class Product implements Functional {
public Object execute() {
return multiplicand * multiplier;
}

}

public static class RunAlarm implements Functional {

@Override
public Object execute() {
return "DONE";
}

}

public static void main(String[] args) {
Expand All @@ -196,4 +201,5 @@ public static void main(String[] args) {

demo.run();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ public static void main(String[] args) {

demo.run();
}
}

}
Loading

0 comments on commit 95c10d5

Please sign in to comment.