From b6006e696f264c69af5c4468e38d74125196a035 Mon Sep 17 00:00:00 2001 From: Remy Ohajinwa Date: Sat, 11 Nov 2023 09:19:25 +0000 Subject: [PATCH 01/11] #390 Create Assistant #390 Create Assistant --- .../openai/assistants/Assistant.java | 24 ++++++++ .../openai/assistants/AssistantBase.java | 55 +++++++++++++++++++ .../theokanning/openai/assistants/Tool.java | 12 ++++ .../openai/utils/TikTokensUtil.java | 7 ++- .../theokanning/openai/client/OpenAiApi.java | 7 +++ .../openai/service/OpenAiService.java | 6 ++ .../openai/service/AssistantTest.java | 41 ++++++++++++++ .../openai/service/ModerationTest.java | 2 +- 8 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 api/src/main/java/com/theokanning/openai/assistants/Assistant.java create mode 100644 api/src/main/java/com/theokanning/openai/assistants/AssistantBase.java create mode 100644 api/src/main/java/com/theokanning/openai/assistants/Tool.java create mode 100644 service/src/test/java/com/theokanning/openai/service/AssistantTest.java diff --git a/api/src/main/java/com/theokanning/openai/assistants/Assistant.java b/api/src/main/java/com/theokanning/openai/assistants/Assistant.java new file mode 100644 index 00000000..111ef169 --- /dev/null +++ b/api/src/main/java/com/theokanning/openai/assistants/Assistant.java @@ -0,0 +1,24 @@ +package com.theokanning.openai.assistants; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; + +@Data +public class Assistant extends AssistantBase { + + /** + * The identifier, which can be referenced in API endpoints. + */ + String id; + + /** + * The object type which is always 'assistant' + */ + String object; + + /** + * The Unix timestamp(in seconds) for when the assistant was created + */ + @JsonProperty("created_at") + Integer createdAt; +} diff --git a/api/src/main/java/com/theokanning/openai/assistants/AssistantBase.java b/api/src/main/java/com/theokanning/openai/assistants/AssistantBase.java new file mode 100644 index 00000000..d771bd30 --- /dev/null +++ b/api/src/main/java/com/theokanning/openai/assistants/AssistantBase.java @@ -0,0 +1,55 @@ +package com.theokanning.openai.assistants; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.NonNull; + +import java.util.List; +import java.util.Map; + +@Builder +@NoArgsConstructor +@AllArgsConstructor +@Data +public class AssistantBase { + + /** + * ID of the model to use + */ + @NonNull + String model; + + /** + * The name of the assistant. The maximum length is 256 + */ + String name; + + /** + * The description of the assistant. + */ + String description; + + /** + * The system instructions that the assistant uses. + */ + String instructions; + + /** + * A list of tools enabled on the assistant. + */ + List tools; + + /** + * A list of file IDs attached to this assistant. + */ + @JsonProperty("file_ids") + List fields; + + /** + * Set of 16 key-value pairs that can be attached to an object. + */ + Map metadata; +} diff --git a/api/src/main/java/com/theokanning/openai/assistants/Tool.java b/api/src/main/java/com/theokanning/openai/assistants/Tool.java new file mode 100644 index 00000000..00027d72 --- /dev/null +++ b/api/src/main/java/com/theokanning/openai/assistants/Tool.java @@ -0,0 +1,12 @@ +package com.theokanning.openai.assistants; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@NoArgsConstructor +@AllArgsConstructor +@Data +public class Tool { + AssistantToolsEnum type; +} diff --git a/api/src/main/java/com/theokanning/openai/utils/TikTokensUtil.java b/api/src/main/java/com/theokanning/openai/utils/TikTokensUtil.java index c30871f7..0a50907e 100644 --- a/api/src/main/java/com/theokanning/openai/utils/TikTokensUtil.java +++ b/api/src/main/java/com/theokanning/openai/utils/TikTokensUtil.java @@ -32,6 +32,7 @@ public class TikTokensUtil { modelMap.put(ModelEnum.GPT_4_32K.getName(), registry.getEncodingForModel(ModelType.GPT_4)); modelMap.put(ModelEnum.GPT_4_32K_0314.getName(), registry.getEncodingForModel(ModelType.GPT_4)); modelMap.put(ModelEnum.GPT_4_0314.getName(), registry.getEncodingForModel(ModelType.GPT_4)); + modelMap.put(ModelEnum.GPT_4_1106_preview.getName(), registry.getEncodingForModel(ModelType.GPT_4)); } /** @@ -261,7 +262,11 @@ public enum ModelEnum { * Temporary model, not recommended for use. */ GPT_4_32K_0314("gpt-4-32k-0314"), - ; + + /** + * Temporary model, not recommended for use. + */ + GPT_4_1106_preview("gpt-4-1106-preview"); private String name; } 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 497dacd5..5881d0c6 100644 --- a/client/src/main/java/com/theokanning/openai/client/OpenAiApi.java +++ b/client/src/main/java/com/theokanning/openai/client/OpenAiApi.java @@ -2,6 +2,8 @@ import com.theokanning.openai.DeleteResult; import com.theokanning.openai.OpenAiResponse; +import com.theokanning.openai.assistants.AssistantBase; +import com.theokanning.openai.assistants.Assistant; import com.theokanning.openai.audio.TranscriptionResult; import com.theokanning.openai.audio.TranslationResult; import com.theokanning.openai.billing.BillingUsage; @@ -181,4 +183,9 @@ public interface OpenAiApi { @GET("v1/dashboard/billing/usage") Single billingUsage(@Query("start_date") LocalDate starDate, @Query("end_date") LocalDate endDate); + + @Headers({"OpenAI-Beta: assistants=v1"}) + @POST("/v1/assistants") + Single createAssistant(@Body AssistantBase 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 0296c15c..01baea8f 100644 --- a/service/src/main/java/com/theokanning/openai/service/OpenAiService.java +++ b/service/src/main/java/com/theokanning/openai/service/OpenAiService.java @@ -8,6 +8,8 @@ import com.theokanning.openai.DeleteResult; import com.theokanning.openai.OpenAiError; import com.theokanning.openai.OpenAiHttpException; +import com.theokanning.openai.assistants.Assistant; +import com.theokanning.openai.assistants.AssistantBase; import com.theokanning.openai.audio.CreateTranscriptionRequest; import com.theokanning.openai.audio.CreateTranslationRequest; import com.theokanning.openai.audio.TranscriptionResult; @@ -345,6 +347,10 @@ public ModerationResult createModeration(ModerationRequest request) { return execute(api.createModeration(request)); } + public Assistant createAssistant(AssistantBase request) { + return execute(api.createAssistant(request)); + } + /** * Calls the Open AI api, returns the response, and parses error messages if the request fails */ diff --git a/service/src/test/java/com/theokanning/openai/service/AssistantTest.java b/service/src/test/java/com/theokanning/openai/service/AssistantTest.java new file mode 100644 index 00000000..e5032fd8 --- /dev/null +++ b/service/src/test/java/com/theokanning/openai/service/AssistantTest.java @@ -0,0 +1,41 @@ +package com.theokanning.openai.service; + +import com.theokanning.openai.assistants.AssistantBase; +import com.theokanning.openai.assistants.AssistantRequest; +import com.theokanning.openai.assistants.Assistant; +import com.theokanning.openai.assistants.AssistantToolsEnum; +import com.theokanning.openai.assistants.Tool; +import com.theokanning.openai.utils.TikTokensUtil; +import org.junit.jupiter.api.Test; + +import java.util.Collections; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + + +public class AssistantTest { + String token = "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"; + + OpenAiService service = new OpenAiService(token); + + @Test + void createAssistant() { + AssistantBase assistantRequest = AssistantRequest.builder() + .model(TikTokensUtil.ModelEnum.GPT_4_1106_preview.getName()) + .name("Math Tutor") + .instructions("You are a personal Math Tutor.") + .tools(Collections.singletonList(new Tool(AssistantToolsEnum.CODE_INTERPRETER))) + .build(); + + Assistant assistantResponse = service.createAssistant(assistantRequest); + System.out.println(assistantResponse.getTools().get(0)); + + assertNotNull(assistantResponse); + assertNotNull(assistantResponse.getId()); + assertNotNull(assistantResponse.getCreatedAt()); + assertNotNull(assistantResponse.getObject()); + assertEquals(assistantResponse.getTools().get(0).getType(), AssistantToolsEnum.CODE_INTERPRETER); + assertEquals(assistantRequest.getName(), assistantResponse.getName()); + } +} diff --git a/service/src/test/java/com/theokanning/openai/service/ModerationTest.java b/service/src/test/java/com/theokanning/openai/service/ModerationTest.java index f28bc083..c66b8ba5 100644 --- a/service/src/test/java/com/theokanning/openai/service/ModerationTest.java +++ b/service/src/test/java/com/theokanning/openai/service/ModerationTest.java @@ -9,7 +9,7 @@ public class ModerationTest { - String token = System.getenv("OPENAI_TOKEN"); + String token = "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"; com.theokanning.openai.service.OpenAiService service = new OpenAiService(token); @Test From 93bb452811f4685b60e362e3e90200f127d33e26 Mon Sep 17 00:00:00 2001 From: Remy Ohajinwa Date: Sat, 11 Nov 2023 09:57:51 +0000 Subject: [PATCH 02/11] #390 Retrieve Assistant --- .../theokanning/openai/client/OpenAiApi.java | 4 +++ .../openai/service/OpenAiService.java | 4 +++ .../openai/service/AssistantTest.java | 26 +++++++++++++++---- 3 files changed, 29 insertions(+), 5 deletions(-) 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 5881d0c6..6f9a75ac 100644 --- a/client/src/main/java/com/theokanning/openai/client/OpenAiApi.java +++ b/client/src/main/java/com/theokanning/openai/client/OpenAiApi.java @@ -188,4 +188,8 @@ public interface OpenAiApi { @POST("/v1/assistants") Single createAssistant(@Body AssistantBase request); + @Headers({"OpenAI-Beta: assistants=v1"}) + @GET("/v1/assistants/{assistant_id}") + Single retrieveAssistant(@Path("assistant_id") String assistantId); + } 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 01baea8f..23adc34b 100644 --- a/service/src/main/java/com/theokanning/openai/service/OpenAiService.java +++ b/service/src/main/java/com/theokanning/openai/service/OpenAiService.java @@ -351,6 +351,10 @@ public Assistant createAssistant(AssistantBase request) { return execute(api.createAssistant(request)); } + public Assistant retrieveAssistant(String assistantId) { + return execute(api.retrieveAssistant(assistantId)); + } + /** * Calls the Open AI api, returns the response, and parses error messages if the request fails */ diff --git a/service/src/test/java/com/theokanning/openai/service/AssistantTest.java b/service/src/test/java/com/theokanning/openai/service/AssistantTest.java index e5032fd8..20a03c6a 100644 --- a/service/src/test/java/com/theokanning/openai/service/AssistantTest.java +++ b/service/src/test/java/com/theokanning/openai/service/AssistantTest.java @@ -15,27 +15,43 @@ public class AssistantTest { + public static final String MATH_TUTOR = "Math Tutor"; + public static final String ASSISTANT_INSTRUCTION = "You are a personal Math Tutor."; String token = "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"; OpenAiService service = new OpenAiService(token); @Test void createAssistant() { + callCreateAssistantAPI(); + } + + @Test + void retrieveAssistant() { + Assistant createAssistantResponse = callCreateAssistantAPI(); + validateAssistantResponse(createAssistantResponse); + + Assistant retrieveAssistantResponse = service.retrieveAssistant(createAssistantResponse.getId()); + validateAssistantResponse(retrieveAssistantResponse); + } + + Assistant callCreateAssistantAPI() { AssistantBase assistantRequest = AssistantRequest.builder() .model(TikTokensUtil.ModelEnum.GPT_4_1106_preview.getName()) - .name("Math Tutor") - .instructions("You are a personal Math Tutor.") + .name(MATH_TUTOR) + .instructions(ASSISTANT_INSTRUCTION) .tools(Collections.singletonList(new Tool(AssistantToolsEnum.CODE_INTERPRETER))) .build(); - Assistant assistantResponse = service.createAssistant(assistantRequest); - System.out.println(assistantResponse.getTools().get(0)); + return service.createAssistant(assistantRequest); + } + private static void validateAssistantResponse(Assistant assistantResponse) { assertNotNull(assistantResponse); assertNotNull(assistantResponse.getId()); assertNotNull(assistantResponse.getCreatedAt()); assertNotNull(assistantResponse.getObject()); assertEquals(assistantResponse.getTools().get(0).getType(), AssistantToolsEnum.CODE_INTERPRETER); - assertEquals(assistantRequest.getName(), assistantResponse.getName()); + assertEquals("Math Tutor", assistantResponse.getName()); } } From ca625b13b53114b772a2c2d935804346633ce33b Mon Sep 17 00:00:00 2001 From: Remy Ohajinwa Date: Sat, 11 Nov 2023 10:18:34 +0000 Subject: [PATCH 03/11] #390 Modify Assistant --- .../theokanning/openai/client/OpenAiApi.java | 4 +++ .../openai/service/OpenAiService.java | 4 +++ .../openai/service/AssistantTest.java | 32 +++++++++++++++---- .../openai/service/ModerationTest.java | 2 +- 4 files changed, 34 insertions(+), 8 deletions(-) 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 6f9a75ac..64cf50b6 100644 --- a/client/src/main/java/com/theokanning/openai/client/OpenAiApi.java +++ b/client/src/main/java/com/theokanning/openai/client/OpenAiApi.java @@ -192,4 +192,8 @@ public interface OpenAiApi { @GET("/v1/assistants/{assistant_id}") Single retrieveAssistant(@Path("assistant_id") String assistantId); + @Headers({"OpenAI-Beta: assistants=v1"}) + @POST("/v1/assistants/{assistant_id}") + Single modifyAssistant(@Path("assistant_id") String assistantId, @Body AssistantBase 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 23adc34b..6cf9c930 100644 --- a/service/src/main/java/com/theokanning/openai/service/OpenAiService.java +++ b/service/src/main/java/com/theokanning/openai/service/OpenAiService.java @@ -355,6 +355,10 @@ public Assistant retrieveAssistant(String assistantId) { return execute(api.retrieveAssistant(assistantId)); } + public Assistant modifyAssistant(String assistantId, AssistantBase request) { + return execute(api.modifyAssistant(assistantId, request)); + } + /** * Calls the Open AI api, returns the response, and parses error messages if the request fails */ diff --git a/service/src/test/java/com/theokanning/openai/service/AssistantTest.java b/service/src/test/java/com/theokanning/openai/service/AssistantTest.java index 20a03c6a..78604909 100644 --- a/service/src/test/java/com/theokanning/openai/service/AssistantTest.java +++ b/service/src/test/java/com/theokanning/openai/service/AssistantTest.java @@ -23,27 +23,45 @@ public class AssistantTest { @Test void createAssistant() { - callCreateAssistantAPI(); + AssistantBase assistantRequest = assistantStub(); + + Assistant createAssistantResponse = service.createAssistant(assistantRequest); + + validateAssistantResponse(createAssistantResponse); } @Test void retrieveAssistant() { - Assistant createAssistantResponse = callCreateAssistantAPI(); + AssistantBase assistantRequest = assistantStub(); + Assistant createAssistantResponse = service.createAssistant(assistantRequest); validateAssistantResponse(createAssistantResponse); Assistant retrieveAssistantResponse = service.retrieveAssistant(createAssistantResponse.getId()); validateAssistantResponse(retrieveAssistantResponse); } - Assistant callCreateAssistantAPI() { - AssistantBase assistantRequest = AssistantRequest.builder() + @Test + void modifyAssistant() { + AssistantBase assistantRequest = assistantStub();//original + Assistant createAssistantResponse = service.createAssistant(assistantRequest); + validateAssistantResponse(createAssistantResponse); + + String modifiedName = MATH_TUTOR + " Modified"; + createAssistantResponse.setName(modifiedName);//modify a field + + Assistant modifiedAssistantResponse = service.modifyAssistant(createAssistantResponse.getId(), createAssistantResponse); + assertNotNull(modifiedAssistantResponse); + assertEquals(modifiedName, modifiedAssistantResponse.getName()); + } + + + private static AssistantBase assistantStub() { + return AssistantRequest.builder() .model(TikTokensUtil.ModelEnum.GPT_4_1106_preview.getName()) .name(MATH_TUTOR) .instructions(ASSISTANT_INSTRUCTION) .tools(Collections.singletonList(new Tool(AssistantToolsEnum.CODE_INTERPRETER))) .build(); - - return service.createAssistant(assistantRequest); } private static void validateAssistantResponse(Assistant assistantResponse) { @@ -52,6 +70,6 @@ private static void validateAssistantResponse(Assistant assistantResponse) { assertNotNull(assistantResponse.getCreatedAt()); assertNotNull(assistantResponse.getObject()); assertEquals(assistantResponse.getTools().get(0).getType(), AssistantToolsEnum.CODE_INTERPRETER); - assertEquals("Math Tutor", assistantResponse.getName()); + assertEquals(MATH_TUTOR, assistantResponse.getName()); } } diff --git a/service/src/test/java/com/theokanning/openai/service/ModerationTest.java b/service/src/test/java/com/theokanning/openai/service/ModerationTest.java index c66b8ba5..f28bc083 100644 --- a/service/src/test/java/com/theokanning/openai/service/ModerationTest.java +++ b/service/src/test/java/com/theokanning/openai/service/ModerationTest.java @@ -9,7 +9,7 @@ public class ModerationTest { - String token = "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"; + String token = System.getenv("OPENAI_TOKEN"); com.theokanning.openai.service.OpenAiService service = new OpenAiService(token); @Test From 823af08ba9ad2e38fdabb97cba896601275227bb Mon Sep 17 00:00:00 2001 From: Remy Ohajinwa Date: Sat, 11 Nov 2023 10:50:56 +0000 Subject: [PATCH 04/11] RemyOhajinwa#390 Delete Assistant --- .../theokanning/openai/client/OpenAiApi.java | 5 +++ .../openai/service/OpenAiService.java | 3 ++ .../openai/service/AssistantTest.java | 35 +++++++++++++------ 3 files changed, 32 insertions(+), 11 deletions(-) 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 64cf50b6..11455cb1 100644 --- a/client/src/main/java/com/theokanning/openai/client/OpenAiApi.java +++ b/client/src/main/java/com/theokanning/openai/client/OpenAiApi.java @@ -4,6 +4,7 @@ import com.theokanning.openai.OpenAiResponse; import com.theokanning.openai.assistants.AssistantBase; import com.theokanning.openai.assistants.Assistant; +import com.theokanning.openai.assistants.DeleteAssistantResult; import com.theokanning.openai.audio.TranscriptionResult; import com.theokanning.openai.audio.TranslationResult; import com.theokanning.openai.billing.BillingUsage; @@ -196,4 +197,8 @@ public interface OpenAiApi { @POST("/v1/assistants/{assistant_id}") Single modifyAssistant(@Path("assistant_id") String assistantId, @Body AssistantBase request); + @Headers({"OpenAI-Beta: assistants=v1"}) + @DELETE("/v1/assistants/{assistant_id}") + Single deleteAssistant(@Path("assistant_id") String assistantId); + } 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 6cf9c930..e9f0e445 100644 --- a/service/src/main/java/com/theokanning/openai/service/OpenAiService.java +++ b/service/src/main/java/com/theokanning/openai/service/OpenAiService.java @@ -359,6 +359,9 @@ public Assistant modifyAssistant(String assistantId, AssistantBase request) { return execute(api.modifyAssistant(assistantId, request)); } + public DeleteResult deleteAssistant(String assistantId) { + return execute(api.deleteAssistant(assistantId)); + } /** * Calls the Open AI api, returns the response, and parses error messages if the request fails */ diff --git a/service/src/test/java/com/theokanning/openai/service/AssistantTest.java b/service/src/test/java/com/theokanning/openai/service/AssistantTest.java index 78604909..2dd6819c 100644 --- a/service/src/test/java/com/theokanning/openai/service/AssistantTest.java +++ b/service/src/test/java/com/theokanning/openai/service/AssistantTest.java @@ -1,5 +1,6 @@ package com.theokanning.openai.service; +import com.theokanning.openai.DeleteResult; import com.theokanning.openai.assistants.AssistantBase; import com.theokanning.openai.assistants.AssistantRequest; import com.theokanning.openai.assistants.Assistant; @@ -12,6 +13,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; public class AssistantTest { @@ -23,18 +25,12 @@ public class AssistantTest { @Test void createAssistant() { - AssistantBase assistantRequest = assistantStub(); - - Assistant createAssistantResponse = service.createAssistant(assistantRequest); - - validateAssistantResponse(createAssistantResponse); + createAndValidateAssistant(); } @Test void retrieveAssistant() { - AssistantBase assistantRequest = assistantStub(); - Assistant createAssistantResponse = service.createAssistant(assistantRequest); - validateAssistantResponse(createAssistantResponse); + Assistant createAssistantResponse = createAndValidateAssistant(); Assistant retrieveAssistantResponse = service.retrieveAssistant(createAssistantResponse.getId()); validateAssistantResponse(retrieveAssistantResponse); @@ -42,9 +38,7 @@ void retrieveAssistant() { @Test void modifyAssistant() { - AssistantBase assistantRequest = assistantStub();//original - Assistant createAssistantResponse = service.createAssistant(assistantRequest); - validateAssistantResponse(createAssistantResponse); + Assistant createAssistantResponse = createAndValidateAssistant(); String modifiedName = MATH_TUTOR + " Modified"; createAssistantResponse.setName(modifiedName);//modify a field @@ -54,6 +48,25 @@ void modifyAssistant() { assertEquals(modifiedName, modifiedAssistantResponse.getName()); } + @Test + void deleteAssistant() { + Assistant createAssistantResponse = createAndValidateAssistant(); + + DeleteResult deletedAssistant = service.deleteAssistant(createAssistantResponse.getId()); + + assertNotNull(deletedAssistant); + assertEquals(createAssistantResponse.getId(), deletedAssistant.getId()); + assertTrue(deletedAssistant.isDeleted()); + } + + private Assistant createAndValidateAssistant() { + AssistantBase assistantRequest = assistantStub(); + Assistant createAssistantResponse = service.createAssistant(assistantRequest); + validateAssistantResponse(createAssistantResponse); + + return createAssistantResponse; + } + private static AssistantBase assistantStub() { return AssistantRequest.builder() From cc3d95d065aa0699d2a1ca580d95638c5553c141 Mon Sep 17 00:00:00 2001 From: Remy Ohajinwa Date: Sat, 11 Nov 2023 12:27:05 +0000 Subject: [PATCH 05/11] RemyOhajinwa#390 List Assistants --- .../openai/assistants/AssistantSortOrder.java | 12 +++ .../openai/assistants/AssistantToolsEnum.java | 15 ++++ .../openai/assistants/ListAssistant.java | 16 ++++ .../assistants/ListAssistantQueryRequest.java | 39 ++++++++ .../theokanning/openai/client/OpenAiApi.java | 7 ++ .../openai/service/OpenAiService.java | 11 +++ .../openai/service/AssistantTest.java | 90 +++++++++++++++++-- 7 files changed, 183 insertions(+), 7 deletions(-) create mode 100644 api/src/main/java/com/theokanning/openai/assistants/AssistantSortOrder.java create mode 100644 api/src/main/java/com/theokanning/openai/assistants/AssistantToolsEnum.java create mode 100644 api/src/main/java/com/theokanning/openai/assistants/ListAssistant.java create mode 100644 api/src/main/java/com/theokanning/openai/assistants/ListAssistantQueryRequest.java diff --git a/api/src/main/java/com/theokanning/openai/assistants/AssistantSortOrder.java b/api/src/main/java/com/theokanning/openai/assistants/AssistantSortOrder.java new file mode 100644 index 00000000..9f784a66 --- /dev/null +++ b/api/src/main/java/com/theokanning/openai/assistants/AssistantSortOrder.java @@ -0,0 +1,12 @@ +package com.theokanning.openai.assistants; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public enum AssistantSortOrder { + + @JsonProperty("asc") + ASC, + + @JsonProperty("desc") + DESC +} diff --git a/api/src/main/java/com/theokanning/openai/assistants/AssistantToolsEnum.java b/api/src/main/java/com/theokanning/openai/assistants/AssistantToolsEnum.java new file mode 100644 index 00000000..f6b5021d --- /dev/null +++ b/api/src/main/java/com/theokanning/openai/assistants/AssistantToolsEnum.java @@ -0,0 +1,15 @@ +package com.theokanning.openai.assistants; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public enum AssistantToolsEnum { + + @JsonProperty("code_interpreter") + CODE_INTERPRETER, + + @JsonProperty("function") + FUNCTION, + + @JsonProperty("retrieval") + RETRIEVAL +} diff --git a/api/src/main/java/com/theokanning/openai/assistants/ListAssistant.java b/api/src/main/java/com/theokanning/openai/assistants/ListAssistant.java new file mode 100644 index 00000000..8478a547 --- /dev/null +++ b/api/src/main/java/com/theokanning/openai/assistants/ListAssistant.java @@ -0,0 +1,16 @@ +package com.theokanning.openai.assistants; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.theokanning.openai.OpenAiResponse; + +public class ListAssistant extends OpenAiResponse { + + @JsonProperty("first_id") + String firstId; + + @JsonProperty("last_id") + String lastId; + + @JsonProperty("has_more") + boolean hasMore; +} diff --git a/api/src/main/java/com/theokanning/openai/assistants/ListAssistantQueryRequest.java b/api/src/main/java/com/theokanning/openai/assistants/ListAssistantQueryRequest.java new file mode 100644 index 00000000..3e5f3c68 --- /dev/null +++ b/api/src/main/java/com/theokanning/openai/assistants/ListAssistantQueryRequest.java @@ -0,0 +1,39 @@ +package com.theokanning.openai.assistants; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Builder +@NoArgsConstructor +@AllArgsConstructor +@Data +public class ListAssistantQueryRequest { + /** + * A limit on the number of objects to be returned. + * Limit can range between 1 and 100, and the default is 20 + */ + + Integer limit; + + /** + * Sort order by the 'created_at' timestamp of the objects. + * 'asc' for ascending order and 'desc' for descending order. + */ + AssistantSortOrder order; + + /** + * A cursor for use in pagination. after is an object ID that defines your place in the list. + * For instance, if you make a list request and receive 100 objects, ending with obj_foo, + * your subsequent call can include after=obj_foo in order to fetch the next page of the list + */ + String after; + + /** + * A cursor for use in pagination. before is an object ID that defines your place in the list. + * For instance, if you make a list request and receive 100 objects, ending with obj_foo, + * your subsequent call can include before=obj_foo in order to fetch the previous page of the list. + */ + String before; +} 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 11455cb1..44d8915d 100644 --- a/client/src/main/java/com/theokanning/openai/client/OpenAiApi.java +++ b/client/src/main/java/com/theokanning/openai/client/OpenAiApi.java @@ -5,6 +5,8 @@ import com.theokanning.openai.assistants.AssistantBase; import com.theokanning.openai.assistants.Assistant; import com.theokanning.openai.assistants.DeleteAssistantResult; +import com.theokanning.openai.assistants.ListAssistant; +import com.theokanning.openai.assistants.ListAssistantQueryRequest; import com.theokanning.openai.audio.TranscriptionResult; import com.theokanning.openai.audio.TranslationResult; import com.theokanning.openai.billing.BillingUsage; @@ -38,6 +40,7 @@ import retrofit2.http.*; import java.time.LocalDate; +import java.util.Map; public interface OpenAiApi { @@ -201,4 +204,8 @@ public interface OpenAiApi { @DELETE("/v1/assistants/{assistant_id}") Single deleteAssistant(@Path("assistant_id") String assistantId); + @Headers({"OpenAI-Beta: assistants=v1"}) + @GET("/v1/assistants") + Single> listAssistants(@QueryMap Map filterRequest); + } 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 e9f0e445..6c20e234 100644 --- a/service/src/main/java/com/theokanning/openai/service/OpenAiService.java +++ b/service/src/main/java/com/theokanning/openai/service/OpenAiService.java @@ -1,6 +1,7 @@ package com.theokanning.openai.service; import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.PropertyNamingStrategy; @@ -10,6 +11,8 @@ import com.theokanning.openai.OpenAiHttpException; import com.theokanning.openai.assistants.Assistant; import com.theokanning.openai.assistants.AssistantBase; +import com.theokanning.openai.assistants.ListAssistant; +import com.theokanning.openai.assistants.ListAssistantQueryRequest; import com.theokanning.openai.audio.CreateTranscriptionRequest; import com.theokanning.openai.audio.CreateTranslationRequest; import com.theokanning.openai.audio.TranscriptionResult; @@ -54,6 +57,7 @@ import java.time.Duration; import java.time.LocalDate; import java.util.List; +import java.util.Map; import java.util.Objects; import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeUnit; @@ -362,6 +366,13 @@ public Assistant modifyAssistant(String assistantId, AssistantBase request) { public DeleteResult deleteAssistant(String assistantId) { return execute(api.deleteAssistant(assistantId)); } + + public ListAssistant listAssistants(ListAssistantQueryRequest filterRequest) { + Map queryParameters = mapper.convertValue(filterRequest, new TypeReference>() {}); + return execute(api.listAssistants(queryParameters)); + } + + /** * Calls the Open AI api, returns the response, and parses error messages if the request fails */ diff --git a/service/src/test/java/com/theokanning/openai/service/AssistantTest.java b/service/src/test/java/com/theokanning/openai/service/AssistantTest.java index 2dd6819c..2567e57e 100644 --- a/service/src/test/java/com/theokanning/openai/service/AssistantTest.java +++ b/service/src/test/java/com/theokanning/openai/service/AssistantTest.java @@ -1,15 +1,20 @@ package com.theokanning.openai.service; import com.theokanning.openai.DeleteResult; +import com.theokanning.openai.assistants.Assistant; import com.theokanning.openai.assistants.AssistantBase; import com.theokanning.openai.assistants.AssistantRequest; -import com.theokanning.openai.assistants.Assistant; +import com.theokanning.openai.assistants.AssistantSortOrder; import com.theokanning.openai.assistants.AssistantToolsEnum; +import com.theokanning.openai.assistants.ListAssistant; +import com.theokanning.openai.assistants.ListAssistantQueryRequest; import com.theokanning.openai.assistants.Tool; import com.theokanning.openai.utils.TikTokensUtil; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import java.util.Collections; +import java.util.List; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -19,15 +24,19 @@ public class AssistantTest { public static final String MATH_TUTOR = "Math Tutor"; public static final String ASSISTANT_INSTRUCTION = "You are a personal Math Tutor."; - String token = "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"; + static String token = "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"; - OpenAiService service = new OpenAiService(token); + static OpenAiService service = new OpenAiService(token); - @Test - void createAssistant() { - createAndValidateAssistant(); + @BeforeAll + static void initAssistants() { + //Done so that listAssistant tests won't fail on initial setup of project + for (int i = 0; i < 3; i++) { + createAndValidateAssistant(); + } } + @Test void retrieveAssistant() { Assistant createAssistantResponse = createAndValidateAssistant(); @@ -59,7 +68,67 @@ void deleteAssistant() { assertTrue(deletedAssistant.isDeleted()); } - private Assistant createAndValidateAssistant() { + @Test + void listAssistants() { + ListAssistant assistants = service.listAssistants(ListAssistantQueryRequest.builder().build()); + + assertNotNull(assistants); + // this should be more than 2 depending on how many times createAndValidateAssistant method is called + assertTrue(assistants.getData().size() > 1); + } + + @Test + void listAssistants_returnsTwoAssistants() { + int expectedLimit = 2; + ListAssistantQueryRequest queryResult = ListAssistantQueryRequest.builder() + .limit(expectedLimit) + .build(); + + ListAssistant assistants = service.listAssistants(queryResult); + + List data = validateListAssistants(assistants); + assertEquals(expectedLimit, data.size()); + } + + + + @Test + void listAssistants_returnsAscSortedAssistants() { + int expectedLimit = 3; + + ListAssistantQueryRequest queryResult = ListAssistantQueryRequest.builder() + .limit(expectedLimit) + .order(AssistantSortOrder.ASC) + .build(); + + ListAssistant assistants = service.listAssistants(queryResult); + + List data = validateListAssistants(assistants); + + boolean firstTwoAscending = data.get(0).getCreatedAt() <= data.get(1).getCreatedAt(); + boolean lastTwoAscending = data.get(1).getCreatedAt() <= data.get(2).getCreatedAt(); + assertTrue(firstTwoAscending && lastTwoAscending); + } + + @Test + void listAssistants_returnsDescSortedAssistants() { + int expectedLimit = 3; + + ListAssistantQueryRequest queryResult = ListAssistantQueryRequest.builder() + .limit(expectedLimit) + .order(AssistantSortOrder.DESC) + .build(); + + ListAssistant assistants = service.listAssistants(queryResult); + + List data = validateListAssistants(assistants); + + boolean firstTwoDescending = data.get(0).getCreatedAt() >= data.get(1).getCreatedAt(); + boolean lastTwoDescending = data.get(1).getCreatedAt() >= data.get(2).getCreatedAt(); + assertTrue(firstTwoDescending && lastTwoDescending); + } + + private static Assistant createAndValidateAssistant() { AssistantBase assistantRequest = assistantStub(); Assistant createAssistantResponse = service.createAssistant(assistantRequest); validateAssistantResponse(createAssistantResponse); @@ -85,4 +154,11 @@ private static void validateAssistantResponse(Assistant assistantResponse) { assertEquals(assistantResponse.getTools().get(0).getType(), AssistantToolsEnum.CODE_INTERPRETER); assertEquals(MATH_TUTOR, assistantResponse.getName()); } + + private static List validateListAssistants(ListAssistant assistants) { + assertNotNull(assistants); + List data = assistants.getData(); + assertNotNull(data); + return data; + } } From 1be8bfe74c69b1fa8dc91fda58d71099c23f1ce6 Mon Sep 17 00:00:00 2001 From: Remy Ohajinwa Date: Sat, 11 Nov 2023 14:43:52 +0000 Subject: [PATCH 06/11] RemyOhajinwa#390 Create Assistant File --- .../openai/assistants/AssistantFile.java | 30 +++++++++++++++++++ .../assistants/AssistantFileRequest.java | 17 +++++++++++ .../theokanning/openai/client/OpenAiApi.java | 5 ++++ .../openai/service/OpenAiService.java | 7 +++++ .../openai/service/AssistantTest.java | 19 +++++++++++- .../test/resources/assistant-file-data.json | 1 + 6 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 api/src/main/java/com/theokanning/openai/assistants/AssistantFile.java create mode 100644 api/src/main/java/com/theokanning/openai/assistants/AssistantFileRequest.java create mode 100644 service/src/test/resources/assistant-file-data.json diff --git a/api/src/main/java/com/theokanning/openai/assistants/AssistantFile.java b/api/src/main/java/com/theokanning/openai/assistants/AssistantFile.java new file mode 100644 index 00000000..c5d551a9 --- /dev/null +++ b/api/src/main/java/com/theokanning/openai/assistants/AssistantFile.java @@ -0,0 +1,30 @@ +package com.theokanning.openai.assistants; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; + +@Data +public class AssistantFile { + + /** + * The identifier of the Assistant File + */ + String id; + + /** + * The object type, which is always assistant.file. + */ + String object; + + /** + * The Unix timestamp (in seconds) for when the assistant file was created. + */ + @JsonProperty("created_at") + String createdAt; + + /** + * The assistant ID that the file is attached to + */ + @JsonProperty("assistant_id") + String assistantId; +} diff --git a/api/src/main/java/com/theokanning/openai/assistants/AssistantFileRequest.java b/api/src/main/java/com/theokanning/openai/assistants/AssistantFileRequest.java new file mode 100644 index 00000000..98ee009f --- /dev/null +++ b/api/src/main/java/com/theokanning/openai/assistants/AssistantFileRequest.java @@ -0,0 +1,17 @@ +package com.theokanning.openai.assistants; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Builder +@NoArgsConstructor +@AllArgsConstructor +@Data +public class AssistantFileRequest { + + @JsonProperty("file_id") + String fileId; +} 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 44d8915d..6aca03a0 100644 --- a/client/src/main/java/com/theokanning/openai/client/OpenAiApi.java +++ b/client/src/main/java/com/theokanning/openai/client/OpenAiApi.java @@ -4,6 +4,8 @@ import com.theokanning.openai.OpenAiResponse; import com.theokanning.openai.assistants.AssistantBase; import com.theokanning.openai.assistants.Assistant; +import com.theokanning.openai.assistants.AssistantFile; +import com.theokanning.openai.assistants.AssistantFileRequest; import com.theokanning.openai.assistants.DeleteAssistantResult; import com.theokanning.openai.assistants.ListAssistant; import com.theokanning.openai.assistants.ListAssistantQueryRequest; @@ -208,4 +210,7 @@ public interface OpenAiApi { @GET("/v1/assistants") Single> listAssistants(@QueryMap Map filterRequest); + @Headers({"OpenAI-Beta: assistants=v1"}) + @POST("/v1/assistants/{assistant_id}/files") + Single createAssistantFile(@Path("assistant_id") String assistantId, @Body AssistantFileRequest fileRequest); } 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 6c20e234..d3f08eb7 100644 --- a/service/src/main/java/com/theokanning/openai/service/OpenAiService.java +++ b/service/src/main/java/com/theokanning/openai/service/OpenAiService.java @@ -11,6 +11,8 @@ import com.theokanning.openai.OpenAiHttpException; import com.theokanning.openai.assistants.Assistant; import com.theokanning.openai.assistants.AssistantBase; +import com.theokanning.openai.assistants.AssistantFile; +import com.theokanning.openai.assistants.AssistantFileRequest; import com.theokanning.openai.assistants.ListAssistant; import com.theokanning.openai.assistants.ListAssistantQueryRequest; import com.theokanning.openai.audio.CreateTranscriptionRequest; @@ -167,6 +169,7 @@ public File uploadFile(String purpose, String filepath) { RequestBody purposeBody = RequestBody.create(okhttp3.MultipartBody.FORM, purpose); RequestBody fileBody = RequestBody.create(MediaType.parse("text"), file); MultipartBody.Part body = MultipartBody.Part.createFormData("file", filepath, fileBody); + System.out.println(purposeBody.contentType()); return execute(api.uploadFile(purposeBody, body)); } @@ -372,6 +375,10 @@ public ListAssistant listAssistants(ListAssistantQueryRequest filterR return execute(api.listAssistants(queryParameters)); } + public AssistantFile createAssistantFile(String assistantId, AssistantFileRequest fileRequest) { + return execute(api.createAssistantFile(assistantId, fileRequest)); + } + /** * Calls the Open AI api, returns the response, and parses error messages if the request fails diff --git a/service/src/test/java/com/theokanning/openai/service/AssistantTest.java b/service/src/test/java/com/theokanning/openai/service/AssistantTest.java index 2567e57e..a5cdcc43 100644 --- a/service/src/test/java/com/theokanning/openai/service/AssistantTest.java +++ b/service/src/test/java/com/theokanning/openai/service/AssistantTest.java @@ -3,12 +3,15 @@ import com.theokanning.openai.DeleteResult; import com.theokanning.openai.assistants.Assistant; import com.theokanning.openai.assistants.AssistantBase; +import com.theokanning.openai.assistants.AssistantFile; +import com.theokanning.openai.assistants.AssistantFileRequest; import com.theokanning.openai.assistants.AssistantRequest; import com.theokanning.openai.assistants.AssistantSortOrder; import com.theokanning.openai.assistants.AssistantToolsEnum; import com.theokanning.openai.assistants.ListAssistant; import com.theokanning.openai.assistants.ListAssistantQueryRequest; import com.theokanning.openai.assistants.Tool; +import com.theokanning.openai.file.File; import com.theokanning.openai.utils.TikTokensUtil; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -32,7 +35,7 @@ public class AssistantTest { static void initAssistants() { //Done so that listAssistant tests won't fail on initial setup of project for (int i = 0; i < 3; i++) { - createAndValidateAssistant(); +// createAndValidateAssistant(); } } @@ -128,6 +131,20 @@ void listAssistants_returnsDescSortedAssistants() { assertTrue(firstTwoDescending && lastTwoDescending); } + @Test + void createAssistantFile() { + String filePath = "src/test/resources/assistant-file-data.json"; + File uploadedFile = service.uploadFile("fine-tune", filePath);//uploading with 'purpose=assistants' seem not to be working + + Assistant assistant = createAndValidateAssistant(); + + AssistantFile assistantFile = service.createAssistantFile(assistant.getId(), new AssistantFileRequest(uploadedFile.getId())); + + assertNotNull(assistantFile); + assertEquals(uploadedFile.getId(), assistantFile.getId()); + assertEquals(assistant.getId(), assistantFile.getAssistantId()); + } + private static Assistant createAndValidateAssistant() { AssistantBase assistantRequest = assistantStub(); Assistant createAssistantResponse = service.createAssistant(assistantRequest); diff --git a/service/src/test/resources/assistant-file-data.json b/service/src/test/resources/assistant-file-data.json new file mode 100644 index 00000000..8b42bc07 --- /dev/null +++ b/service/src/test/resources/assistant-file-data.json @@ -0,0 +1 @@ +{"prompt": "prompt", "completion": "text"} \ No newline at end of file From 80afdff55f759a30d9dfba2a60911456648e706d Mon Sep 17 00:00:00 2001 From: Remy Ohajinwa Date: Sat, 11 Nov 2023 16:06:28 +0000 Subject: [PATCH 07/11] RemyOhajinwa#390 Assistant File --- .../theokanning/openai/client/OpenAiApi.java | 12 +++++ .../openai/service/OpenAiService.java | 14 +++++- .../openai/service/AssistantTest.java | 49 ++++++++++++++++--- .../src/test/resources/assistants-data.html | 1 + 4 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 service/src/test/resources/assistants-data.html 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 6aca03a0..03964721 100644 --- a/client/src/main/java/com/theokanning/openai/client/OpenAiApi.java +++ b/client/src/main/java/com/theokanning/openai/client/OpenAiApi.java @@ -213,4 +213,16 @@ public interface OpenAiApi { @Headers({"OpenAI-Beta: assistants=v1"}) @POST("/v1/assistants/{assistant_id}/files") Single createAssistantFile(@Path("assistant_id") String assistantId, @Body AssistantFileRequest fileRequest); + + @Headers({"OpenAI-Beta: assistants=v1"}) + @GET("/v1/assistants/{assistant_id}/files/{file_id}") + Single retrieveAssistantFile(@Path("assistant_id") String assistantId, @Path("file_id") String fileId); + + @Headers({"OpenAI-Beta: assistants=v1"}) + @DELETE("/v1/assistants/{assistant_id}/files/{file_id}") + Single deleteAssistantFile(@Path("assistant_id") String assistantId, @Path("file_id") String fileId); + + @Headers({"OpenAI-Beta: assistants=v1"}) + @GET("/v1/assistants/{assistant_id}/files") + Single> listAssistantFiles(@Path("assistant_id") String assistantId, @QueryMap Map filterRequest); } 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 d3f08eb7..7258cd9f 100644 --- a/service/src/main/java/com/theokanning/openai/service/OpenAiService.java +++ b/service/src/main/java/com/theokanning/openai/service/OpenAiService.java @@ -169,7 +169,6 @@ public File uploadFile(String purpose, String filepath) { RequestBody purposeBody = RequestBody.create(okhttp3.MultipartBody.FORM, purpose); RequestBody fileBody = RequestBody.create(MediaType.parse("text"), file); MultipartBody.Part body = MultipartBody.Part.createFormData("file", filepath, fileBody); - System.out.println(purposeBody.contentType()); return execute(api.uploadFile(purposeBody, body)); } @@ -379,6 +378,19 @@ public AssistantFile createAssistantFile(String assistantId, AssistantFileReques return execute(api.createAssistantFile(assistantId, fileRequest)); } + public AssistantFile retrieveAssistantFile(String assistantId, String fileId) { + return execute(api.retrieveAssistantFile(assistantId, fileId)); + } + + public DeleteResult deleteAssistantFile(String assistantId, String fileId) { + return execute(api.deleteAssistantFile(assistantId, fileId)); + } + + public ListAssistant listAssistantFiles(String assistantId, ListAssistantQueryRequest filterRequest) { + Map queryParameters = mapper.convertValue(filterRequest, new TypeReference>() {}); + return execute(api.listAssistantFiles(assistantId, queryParameters)); + } + /** * Calls the Open AI api, returns the response, and parses error messages if the request fails diff --git a/service/src/test/java/com/theokanning/openai/service/AssistantTest.java b/service/src/test/java/com/theokanning/openai/service/AssistantTest.java index a5cdcc43..63f0bf68 100644 --- a/service/src/test/java/com/theokanning/openai/service/AssistantTest.java +++ b/service/src/test/java/com/theokanning/openai/service/AssistantTest.java @@ -13,6 +13,7 @@ import com.theokanning.openai.assistants.Tool; import com.theokanning.openai.file.File; import com.theokanning.openai.utils.TikTokensUtil; +import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -27,16 +28,13 @@ public class AssistantTest { public static final String MATH_TUTOR = "Math Tutor"; public static final String ASSISTANT_INSTRUCTION = "You are a personal Math Tutor."; + static String token = "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"; static OpenAiService service = new OpenAiService(token); @BeforeAll static void initAssistants() { - //Done so that listAssistant tests won't fail on initial setup of project - for (int i = 0; i < 3; i++) { -// createAndValidateAssistant(); - } } @@ -133,8 +131,7 @@ void listAssistants_returnsDescSortedAssistants() { @Test void createAssistantFile() { - String filePath = "src/test/resources/assistant-file-data.json"; - File uploadedFile = service.uploadFile("fine-tune", filePath);//uploading with 'purpose=assistants' seem not to be working + File uploadedFile = uploadAssistantFile(); Assistant assistant = createAndValidateAssistant(); @@ -145,6 +142,46 @@ void createAssistantFile() { assertEquals(assistant.getId(), assistantFile.getAssistantId()); } + + + @Test + void retrieveAssistantFile() { + //TODO + //There is a bug with uploading assistant files https://community.openai.com/t/possible-bug-with-agent-creation-php-file-upload/484490/5 + //So this would have to be done later + } + + @Test + void deleteAssistantFile() { + //TODO + //There is a bug with uploading assistant files https://community.openai.com/t/possible-bug-with-agent-creation-php-file-upload/484490/5 + //So this would have to be done later + } + + @Test + void listAssistantFiles() { + //TODO + //There is a bug with uploading assistant files https://community.openai.com/t/possible-bug-with-agent-creation-php-file-upload/484490/5 + //So this would have to be done later + } + + @AfterAll + static void clean() { + //Clean up all data created during this test + ListAssistantQueryRequest queryFilter = ListAssistantQueryRequest.builder() + .limit(100) + .build(); + ListAssistant assistantListAssistant = service.listAssistants(queryFilter); + assistantListAssistant.getData().forEach(assistant ->{ + service.deleteAssistant(assistant.getId()); + }); + } + + private static File uploadAssistantFile() { + String filePath = "src/test/resources/assistants-data.html"; + return service.uploadFile("assistants", filePath); + } + private static Assistant createAndValidateAssistant() { AssistantBase assistantRequest = assistantStub(); Assistant createAssistantResponse = service.createAssistant(assistantRequest); diff --git a/service/src/test/resources/assistants-data.html b/service/src/test/resources/assistants-data.html new file mode 100644 index 00000000..6c70bcfe --- /dev/null +++ b/service/src/test/resources/assistants-data.html @@ -0,0 +1 @@ + \ No newline at end of file From 0df3060532cb0dcbb723cf4fb3c6d0440bda62ca Mon Sep 17 00:00:00 2001 From: Remy Ohajinwa Date: Sat, 11 Nov 2023 16:07:09 +0000 Subject: [PATCH 08/11] RemyOhajinwa#390 Assistant File --- .../java/com/theokanning/openai/service/AssistantTest.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/service/src/test/java/com/theokanning/openai/service/AssistantTest.java b/service/src/test/java/com/theokanning/openai/service/AssistantTest.java index 63f0bf68..f1a99fef 100644 --- a/service/src/test/java/com/theokanning/openai/service/AssistantTest.java +++ b/service/src/test/java/com/theokanning/openai/service/AssistantTest.java @@ -29,13 +29,10 @@ public class AssistantTest { public static final String MATH_TUTOR = "Math Tutor"; public static final String ASSISTANT_INSTRUCTION = "You are a personal Math Tutor."; - static String token = "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"; + static String token = System.getenv("OPENAI_TOKEN");; static OpenAiService service = new OpenAiService(token); - @BeforeAll - static void initAssistants() { - } @Test From f6bb2560ed482a84903ad6a5cafa79dd8880758a Mon Sep 17 00:00:00 2001 From: Remy Ohajinwa Date: Sat, 11 Nov 2023 16:26:06 +0000 Subject: [PATCH 09/11] RemyOhajinwa#390 Assistant File --- .../test/java/com/theokanning/openai/service/AssistantTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/service/src/test/java/com/theokanning/openai/service/AssistantTest.java b/service/src/test/java/com/theokanning/openai/service/AssistantTest.java index f1a99fef..4f21b739 100644 --- a/service/src/test/java/com/theokanning/openai/service/AssistantTest.java +++ b/service/src/test/java/com/theokanning/openai/service/AssistantTest.java @@ -14,7 +14,6 @@ import com.theokanning.openai.file.File; import com.theokanning.openai.utils.TikTokensUtil; import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import java.util.Collections; From cd776d0bcece5e200af18971f95ba80790e344c9 Mon Sep 17 00:00:00 2001 From: Remy Ohajinwa Date: Sat, 11 Nov 2023 16:36:15 +0000 Subject: [PATCH 10/11] RemyOhajinwa#390 Assistant File --- .../com/theokanning/openai/assistants/AssistantRequest.java | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 api/src/main/java/com/theokanning/openai/assistants/AssistantRequest.java diff --git a/api/src/main/java/com/theokanning/openai/assistants/AssistantRequest.java b/api/src/main/java/com/theokanning/openai/assistants/AssistantRequest.java new file mode 100644 index 00000000..dc0a66df --- /dev/null +++ b/api/src/main/java/com/theokanning/openai/assistants/AssistantRequest.java @@ -0,0 +1,6 @@ +package com.theokanning.openai.assistants; + + +public class AssistantRequest extends AssistantBase { + +} From 55d120827554b9b298ed7c1211d795eef08abbfc Mon Sep 17 00:00:00 2001 From: Theo Kanning Date: Sun, 12 Nov 2023 15:31:45 -0600 Subject: [PATCH 11/11] Remove DeleteAssistantResult import --- .../src/main/java/com/theokanning/openai/client/OpenAiApi.java | 1 - 1 file changed, 1 deletion(-) 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 0ec1ae1e..0bfc03aa 100644 --- a/client/src/main/java/com/theokanning/openai/client/OpenAiApi.java +++ b/client/src/main/java/com/theokanning/openai/client/OpenAiApi.java @@ -6,7 +6,6 @@ import com.theokanning.openai.assistants.Assistant; import com.theokanning.openai.assistants.AssistantFile; import com.theokanning.openai.assistants.AssistantFileRequest; -import com.theokanning.openai.assistants.DeleteAssistantResult; import com.theokanning.openai.assistants.ListAssistant; import com.theokanning.openai.assistants.ListAssistantQueryRequest; import com.theokanning.openai.audio.CreateSpeechRequest;