Skip to content
Closed
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 @@ -186,9 +186,7 @@ public OpenAiApi(String baseUrl, String apiKey, MultiValueMap<String, String> he
* @param restClientBuilder RestClient builder.
* @param webClientBuilder WebClient builder.
* @param responseErrorHandler Response error handler.
* @deprecated since 1.0.0.M6 - use {@link #builder()} instead
*/
@Deprecated(since = "1.0.0.M6")
public OpenAiApi(String baseUrl, ApiKey apiKey, MultiValueMap<String, String> headers, String completionsPath,
String embeddingsPath, RestClient.Builder restClientBuilder, WebClient.Builder webClientBuilder,
ResponseErrorHandler responseErrorHandler) {
Expand Down Expand Up @@ -1680,7 +1678,7 @@ public Builder apiKey(ApiKey apiKey) {
}

public Builder apiKey(String simpleApiKey) {
Assert.notNull(simpleApiKey, "apiKey cannot be null");
Assert.notNull(simpleApiKey, "simpleApiKey cannot be null");
this.apiKey = new SimpleApiKey(simpleApiKey);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import org.springframework.ai.model.ApiKey;
import org.springframework.ai.model.NoopApiKey;
import org.springframework.ai.model.SimpleApiKey;
import org.springframework.ai.openai.api.common.OpenAiApiConstants;
import org.springframework.ai.retry.RetryUtils;
import org.springframework.core.io.ByteArrayResource;
Expand All @@ -45,6 +48,7 @@
* <a href="https://platform.openai.com/docs/api-reference/audio">OpenAI Audio</a>
*
* @author Christian Tzolov
* @author Ilayaperumal Gopinathan
* @since 0.8.1
*/
public class OpenAiAudioApi {
Expand All @@ -56,7 +60,9 @@ public class OpenAiAudioApi {
/**
* Create a new audio api.
* @param openAiToken OpenAI apiKey.
* @deprecated use {@link Builder} instead.
*/
@Deprecated(forRemoval = true, since = "1.0.0-M6")
public OpenAiAudioApi(String openAiToken) {
this(OpenAiApiConstants.DEFAULT_BASE_URL, openAiToken, RestClient.builder(), WebClient.builder(),
RetryUtils.DEFAULT_RESPONSE_ERROR_HANDLER);
Expand All @@ -68,10 +74,11 @@ public OpenAiAudioApi(String openAiToken) {
* @param openAiToken OpenAI apiKey.
* @param restClientBuilder RestClient builder.
* @param responseErrorHandler Response error handler.
* @deprecated use {@link Builder} instead.
*/
@Deprecated(forRemoval = true, since = "1.0.0-M6")
public OpenAiAudioApi(String baseUrl, String openAiToken, RestClient.Builder restClientBuilder,
ResponseErrorHandler responseErrorHandler) {

Consumer<HttpHeaders> authHeaders;
if (openAiToken != null && !openAiToken.isEmpty()) {
authHeaders = h -> h.setBearerAuth(openAiToken);
Expand All @@ -96,7 +103,9 @@ public OpenAiAudioApi(String baseUrl, String openAiToken, RestClient.Builder res
* @param restClientBuilder RestClient builder.
* @param webClientBuilder WebClient builder.
* @param responseErrorHandler Response error handler.
* @deprecated use {@link Builder} instead.
*/
@Deprecated(forRemoval = true, since = "1.0.0-M6")
public OpenAiAudioApi(String baseUrl, String apiKey, RestClient.Builder restClientBuilder,
WebClient.Builder webClientBuilder, ResponseErrorHandler responseErrorHandler) {

Expand All @@ -112,14 +121,31 @@ public OpenAiAudioApi(String baseUrl, String apiKey, RestClient.Builder restClie
* @param restClientBuilder RestClient builder.
* @param webClientBuilder WebClient builder.
* @param responseErrorHandler Response error handler.
* @deprecated use {@link Builder} instead.
*/
@Deprecated(forRemoval = true, since = "1.0.0-M6")
public OpenAiAudioApi(String baseUrl, String apiKey, MultiValueMap<String, String> headers,
RestClient.Builder restClientBuilder, WebClient.Builder webClientBuilder,
ResponseErrorHandler responseErrorHandler) {
this(baseUrl, new SimpleApiKey(apiKey), headers, restClientBuilder, webClientBuilder, responseErrorHandler);
}

/**
* Create a new audio api.
* @param baseUrl api base URL.
* @param apiKey OpenAI apiKey.
* @param headers the http headers to use.
* @param restClientBuilder RestClient builder.
* @param webClientBuilder WebClient builder.
* @param responseErrorHandler Response error handler.
*/
public OpenAiAudioApi(String baseUrl, ApiKey apiKey, MultiValueMap<String, String> headers,
RestClient.Builder restClientBuilder, WebClient.Builder webClientBuilder,
ResponseErrorHandler responseErrorHandler) {

Consumer<HttpHeaders> authHeaders = h -> {
if (apiKey != null && !apiKey.isEmpty()) {
h.setBearerAuth(apiKey);
if (!(apiKey instanceof NoopApiKey)) {
h.setBearerAuth(apiKey.getValue());
}
h.addAll(headers);
// h.setContentType(MediaType.APPLICATION_JSON);
Expand All @@ -133,6 +159,10 @@ public OpenAiAudioApi(String baseUrl, String apiKey, MultiValueMap<String, Strin
this.webClient = webClientBuilder.baseUrl(baseUrl).defaultHeaders(authHeaders).build();
}

public static Builder builder() {
return new Builder();
}

/**
* Request to generates audio from the input text.
* @param requestBody The request body.
Expand Down Expand Up @@ -932,4 +962,71 @@ public record Segment(

}

/**
* Builder to construct {@link OpenAiAudioApi} instance.
*/
public static class Builder {

private String baseUrl = OpenAiApiConstants.DEFAULT_BASE_URL;

private ApiKey apiKey;

private MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();

private RestClient.Builder restClientBuilder = RestClient.builder();

private WebClient.Builder webClientBuilder = WebClient.builder();

private ResponseErrorHandler responseErrorHandler = RetryUtils.DEFAULT_RESPONSE_ERROR_HANDLER;

public Builder baseUrl(String baseUrl) {
Assert.hasText(baseUrl, "baseUrl cannot be null or empty");
this.baseUrl = baseUrl;
return this;
}

public Builder apiKey(ApiKey apiKey) {
Assert.notNull(apiKey, "apiKey cannot be null");
this.apiKey = apiKey;
return this;
}

public Builder apiKey(String simpleApiKey) {
Assert.notNull(simpleApiKey, "simpleApiKey cannot be null");
this.apiKey = new SimpleApiKey(simpleApiKey);
return this;
}

public Builder headers(MultiValueMap<String, String> headers) {
Assert.notNull(headers, "headers cannot be null");
this.headers = headers;
return this;
}

public Builder restClientBuilder(RestClient.Builder restClientBuilder) {
Assert.notNull(restClientBuilder, "restClientBuilder cannot be null");
this.restClientBuilder = restClientBuilder;
return this;
}

public Builder webClientBuilder(WebClient.Builder webClientBuilder) {
Assert.notNull(webClientBuilder, "webClientBuilder cannot be null");
this.webClientBuilder = webClientBuilder;
return this;
}

public Builder responseErrorHandler(ResponseErrorHandler responseErrorHandler) {
Assert.notNull(responseErrorHandler, "responseErrorHandler cannot be null");
this.responseErrorHandler = responseErrorHandler;
return this;
}

public OpenAiAudioApi build() {
Assert.notNull(this.apiKey, "apiKey must be set");
return new OpenAiAudioApi(this.baseUrl, this.apiKey, this.headers, this.restClientBuilder,
this.webClientBuilder, this.responseErrorHandler);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

import org.springframework.ai.model.ApiKey;
import org.springframework.ai.model.NoopApiKey;
import org.springframework.ai.model.SimpleApiKey;
import org.springframework.ai.openai.api.common.OpenAiApiConstants;
import org.springframework.ai.retry.RetryUtils;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.ResponseErrorHandler;
import org.springframework.web.client.RestClient;
Expand All @@ -46,7 +50,9 @@ public class OpenAiImageApi {
/**
* Create a new OpenAI Image api with base URL set to {@code https://api.openai.com}.
* @param openAiToken OpenAI apiKey.
* @deprecated use {@link Builder} instead.
*/
@Deprecated(forRemoval = true, since = "1.0.0-M6")
public OpenAiImageApi(String openAiToken) {
this(OpenAiApiConstants.DEFAULT_BASE_URL, openAiToken, RestClient.builder());
}
Expand All @@ -55,8 +61,9 @@ public OpenAiImageApi(String openAiToken) {
* Create a new OpenAI Image API with the provided base URL.
* @param baseUrl the base URL for the OpenAI API.
* @param openAiToken OpenAI apiKey.
* @param restClientBuilder the rest client builder to use.
* @deprecated use {@link Builder} instead.
*/
@Deprecated(forRemoval = true, since = "1.0.0-M6")
public OpenAiImageApi(String baseUrl, String openAiToken, RestClient.Builder restClientBuilder) {
this(baseUrl, openAiToken, restClientBuilder, RetryUtils.DEFAULT_RESPONSE_ERROR_HANDLER);
}
Expand All @@ -66,8 +73,9 @@ public OpenAiImageApi(String baseUrl, String openAiToken, RestClient.Builder res
* @param baseUrl the base URL for the OpenAI API.
* @param apiKey OpenAI apiKey.
* @param restClientBuilder the rest client builder to use.
* @param responseErrorHandler the response error handler to use.
* @deprecated use {@link Builder} instead.
*/
@Deprecated(forRemoval = true, since = "1.0.0-M6")
public OpenAiImageApi(String baseUrl, String apiKey, RestClient.Builder restClientBuilder,
ResponseErrorHandler responseErrorHandler) {
this(baseUrl, apiKey, CollectionUtils.toMultiValueMap(Map.of()), restClientBuilder, responseErrorHandler);
Expand All @@ -80,15 +88,31 @@ public OpenAiImageApi(String baseUrl, String apiKey, RestClient.Builder restClie
* @param headers the http headers to use.
* @param restClientBuilder the rest client builder to use.
* @param responseErrorHandler the response error handler to use.
* @deprecated use {@link Builder} instead.
*/
@Deprecated(forRemoval = true, since = "1.0.0-M6")
public OpenAiImageApi(String baseUrl, String apiKey, MultiValueMap<String, String> headers,
RestClient.Builder restClientBuilder, ResponseErrorHandler responseErrorHandler) {

this(baseUrl, new SimpleApiKey(apiKey), headers, restClientBuilder, responseErrorHandler);
}

/**
* Create a new OpenAI Image API with the provided base URL.
* @param baseUrl the base URL for the OpenAI API.
* @param apiKey OpenAI apiKey.
* @param headers the http headers to use.
* @param restClientBuilder the rest client builder to use.
* @param responseErrorHandler the response error handler to use.
*/
public OpenAiImageApi(String baseUrl, ApiKey apiKey, MultiValueMap<String, String> headers,
RestClient.Builder restClientBuilder, ResponseErrorHandler responseErrorHandler) {

// @formatter:off
this.restClient = restClientBuilder.baseUrl(baseUrl)
.defaultHeaders(h -> {
if(apiKey != null && !apiKey.isEmpty()) {
h.setBearerAuth(apiKey);
if(!(apiKey instanceof NoopApiKey)) {
h.setBearerAuth(apiKey.getValue());
}
h.setContentType(MediaType.APPLICATION_JSON);
h.addAll(headers);
Expand Down Expand Up @@ -169,4 +193,67 @@ public record Data(@JsonProperty("url") String url, @JsonProperty("b64_json") St

}

public static Builder builder() {
return new Builder();
}

/**
* Builder to construct {@link OpenAiImageApi} instance.
*/
public static class Builder {

private String baseUrl = OpenAiApiConstants.DEFAULT_BASE_URL;

private ApiKey apiKey;

private MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();

private RestClient.Builder restClientBuilder = RestClient.builder();

private ResponseErrorHandler responseErrorHandler = RetryUtils.DEFAULT_RESPONSE_ERROR_HANDLER;

public Builder baseUrl(String baseUrl) {
Assert.hasText(baseUrl, "baseUrl cannot be null or empty");
this.baseUrl = baseUrl;
return this;
}

public Builder apiKey(ApiKey apiKey) {
Assert.notNull(apiKey, "apiKey cannot be null");
this.apiKey = apiKey;
return this;
}

public Builder apiKey(String simpleApiKey) {
Assert.notNull(simpleApiKey, "simpleApiKey cannot be null");
this.apiKey = new SimpleApiKey(simpleApiKey);
return this;
}

public Builder headers(MultiValueMap<String, String> headers) {
Assert.notNull(headers, "headers cannot be null");
this.headers = headers;
return this;
}

public Builder restClientBuilder(RestClient.Builder restClientBuilder) {
Assert.notNull(restClientBuilder, "restClientBuilder cannot be null");
this.restClientBuilder = restClientBuilder;
return this;
}

public Builder responseErrorHandler(ResponseErrorHandler responseErrorHandler) {
Assert.notNull(responseErrorHandler, "responseErrorHandler cannot be null");
this.responseErrorHandler = responseErrorHandler;
return this;
}

public OpenAiImageApi build() {
Assert.notNull(this.apiKey, "apiKey must be set");
return new OpenAiImageApi(this.baseUrl, this.apiKey, this.headers, this.restClientBuilder,
this.responseErrorHandler);
}

}

}
Loading