Skip to content

Commit 7f58dcb

Browse files
Jiakaicclaude
authored andcommitted
feat: extend file list API with enhanced parameters
Add support for querying specific files with URL generation options: - Add FileListRequest entity with fileIds, getUrl, and expires parameters - Extend OpenAiApi with POST /files/list endpoint - Add overloaded listFiles method in OpenAiService - Include comprehensive tests for new functionality 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent fe62767 commit 7f58dcb

File tree

9 files changed

+102
-5
lines changed

9 files changed

+102
-5
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.89</version>
9+
<version>0.23.90</version>
1010
</parent>
1111
<packaging>jar</packaging>
1212
<artifactId>openai-api</artifactId>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.theokanning.openai.file;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Builder;
6+
import lombok.Data;
7+
import lombok.NoArgsConstructor;
8+
9+
import java.util.List;
10+
11+
/**
12+
* Request object for listing files with specific parameters
13+
*/
14+
@Data
15+
@Builder
16+
@AllArgsConstructor
17+
@NoArgsConstructor
18+
public class FileListRequest {
19+
20+
/**
21+
* List of file IDs to retrieve
22+
*/
23+
@JsonProperty("file_ids")
24+
private List<String> fileIds;
25+
26+
/**
27+
* Whether to include file URLs in the response
28+
*/
29+
@JsonProperty("get_url")
30+
private Boolean getUrl;
31+
32+
/**
33+
* URL expiration time in seconds (only used when getUrl is true)
34+
*/
35+
@JsonProperty("expires")
36+
private Long expires;
37+
}

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.89</version>
9+
<version>0.23.90</version>
1010
</parent>
1111
<packaging>jar</packaging>
1212

client/src/main/java/com/theokanning/openai/client/OpenAiApi.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.theokanning.openai.embedding.EmbeddingRequest;
3434
import com.theokanning.openai.embedding.EmbeddingResult;
3535
import com.theokanning.openai.file.File;
36+
import com.theokanning.openai.file.FileListRequest;
3637
import com.theokanning.openai.file.FileUrl;
3738
import com.theokanning.openai.fine_tuning.FineTuningEvent;
3839
import com.theokanning.openai.fine_tuning.FineTuningJob;
@@ -101,6 +102,9 @@ public interface OpenAiApi {
101102
@GET("files")
102103
Single<OpenAiResponse<File>> listFiles();
103104

105+
@POST("files/list")
106+
Single<List<File>> listFiles(@Body FileListRequest request);
107+
104108
@DELETE("files/{file_id}")
105109
Single<DeleteResult> deleteFile(@Path("file_id") String fileId);
106110

example/pom.xml

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

77
<groupId>io.github.ke</groupId>
88
<artifactId>example</artifactId>
9-
<version>0.23.89</version>
9+
<version>0.23.90</version>
1010
<name>example</name>
1111

1212
<properties>

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.89</version>
8+
<version>0.23.90</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.89</version>
9+
<version>0.23.90</version>
1010
</parent>
1111
<packaging>jar</packaging>
1212

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import com.theokanning.openai.embedding.EmbeddingRequest;
6060
import com.theokanning.openai.embedding.EmbeddingResult;
6161
import com.theokanning.openai.file.File;
62+
import com.theokanning.openai.file.FileListRequest;
6263
import com.theokanning.openai.file.FileUrl;
6364
import com.theokanning.openai.fine_tuning.FineTuningEvent;
6465
import com.theokanning.openai.fine_tuning.FineTuningJob;
@@ -268,6 +269,10 @@ public List<File> listFiles() {
268269
return execute(api.listFiles()).data;
269270
}
270271

272+
public List<File> listFiles(FileListRequest request) {
273+
return execute(api.listFiles(request));
274+
}
275+
271276
public DeleteResult deleteFile(String fileId) {
272277
return execute(api.deleteFile(fileId));
273278
}

service/src/test/java/com/theokanning/openai/service/FileTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.theokanning.openai.DeleteResult;
44
import com.theokanning.openai.file.File;
5+
import com.theokanning.openai.file.FileListRequest;
56
import org.junit.jupiter.api.MethodOrderer;
67
import org.junit.jupiter.api.Order;
78
import org.junit.jupiter.api.Test;
@@ -11,10 +12,12 @@
1112
import java.nio.charset.StandardCharsets;
1213
import java.nio.file.Files;
1314
import java.nio.file.Paths;
15+
import java.util.Arrays;
1416
import java.util.List;
1517
import java.util.concurrent.TimeUnit;
1618

1719
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertNotNull;
1821
import static org.junit.jupiter.api.Assertions.assertTrue;
1922

2023
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@@ -66,4 +69,52 @@ void uploadFileStream() throws Exception {
6669
DeleteResult result = service.deleteFile(fileId);
6770
assertTrue(result.isDeleted());
6871
}
72+
73+
@Test
74+
@Order(3)
75+
void listFilesWithRequest() {
76+
// 首先上传一些测试文件
77+
String filePath1 = "src/test/resources/fine-tuning-data.jsonl";
78+
String filePath2 = "batch-task-data.jsonl";
79+
80+
File file1 = service.uploadFile("fine-tune", filePath1);
81+
InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(filePath2);
82+
File file2 = service.uploadFile("fine-tune", resourceAsStream, "batch-task-data.jsonl");
83+
84+
String fileId1 = file1.getId();
85+
String fileId2 = file2.getId();
86+
87+
try {
88+
// 测试使用 FileListRequest 查询指定文件
89+
FileListRequest request = FileListRequest.builder()
90+
.fileIds(Arrays.asList(fileId1, fileId2))
91+
.getUrl(true)
92+
.expires(3600L)
93+
.build();
94+
95+
List<File> files = service.listFiles(request);
96+
97+
// 验证结果
98+
assertNotNull(files);
99+
assertEquals(2, files.size());
100+
assertTrue(files.stream().anyMatch(f -> f.getId().equals(fileId1)));
101+
assertTrue(files.stream().anyMatch(f -> f.getId().equals(fileId2)));
102+
103+
// 测试只查询一个文件
104+
FileListRequest singleFileRequest = FileListRequest.builder()
105+
.fileIds(Arrays.asList(fileId1))
106+
.getUrl(false)
107+
.build();
108+
109+
List<File> singleFileResult = service.listFiles(singleFileRequest);
110+
assertNotNull(singleFileResult);
111+
assertEquals(1, singleFileResult.size());
112+
assertEquals(fileId1, singleFileResult.get(0).getId());
113+
114+
} finally {
115+
// 清理测试文件
116+
service.deleteFile(fileId1);
117+
service.deleteFile(fileId2);
118+
}
119+
}
69120
}

0 commit comments

Comments
 (0)