Skip to content

Commit

Permalink
Fix Sonar and test coverage issues
Browse files Browse the repository at this point in the history
  • Loading branch information
sashirestela committed Oct 21, 2024
1 parent 87c578b commit d762043
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ public class ChatRequest {
@Range(min = 0, max = 20)
private Integer topLogprobs;

@Deprecated
/**
* @deprecated OpenAI has deperecated this field in favor of max_completion_tokens.
*/
@Deprecated(since = "3.9.0", forRemoval = true)
private Integer maxTokens;

private Integer maxCompletionTokens;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.sashirestela.openai.support;

import io.github.sashirestela.openai.SimpleUncheckedException;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -21,8 +23,7 @@ public static String encode(String filePath, MediaType mediaType) {
return prefix + base64String;
}
} catch (Exception e) {
e.printStackTrace();
return null;
throw new SimpleUncheckedException("Cannot encode from file {0}.", filePath, e);
}
}

Expand All @@ -32,7 +33,7 @@ public static void decode(String base64String, String filePath) {
File outputFile = new File(filePath);
Files.write(outputFile.toPath(), bytes);
} catch (Exception e) {
e.printStackTrace();
throw new SimpleUncheckedException("Cannot decode to file {0}.", filePath, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import io.github.sashirestela.openai.domain.chat.ChatMessage.UserMessage;
import io.github.sashirestela.openai.domain.chat.ChatRequest.Audio;
import io.github.sashirestela.openai.domain.chat.ChatRequest.Modality;
import io.github.sashirestela.openai.support.Base64Util;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -235,7 +234,7 @@ void testChatCompletionsCreateWithAudio() throws IOException {
var messages = new ArrayList<ChatMessage>();
messages.add(SystemMessage.of("Respond in a short and concise way."));
messages.add(UserMessage.of(List.of(ContentPartInputAudio.of(InputAudio.of(
Base64Util.encode("src/demo/resources/question1.mp3", null), InputAudioFormat.MP3)))));
"question_1_in_base64", InputAudioFormat.MP3)))));
var chatRequest = ChatRequest.builder()
.model("modelAudio")
.modality(Modality.TEXT)
Expand All @@ -245,13 +244,12 @@ void testChatCompletionsCreateWithAudio() throws IOException {
.build();
var chatResponse = openAI.chatCompletions().create(chatRequest).join();
var audio = chatResponse.firstMessage().getAudio();
Base64Util.decode(audio.getData(), "src/demo/resources/answer1.mp3");
System.out.println("Answer 1: " + audio.getTranscript());
assertNotNull(chatResponse);

messages.add(AssistantMessage.builder().audioId(audio.getId()).build());
messages.add(UserMessage.of(List.of(ContentPartInputAudio.of(InputAudio.of(
Base64Util.encode("src/demo/resources/question2.mp3", null), InputAudioFormat.MP3)))));
"question_2_in_base64", InputAudioFormat.MP3)))));
chatRequest = ChatRequest.builder()
.model("modelAudio")
.modality(Modality.TEXT)
Expand All @@ -261,7 +259,6 @@ void testChatCompletionsCreateWithAudio() throws IOException {
.build();
chatResponse = openAI.chatCompletions().create(chatRequest).join();
audio = chatResponse.firstMessage().getAudio();
Base64Util.decode(audio.getData(), "src/demo/resources/answer2.mp3");
System.out.println("Answer 2: " + audio.getTranscript());
assertNotNull(chatResponse);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.github.sashirestela.openai.support;

import io.github.sashirestela.openai.support.Base64Util.MediaType;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

class Base64UtilTest {

@Test
void testEncodeDecode() {
var filePath = "src/test/resources/audios_speak.mp3";
var base64StringWithMedia = Base64Util.encode(filePath, MediaType.AUDIO);
assertTrue(base64StringWithMedia.startsWith("data:"));
var base64String = Base64Util.encode(filePath, null);
assertFalse(base64String.startsWith("data:"));
assertDoesNotThrow(() -> Base64Util.decode(base64String, filePath));
}

@Test
void testEncodeException() {
var filePath = "src/test/resources/image_not_found.png";
assertThrows(Exception.class, () -> Base64Util.encode(filePath, MediaType.IMAGE));
}

@Test
void testDecodeException() {
var base64String = "ºªÇ";
var filePath = "src/test/resources/image_error.png";
assertThrows(Exception.class, () -> Base64Util.decode(base64String, filePath));
}

}

0 comments on commit d762043

Please sign in to comment.