Skip to content

Commit 3880c84

Browse files
committed
feat: upload and download file use openaiservice
1 parent 8a596c0 commit 3880c84

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

api/src/main/java/com/ke/bella/batch/configuration/BellaAutoConf.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,13 @@ public class BellaAutoConf {
5454
@Resource
5555
private BatchService bs;
5656

57+
@Autowired
58+
@Lazy
59+
private OpenAiService openAiService;
60+
5761
@PostConstruct
5862
public void postConstruct() {
59-
OpenapiUtils.initialize(openapiClient, openAiServiceFactory);
63+
OpenapiUtils.initialize(openapiClient, openAiServiceFactory, openAiService);
6064
Long id = instanceRepo.register(BellaServerContextHolder.getIp(), BellaServerContextHolder.getPort());
6165
IDGenerator.setInstanceId(id);
6266
}

api/src/main/java/com/ke/bella/batch/utils/OpenapiUtils.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.ke.bella.openapi.protocol.route.RouteResult;
1010
import com.ke.bella.openapi.server.OpenAiServiceFactory;
1111
import com.ke.bella.queue.QueueMode;
12+
import com.theokanning.openai.service.OpenAiService;
1213
import lombok.SneakyThrows;
1314
import lombok.extern.slf4j.Slf4j;
1415
import org.apache.commons.collections4.MapUtils;
@@ -33,19 +34,24 @@ public class OpenapiUtils {
3334

3435
static OpenAiServiceFactory openAiServiceFactory;
3536

37+
static OpenAiService openAiService;
38+
3639
private static final Cache<String, String> QUEUE_CACHE = CacheBuilder.newBuilder()
3740
.expireAfterWrite(10, TimeUnit.MINUTES).maximumSize(1000).build();
3841

3942
private static final Cache<String, Optional<Channel>> CHANNEL_CACHE = CacheBuilder.newBuilder()
4043
.expireAfterWrite(10, TimeUnit.MINUTES).maximumSize(1000).build();
4144

42-
public synchronized static void initialize(OpenapiClient openApiClient, OpenAiServiceFactory openAiServiceFactory) {
45+
public synchronized static void initialize(OpenapiClient openApiClient, OpenAiServiceFactory openAiServiceFactory, OpenAiService openAiService) {
4346
if(OpenapiUtils.openApiClient == null) {
4447
OpenapiUtils.openApiClient = openApiClient;
4548
}
4649
if(OpenapiUtils.openAiServiceFactory == null) {
4750
OpenapiUtils.openAiServiceFactory = openAiServiceFactory;
4851
}
52+
if(OpenapiUtils.openAiService == null) {
53+
OpenapiUtils.openAiService = openAiService;
54+
}
4955
}
5056

5157
public static OpenapiClient getInstance() {
@@ -90,8 +96,7 @@ public static Channel getChannelByQueue(String queueName) {
9096

9197
public static String saveStringAsFile(String data) {
9298
String fileName = UUID.randomUUID() + ".txt";
93-
return openAiServiceFactory.create(Configs.OPENAPI_CONSOLE_KEY)
94-
.uploadFile(Configs.FILE_API_PURPOSE, data.getBytes(), fileName).getId();
99+
return openAiService.uploadFile(Configs.FILE_API_PURPOSE, data.getBytes(), fileName).getId();
95100
}
96101

97102
public static String fetchStringFromFile(String fileId) {
@@ -100,7 +105,7 @@ public static String fetchStringFromFile(String fileId) {
100105
}
101106
byte[] fileContent;
102107
try {
103-
fileContent = openAiServiceFactory.create(Configs.OPENAPI_CONSOLE_KEY).retrieveFileContent(fileId).bytes();
108+
fileContent = openAiService.retrieveFileContent(fileId).bytes();
104109
} catch (IOException e) {
105110
throw new IllegalStateException(e);
106111
}
@@ -110,7 +115,7 @@ public static String fetchStringFromFile(String fileId) {
110115
@SneakyThrows
111116
public static void download(String fileId, Path path) {
112117
try {
113-
openAiServiceFactory.create().retrieveFileContentAndSave(fileId, path);
118+
openAiService.retrieveFileContentAndSave(fileId, path);
114119
} catch (IOException e) {
115120
log.error("Failed to download file after all retry attempts: {} to {}", fileId, path, e);
116121
throw new IllegalStateException("Failed to download file: " + fileId, e);

api/src/test/java/com/ke/bella/batch/utils/OpenapiUtilsTest.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@ public void setUp() {
5353
// Clear any previous static state
5454
ReflectionTestUtils.setField(OpenapiUtils.class, "openApiClient", null);
5555
ReflectionTestUtils.setField(OpenapiUtils.class, "openAiServiceFactory", null);
56+
ReflectionTestUtils.setField(OpenapiUtils.class, "openAiService", null);
5657

57-
// Initialize OpenapiUtils
58-
OpenapiUtils.initialize(mockOpenapiClient, mockOpenAiServiceFactory);
58+
// Initialize OpenapiUtils with all three parameters
59+
OpenapiUtils.initialize(mockOpenapiClient, mockOpenAiServiceFactory, mockOpenAiService);
5960

6061
// Set up BellaContext
6162
BellaContext.setApikey(ApikeyInfo.builder().apikey(TEST_APIKEY).build());
@@ -131,14 +132,12 @@ public void testSaveStringAsFile_Success() {
131132
String expectedFileId = "file-123";
132133

133134
when(mockFile.getId()).thenReturn(expectedFileId);
134-
when(mockOpenAiServiceFactory.create(TEST_CONSOLE_KEY)).thenReturn(mockOpenAiService);
135135
when(mockOpenAiService.uploadFile(eq(Configs.FILE_API_PURPOSE), any(byte[].class), anyString()))
136136
.thenReturn(mockFile);
137137

138138
String result = OpenapiUtils.saveStringAsFile(testData);
139139

140140
assertEquals(expectedFileId, result);
141-
verify(mockOpenAiServiceFactory).create(TEST_CONSOLE_KEY);
142141
verify(mockOpenAiService).uploadFile(eq(Configs.FILE_API_PURPOSE),
143142
eq(testData.getBytes()), anyString());
144143
}
@@ -152,7 +151,6 @@ public void testFetchStringFromFile_Success() throws IOException {
152151
okhttp3.ResponseBody mockResponseBody = mock(okhttp3.ResponseBody.class);
153152
when(mockResponseBody.bytes()).thenReturn(contentBytes);
154153

155-
when(mockOpenAiServiceFactory.create(TEST_CONSOLE_KEY)).thenReturn(mockOpenAiService);
156154
when(mockOpenAiService.retrieveFileContent(fileId)).thenReturn(mockResponseBody);
157155

158156
String result = OpenapiUtils.fetchStringFromFile(fileId);
@@ -179,7 +177,6 @@ public void testFetchStringFromFile_IOException() throws IOException {
179177
okhttp3.ResponseBody mockResponseBody = mock(okhttp3.ResponseBody.class);
180178
when(mockResponseBody.bytes()).thenThrow(new IOException("Network error"));
181179

182-
when(mockOpenAiServiceFactory.create(TEST_CONSOLE_KEY)).thenReturn(mockOpenAiService);
183180
when(mockOpenAiService.retrieveFileContent(fileId)).thenReturn(mockResponseBody);
184181

185182
OpenapiUtils.fetchStringFromFile(fileId);
@@ -190,8 +187,6 @@ public void testDownload_Success() throws IOException {
190187
String fileId = "file-123";
191188
Path testPath = Paths.get("/tmp/test-file.txt");
192189

193-
when(mockOpenAiServiceFactory.create()).thenReturn(mockOpenAiService);
194-
195190
OpenapiUtils.download(fileId, testPath);
196191

197192
verify(mockOpenAiService).retrieveFileContentAndSave(fileId, testPath);
@@ -202,7 +197,6 @@ public void testDownload_IOException() throws IOException {
202197
String fileId = "file-123";
203198
Path testPath = Paths.get("/tmp/test-file.txt");
204199

205-
when(mockOpenAiServiceFactory.create()).thenReturn(mockOpenAiService);
206200
doThrow(new IOException("Download failed")).when(mockOpenAiService)
207201
.retrieveFileContentAndSave(fileId, testPath);
208202

0 commit comments

Comments
 (0)