Skip to content

Commit d910267

Browse files
docs: add batch processing examples (openai#462) (openai#476)
* docs: add batch processing example to README Adds a comprehensive example demonstrating how to use the batch processing API, including file upload, batch creation, status checking, and result processing. The example shows the complete workflow for processing multiple requests efficiently through the batch API. * docs: add batch processing example to README Adds a comprehensive example demonstrating how to use the batch processing API, including file upload, batch creation, status checking, and result processing. The example shows the complete workflow for processing multiple requests efficiently through the batch API. * docs: restructure --------- Co-authored-by: Tomer Aberbach <[email protected]>
1 parent c054cea commit d910267

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.openai.example;
2+
3+
import com.openai.client.OpenAIClient;
4+
import com.openai.client.okhttp.OpenAIOkHttpClient;
5+
import com.openai.core.http.HttpResponse;
6+
import com.openai.models.batches.Batch;
7+
import com.openai.models.batches.BatchCreateParams;
8+
import com.openai.models.batches.BatchCreateParams.CompletionWindow;
9+
import com.openai.models.batches.BatchCreateParams.Endpoint;
10+
import com.openai.models.files.FileCreateParams;
11+
import com.openai.models.files.FileObject;
12+
import com.openai.models.files.FilePurpose;
13+
import java.io.IOException;
14+
import java.net.URISyntaxException;
15+
import java.nio.file.Path;
16+
import java.nio.file.Paths;
17+
18+
public final class BatchProcessingExample {
19+
private BatchProcessingExample() {}
20+
21+
public static void main(String[] args) throws URISyntaxException, InterruptedException, IOException {
22+
// Configures using one of:
23+
// - The `OPENAI_API_KEY` environment variable
24+
// - The `OPENAI_BASE_URL` and `AZURE_OPENAI_KEY` environment variables
25+
OpenAIClient client = OpenAIOkHttpClient.fromEnv();
26+
27+
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
28+
Path requestsPath = Paths.get(classloader.getResource("requests.jsonl").toURI());
29+
30+
FileCreateParams fileParams = FileCreateParams.builder()
31+
.purpose(FilePurpose.BATCH)
32+
.file(requestsPath)
33+
.build();
34+
FileObject file = client.files().create(fileParams);
35+
36+
BatchCreateParams batchParams = BatchCreateParams.builder()
37+
.inputFileId(file.id())
38+
.endpoint(Endpoint.V1_CHAT_COMPLETIONS)
39+
.completionWindow(CompletionWindow._24H)
40+
.build();
41+
Batch batch = client.batches().create(batchParams);
42+
43+
while (batch.outputFileId().isEmpty()) {
44+
System.out.println("Polling...");
45+
Thread.sleep(60_000);
46+
batch = client.batches().retrieve(batch.id());
47+
}
48+
49+
try (HttpResponse response = client.files().content(batch.outputFileId().orElseThrow())) {
50+
response.body().transferTo(System.out);
51+
}
52+
}
53+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{"model": "gpt-4", "messages": [{"role": "user", "content": "Hello"}]}
2+
{"model": "gpt-4", "messages": [{"role": "user", "content": "World"}]}

0 commit comments

Comments
 (0)