Skip to content

Commit

Permalink
use blocking mode for vision + update URL modification logic
Browse files Browse the repository at this point in the history
  • Loading branch information
the-gigi committed Apr 4, 2024
1 parent e9203f0 commit 8011594
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,9 @@ public void demoCallChatWithVisionExternalImage() {
.temperature(0.0)
.maxTokens(500)
.build();
var chatResponse = openAI.chatCompletions().createStream(chatRequest).join();
chatResponse.filter(chatResp -> chatResp.firstContent() != null)
.map(chatResp -> chatResp.firstContent())
.forEach(System.out::print);
System.out.println();

var chatResponse = openAI.chatCompletions().create(chatRequest).join();
System.out.println(chatResponse.firstContent());
}

public void demoCallChatWithVisionLocalImage() {
Expand All @@ -131,11 +129,8 @@ public void demoCallChatWithVisionLocalImage() {
.temperature(0.0)
.maxTokens(500)
.build();
var chatResponse = openAI.chatCompletions().createStream(chatRequest).join();
chatResponse.filter(chatResp -> chatResp.firstContent() != null)
.map(chatResp -> chatResp.firstContent())
.forEach(System.out::print);
System.out.println();
var chatResponse = openAI.chatCompletions().create(chatRequest).join();
System.out.println(chatResponse.firstContent());
}

private static ImageUrl loadImageAsBase64(String imagePath) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static BaseSimpleOpenAIArgs prepareBaseSimpleOpenAIArgs(String apiKey, St
if (contentType.equals(ContentType.APPLICATION_JSON)) {
var bodyJson = (String) request.getBody();
var model = "";
if (!url.contains("/chat/completions/")) {
if (url.contains("/assistants")) {
model = "\"" + MODEL_LITERAL + "\":\"" + deployment + "\"";
}
bodyJson = bodyJson.replaceFirst(MODEL_REGEX, model);
Expand All @@ -93,7 +93,7 @@ public static BaseSimpleOpenAIArgs prepareBaseSimpleOpenAIArgs(String apiKey, St
if (contentType.equals(ContentType.MULTIPART_FORMDATA)) {
@SuppressWarnings("unchecked")
var bodyMap = (Map<String, Object>) request.getBody();
if (url.contains("/assistants/")) {
if (url.contains("/assistants")) {
bodyMap.put(MODEL_LITERAL, deployment);
} else {
bodyMap.remove(MODEL_LITERAL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,22 @@ void shouldPrepareBaseOpenSimpleAIArgsCorrectly() {

@Test
void shouldInterceptUrlCorrectlyWhenBodyIsJson() {
var baseUrl = "https://example.org/openai/deployments/some-deployment";
var request = HttpRequestData.builder()
.url("https://example.org/v1/endpoint")
.url(baseUrl + "/chat/completions")
.contentType(ContentType.APPLICATION_JSON)
.headers(Map.of(Constant.AZURE_APIKEY_HEADER, "the-api-key"))
.body("{\"model\":\"model1\"}")
.build();
var expectedRequest = HttpRequestData.builder()
.url("https://example.org/endpoint?" + Constant.AZURE_API_VERSION + "=12-34-5678")
.url(baseUrl + "/chat/completions?" + Constant.AZURE_API_VERSION + "=12-34-5678")
.contentType(ContentType.APPLICATION_JSON)
.headers(Map.of(Constant.AZURE_APIKEY_HEADER, "the-api-key"))
.body("{}")
.build();
var args = SimpleOpenAIAzure.prepareBaseSimpleOpenAIArgs(
"the-api-key",
"https://example.org",
baseUrl,
"12-34-5678",
null);
var actualRequest = args.getRequestInterceptor().apply(request);
Expand Down Expand Up @@ -93,13 +94,10 @@ void shouldThrownExceptionWhenCallingUnimplementedMethods() {
openAI::audios,
openAI::completions,
openAI::embeddings,
openAI::files,
openAI::fineTunings,
openAI::images,
openAI::models,
openAI::moderations,
openAI::assistants,
openAI::threads
};
for (Runnable calling : callingData) {
assertThrows(UnsupportedOperationException.class, () -> calling.run());
Expand Down

0 comments on commit 8011594

Please sign in to comment.