Possible fix for issue #2218, allowing to disable setting the Authorization header.

Signed-off-by: pbakker <pbakker@netflix.com>
This commit is contained in:
pbakker
2025-02-11 15:00:09 -08:00
committed by Ilayaperumal Gopinathan
parent 0d0eafec6c
commit b936a9c429
5 changed files with 55 additions and 7 deletions

View File

@@ -34,6 +34,7 @@ import reactor.core.publisher.Mono;
import org.springframework.ai.model.ApiKey;
import org.springframework.ai.model.ChatModelDescription;
import org.springframework.ai.model.ModelOptionsUtils;
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;
@@ -200,7 +201,10 @@ public class OpenAiApi {
this.embeddingsPath = embeddingsPath;
// @formatter:off
Consumer<HttpHeaders> finalHeaders = h -> {
h.setBearerAuth(apiKey.getValue());
if(!(apiKey instanceof NoopApiKey)) {
h.setBearerAuth(apiKey.getValue());
}
h.setContentType(MediaType.APPLICATION_JSON);
h.addAll(headers);
};

View File

@@ -72,7 +72,14 @@ public class OpenAiAudioApi {
public OpenAiAudioApi(String baseUrl, String openAiToken, RestClient.Builder restClientBuilder,
ResponseErrorHandler responseErrorHandler) {
Consumer<HttpHeaders> authHeaders = h -> h.setBearerAuth(openAiToken);
Consumer<HttpHeaders> authHeaders;
if (openAiToken != null && !openAiToken.isEmpty()) {
authHeaders = h -> h.setBearerAuth(openAiToken);
}
else {
authHeaders = h -> {
};
}
this.restClient = restClientBuilder.baseUrl(baseUrl)
.defaultHeaders(authHeaders)
@@ -111,7 +118,9 @@ public class OpenAiAudioApi {
ResponseErrorHandler responseErrorHandler) {
Consumer<HttpHeaders> authHeaders = h -> {
h.setBearerAuth(apiKey);
if (apiKey != null && !apiKey.isEmpty()) {
h.setBearerAuth(apiKey);
}
h.addAll(headers);
// h.setContentType(MediaType.APPLICATION_JSON);
};
@@ -135,7 +144,7 @@ public class OpenAiAudioApi {
/**
* Streams audio generated from the input text.
*
* <p>
* This method sends a POST request to the OpenAI API to generate audio from the
* provided text. The audio is streamed back as a Flux of ResponseEntity objects, each
* containing a byte array of the audio data.

View File

@@ -87,7 +87,9 @@ public class OpenAiImageApi {
// @formatter:off
this.restClient = restClientBuilder.baseUrl(baseUrl)
.defaultHeaders(h -> {
h.setBearerAuth(apiKey);
if(apiKey != null && !apiKey.isEmpty()) {
h.setBearerAuth(apiKey);
}
h.setContentType(MediaType.APPLICATION_JSON);
h.addAll(headers);
})

View File

@@ -20,7 +20,6 @@ import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.ai.retry.RetryUtils;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
@@ -59,7 +58,9 @@ public class OpenAiModerationApi {
this.objectMapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
this.restClient = restClientBuilder.baseUrl(baseUrl).defaultHeaders(h -> {
h.setBearerAuth(openAiToken);
if (openAiToken != null && !openAiToken.isEmpty()) {
h.setBearerAuth(openAiToken);
}
h.setContentType(MediaType.APPLICATION_JSON);
}).defaultStatusHandler(responseErrorHandler).build();
}

View File

@@ -0,0 +1,32 @@
/*
* Copyright 2025-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ai.model;
/**
* This implementation of ApiKey indicates that no API key should be used, e.g. no HTTP
* headers should be set.
*
* @author Paul Bakker
*/
public class NoopApiKey implements ApiKey {
@Override
public String getValue() {
return "";
}
}