@@ -175,12 +175,17 @@ public ResponseEntity<ChatCompletion> chatCompletionEntity(ChatCompletionRequest
175
175
Assert .isTrue (!chatRequest .stream (), "Request must set the stream property to false." );
176
176
Assert .notNull (additionalHttpHeader , "The additional HTTP headers can not be null." );
177
177
178
- return this .restClient .post ().uri (this .completionsPath ).headers (headers -> {
179
- headers .addAll (additionalHttpHeader );
180
- if (!headers .containsKey (HttpHeaders .AUTHORIZATION ) && !(this .apiKey instanceof NoopApiKey )) {
181
- headers .setBearerAuth (this .apiKey .getValue ());
182
- }
183
- }).body (chatRequest ).retrieve ().toEntity (ChatCompletion .class );
178
+ // @formatter:off
179
+ return this .restClient .post ()
180
+ .uri (this .completionsPath )
181
+ .headers (headers -> {
182
+ headers .addAll (additionalHttpHeader );
183
+ addDefaultHeadersIfMissing (headers );
184
+ })
185
+ .body (chatRequest )
186
+ .retrieve ()
187
+ .toEntity (ChatCompletion .class );
188
+ // @formatter:on
184
189
}
185
190
186
191
/**
@@ -209,12 +214,13 @@ public Flux<ChatCompletionChunk> chatCompletionStream(ChatCompletionRequest chat
209
214
210
215
AtomicBoolean isInsideTool = new AtomicBoolean (false );
211
216
212
- return this .webClient .post ().uri (this .completionsPath ).headers (headers -> {
213
- headers .addAll (additionalHttpHeader );
214
- if (!headers .containsKey (HttpHeaders .AUTHORIZATION ) && !(this .apiKey instanceof NoopApiKey )) {
215
- headers .setBearerAuth (this .apiKey .getValue ());
216
- }
217
- })
217
+ // @formatter:off
218
+ return this .webClient .post ()
219
+ .uri (this .completionsPath )
220
+ .headers (headers -> {
221
+ headers .addAll (additionalHttpHeader );
222
+ addDefaultHeadersIfMissing (headers );
223
+ }) // @formatter:on
218
224
.body (Mono .just (chatRequest ), ChatCompletionRequest .class )
219
225
.retrieve ()
220
226
.bodyToFlux (String .class )
@@ -288,13 +294,20 @@ public <T> ResponseEntity<EmbeddingList<Embedding>> embeddings(EmbeddingRequest<
288
294
289
295
return this .restClient .post ()
290
296
.uri (this .embeddingsPath )
297
+ .headers (this ::addDefaultHeadersIfMissing )
291
298
.body (embeddingRequest )
292
299
.retrieve ()
293
300
.toEntity (new ParameterizedTypeReference <>() {
294
301
295
302
});
296
303
}
297
304
305
+ private void addDefaultHeadersIfMissing (HttpHeaders headers ) {
306
+ if (!headers .containsKey (HttpHeaders .AUTHORIZATION ) && !(this .apiKey instanceof NoopApiKey )) {
307
+ headers .setBearerAuth (this .apiKey .getValue ());
308
+ }
309
+ }
310
+
298
311
// Package-private getters for mutate/copy
299
312
String getBaseUrl () {
300
313
return this .baseUrl ;
0 commit comments