Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

package com.azure.ai.agents.implementation.http;

import com.azure.core.exception.AzureException;
import com.azure.core.exception.HttpResponseException;
import com.azure.core.http.HttpHeaderName;
import com.azure.core.http.HttpHeaders;
Expand Down Expand Up @@ -169,7 +170,11 @@ private static Throwable mapAzureExceptionToOpenAI(Throwable throwable) {
} else if (throwable instanceof TimeoutException) {
return throwable;
} else {
return new OpenAIException(throwable.getMessage(), throwable);
if (throwable instanceof AzureException) {
return new OpenAIException(throwable.getMessage(), throwable.getCause());
} else {
return new OpenAIException(throwable.getMessage(), throwable);
}
}
}

Expand Down Expand Up @@ -237,7 +242,7 @@ private static HttpHeaders toAzureHeaders(Headers sourceHeaders) {
private static Context buildRequestContext(RequestOptions requestOptions) {
Context context = new Context("azure-eagerly-read-response", true);
Timeout timeout = requestOptions.getTimeout();
// we use "read" as it's the closes thing to the "response timeout"
// we use "read" as it's the closest thing to the "response timeout"
if (timeout != null && !timeout.read().isZero() && !timeout.read().isNegative()) {
context = context.addData("azure-response-timeout", timeout.read());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import com.azure.ai.projects.implementation.AIProjectClientImpl;
import com.azure.ai.projects.implementation.TokenUtils;
import com.azure.ai.projects.implementation.http.HttpClientHelper;
import com.azure.core.annotation.Generated;
import com.azure.core.annotation.ServiceClientBuilder;
import com.azure.core.client.traits.ConfigurationTrait;
Expand Down Expand Up @@ -32,7 +33,6 @@
import com.azure.core.util.ClientOptions;
import com.azure.core.util.Configuration;
import com.azure.core.util.CoreUtils;
import com.azure.core.util.UserAgentUtil;
import com.azure.core.util.builder.ClientBuilderUtil;
import com.azure.core.util.logging.ClientLogger;
import com.azure.core.util.serializer.JacksonAdapter;
Expand All @@ -41,7 +41,7 @@
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.client.okhttp.OpenAIOkHttpClientAsync;
import com.openai.credential.BearerTokenCredential;
import java.time.Duration;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -544,8 +544,9 @@ public SchedulesClient buildSchedulesClient() {
* @return an instance of EvaluationsClient
*/
public EvaluationsClient buildEvaluationsClient() {
OpenAIOkHttpClient.Builder builder = getOpenAIClientBuilder();
return new EvaluationsClient(builder.build());
return new EvaluationsClient(getOpenAIClientBuilder().build()
.withOptions(optionBuilder -> optionBuilder
.httpClient(HttpClientHelper.mapToOpenAIHttpClient(createHttpPipeline()))));
}

/**
Expand All @@ -554,20 +555,22 @@ public EvaluationsClient buildEvaluationsClient() {
* @return an instance of EvaluationsClientAsync
*/
public EvaluationsAsyncClient buildEvaluationsAsyncClient() {
return new EvaluationsAsyncClient(getOpenAIAsyncClientBuilder().build());
return new EvaluationsAsyncClient(getOpenAIAsyncClientBuilder().build()
.withOptions(optionBuilder -> optionBuilder
.httpClient(HttpClientHelper.mapToOpenAIHttpClient(createHttpPipeline()))));
}

private OpenAIOkHttpClient.Builder getOpenAIClientBuilder() {
OpenAIOkHttpClient.Builder builder = OpenAIOkHttpClient.builder()
.credential(
BearerTokenCredential.create(TokenUtils.getBearerTokenSupplier(this.tokenCredential, DEFAULT_SCOPES)));
builder.baseUrl(this.endpoint + (this.endpoint.endsWith("/") ? "openai" : "/openai"));
builder.replaceHeaders("User-Agent", getUserAgent());
if (this.serviceVersion != null) {
builder.azureServiceVersion(AzureOpenAIServiceVersion.fromString(this.serviceVersion.getVersion()));
builder.azureUrlPathMode(AzureUrlPathMode.UNIFIED);
}
builder.timeout(Duration.ofSeconds(30));
// We set the builder retries to 0 to avoid conflicts with the retry policy added through the HttpPipeline.
builder.maxRetries(0);
return builder;
}

Expand All @@ -576,23 +579,14 @@ private OpenAIOkHttpClientAsync.Builder getOpenAIAsyncClientBuilder() {
.credential(
BearerTokenCredential.create(TokenUtils.getBearerTokenSupplier(this.tokenCredential, DEFAULT_SCOPES)));
builder.baseUrl(this.endpoint + (this.endpoint.endsWith("/") ? "openai" : "/openai"));
builder.replaceHeaders("User-Agent", getUserAgent());
if (this.serviceVersion != null) {
builder.azureServiceVersion(AzureOpenAIServiceVersion.fromString(this.serviceVersion.getVersion()));
builder.azureUrlPath(AzureUrlPathMode.UNIFIED);
}
builder.timeout(Duration.ofSeconds(30));
// We set the builder retries to 0 to avoid conflicts with the retry policy added through the HttpPipeline.
builder.maxRetries(0);
return builder;
}

private String getUserAgent() {
HttpLogOptions localHttpLogOptions = this.httpLogOptions == null ? new HttpLogOptions() : this.httpLogOptions;
ClientOptions localClientOptions = this.clientOptions == null ? new ClientOptions() : this.clientOptions;
String sdkName = PROPERTIES.getOrDefault(SDK_NAME, "UnknownName");
String sdkVersion = PROPERTIES.getOrDefault(SDK_VERSION, "UnknownVersion");
String applicationId = CoreUtils.getApplicationId(localClientOptions, localHttpLogOptions);
return UserAgentUtil.toUserAgentString(applicationId, sdkName, sdkVersion, configuration);
}

private static final ClientLogger LOGGER = new ClientLogger(AIProjectClientBuilder.class);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.ai.projects.implementation.http;

import com.azure.core.http.HttpHeader;
import com.azure.core.http.HttpHeaders;
import com.azure.core.util.logging.ClientLogger;
import com.openai.core.http.Headers;
import com.openai.core.http.HttpResponse;

import java.io.InputStream;

/**
* Adapter that exposes an Azure {@link com.azure.core.http.HttpResponse} as an OpenAI {@link HttpResponse}. This keeps
* the translation logic encapsulated so response handling elsewhere can remain framework agnostic.
*/
final class AzureHttpResponseAdapter implements HttpResponse {

private static final ClientLogger LOGGER = new ClientLogger(AzureHttpResponseAdapter.class);

private final com.azure.core.http.HttpResponse azureResponse;

/**
* Creates a new adapter instance for the provided Azure response.
*
* @param azureResponse Response returned by the Azure pipeline.
*/
AzureHttpResponseAdapter(com.azure.core.http.HttpResponse azureResponse) {
this.azureResponse = azureResponse;
}

@Override
public int statusCode() {
return azureResponse.getStatusCode();
}

@Override
public Headers headers() {
return toOpenAiHeaders(azureResponse.getHeaders());
}

@Override
public InputStream body() {
return azureResponse.getBodyAsBinaryData().toStream();
}

@Override
public void close() {
azureResponse.close();
}

/**
* Copies headers from the Azure response into the immutable OpenAI {@link Headers} collection.
*/
private static Headers toOpenAiHeaders(HttpHeaders httpHeaders) {
Headers.Builder builder = Headers.builder();
for (HttpHeader header : httpHeaders) {
builder.put(header.getName(), header.getValuesList());
}
return builder.build();
}
}
Loading
Loading