Skip to content

Commit 9650d1c

Browse files
committed
wip
1 parent 27278e8 commit 9650d1c

File tree

3 files changed

+227
-96
lines changed

3 files changed

+227
-96
lines changed

common/src/main/java/com/box/l10n/mojito/openai/OpenAIClient.java

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public CompletableFuture<ChatCompletionsResponse> getChatCompletions(
145145
httpResponse.body(), ChatCompletionsResponse.class);
146146
} catch (JsonProcessingException e) {
147147
throw new OpenAIClientResponseException(
148-
"Can't deserialize ChatCompletionsResponse", e, httpResponse);
148+
"Can't deserialize ChatCompletionsResponse", e, httpResponse);
149149
}
150150
}
151151
});
@@ -183,7 +183,7 @@ public CompletableFuture<Stream<ChatCompletionsStreamResponse>> streamChatComple
183183
httpResponse -> {
184184
if (httpResponse.statusCode() != 200) {
185185
throw new OpenAIClientResponseException(
186-
"ChatCompletion stream failed", httpResponse);
186+
"ChatCompletion stream failed", httpResponse);
187187
}
188188
return httpResponse
189189
.body()
@@ -193,9 +193,9 @@ public CompletableFuture<Stream<ChatCompletionsStreamResponse>> streamChatComple
193193
body -> {
194194
if (!body.startsWith("data: ")) {
195195
throw new OpenAIClientResponseException(
196-
"Only support \"data\" lines in stream are supported, got: \"%s\""
197-
.formatted(body),
198-
httpResponse);
196+
"Only support \"data\" lines in stream are supported, got: \"%s\""
197+
.formatted(body),
198+
httpResponse);
199199
}
200200

201201
String jsonPart = body.substring(5);
@@ -204,9 +204,9 @@ public CompletableFuture<Stream<ChatCompletionsStreamResponse>> streamChatComple
204204
jsonPart, ChatCompletionsStreamResponse.class);
205205
} catch (JsonProcessingException e) {
206206
throw new OpenAIClientResponseException(
207-
"Can't deserialize ChatCompletionsStreamResponse",
208-
e,
209-
httpResponse);
207+
"Can't deserialize ChatCompletionsStreamResponse",
208+
e,
209+
httpResponse);
210210
}
211211
});
212212
});
@@ -552,7 +552,7 @@ public CompletableFuture<EmbeddingResponse> getEmbedding(EmbeddingRequest embedd
552552
return objectMapper.readValue(httpResponse.body(), EmbeddingResponse.class);
553553
} catch (JsonProcessingException e) {
554554
throw new OpenAIClientResponseException(
555-
"Can't deserialize EmbeddingResponse", e, httpResponse);
555+
"Can't deserialize EmbeddingResponse", e, httpResponse);
556556
}
557557
});
558558

@@ -630,7 +630,6 @@ public UploadFileResponse uploadFile(UploadFileRequest uploadFileRequest) {
630630
HttpResponse<String> response;
631631
try {
632632
response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
633-
System.out.printf("response: %s%n", response.body());
634633
} catch (IOException | InterruptedException e) {
635634
throw new RuntimeException(e);
636635
}
@@ -713,15 +712,24 @@ public record UploadFileResponse(
713712
String status,
714713
@JsonProperty("status_details") String statusDetails) {}
715714

716-
public record BatchFileLine(
715+
public record RequestBatchFileLine(
717716
@JsonProperty("custom_id") String customId, String method, String url, Object body) {
718717

719-
public static BatchFileLine forChatCompletion(
718+
public static RequestBatchFileLine forChatCompletion(
720719
String customId, ChatCompletionsRequest chatCompletionsRequest) {
721-
return new BatchFileLine(customId, "POST", "/v1/chat/completions", chatCompletionsRequest);
720+
return new RequestBatchFileLine(
721+
customId, "POST", "/v1/chat/completions", chatCompletionsRequest);
722722
}
723723
}
724724

725+
public record ChatCompletionResponseBatchFileLine(
726+
String id, @JsonProperty("custom_id") String customId, Response response) {
727+
public record Response(
728+
@JsonProperty("status_code") int statusCode,
729+
@JsonProperty("request_id") String requestId,
730+
@JsonProperty("body") ChatCompletionsResponse chatCompletionsResponse) {}
731+
}
732+
725733
public DownloadFileContentResponse downloadFileContent(
726734
DownloadFileContentRequest downloadFileContentRequest) {
727735
HttpResponse<String> response;
@@ -799,12 +807,14 @@ public CreateBatchResponse createBatch(CreateBatchRequest createBatchRequest) {
799807
public record CreateBatchRequest(
800808
@JsonProperty("input_file_id") String inputFileId,
801809
String endpoint,
802-
@JsonProperty("completion_window") String completionWindow) {
810+
@JsonProperty("completion_window") String completionWindow,
811+
Map<String, String> metadata) {
803812

804-
public static final String ENDPOINT = "/v1/batches";
813+
public static final String ENDPOINT = "/v1/batches";
805814

806-
public static CreateBatchRequest forChatCompletion(String fileId) {
807-
return new CreateBatchRequest(fileId, "/v1/chat/completions", "24h");
815+
public static CreateBatchRequest forChatCompletion(
816+
String fileId, Map<String, String> metadata) {
817+
return new CreateBatchRequest(fileId, "/v1/chat/completions", "24h", metadata);
808818
}
809819
}
810820

@@ -825,7 +835,7 @@ public record CreateBatchResponse(
825835
@JsonProperty("failed_at") Long failedAt,
826836
@JsonProperty("expired_at") Long expiredAt,
827837
@JsonProperty("request_counts") RequestCounts requestCounts,
828-
Object metadata) {
838+
Map<String, String> metadata) {
829839
record RequestCounts(int total, int completed, int failed) {}
830840
}
831841

@@ -882,7 +892,7 @@ public record RetrieveBatchResponse(
882892
@JsonProperty("failed_at") Long failedAt,
883893
@JsonProperty("expired_at") Long expiredAt,
884894
@JsonProperty("request_counts") RequestCounts requestCounts,
885-
Object metadata) {
895+
Map<String, String> metadata) {
886896
record RequestCounts(int total, int completed, int failed) {}
887897
}
888898

@@ -898,7 +908,8 @@ public OpenAIClientResponseException(String message, HttpResponse<?> httpRespons
898908
this.httpResponse = Objects.requireNonNull(httpResponse);
899909
}
900910

901-
public OpenAIClientResponseException(String message, Exception e, HttpResponse<?> httpResponse) {
911+
public OpenAIClientResponseException(
912+
String message, Exception e, HttpResponse<?> httpResponse) {
902913
super(message, e);
903914
this.httpResponse = Objects.requireNonNull(httpResponse);
904915
}

common/src/test/java/com/box/l10n/mojito/openai/OpenAIClientTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@
1313
import static org.mockito.Mockito.mock;
1414
import static org.mockito.Mockito.when;
1515

16+
import com.box.l10n.mojito.json.ObjectMapper;
1617
import com.box.l10n.mojito.openai.OpenAIClient.ChatCompletionsResponse;
1718
import com.box.l10n.mojito.openai.OpenAIClient.UploadFileRequest;
1819
import com.box.l10n.mojito.openai.OpenAIClient.UploadFileResponse;
20+
import com.fasterxml.jackson.annotation.JsonInclude;
21+
import com.fasterxml.jackson.databind.DeserializationFeature;
22+
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
1923
import java.net.http.HttpClient;
2024
import java.net.http.HttpRequest;
2125
import java.net.http.HttpResponse;

0 commit comments

Comments
 (0)