|
| 1 | +package com.openai.example; |
| 2 | + |
| 3 | +import com.openai.client.OpenAIClientAsync; |
| 4 | +import com.openai.client.okhttp.OpenAIOkHttpClientAsync; |
| 5 | +import com.openai.models.batches.Batch; |
| 6 | +import com.openai.models.batches.BatchCreateParams; |
| 7 | +import com.openai.models.batches.BatchCreateParams.CompletionWindow; |
| 8 | +import com.openai.models.batches.BatchCreateParams.Endpoint; |
| 9 | +import com.openai.models.files.FileCreateParams; |
| 10 | +import com.openai.models.files.FileObject; |
| 11 | +import com.openai.models.files.FilePurpose; |
| 12 | +import java.io.IOException; |
| 13 | +import java.io.UncheckedIOException; |
| 14 | +import java.net.URISyntaxException; |
| 15 | +import java.nio.file.Path; |
| 16 | +import java.nio.file.Paths; |
| 17 | +import java.util.concurrent.CompletableFuture; |
| 18 | +import java.util.concurrent.Executor; |
| 19 | +import java.util.concurrent.TimeUnit; |
| 20 | + |
| 21 | +public final class BatchProcessingAsyncExample { |
| 22 | + private BatchProcessingAsyncExample() {} |
| 23 | + |
| 24 | + public static void main(String[] args) throws URISyntaxException { |
| 25 | + // Configures using one of: |
| 26 | + // - The `OPENAI_API_KEY` environment variable |
| 27 | + // - The `OPENAI_BASE_URL` and `AZURE_OPENAI_KEY` environment variables |
| 28 | + OpenAIClientAsync client = OpenAIOkHttpClientAsync.fromEnv(); |
| 29 | + |
| 30 | + ClassLoader classloader = Thread.currentThread().getContextClassLoader(); |
| 31 | + Path requestsPath = Paths.get(classloader.getResource("requests.jsonl").toURI()); |
| 32 | + |
| 33 | + FileCreateParams fileParams = FileCreateParams.builder() |
| 34 | + .purpose(FilePurpose.BATCH) |
| 35 | + .file(requestsPath) |
| 36 | + .build(); |
| 37 | + CompletableFuture<FileObject> fileFuture = client.files().create(fileParams); |
| 38 | + |
| 39 | + CompletableFuture<Batch> batchFuture = fileFuture.thenComposeAsync(file -> { |
| 40 | + BatchCreateParams batchParams = BatchCreateParams.builder() |
| 41 | + .inputFileId(file.id()) |
| 42 | + .endpoint(Endpoint.V1_CHAT_COMPLETIONS) |
| 43 | + .completionWindow(CompletionWindow._24H) |
| 44 | + .build(); |
| 45 | + return client.batches().create(batchParams); |
| 46 | + }); |
| 47 | + |
| 48 | + Executor delayedExecutor = CompletableFuture.delayedExecutor(1, TimeUnit.MINUTES); |
| 49 | + batchFuture = pollBatch(client, batchFuture, delayedExecutor); |
| 50 | + |
| 51 | + batchFuture |
| 52 | + .thenCompose( |
| 53 | + batch -> client.files().content(batch.outputFileId().orElseThrow())) |
| 54 | + .thenAccept(response -> { |
| 55 | + try (response) { |
| 56 | + response.body().transferTo(System.out); |
| 57 | + } catch (IOException e) { |
| 58 | + throw new UncheckedIOException(e); |
| 59 | + } |
| 60 | + }) |
| 61 | + .join(); |
| 62 | + } |
| 63 | + |
| 64 | + private static CompletableFuture<Batch> pollBatch( |
| 65 | + OpenAIClientAsync client, CompletableFuture<Batch> batchFuture, Executor delayedExecutor) { |
| 66 | + return batchFuture.thenComposeAsync(batch -> { |
| 67 | + if (batch.outputFileId().isPresent()) { |
| 68 | + return CompletableFuture.completedFuture(batch); |
| 69 | + } |
| 70 | + |
| 71 | + System.out.println("Polling..."); |
| 72 | + return pollBatch( |
| 73 | + client, |
| 74 | + CompletableFuture.supplyAsync(() -> null, delayedExecutor) |
| 75 | + .thenComposeAsync((unused) -> client.batches().retrieve(batch.id())), |
| 76 | + delayedExecutor); |
| 77 | + }); |
| 78 | + } |
| 79 | +} |
0 commit comments