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 @@ -128,10 +128,6 @@ public OpenAiApi(String baseUrl, ApiKey apiKey, MultiValueMap<String, String> he

// @formatter:off
Consumer<HttpHeaders> finalHeaders = h -> {
if (!(apiKey instanceof NoopApiKey)) {
h.setBearerAuth(apiKey.getValue());
}

h.setContentType(MediaType.APPLICATION_JSON);
h.addAll(headers);
};
Expand Down Expand Up @@ -179,12 +175,17 @@ public ResponseEntity<ChatCompletion> chatCompletionEntity(ChatCompletionRequest
Assert.isTrue(!chatRequest.stream(), "Request must set the stream property to false.");
Assert.notNull(additionalHttpHeader, "The additional HTTP headers can not be null.");

// @formatter:off
return this.restClient.post()
.uri(this.completionsPath)
.headers(headers -> headers.addAll(additionalHttpHeader))
.headers(headers -> {
headers.addAll(additionalHttpHeader);
addDefaultHeadersIfMissing(headers);
})
.body(chatRequest)
.retrieve()
.toEntity(ChatCompletion.class);
// @formatter:on
}

/**
Expand Down Expand Up @@ -213,9 +214,13 @@ public Flux<ChatCompletionChunk> chatCompletionStream(ChatCompletionRequest chat

AtomicBoolean isInsideTool = new AtomicBoolean(false);

// @formatter:off
return this.webClient.post()
.uri(this.completionsPath)
.headers(headers -> headers.addAll(additionalHttpHeader))
.headers(headers -> {
headers.addAll(additionalHttpHeader);
addDefaultHeadersIfMissing(headers);
}) // @formatter:on
.body(Mono.just(chatRequest), ChatCompletionRequest.class)
.retrieve()
.bodyToFlux(String.class)
Expand Down Expand Up @@ -289,13 +294,20 @@ public <T> ResponseEntity<EmbeddingList<Embedding>> embeddings(EmbeddingRequest<

return this.restClient.post()
.uri(this.embeddingsPath)
.headers(this::addDefaultHeadersIfMissing)
.body(embeddingRequest)
.retrieve()
.toEntity(new ParameterizedTypeReference<>() {

});
}

private void addDefaultHeadersIfMissing(HttpHeaders headers) {
if (!headers.containsKey(HttpHeaders.AUTHORIZATION) && !(this.apiKey instanceof NoopApiKey)) {
headers.setBearerAuth(this.apiKey.getValue());
}
}

// Package-private getters for mutate/copy
String getBaseUrl() {
return this.baseUrl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,30 @@ public OpenAiAudioApi(String baseUrl, ApiKey apiKey, MultiValueMap<String, Strin
ResponseErrorHandler responseErrorHandler) {

Consumer<HttpHeaders> authHeaders = h -> {
if (!(apiKey instanceof NoopApiKey)) {
h.setBearerAuth(apiKey.getValue());
}
h.addAll(headers);
// h.setContentType(MediaType.APPLICATION_JSON);
};

// @formatter:off
this.restClient = restClientBuilder.clone()
.baseUrl(baseUrl)
.defaultHeaders(authHeaders)
.defaultStatusHandler(responseErrorHandler)
.defaultRequest(requestHeadersSpec -> {
if (!(apiKey instanceof NoopApiKey)) {
requestHeadersSpec.header(HttpHeaders.AUTHORIZATION, "Bearer " + apiKey.getValue());
}
})
.build();

this.webClient = webClientBuilder.clone().baseUrl(baseUrl).defaultHeaders(authHeaders).build();
this.webClient = webClientBuilder.clone()
.baseUrl(baseUrl)
.defaultHeaders(authHeaders)
.defaultRequest(requestHeadersSpec -> {
if (!(apiKey instanceof NoopApiKey)) {
requestHeadersSpec.header(HttpHeaders.AUTHORIZATION, "Bearer " + apiKey.getValue());
}
})
.build(); // @formatter:on
}

public static Builder builder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.ai.model.SimpleApiKey;
import org.springframework.ai.openai.api.common.OpenAiApiConstants;
import org.springframework.ai.retry.RetryUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
Expand Down Expand Up @@ -62,15 +63,18 @@ public OpenAiImageApi(String baseUrl, ApiKey apiKey, MultiValueMap<String, Strin
RestClient.Builder restClientBuilder, ResponseErrorHandler responseErrorHandler) {

// @formatter:off
this.restClient = restClientBuilder.baseUrl(baseUrl)
this.restClient = restClientBuilder.clone()
.baseUrl(baseUrl)
.defaultHeaders(h -> {
if (!(apiKey instanceof NoopApiKey)) {
h.setBearerAuth(apiKey.getValue());
}
h.setContentType(MediaType.APPLICATION_JSON);
h.addAll(headers);
})
.defaultStatusHandler(responseErrorHandler)
.defaultRequest(requestHeadersSpec -> {
if (!(apiKey instanceof NoopApiKey)) {
requestHeadersSpec.header(HttpHeaders.AUTHORIZATION, "Bearer " + apiKey.getValue());
}
})
.build();
// @formatter:on

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.ai.model.SimpleApiKey;
import org.springframework.ai.openai.api.common.OpenAiApiConstants;
import org.springframework.ai.retry.RetryUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
Expand Down Expand Up @@ -64,13 +65,20 @@ public OpenAiModerationApi(String baseUrl, ApiKey apiKey, MultiValueMap<String,

this.objectMapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

this.restClient = restClientBuilder.baseUrl(baseUrl).defaultHeaders(h -> {
if (!(apiKey instanceof NoopApiKey)) {
h.setBearerAuth(apiKey.getValue());
}
h.setContentType(MediaType.APPLICATION_JSON);
h.addAll(headers);
}).defaultStatusHandler(responseErrorHandler).build();
// @formatter:off
this.restClient = restClientBuilder.clone()
.baseUrl(baseUrl)
.defaultHeaders(h -> {
h.setContentType(MediaType.APPLICATION_JSON);
h.addAll(headers);
})
.defaultStatusHandler(responseErrorHandler)
.defaultRequest(requestHeadersSpec -> {
if (!(apiKey instanceof NoopApiKey)) {
requestHeadersSpec.header(HttpHeaders.AUTHORIZATION, "Bearer " + apiKey.getValue());
}
})
.build(); // @formatter:on
}

public ResponseEntity<OpenAiModerationResponse> createModeration(OpenAiModerationRequest openAiModerationRequest) {
Expand Down
Loading