Skip to content

Commit e3e2c09

Browse files
committed
feat: extend file upload api and file utils
1 parent 28dac29 commit e3e2c09

File tree

6 files changed

+168
-36
lines changed

6 files changed

+168
-36
lines changed

api/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>top.bella</groupId>
88
<artifactId>openai-java</artifactId>
9-
<version>0.23.65</version>
9+
<version>0.23.66</version>
1010
</parent>
1111
<packaging>jar</packaging>
1212
<artifactId>openai-api</artifactId>

client/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>top.bella</groupId>
88
<artifactId>openai-java</artifactId>
9-
<version>0.23.65</version>
9+
<version>0.23.66</version>
1010
</parent>
1111
<packaging>jar</packaging>
1212

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>top.bella</groupId>
77
<artifactId>openai-java</artifactId>
8-
<version>0.23.65</version>
8+
<version>0.23.66</version>
99
<packaging>pom</packaging>
1010
<description>openai java 版本</description>
1111
<name>openai-java</name>

service/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>top.bella</groupId>
88
<artifactId>openai-java</artifactId>
9-
<version>0.23.65</version>
9+
<version>0.23.66</version>
1010
</parent>
1111
<packaging>jar</packaging>
1212

service/src/main/java/com/theokanning/openai/service/FileUtil.java

Lines changed: 118 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,133 @@
1313
* @date 2024年05月08 17:29
1414
**/
1515
public class FileUtil {
16-
private static final Map<String, String> mimeMap = new HashMap<>();
16+
private static final Map<String, String> MIME_TO_EXTENSION = new HashMap<>();
17+
private static final Map<String, String> EXTENSION_TO_MIME = new HashMap<>();
1718

1819
static {
19-
mimeMap.put(".c", "text/x-c");
20-
mimeMap.put(".cs", "text/x-csharp");
21-
mimeMap.put(".cpp", "text/x-c++");
22-
mimeMap.put(".doc", "application/msword");
23-
mimeMap.put(".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
24-
mimeMap.put(".html", "text/html");
25-
mimeMap.put(".java", "text/x-java");
26-
mimeMap.put(".json", "application/json");
27-
mimeMap.put(".md", "text/markdown");
28-
mimeMap.put(".pdf", "application/pdf");
29-
mimeMap.put(".php", "text/x-php");
30-
mimeMap.put(".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation");
31-
mimeMap.put(".py", "text/x-python");
32-
mimeMap.put(".rb", "text/x-ruby");
33-
mimeMap.put(".tex", "text/x-tex");
34-
mimeMap.put(".txt", "text/plain");
35-
mimeMap.put(".css", "text/css");
36-
mimeMap.put(".js", "text/javascript");
37-
mimeMap.put(".sh", "application/x-sh");
38-
mimeMap.put(".ts", "application/typescript");
20+
// 初始化MIME类型到文件扩展名的映射
21+
MIME_TO_EXTENSION.put("text/html", "html");
22+
MIME_TO_EXTENSION.put("text/css", "css");
23+
MIME_TO_EXTENSION.put("text/javascript", "js");
24+
MIME_TO_EXTENSION.put("image/jpg", "jpg");
25+
MIME_TO_EXTENSION.put("image/jpeg", "jpg");
26+
MIME_TO_EXTENSION.put("image/png", "png");
27+
MIME_TO_EXTENSION.put("image/gif", "gif");
28+
MIME_TO_EXTENSION.put("image/webp", "webp");
29+
MIME_TO_EXTENSION.put("image/svg+xml", "svg");
30+
MIME_TO_EXTENSION.put("application/pdf", "pdf");
31+
MIME_TO_EXTENSION.put("application/zip", "zip");
32+
MIME_TO_EXTENSION.put("audio/mpeg", "mp3");
33+
MIME_TO_EXTENSION.put("audio/mp3", "mp3");
34+
MIME_TO_EXTENSION.put("audio/mp4", "m4a");
35+
MIME_TO_EXTENSION.put("audio/wav", "wav");
36+
MIME_TO_EXTENSION.put("audio/wave", "wav");
37+
MIME_TO_EXTENSION.put("audio/webm", "webm");
38+
MIME_TO_EXTENSION.put("audio/amr", "amr");
39+
MIME_TO_EXTENSION.put("video/mp4", "mp4");
40+
MIME_TO_EXTENSION.put("video/quicktime", "mov");
41+
MIME_TO_EXTENSION.put("video/mpeg", "mpeg");
42+
43+
// Microsoft Word文档
44+
MIME_TO_EXTENSION.put("application/msword", "doc");
45+
MIME_TO_EXTENSION.put("application/json", "json");
46+
MIME_TO_EXTENSION.put("application/vnd.openxmlformats-officedocument.wordprocessingml.document", "docx");
47+
48+
// Microsoft Excel电子表格
49+
MIME_TO_EXTENSION.put("application/vnd.ms-excel", "xls");
50+
MIME_TO_EXTENSION.put("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx");
51+
52+
MIME_TO_EXTENSION.put("application/vnd.ms-powerpoint", "ppt");
53+
MIME_TO_EXTENSION.put("application/vnd.openxmlformats-officedocument.presentationml.presentation", "pptx");
54+
55+
MIME_TO_EXTENSION.put("text/plain", "txt");
56+
MIME_TO_EXTENSION.put("text/markdown", "md");
57+
MIME_TO_EXTENSION.put("text/csv", "csv");
58+
59+
MIME_TO_EXTENSION.put("application/xml", "xml");
60+
MIME_TO_EXTENSION.put("application/epub+zip", "epub");
61+
MIME_TO_EXTENSION.put("message/rfc822", "eml");
62+
MIME_TO_EXTENSION.put("application/vnd.ms-outlook", "msg");
63+
64+
// 编程语言相关的MIME类型
65+
MIME_TO_EXTENSION.put("text/x-c", "c");
66+
MIME_TO_EXTENSION.put("text/x-csharp", "cs");
67+
MIME_TO_EXTENSION.put("text/x-c++", "cpp");
68+
MIME_TO_EXTENSION.put("text/x-java", "java");
69+
MIME_TO_EXTENSION.put("text/x-php", "php");
70+
MIME_TO_EXTENSION.put("text/x-python", "py");
71+
MIME_TO_EXTENSION.put("text/x-ruby", "rb");
72+
MIME_TO_EXTENSION.put("text/x-tex", "tex");
73+
MIME_TO_EXTENSION.put("application/x-sh", "sh");
74+
MIME_TO_EXTENSION.put("application/typescript", "ts");
75+
// 可以根据需要添加更多的MIME类型
76+
77+
EXTENSION_TO_MIME.put("html", "text/html");
78+
EXTENSION_TO_MIME.put("css", "text/css");
79+
EXTENSION_TO_MIME.put("js", "text/javascript");
80+
EXTENSION_TO_MIME.put("jpg", "image/jpg");
81+
EXTENSION_TO_MIME.put("jpeg", "image/jpeg");
82+
EXTENSION_TO_MIME.put("png", "image/png");
83+
EXTENSION_TO_MIME.put("gif", "image/gif");
84+
EXTENSION_TO_MIME.put("webp", "image/webp");
85+
EXTENSION_TO_MIME.put("svg", "image/svg+xml");
86+
EXTENSION_TO_MIME.put("pdf", "application/pdf");
87+
EXTENSION_TO_MIME.put("zip", "application/zip");
88+
EXTENSION_TO_MIME.put("mp3", "audio/mpeg");
89+
EXTENSION_TO_MIME.put("m4a", "audio/mp4");
90+
EXTENSION_TO_MIME.put("wav", "audio/wav");
91+
EXTENSION_TO_MIME.put("webm", "audio/webm");
92+
EXTENSION_TO_MIME.put("amr", "audio/amr");
93+
EXTENSION_TO_MIME.put("mp4", "video/mp4");
94+
EXTENSION_TO_MIME.put("mov", "video/quicktime");
95+
EXTENSION_TO_MIME.put("mpeg", "video/mpeg");
96+
EXTENSION_TO_MIME.put("c", "text/x-c");
97+
EXTENSION_TO_MIME.put("cs", "text/x-csharp");
98+
EXTENSION_TO_MIME.put("cpp", "text/x-c++");
99+
EXTENSION_TO_MIME.put("java", "text/x-java");
100+
EXTENSION_TO_MIME.put("php", "text/x-php");
101+
EXTENSION_TO_MIME.put("py", "text/x-python");
102+
EXTENSION_TO_MIME.put("rb", "text/x-ruby");
103+
EXTENSION_TO_MIME.put("tex", "text/x-tex");
104+
EXTENSION_TO_MIME.put("sh", "application/x-sh");
105+
EXTENSION_TO_MIME.put("ts", "application/typescript");
106+
EXTENSION_TO_MIME.put("doc", "application/msword");
107+
EXTENSION_TO_MIME.put("json", "application/json");
108+
EXTENSION_TO_MIME.put("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
109+
EXTENSION_TO_MIME.put("xls", "application/vnd.ms-excel");
110+
EXTENSION_TO_MIME.put("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
111+
EXTENSION_TO_MIME.put("ppt", "application/vnd.ms-powerpoint");
112+
EXTENSION_TO_MIME.put("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation");
113+
EXTENSION_TO_MIME.put("txt", "text/plain");
114+
EXTENSION_TO_MIME.put("md", "text/markdown");
115+
EXTENSION_TO_MIME.put("csv", "text/csv");
116+
EXTENSION_TO_MIME.put("xml", "application/xml");
117+
EXTENSION_TO_MIME.put("epub", "application/epub+zip");
118+
EXTENSION_TO_MIME.put("eml", "message/rfc822");
119+
EXTENSION_TO_MIME.put("msg", "application/vnd.ms-outlook");
120+
121+
39122
}
40123

41124
public static MediaType getFileUploadMediaType(String fileName) {
42-
return MediaType.parse(mimeMap.getOrDefault(getFileExtension(fileName), "text/plain"));
125+
return MediaType.parse(EXTENSION_TO_MIME.getOrDefault(getFileExtension(fileName), "text/plain"));
126+
}
127+
128+
public static String getSubType(MediaType mediaType) {
129+
if(mediaType == null) {
130+
return null;
131+
}
132+
return mediaType.subtype();
133+
}
134+
135+
public static String extraPureMediaType(MediaType mediaType) {
136+
return mediaType.type() + "/" + mediaType.subtype();
43137
}
44138

45139
public static String getFileExtension(String filename) {
46140
int dotIndex = filename.lastIndexOf('.');
47141
if (dotIndex >= 0 && dotIndex < filename.length() - 1) {
48-
return filename.substring(dotIndex); // Includes the dot
142+
return filename.substring(dotIndex + 1); // Includes the dot
49143
}
50144
return ""; // No extension found
51145
}
@@ -59,7 +153,7 @@ public static String getFileExtension(String filename) {
59153
public static byte[] readAllBytes(InputStream inputStream) {
60154
try (ByteArrayOutputStream buffer = new ByteArrayOutputStream()) {
61155
int nRead;
62-
byte[] data = new byte[2048];
156+
byte[] data = new byte[8192];
63157
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
64158
buffer.write(data, 0, nRead);
65159
}

service/src/main/java/com/theokanning/openai/service/OpenAiService.java

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
import com.fasterxml.jackson.databind.ObjectMapper;
88
import com.fasterxml.jackson.databind.node.ObjectNode;
99
import com.fasterxml.jackson.databind.node.TextNode;
10-
import com.theokanning.openai.*;
10+
import com.theokanning.openai.DeleteResult;
11+
import com.theokanning.openai.ListSearchParameters;
12+
import com.theokanning.openai.OpenAiError;
13+
import com.theokanning.openai.OpenAiHttpException;
14+
import com.theokanning.openai.OpenAiResponse;
1115
import com.theokanning.openai.assistants.assistant.Assistant;
1216
import com.theokanning.openai.assistants.assistant.AssistantRequest;
1317
import com.theokanning.openai.assistants.assistant.ModifyAssistantRequest;
@@ -16,7 +20,11 @@
1620
import com.theokanning.openai.assistants.message.MessageListSearchParameters;
1721
import com.theokanning.openai.assistants.message.MessageRequest;
1822
import com.theokanning.openai.assistants.message.ModifyMessageRequest;
19-
import com.theokanning.openai.assistants.run.*;
23+
import com.theokanning.openai.assistants.run.CreateThreadAndRunRequest;
24+
import com.theokanning.openai.assistants.run.ModifyRunRequest;
25+
import com.theokanning.openai.assistants.run.Run;
26+
import com.theokanning.openai.assistants.run.RunCreateRequest;
27+
import com.theokanning.openai.assistants.run.SubmitToolOutputsRequest;
2028
import com.theokanning.openai.assistants.run_step.RunStep;
2129
import com.theokanning.openai.assistants.thread.Thread;
2230
import com.theokanning.openai.assistants.thread.ThreadRequest;
@@ -26,7 +34,11 @@
2634
import com.theokanning.openai.assistants.vector_store_file.VectorStoreFile;
2735
import com.theokanning.openai.assistants.vector_store_file_batch.VectorStoreFilesBatch;
2836
import com.theokanning.openai.assistants.vector_store_file_batch.VectorStoreFilesBatchRequest;
29-
import com.theokanning.openai.audio.*;
37+
import com.theokanning.openai.audio.CreateSpeechRequest;
38+
import com.theokanning.openai.audio.CreateTranscriptionRequest;
39+
import com.theokanning.openai.audio.CreateTranslationRequest;
40+
import com.theokanning.openai.audio.TranscriptionResult;
41+
import com.theokanning.openai.audio.TranslationResult;
3042
import com.theokanning.openai.batch.Batch;
3143
import com.theokanning.openai.batch.BatchRequest;
3244
import com.theokanning.openai.billing.BillingUsage;
@@ -36,7 +48,14 @@
3648
import com.theokanning.openai.completion.CompletionChunk;
3749
import com.theokanning.openai.completion.CompletionRequest;
3850
import com.theokanning.openai.completion.CompletionResult;
39-
import com.theokanning.openai.completion.chat.*;
51+
import com.theokanning.openai.completion.chat.AssistantMessage;
52+
import com.theokanning.openai.completion.chat.ChatCompletionChoice;
53+
import com.theokanning.openai.completion.chat.ChatCompletionChunk;
54+
import com.theokanning.openai.completion.chat.ChatCompletionRequest;
55+
import com.theokanning.openai.completion.chat.ChatCompletionResult;
56+
import com.theokanning.openai.completion.chat.ChatFunction;
57+
import com.theokanning.openai.completion.chat.ChatFunctionCall;
58+
import com.theokanning.openai.completion.chat.ChatToolCall;
4059
import com.theokanning.openai.embedding.EmbeddingRequest;
4160
import com.theokanning.openai.embedding.EmbeddingResult;
4261
import com.theokanning.openai.file.File;
@@ -61,7 +80,12 @@
6180
import io.reactivex.BackpressureStrategy;
6281
import io.reactivex.Flowable;
6382
import io.reactivex.Single;
64-
import okhttp3.*;
83+
import okhttp3.ConnectionPool;
84+
import okhttp3.MediaType;
85+
import okhttp3.MultipartBody;
86+
import okhttp3.OkHttpClient;
87+
import okhttp3.RequestBody;
88+
import okhttp3.ResponseBody;
6589
import okio.BufferedSink;
6690
import okio.Okio;
6791
import okio.Source;
@@ -72,16 +96,20 @@
7296
import retrofit2.converter.jackson.JacksonConverterFactory;
7397

7498
import javax.validation.constraints.NotNull;
75-
import java.io.FileInputStream;
76-
import java.io.FileNotFoundException;
7799
import java.io.IOException;
78100
import java.io.InputStream;
79101
import java.nio.file.Files;
80102
import java.nio.file.Path;
81103
import java.nio.file.Paths;
82104
import java.time.Duration;
83105
import java.time.LocalDate;
84-
import java.util.*;
106+
import java.util.ArrayList;
107+
import java.util.Comparator;
108+
import java.util.HashMap;
109+
import java.util.List;
110+
import java.util.Map;
111+
import java.util.Objects;
112+
import java.util.Optional;
85113
import java.util.concurrent.ExecutorService;
86114
import java.util.concurrent.TimeUnit;
87115
import java.util.function.BiConsumer;
@@ -277,6 +305,16 @@ public Flowable<CompletionChunk> streamCompletion(CompletionRequest request) {
277305
return stream(api.createCompletionStream(request), CompletionChunk.class);
278306
}
279307

308+
/**
309+
* Upload a file using bytes.
310+
*/
311+
public File uploadFile(String purpose, byte[] bytes, String filename) {
312+
RequestBody purposeBody = RequestBody.create(MultipartBody.FORM, purpose);
313+
RequestBody fileBody = RequestBody.create(FileUtil.getFileUploadMediaType(filename), bytes);
314+
MultipartBody.Part body = MultipartBody.Part.createFormData("file", filename, fileBody);
315+
return execute(api.uploadFile(purposeBody, body));
316+
}
317+
280318
/**
281319
* Upload a file using file path.
282320
*/

0 commit comments

Comments
 (0)