|
| 1 | +package io.github.sashirestela.openai.demo; |
| 2 | + |
| 3 | +import io.github.sashirestela.openai.domain.file.FileRequest.PurposeType; |
| 4 | +import io.github.sashirestela.openai.domain.upload.UploadCompleteRequest; |
| 5 | +import io.github.sashirestela.openai.domain.upload.UploadPartRequest; |
| 6 | +import io.github.sashirestela.openai.domain.upload.UploadRequest; |
| 7 | + |
| 8 | +import java.io.File; |
| 9 | +import java.io.FileInputStream; |
| 10 | +import java.io.FileOutputStream; |
| 11 | +import java.io.IOException; |
| 12 | +import java.nio.file.Files; |
| 13 | +import java.nio.file.Path; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +public class UploadDemo extends AbstractDemo { |
| 18 | + |
| 19 | + private String fileName; |
| 20 | + private String splitPath; |
| 21 | + private String mimeType; |
| 22 | + private long totalBytes; |
| 23 | + private int totalSplits; |
| 24 | + private String uploadId; |
| 25 | + private List<String> uploadPartIds; |
| 26 | + |
| 27 | + public void splitSourceFile() throws IOException { |
| 28 | + String fullFileName; |
| 29 | + String chunkSizeMB; |
| 30 | + |
| 31 | + while ((fullFileName = System.console().readLine("Enter the full file name to upload: ")).isBlank()); |
| 32 | + while ((chunkSizeMB = System.console().readLine("Enter the chunk size in MB: ")).isBlank()); |
| 33 | + while ((splitPath = System.console().readLine("Enter the path to put splittings: ")).isBlank()); |
| 34 | + File sourceFile = new File(fullFileName); |
| 35 | + fileName = sourceFile.getName(); |
| 36 | + mimeType = Files.probeContentType(Path.of(fullFileName)); |
| 37 | + FileInputStream fis = new FileInputStream(sourceFile); |
| 38 | + int chunkSize = Integer.parseInt(chunkSizeMB) * 1024 * 1024; |
| 39 | + byte[] buffer = new byte[chunkSize]; |
| 40 | + int bytesRead = 0; |
| 41 | + int chunkIndex = 1; |
| 42 | + while ((bytesRead = fis.read(buffer)) > 0) { |
| 43 | + File chunkFile = new File(splitPath, fileName + ".part" + chunkIndex); |
| 44 | + try (FileOutputStream fos = new FileOutputStream(chunkFile)) { |
| 45 | + fos.write(buffer, 0, bytesRead); |
| 46 | + } |
| 47 | + totalBytes += bytesRead; |
| 48 | + chunkIndex++; |
| 49 | + } |
| 50 | + fis.close(); |
| 51 | + totalSplits = chunkIndex - 1; |
| 52 | + } |
| 53 | + |
| 54 | + public void demoCreateUpload() { |
| 55 | + var uploadRequest = UploadRequest.builder() |
| 56 | + .filename(fileName) |
| 57 | + .purpose(PurposeType.ASSISTANTS) |
| 58 | + .bytes(totalBytes) |
| 59 | + .mimeType(mimeType) |
| 60 | + .build(); |
| 61 | + var uploadResponse = openAI.uploads().create(uploadRequest).join(); |
| 62 | + uploadId = uploadResponse.getId(); |
| 63 | + System.out.println(uploadResponse); |
| 64 | + } |
| 65 | + |
| 66 | + public void demoAddPartsUpload() { |
| 67 | + uploadPartIds = new ArrayList<>(); |
| 68 | + for (int i = 1; i <= totalSplits; i++) { |
| 69 | + Path partPath = Path.of(splitPath, fileName + ".part" + i); |
| 70 | + var uploadPartResponse = openAI.uploads() |
| 71 | + .addPart(uploadId, |
| 72 | + UploadPartRequest.builder().data(partPath).build()) |
| 73 | + .join(); |
| 74 | + uploadPartIds.add(uploadPartResponse.getId()); |
| 75 | + System.out.println(uploadPartResponse); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + public void demoCompleteUpload() { |
| 80 | + var uploadCompleteResponse = openAI.uploads() |
| 81 | + .complete(uploadId, |
| 82 | + UploadCompleteRequest.builder().partIds(uploadPartIds).build()) |
| 83 | + .join(); |
| 84 | + System.out.println(uploadCompleteResponse); |
| 85 | + |
| 86 | + var fileId = uploadCompleteResponse.getFile().getId(); |
| 87 | + FileDemo fileServiceDemo = new FileDemo(); |
| 88 | + fileServiceDemo.waitUntilFileIsProcessed(fileId); |
| 89 | + System.out.println("The file was processed."); |
| 90 | + fileServiceDemo.deleteFile(fileId); |
| 91 | + System.out.println("The file was deleted."); |
| 92 | + } |
| 93 | + |
| 94 | + public void demoCancelUpload() { |
| 95 | + var uploadRequest = UploadRequest.builder() |
| 96 | + .filename(fileName + ".tmp") |
| 97 | + .purpose(PurposeType.ASSISTANTS) |
| 98 | + .bytes(totalBytes) |
| 99 | + .mimeType(mimeType) |
| 100 | + .build(); |
| 101 | + var uploadResponse = openAI.uploads().create(uploadRequest).join(); |
| 102 | + var uploadIdToCancel = uploadResponse.getId(); |
| 103 | + |
| 104 | + var uploadCancelResponse = openAI.uploads().cancel(uploadIdToCancel).join(); |
| 105 | + System.out.println(uploadCancelResponse); |
| 106 | + } |
| 107 | + |
| 108 | + public static void main(String[] args) throws IOException { |
| 109 | + var demo = new UploadDemo(); |
| 110 | + |
| 111 | + demo.splitSourceFile(); |
| 112 | + |
| 113 | + demo.addTitleAction("Call Upload Create", demo::demoCreateUpload); |
| 114 | + demo.addTitleAction("Call Upload Add Parts", demo::demoAddPartsUpload); |
| 115 | + demo.addTitleAction("Call Upload Complete", demo::demoCompleteUpload); |
| 116 | + demo.addTitleAction("Call Upload Cancel", demo::demoCancelUpload); |
| 117 | + |
| 118 | + demo.run(); |
| 119 | + } |
| 120 | + |
| 121 | +} |
0 commit comments