-
Notifications
You must be signed in to change notification settings - Fork 26
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
5db4c2c
commit ac55a9b
Showing
17 changed files
with
442 additions
and
12 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
121 changes: 121 additions & 0 deletions
121
src/demo/java/io/github/sashirestela/openai/demo/UploadDemo.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,121 @@ | ||
package io.github.sashirestela.openai.demo; | ||
|
||
import io.github.sashirestela.openai.domain.file.FileRequest.PurposeType; | ||
import io.github.sashirestela.openai.domain.upload.UploadCompleteRequest; | ||
import io.github.sashirestela.openai.domain.upload.UploadPartRequest; | ||
import io.github.sashirestela.openai.domain.upload.UploadRequest; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class UploadDemo extends AbstractDemo { | ||
|
||
private String fileName; | ||
private String splitPath; | ||
private String mimeType; | ||
private long totalBytes; | ||
private int totalSplits; | ||
private String uploadId; | ||
private List<String> uploadPartIds; | ||
|
||
public void splitSourceFile() throws IOException { | ||
String fullFileName; | ||
String chunkSizeMB; | ||
|
||
while ((fullFileName = System.console().readLine("Enter the full file name to upload: ")).isBlank()); | ||
while ((chunkSizeMB = System.console().readLine("Enter the chunk size in MB: ")).isBlank()); | ||
while ((splitPath = System.console().readLine("Enter the path to put splittings: ")).isBlank()); | ||
File sourceFile = new File(fullFileName); | ||
fileName = sourceFile.getName(); | ||
mimeType = Files.probeContentType(Path.of(fullFileName)); | ||
FileInputStream fis = new FileInputStream(sourceFile); | ||
int chunkSize = Integer.parseInt(chunkSizeMB) * 1024 * 1024; | ||
byte[] buffer = new byte[chunkSize]; | ||
int bytesRead = 0; | ||
int chunkIndex = 1; | ||
while ((bytesRead = fis.read(buffer)) > 0) { | ||
File chunkFile = new File(splitPath, fileName + ".part" + chunkIndex); | ||
try (FileOutputStream fos = new FileOutputStream(chunkFile)) { | ||
fos.write(buffer, 0, bytesRead); | ||
} | ||
totalBytes += bytesRead; | ||
chunkIndex++; | ||
} | ||
fis.close(); | ||
totalSplits = chunkIndex - 1; | ||
} | ||
|
||
public void demoCreateUpload() { | ||
var uploadRequest = UploadRequest.builder() | ||
.filename(fileName) | ||
.purpose(PurposeType.ASSISTANTS) | ||
.bytes(totalBytes) | ||
.mimeType(mimeType) | ||
.build(); | ||
var uploadResponse = openAI.uploads().create(uploadRequest).join(); | ||
uploadId = uploadResponse.getId(); | ||
System.out.println(uploadResponse); | ||
} | ||
|
||
public void demoAddPartsUpload() { | ||
uploadPartIds = new ArrayList<>(); | ||
for (int i = 1; i <= totalSplits; i++) { | ||
Path partPath = Path.of(splitPath, fileName + ".part" + i); | ||
var uploadPartResponse = openAI.uploads() | ||
.addPart(uploadId, | ||
UploadPartRequest.builder().data(partPath).build()) | ||
.join(); | ||
uploadPartIds.add(uploadPartResponse.getId()); | ||
System.out.println(uploadPartResponse); | ||
} | ||
} | ||
|
||
public void demoCompleteUpload() { | ||
var uploadCompleteResponse = openAI.uploads() | ||
.complete(uploadId, | ||
UploadCompleteRequest.builder().partIds(uploadPartIds).build()) | ||
.join(); | ||
System.out.println(uploadCompleteResponse); | ||
|
||
var fileId = uploadCompleteResponse.getFile().getId(); | ||
FileDemo fileServiceDemo = new FileDemo(); | ||
fileServiceDemo.waitUntilFileIsProcessed(fileId); | ||
System.out.println("The file was processed."); | ||
fileServiceDemo.deleteFile(fileId); | ||
System.out.println("The file was deleted."); | ||
} | ||
|
||
public void demoCancelUpload() { | ||
var uploadRequest = UploadRequest.builder() | ||
.filename(fileName + ".tmp") | ||
.purpose(PurposeType.ASSISTANTS) | ||
.bytes(totalBytes) | ||
.mimeType(mimeType) | ||
.build(); | ||
var uploadResponse = openAI.uploads().create(uploadRequest).join(); | ||
var uploadIdToCancel = uploadResponse.getId(); | ||
|
||
var uploadCancelResponse = openAI.uploads().cancel(uploadIdToCancel).join(); | ||
System.out.println(uploadCancelResponse); | ||
} | ||
|
||
public static void main(String[] args) throws IOException { | ||
var demo = new UploadDemo(); | ||
|
||
demo.splitSourceFile(); | ||
|
||
demo.addTitleAction("Call Upload Create", demo::demoCreateUpload); | ||
demo.addTitleAction("Call Upload Add Parts", demo::demoAddPartsUpload); | ||
demo.addTitleAction("Call Upload Complete", demo::demoCompleteUpload); | ||
demo.addTitleAction("Call Upload Cancel", demo::demoCancelUpload); | ||
|
||
demo.run(); | ||
} | ||
|
||
} |
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
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
43 changes: 43 additions & 0 deletions
43
src/main/java/io/github/sashirestela/openai/domain/upload/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,43 @@ | ||
package io.github.sashirestela.openai.domain.upload; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.databind.PropertyNamingStrategies; | ||
import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
import io.github.sashirestela.openai.domain.file.FileResponse; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.ToString; | ||
|
||
@NoArgsConstructor | ||
@Getter | ||
@ToString | ||
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) | ||
public class Upload { | ||
|
||
private String id; | ||
private Long createdAt; | ||
private String filename; | ||
private Long bytes; | ||
private String purpose; | ||
private UploadStatus status; | ||
private Long expiresAt; | ||
private String object; | ||
private FileResponse file; | ||
|
||
public enum UploadStatus { | ||
|
||
@JsonProperty("pending") | ||
PENDING, | ||
|
||
@JsonProperty("completed") | ||
COMPLETED, | ||
|
||
@JsonProperty("cancelled") | ||
CANCELLED, | ||
|
||
@JsonProperty("expired") | ||
EXPIRED; | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.