Skip to content

Commit 7ba3908

Browse files
committed
refactor(spring-ai-openai): fix code style
Signed-off-by: SenreySong <[email protected]>
1 parent cf69dae commit 7ba3908

File tree

10 files changed

+88
-58
lines changed

10 files changed

+88
-58
lines changed

models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiChatModel.java

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,23 @@
1616

1717
package org.springframework.ai.openai;
1818

19-
import io.micrometer.observation.Observation;
20-
import io.micrometer.observation.ObservationRegistry;
21-
import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor;
2219
import java.util.ArrayList;
2320
import java.util.Base64;
2421
import java.util.HashMap;
2522
import java.util.List;
2623
import java.util.Map;
2724
import java.util.concurrent.ConcurrentHashMap;
2825
import java.util.stream.Collectors;
26+
27+
import io.micrometer.observation.Observation;
28+
import io.micrometer.observation.ObservationRegistry;
29+
import io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor;
2930
import org.slf4j.Logger;
3031
import org.slf4j.LoggerFactory;
32+
import reactor.core.publisher.Flux;
33+
import reactor.core.publisher.Mono;
34+
import reactor.core.scheduler.Schedulers;
35+
3136
import org.springframework.ai.chat.messages.AssistantMessage;
3237
import org.springframework.ai.chat.messages.MessageType;
3338
import org.springframework.ai.chat.messages.ToolResponseMessage;
@@ -81,9 +86,6 @@
8186
import org.springframework.util.MimeTypeUtils;
8287
import org.springframework.util.MultiValueMap;
8388
import org.springframework.util.StringUtils;
84-
import reactor.core.publisher.Flux;
85-
import reactor.core.publisher.Mono;
86-
import reactor.core.scheduler.Schedulers;
8789

8890
/**
8991
* {@link ChatModel} and {@link StreamingChatModel} implementation for {@literal OpenAI}
@@ -307,7 +309,10 @@ public Flux<ChatResponse> internalStream(Prompt prompt, ChatResponse previousCha
307309
Flux<ChatResponse> chatResponse = completionChunks.map(this::chunkToChatCompletion)
308310
.switchMap(chatCompletion -> Mono.just(chatCompletion).map(chatCompletion2 -> {
309311
try {
310-
// If an id is not provided, set to "NO_ID" (for compatible
312+
// If an id is not
313+
// provided, set to
314+
// "NO_ID" (for
315+
// compatible
311316
// APIs).
312317
String id = chatCompletion2.id() == null ? "NO_ID" : chatCompletion2.id();
313318

@@ -360,12 +365,22 @@ public Flux<ChatResponse> internalStream(Prompt prompt, ChatResponse previousCha
360365
logger.error("Error processing chat completion", e);
361366
return new ChatResponse(List.of());
362367
}
363-
// When in stream mode and enabled to include the usage, the
368+
// When in stream mode and
369+
// enabled to include the
370+
// usage, the
364371
// OpenAI
365-
// Chat completion response would have the usage set only in its
366-
// final response. Hence, the following overlapping buffer is
367-
// created to store both the current and the subsequent response
368-
// to accumulate the usage from the subsequent response.
372+
// Chat completion response
373+
// would have the usage set
374+
// only in its
375+
// final response. Hence,
376+
// the following overlapping
377+
// buffer is
378+
// created to store both the
379+
// current and the
380+
// subsequent response
381+
// to accumulate the usage
382+
// from the subsequent
383+
// response.
369384
}))
370385
.buffer(2, 1)
371386
.map(bufferList -> {
@@ -374,12 +389,15 @@ public Flux<ChatResponse> internalStream(Prompt prompt, ChatResponse previousCha
374389
if (bufferList.size() == 2) {
375390
ChatResponse secondResponse = bufferList.get(1);
376391
if (secondResponse != null && secondResponse.getMetadata() != null) {
377-
// This is the usage from the final Chat response for a
392+
// This is the usage from the final Chat
393+
// response for a
378394
// given Chat request.
379395
Usage usage = secondResponse.getMetadata().getUsage();
380396
if (!UsageCalculator.isEmpty(usage)) {
381-
// Store the usage from the final response to the
382-
// penultimate response for accumulation.
397+
// Store the usage from the final
398+
// response to the
399+
// penultimate response for
400+
// accumulation.
383401
return new ChatResponse(firstResponse.getResults(),
384402
from(firstResponse.getMetadata(), usage));
385403
}
@@ -396,7 +414,8 @@ public Flux<ChatResponse> internalStream(Prompt prompt, ChatResponse previousCha
396414
response -> {
397415
if (this.toolExecutionEligibilityPredicate.isToolExecutionRequired(
398416
prompt.getOptions(), response)) {
399-
// FIXME: bounded elastic needs to be used since tool calling
417+
// FIXME: bounded elastic needs to be used since
418+
// tool calling
400419
// is currently only synchronous
401420
return Flux.deferContextual(
402421
ctx -> {
@@ -410,7 +429,9 @@ public Flux<ChatResponse> internalStream(Prompt prompt, ChatResponse previousCha
410429
ToolCallReactiveContextHolder.clearContext();
411430
}
412431
if (toolExecutionResult.returnDirect()) {
413-
// Return tool execution result directly to the client.
432+
// Return tool execution
433+
// result directly to
434+
// the client.
414435
return Flux.just(
415436
ChatResponse.builder()
416437
.from(response)
@@ -419,7 +440,9 @@ public Flux<ChatResponse> internalStream(Prompt prompt, ChatResponse previousCha
419440
toolExecutionResult))
420441
.build());
421442
} else {
422-
// Send the tool execution result back to the model.
443+
// Send the tool
444+
// execution result back
445+
// to the model.
423446
return this.internalStream(
424447
new Prompt(
425448
toolExecutionResult.conversationHistory(),

models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiChatOptions.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616

1717
package org.springframework.ai.openai;
1818

19-
import com.fasterxml.jackson.annotation.JsonIgnore;
20-
import com.fasterxml.jackson.annotation.JsonInclude;
21-
import com.fasterxml.jackson.annotation.JsonInclude.Include;
22-
import com.fasterxml.jackson.annotation.JsonProperty;
2319
import java.util.ArrayList;
2420
import java.util.Arrays;
2521
import java.util.HashMap;
@@ -28,8 +24,14 @@
2824
import java.util.Map;
2925
import java.util.Objects;
3026
import java.util.Set;
27+
28+
import com.fasterxml.jackson.annotation.JsonIgnore;
29+
import com.fasterxml.jackson.annotation.JsonInclude;
30+
import com.fasterxml.jackson.annotation.JsonInclude.Include;
31+
import com.fasterxml.jackson.annotation.JsonProperty;
3132
import org.slf4j.Logger;
3233
import org.slf4j.LoggerFactory;
34+
3335
import org.springframework.ai.model.ModelOptionsUtils;
3436
import org.springframework.ai.model.tool.ToolCallingChatOptions;
3537
import org.springframework.ai.openai.api.OpenAiApi;

models/spring-ai-openai/src/main/java/org/springframework/ai/openai/api/OpenAiApi.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616

1717
package org.springframework.ai.openai.api;
1818

19+
import java.util.List;
20+
import java.util.Map;
21+
import java.util.concurrent.atomic.AtomicBoolean;
22+
import java.util.function.Consumer;
23+
import java.util.function.Predicate;
24+
import java.util.stream.Collectors;
25+
1926
import com.fasterxml.jackson.annotation.JsonFormat;
2027
import com.fasterxml.jackson.annotation.JsonIgnore;
2128
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@@ -26,12 +33,9 @@
2633
import com.fasterxml.jackson.databind.ObjectMapper;
2734
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
2835
import com.fasterxml.jackson.databind.node.ObjectNode;
29-
import java.util.List;
30-
import java.util.Map;
31-
import java.util.concurrent.atomic.AtomicBoolean;
32-
import java.util.function.Consumer;
33-
import java.util.function.Predicate;
34-
import java.util.stream.Collectors;
36+
import reactor.core.publisher.Flux;
37+
import reactor.core.publisher.Mono;
38+
3539
import org.springframework.ai.model.ApiKey;
3640
import org.springframework.ai.model.ChatModelDescription;
3741
import org.springframework.ai.model.ModelOptionsUtils;
@@ -50,8 +54,6 @@
5054
import org.springframework.web.client.ResponseErrorHandler;
5155
import org.springframework.web.client.RestClient;
5256
import org.springframework.web.reactive.function.client.WebClient;
53-
import reactor.core.publisher.Flux;
54-
import reactor.core.publisher.Mono;
5557

5658
/**
5759
* Single class implementation of the
@@ -260,7 +262,7 @@ public Flux<ChatCompletionChunk> chatCompletionStream(ChatCompletionRequest chat
260262
throw new RuntimeException(e);
261263
}
262264
Object dynamicBody = createDynamicRequestBody(chatRequest);
263-
// @formatter:off
265+
// @formatter:off
264266
return this.webClient
265267
.post()
266268
.uri(this.completionsPath)
@@ -310,13 +312,13 @@ public Flux<ChatCompletionChunk> chatCompletionStream(ChatCompletionRequest chat
310312
/**
311313
* Creates an embedding vector representing the input text or token array.
312314
* @param embeddingRequest The embedding request.
313-
* @return Returns list of {@link Embedding} wrapped in {@link EmbeddingList}.
314315
* @param <T> Type of the entity in the data list. Can be a {@link String} or
315316
* {@link List} of tokens (e.g. Integers). For embedding multiple inputs in a single
316317
* request, You can pass a {@link List} of {@link String} or {@link List} of
317318
* {@link List} of tokens. For example:
318319
*
319320
* <pre>{@code List.of("text1", "text2", "text3") or List.of(List.of(1, 2, 3), List.of(3, 4, 5))} </pre>
321+
* @return Returns list of {@link Embedding} wrapped in {@link EmbeddingList}.
320322
*/
321323
public <T> ResponseEntity<EmbeddingList<Embedding>> embeddings(EmbeddingRequest<T> embeddingRequest) {
322324

models/spring-ai-openai/src/main/java/org/springframework/ai/openai/api/OpenAiStreamFunctionCallingHelper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.ArrayList;
2020
import java.util.List;
21+
2122
import org.springframework.ai.openai.api.OpenAiApi.ChatCompletion;
2223
import org.springframework.ai.openai.api.OpenAiApi.ChatCompletion.Choice;
2324
import org.springframework.ai.openai.api.OpenAiApi.ChatCompletionChunk;

models/spring-ai-openai/src/test/java/org/springframework/ai/openai/api/OpenAiApiBuilderTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,12 @@
1616

1717
package org.springframework.ai.openai.api;
1818

19-
import static org.assertj.core.api.Assertions.assertThat;
20-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
21-
import static org.mockito.Mockito.mock;
22-
2319
import java.io.IOException;
2420
import java.util.LinkedList;
2521
import java.util.List;
2622
import java.util.Objects;
2723
import java.util.Queue;
24+
2825
import okhttp3.mockwebserver.MockResponse;
2926
import okhttp3.mockwebserver.MockWebServer;
3027
import okhttp3.mockwebserver.RecordedRequest;
@@ -33,6 +30,7 @@
3330
import org.junit.jupiter.api.Nested;
3431
import org.junit.jupiter.api.Test;
3532
import org.opentest4j.AssertionFailedError;
33+
3634
import org.springframework.ai.model.ApiKey;
3735
import org.springframework.ai.model.SimpleApiKey;
3836
import org.springframework.http.HttpHeaders;
@@ -44,6 +42,8 @@
4442
import org.springframework.web.client.ResponseErrorHandler;
4543
import org.springframework.web.client.RestClient;
4644
import org.springframework.web.reactive.function.client.WebClient;
45+
import static org.assertj.core.api.Assertions.*;
46+
import static org.mockito.Mockito.*;
4747

4848
public class OpenAiApiBuilderTests {
4949

models/spring-ai-openai/src/test/java/org/springframework/ai/openai/api/OpenAiApiIT.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@
1616

1717
package org.springframework.ai.openai.api;
1818

19-
import static org.assertj.core.api.Assertions.assertThat;
20-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
21-
2219
import java.io.IOException;
2320
import java.util.Base64;
2421
import java.util.List;
22+
2523
import org.junit.jupiter.api.Disabled;
2624
import org.junit.jupiter.api.Test;
2725
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
2826
import org.junit.jupiter.params.ParameterizedTest;
2927
import org.junit.jupiter.params.provider.EnumSource;
28+
import reactor.core.publisher.Flux;
29+
3030
import org.springframework.ai.openai.api.OpenAiApi.ChatCompletion;
3131
import org.springframework.ai.openai.api.OpenAiApi.ChatCompletionChunk;
3232
import org.springframework.ai.openai.api.OpenAiApi.ChatCompletionMessage;
@@ -36,7 +36,7 @@
3636
import org.springframework.ai.openai.api.OpenAiApi.EmbeddingList;
3737
import org.springframework.core.io.ClassPathResource;
3838
import org.springframework.http.ResponseEntity;
39-
import reactor.core.publisher.Flux;
39+
import static org.assertj.core.api.Assertions.*;
4040

4141
/**
4242
* @author Christian Tzolov

models/spring-ai-openai/src/test/java/org/springframework/ai/openai/api/OpenAiStreamFunctionCallingHelperTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@
1616

1717
package org.springframework.ai.openai.api;
1818

19-
import static org.assertj.core.api.Assertions.assertThat;
20-
2119
import java.util.Arrays;
2220
import java.util.Collections;
2321
import java.util.List;
2422
import java.util.function.Consumer;
23+
2524
import org.junit.jupiter.api.Test;
2625
import org.mockito.Mockito;
2726
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
2827

28+
import static org.assertj.core.api.Assertions.*;
29+
2930
/**
3031
* Unit tests for {@link OpenAiStreamFunctionCallingHelper}
3132
*

models/spring-ai-openai/src/test/java/org/springframework/ai/openai/api/tool/OpenAiApiToolFunctionCallIT.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@
1616

1717
package org.springframework.ai.openai.api.tool;
1818

19-
import static org.assertj.core.api.Assertions.assertThat;
19+
import java.util.ArrayList;
20+
import java.util.List;
2021

2122
import com.fasterxml.jackson.core.JsonProcessingException;
2223
import com.fasterxml.jackson.databind.ObjectMapper;
23-
import java.util.ArrayList;
24-
import java.util.List;
2524
import org.junit.jupiter.api.Test;
2625
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
2726
import org.slf4j.Logger;
2827
import org.slf4j.LoggerFactory;
28+
2929
import org.springframework.ai.model.ModelOptionsUtils;
3030
import org.springframework.ai.openai.api.OpenAiApi;
3131
import org.springframework.ai.openai.api.OpenAiApi.ChatCompletion;
@@ -35,6 +35,7 @@
3535
import org.springframework.ai.openai.api.OpenAiApi.ChatCompletionRequest;
3636
import org.springframework.ai.openai.api.OpenAiApi.ChatCompletionRequest.ToolChoiceBuilder;
3737
import org.springframework.http.ResponseEntity;
38+
import static org.assertj.core.api.Assertions.*;
3839

3940
/**
4041
* Based on the OpenAI Function Calling tutorial:

models/spring-ai-openai/src/test/java/org/springframework/ai/openai/chat/OpenAiRetryTests.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,9 @@
1616

1717
package org.springframework.ai.openai.chat;
1818

19-
import static org.assertj.core.api.Assertions.assertThat;
20-
import static org.junit.jupiter.api.Assertions.assertThrows;
21-
import static org.mockito.ArgumentMatchers.any;
22-
import static org.mockito.ArgumentMatchers.isA;
23-
import static org.mockito.BDDMockito.given;
24-
2519
import java.util.List;
2620
import java.util.Optional;
21+
2722
import org.junit.jupiter.api.BeforeEach;
2823
import org.junit.jupiter.api.Disabled;
2924
import org.junit.jupiter.api.Test;
@@ -32,6 +27,8 @@
3227
import org.mockito.junit.jupiter.MockitoExtension;
3328
import org.slf4j.Logger;
3429
import org.slf4j.LoggerFactory;
30+
import reactor.core.publisher.Flux;
31+
3532
import org.springframework.ai.audio.transcription.AudioTranscriptionPrompt;
3633
import org.springframework.ai.audio.transcription.AudioTranscriptionResponse;
3734
import org.springframework.ai.chat.prompt.Prompt;
@@ -73,7 +70,10 @@
7370
import org.springframework.retry.RetryContext;
7471
import org.springframework.retry.RetryListener;
7572
import org.springframework.retry.support.RetryTemplate;
76-
import reactor.core.publisher.Flux;
73+
import static org.assertj.core.api.Assertions.*;
74+
import static org.junit.jupiter.api.Assertions.*;
75+
import static org.mockito.ArgumentMatchers.*;
76+
import static org.mockito.BDDMockito.*;
7777

7878
/**
7979
* @author Christian Tzolov

0 commit comments

Comments
 (0)