-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
71ffbbb
commit 7d6bb0e
Showing
11 changed files
with
399 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/java/io/github/stefanbratanov/jvm/openai/CompleteUploadRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package io.github.stefanbratanov.jvm.openai; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public record CompleteUploadRequest(List<String> partIds, Optional<String> md5) { | ||
|
||
public static Builder newBuilder() { | ||
return new Builder(); | ||
} | ||
|
||
public static class Builder { | ||
|
||
private List<String> partIds; | ||
|
||
private Optional<String> md5 = Optional.empty(); | ||
|
||
/** | ||
* @param partIds The ordered list of Part IDs. | ||
*/ | ||
public Builder partIds(List<String> partIds) { | ||
this.partIds = partIds; | ||
return this; | ||
} | ||
|
||
/** | ||
* @param md5 The optional md5 checksum for the file contents to verify if the bytes uploaded | ||
* matches what you expect. | ||
*/ | ||
public Builder md5(String md5) { | ||
this.md5 = Optional.of(md5); | ||
return this; | ||
} | ||
|
||
public CompleteUploadRequest build() { | ||
return new CompleteUploadRequest(partIds, md5); | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/main/java/io/github/stefanbratanov/jvm/openai/CreateUploadRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package io.github.stefanbratanov.jvm.openai; | ||
|
||
public record CreateUploadRequest(String filename, String purpose, int bytes, String mimeType) { | ||
|
||
public static Builder newBuilder() { | ||
return new Builder(); | ||
} | ||
|
||
public static class Builder { | ||
|
||
private String filename; | ||
private String purpose; | ||
private int bytes; | ||
private String mimeType; | ||
|
||
/** | ||
* @param filename The name of the file to upload. | ||
*/ | ||
public Builder filename(String filename) { | ||
this.filename = filename; | ||
return this; | ||
} | ||
|
||
/** | ||
* @param purpose The intended purpose of the uploaded file. | ||
*/ | ||
public Builder purpose(String purpose) { | ||
this.purpose = purpose; | ||
return this; | ||
} | ||
|
||
/** | ||
* @param purpose The intended purpose of the uploaded file. | ||
*/ | ||
public Builder purpose(Purpose purpose) { | ||
this.purpose = purpose.getId(); | ||
return this; | ||
} | ||
|
||
/** | ||
* @param bytes The number of bytes in the file you are uploading. | ||
*/ | ||
public Builder bytes(int bytes) { | ||
this.bytes = bytes; | ||
return this; | ||
} | ||
|
||
/** | ||
* @param mimeType The MIME type of the file. | ||
* <p>This must fall within the supported MIME types for your file purpose. See the | ||
* supported MIME types for assistants and vision. | ||
*/ | ||
public Builder mimeType(String mimeType) { | ||
this.mimeType = mimeType; | ||
return this; | ||
} | ||
|
||
public CreateUploadRequest build() { | ||
return new CreateUploadRequest(filename, purpose, bytes, mimeType); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/io/github/stefanbratanov/jvm/openai/Upload.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package io.github.stefanbratanov.jvm.openai; | ||
|
||
public record Upload( | ||
String id, | ||
int createdAt, | ||
String filename, | ||
int bytes, | ||
String purpose, | ||
String status, | ||
int expiresAt, | ||
File file) {} |
3 changes: 3 additions & 0 deletions
3
src/main/java/io/github/stefanbratanov/jvm/openai/UploadPart.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package io.github.stefanbratanov.jvm.openai; | ||
|
||
public record UploadPart(String id, int createdAt, String uploadId) {} |
119 changes: 119 additions & 0 deletions
119
src/main/java/io/github/stefanbratanov/jvm/openai/UploadsClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package io.github.stefanbratanov.jvm.openai; | ||
|
||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpRequest.BodyPublishers; | ||
import java.net.http.HttpResponse; | ||
import java.nio.file.Path; | ||
import java.time.Duration; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Allows you to upload large files in multiple parts. | ||
* | ||
* <p>Based on <a href="https://platform.openai.com/docs/api-reference/uploads">Uploads</a> | ||
*/ | ||
public final class UploadsClient extends OpenAIClient { | ||
|
||
private static final String PARTS_SEGMENT = "/parts"; | ||
private static final String COMPLETE_SEGMENT = "/complete"; | ||
private static final String CANCEL_SEGMENT = "/cancel"; | ||
|
||
private final URI baseUrl; | ||
|
||
UploadsClient( | ||
URI baseUrl, | ||
String[] authenticationHeaders, | ||
HttpClient httpClient, | ||
Optional<Duration> requestTimeout) { | ||
super(authenticationHeaders, httpClient, requestTimeout); | ||
this.baseUrl = baseUrl; | ||
} | ||
|
||
/** | ||
* Creates an intermediate Upload object that you can add Parts to. Currently, an Upload can | ||
* accept at most 8 GB in total and expires after an hour after you create it. | ||
* | ||
* <p>Once you complete the Upload, we will create a File object that contains all the parts you | ||
* uploaded. This File is usable in the rest of our platform as a regular File object. | ||
* | ||
* @throws OpenAIException in case of API errors | ||
*/ | ||
public Upload createUpload(CreateUploadRequest request) { | ||
HttpRequest httpRequest = | ||
newHttpRequestBuilder(Constants.CONTENT_TYPE_HEADER, Constants.JSON_MEDIA_TYPE) | ||
.uri(baseUrl.resolve(Endpoint.UPLOADS.getPath())) | ||
.POST(createBodyPublisher(request)) | ||
.build(); | ||
HttpResponse<byte[]> httpResponse = sendHttpRequest(httpRequest); | ||
return deserializeResponse(httpResponse.body(), Upload.class); | ||
} | ||
|
||
/** | ||
* Adds a Part to an Upload object. A Part represents a chunk of bytes from the file you are | ||
* trying to upload. | ||
* | ||
* <p>Each Part can be at most 64 MB, and you can add Parts until you hit the Upload maximum of 8 | ||
* GB. | ||
* | ||
* <p>It is possible to add multiple Parts in parallel. You can decide the intended order of the | ||
* Parts when you complete the Upload. | ||
* | ||
* @param uploadId The ID of the Upload. | ||
* @param data The chunk of bytes for this Part. | ||
* @throws OpenAIException in case of API errors | ||
*/ | ||
public UploadPart addUploadPart(String uploadId, Path data) { | ||
MultipartBodyPublisher multipartBodyPublisher = | ||
MultipartBodyPublisher.newBuilder().filePart("data", data).build(); | ||
HttpRequest httpRequest = | ||
newHttpRequestBuilder( | ||
Constants.CONTENT_TYPE_HEADER, multipartBodyPublisher.getContentTypeHeader()) | ||
.uri(baseUrl.resolve(Endpoint.UPLOADS.getPath() + "/" + uploadId + PARTS_SEGMENT)) | ||
.POST(multipartBodyPublisher) | ||
.build(); | ||
HttpResponse<byte[]> httpResponse = sendHttpRequest(httpRequest); | ||
return deserializeResponse(httpResponse.body(), UploadPart.class); | ||
} | ||
|
||
/** | ||
* Completes the Upload. | ||
* | ||
* <p>Within the returned Upload object, there is a nested File object that is ready to use in the | ||
* rest of the platform. | ||
* | ||
* <p>You can specify the order of the Parts by passing in an ordered list of the Part IDs. | ||
* | ||
* <p>The number of bytes uploaded upon completion must match the number of bytes initially | ||
* specified when creating the Upload object. No Parts may be added after an Upload is completed. | ||
* | ||
* @param uploadId The ID of the Upload. | ||
* @throws OpenAIException in case of API errors | ||
*/ | ||
public Upload completeUpload(String uploadId, CompleteUploadRequest request) { | ||
HttpRequest httpRequest = | ||
newHttpRequestBuilder(Constants.CONTENT_TYPE_HEADER, Constants.JSON_MEDIA_TYPE) | ||
.uri(baseUrl.resolve(Endpoint.UPLOADS.getPath() + "/" + uploadId + COMPLETE_SEGMENT)) | ||
.POST(createBodyPublisher(request)) | ||
.build(); | ||
HttpResponse<byte[]> httpResponse = sendHttpRequest(httpRequest); | ||
return deserializeResponse(httpResponse.body(), Upload.class); | ||
} | ||
|
||
/** | ||
* Cancels the Upload. No Parts may be added after an Upload is cancelled. | ||
* | ||
* @param uploadId The ID of the Upload. | ||
* @throws OpenAIException in case of API errors | ||
*/ | ||
public Upload cancelUpload(String uploadId) { | ||
HttpRequest httpRequest = | ||
newHttpRequestBuilder() | ||
.uri(baseUrl.resolve(Endpoint.UPLOADS.getPath() + "/" + uploadId + CANCEL_SEGMENT)) | ||
.POST(BodyPublishers.noBody()) | ||
.build(); | ||
HttpResponse<byte[]> httpResponse = sendHttpRequest(httpRequest); | ||
return deserializeResponse(httpResponse.body(), Upload.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.