From 5baeb539dab4c5467275ef2fed101cddc3656a69 Mon Sep 17 00:00:00 2001 From: Stefan Bratanov Date: Thu, 18 Apr 2024 09:09:47 +0100 Subject: [PATCH] [batchapi] add list batch --- .../jvm/openai/BatchClient.java | 26 +++++++++++++++++++ .../jvm/openai/OpenAIIntegrationTest.java | 10 +++++++ .../OpenApiSpecificationValidationTest.java | 15 +++++++++++ .../jvm/openai/TestDataUtil.java | 8 ++++++ 4 files changed, 59 insertions(+) diff --git a/src/main/java/io/github/stefanbratanov/jvm/openai/BatchClient.java b/src/main/java/io/github/stefanbratanov/jvm/openai/BatchClient.java index 020c628..76370a8 100644 --- a/src/main/java/io/github/stefanbratanov/jvm/openai/BatchClient.java +++ b/src/main/java/io/github/stefanbratanov/jvm/openai/BatchClient.java @@ -6,6 +6,8 @@ import java.net.http.HttpRequest.BodyPublishers; import java.net.http.HttpResponse; import java.time.Duration; +import java.util.List; +import java.util.Map; import java.util.Optional; /** @@ -71,4 +73,28 @@ public Batch cancelBatch(String batchId) { HttpResponse httpResponse = sendHttpRequest(httpRequest); return deserializeResponse(httpResponse.body(), Batch.class); } + + /** + * List your organization's batches + * + * @param after A cursor for use in pagination. after is an object ID that defines your place in + * the list. + * @param limit A limit on the number of objects to be returned. + * @throws OpenAIException in case of API errors + */ + public PaginatedBatches listBatches(Optional after, Optional limit) { + String queryParameters = + createQueryParameters( + Map.of(Constants.LIMIT_QUERY_PARAMETER, limit, Constants.AFTER_QUERY_PARAMETER, after)); + HttpRequest httpRequest = + newHttpRequestBuilder() + .uri(baseUrl.resolve(Endpoint.BATCHES.getPath() + queryParameters)) + .GET() + .build(); + HttpResponse httpResponse = sendHttpRequest(httpRequest); + return deserializeResponse(httpResponse.body(), PaginatedBatches.class); + } + + public record PaginatedBatches( + List data, String firstId, String lastId, boolean hasMore) {} } diff --git a/src/test/java/io/github/stefanbratanov/jvm/openai/OpenAIIntegrationTest.java b/src/test/java/io/github/stefanbratanov/jvm/openai/OpenAIIntegrationTest.java index 9ca8087..a66bfcd 100644 --- a/src/test/java/io/github/stefanbratanov/jvm/openai/OpenAIIntegrationTest.java +++ b/src/test/java/io/github/stefanbratanov/jvm/openai/OpenAIIntegrationTest.java @@ -281,6 +281,16 @@ void testBatchClient() { assertThat(batch.inputFileId()).isEqualTo(inputFile.id()); assertThat(batch.errors()).isNull(); + BatchClient.PaginatedBatches paginatedBatches = + batchClient.listBatches(Optional.empty(), Optional.empty()); + + assertThat(paginatedBatches.data()).isNotEmpty(); + assertThat(paginatedBatches.firstId()).isNotNull(); + assertThat(paginatedBatches.lastId()).isNotNull(); + // assert that the batch we just created is listed + assertThat(paginatedBatches.data()) + .anySatisfy(listedBatch -> assertThat(listedBatch.id()).isEqualTo(batch.id())); + // immediately cancel the batch, because can't wait for batch to finish in tests Batch cancelledBatch = batchClient.cancelBatch(batch.id()); diff --git a/src/test/java/io/github/stefanbratanov/jvm/openai/OpenApiSpecificationValidationTest.java b/src/test/java/io/github/stefanbratanov/jvm/openai/OpenApiSpecificationValidationTest.java index 87fc730..c15c1cb 100644 --- a/src/test/java/io/github/stefanbratanov/jvm/openai/OpenApiSpecificationValidationTest.java +++ b/src/test/java/io/github/stefanbratanov/jvm/openai/OpenApiSpecificationValidationTest.java @@ -20,6 +20,7 @@ import java.util.List; import java.util.Map; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.RepeatedTest; class OpenApiSpecificationValidationTest { @@ -145,6 +146,16 @@ void validateBatch() { request, response, "Instance type (integer) does not match any allowed primitive type (allowed: [\"string\"]"); + + BatchClient.PaginatedBatches paginatedBatches = testDataUtil.randomPaginatedBatches(); + + Response listBatchesResponse = createResponseWithBody(serializeObject(paginatedBatches)); + + validate( + "/" + Endpoint.BATCHES.getPath(), + Method.GET, + listBatchesResponse, + "Instance type (integer) does not match any allowed primitive type (allowed: [\"string\"]"); } @RepeatedTest(50) @@ -200,6 +211,7 @@ void validateModerations() { validate(request, response); } + @Disabled("V1 is legacy") @RepeatedTest(50) void validateAssistants() { CreateAssistantRequest createAssistantRequest = testDataUtil.randomCreateAssistantRequest(); @@ -227,6 +239,7 @@ void validateAssistants() { validate(request, response); } + @Disabled("V1 is legacy") @RepeatedTest(50) void validateThreads() { CreateThreadRequest createThreadRequest = testDataUtil.randomCreateThreadRequest(); @@ -252,6 +265,7 @@ void validateThreads() { validate(request, response); } + @Disabled("V1 is legacy") @RepeatedTest(50) void validateMessages() { CreateMessageRequest createMessageRequest = testDataUtil.randomCreateMessageRequest(); @@ -278,6 +292,7 @@ void validateMessages() { response); } + @Disabled("V1 is legacy") @RepeatedTest(50) void validateRuns() { CreateRunRequest createRunRequest = testDataUtil.randomCreateRunRequest(); diff --git a/src/test/java/io/github/stefanbratanov/jvm/openai/TestDataUtil.java b/src/test/java/io/github/stefanbratanov/jvm/openai/TestDataUtil.java index b0e23c7..6ecec13 100644 --- a/src/test/java/io/github/stefanbratanov/jvm/openai/TestDataUtil.java +++ b/src/test/java/io/github/stefanbratanov/jvm/openai/TestDataUtil.java @@ -217,6 +217,14 @@ public CreateBatchRequest randomCreateBatchRequest() { .build(); } + public BatchClient.PaginatedBatches randomPaginatedBatches() { + return new BatchClient.PaginatedBatches( + listOf(randomInt(1, 20), this::randomBatch), + randomString(5), + randomString(5), + randomBoolean()); + } + public File randomFile() { return new File( randomString(15),