From 44208305716435bab5e834a3601009f0f1f8c978 Mon Sep 17 00:00:00 2001 From: Andrei Costescu Date: Sun, 22 Oct 2023 17:36:03 +0300 Subject: [PATCH] Implemented missing API for issue #366: No Retrieve file content api (#367) --- .../java/com/theokanning/openai/client/OpenAiApi.java | 4 ++++ .../com/theokanning/openai/service/OpenAiService.java | 4 ++++ .../java/com/theokanning/openai/service/FileTest.java | 10 ++++++++++ 3 files changed, 18 insertions(+) diff --git a/client/src/main/java/com/theokanning/openai/client/OpenAiApi.java b/client/src/main/java/com/theokanning/openai/client/OpenAiApi.java index 60ffdbf6..497dacd5 100644 --- a/client/src/main/java/com/theokanning/openai/client/OpenAiApi.java +++ b/client/src/main/java/com/theokanning/openai/client/OpenAiApi.java @@ -89,6 +89,10 @@ public interface OpenAiApi { @GET("/v1/files/{file_id}") Single retrieveFile(@Path("file_id") String fileId); + @Streaming + @GET("/v1/files/{file_id}/content") + Single retrieveFileContent(@Path("file_id") String fileId); + @POST("/v1/fine_tuning/jobs") Single createFineTuningJob(@Body FineTuningJobRequest request); diff --git a/service/src/main/java/com/theokanning/openai/service/OpenAiService.java b/service/src/main/java/com/theokanning/openai/service/OpenAiService.java index 7114531b..0296c15c 100644 --- a/service/src/main/java/com/theokanning/openai/service/OpenAiService.java +++ b/service/src/main/java/com/theokanning/openai/service/OpenAiService.java @@ -173,6 +173,10 @@ public File retrieveFile(String fileId) { return execute(api.retrieveFile(fileId)); } + public ResponseBody retrieveFileContent(String fileId) { + return execute(api.retrieveFileContent(fileId)); + } + public FineTuningJob createFineTuningJob(FineTuningJobRequest request) { return execute(api.createFineTuningJob(request)); } diff --git a/service/src/test/java/com/theokanning/openai/service/FileTest.java b/service/src/test/java/com/theokanning/openai/service/FileTest.java index 2b51aee3..74fe2084 100644 --- a/service/src/test/java/com/theokanning/openai/service/FileTest.java +++ b/service/src/test/java/com/theokanning/openai/service/FileTest.java @@ -7,6 +7,9 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestMethodOrder; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.List; import java.util.concurrent.TimeUnit; @@ -52,6 +55,13 @@ void retrieveFile() { @Test @Order(4) + void retrieveFileContent() throws IOException { + String fileBytesToString = service.retrieveFileContent(fileId).string(); + assertEquals(Files.readString(Path.of(filePath)), fileBytesToString); + } + + @Test + @Order(5) void deleteFile() { DeleteResult result = service.deleteFile(fileId); assertTrue(result.isDeleted());