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 @@ -16,25 +16,9 @@

package org.springframework.ai.openai.api;

import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import java.util.function.Predicate;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import reactor.core.publisher.Flux;
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.SimpleApiKey;
import org.springframework.ai.model.*;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wildcard import isn't allowed and will fix when merging.

import org.springframework.ai.openai.api.common.OpenAiApiConstants;
import org.springframework.ai.retry.RetryUtils;
import org.springframework.core.ParameterizedTypeReference;
Expand All @@ -48,6 +32,14 @@
import org.springframework.web.client.ResponseErrorHandler;
import org.springframework.web.client.RestClient;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import java.util.function.Predicate;

/**
* Single class implementation of the
Expand Down Expand Up @@ -200,7 +192,10 @@ public OpenAiApi(String baseUrl, ApiKey apiKey, MultiValueMap<String, String> he
this.embeddingsPath = embeddingsPath;
// @formatter:off
Consumer<HttpHeaders> finalHeaders = h -> {
h.setBearerAuth(apiKey.getValue());
if(!(apiKey instanceof NoopApiKey)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of checking for a specific impl, we should check for the return value, if it is empty then don't set header.

h.setBearerAuth(apiKey.getValue());
}

h.setContentType(MediaType.APPLICATION_JSON);
h.addAll(headers);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,14 @@ public OpenAiAudioApi(String openAiToken) {
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)
Expand Down Expand Up @@ -111,7 +118,9 @@ public OpenAiAudioApi(String baseUrl, String apiKey, MultiValueMap<String, Strin
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);
};
Expand All @@ -135,7 +144,7 @@ public ResponseEntity<byte[]> createSpeech(SpeechRequest requestBody) {

/**
* 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ public OpenAiImageApi(String baseUrl, String apiKey, MultiValueMap<String, Strin
// @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);
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
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;
Expand Down Expand Up @@ -59,7 +58,9 @@ public OpenAiModerationApi(String baseUrl, String openAiToken, RestClient.Builde
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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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.
*/
public class NoopApiKey implements ApiKey {

@Override
public String getValue() {
return "";
}

}